하아찡

[C++] 몬스터 스포너 xml 본문

C++/이것저것서버테스트

[C++] 몬스터 스포너 xml

하아찡 2025. 3. 27. 20:11

언리얼 버전 5.5.3

서버언어 C++로 구성했습니다.


몬스터 스포너를 코드에 직접 입력해서 사용하기 번거롭고 지저분해보여서 xml로 스포너 위치를 저장해두면 각각 Room에서 맞는 xml파일만 호출하기 위해서 만들었습니다.

 

SpawnTable.h

#pragma once
struct StructMonsterSpawn
{
    uint32 monsterID = -1;
    float x = 0.0f;
    float y = 0.0f;
    float z = 0.0f;
};
class SpawnTable
{

public:
    static vector<StructMonsterSpawn> LoadSpawnMonstersFromXML(const char* FilePath);
};

 

SpawnTable.cpp

#include "pch.h"
#include "SpawnTable.h"
#include "tinyxml2.h"
using namespace tinyxml2;

vector<StructMonsterSpawn> SpawnTable::LoadSpawnMonstersFromXML(const char* FilePath)
{
    vector<StructMonsterSpawn> list;
    XMLDocument doc;
    if (doc.LoadFile(FilePath) == XML_SUCCESS)
    {
        XMLElement* root = doc.FirstChildElement("Monsters");
        for (XMLElement* item = root->FirstChildElement("Monster"); item != nullptr; item = item->NextSiblingElement("Monster"))
        {
            StructMonsterSpawn MonsterInfo;
            MonsterInfo.monsterID = item->IntAttribute("monsterid");
            MonsterInfo.x = item->FloatAttribute("x");
            MonsterInfo.y = item->FloatAttribute("y");
            MonsterInfo.z = item->FloatAttribute("z");

            list.push_back(MonsterInfo);
        }
        LOG("MonsterSpawn Load Success!!!");
    }
    else {
        LOG("MonsterSpawn Load Fail!!!");
    }

    return list;

}

 


 

 

기존 스포너 설정방식

BaseLevel::BaseLevel()
{
	uint64 index = 0;

	for (int i = 0; i < 5; i++) {
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 1600 + (i * 300), 700 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 1300 + (i * 300), 800 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 1100 + (i * 200), 400 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 1000 + (i * 300), 500 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 900 + (i * 300), 600 + (i * 500), -150);
	}

	for (int i = 0; i < 5; i++) {
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 4000 + (i * 300), 2000 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 4300 + (i * 300), 2500 + (i * 500), -150);
		SetMonsterSpawner(index++, Protocol::MONSTER_ID_GOBLIN, 4500 + (i * 200), 2000 + (i * 500), -150);
	}

}

 

 

변경된 스포너 설정방식

BaseLevel::BaseLevel()
{
	uint64 index = 0;
    vector<StructMonsterSpawn> spawnList = SpawnTable::LoadSpawnMonstersFromXML("BaseLevel.xml");
    
    for (auto monster : spawnList) {
        SetMonsterSpawner(index++, monster.monsterID, monster.x, monster.y, monster.z);
    }

}

 

 

XML파일

<?xml version="1.0" encoding="utf-8"?>
<Monsters>
  <Monster monsterid="0" x="900" y="600" z="-150"></Monster>
  <Monster monsterid="0" x="1000" y="800" z="-150"></Monster>
  <Monster monsterid="0" x="1100" y="1000" z="-150"></Monster>
  <Monster monsterid="0" x="1200" y="1200" z="-150"></Monster>
  <Monster monsterid="0" x="1300" y="1400" z="-150"></Monster>
  <Monster monsterid="0" x="1500" y="1600" z="-150"></Monster>
  
  <Monster monsterid="0" x="410" y="640.0" z="-259.9374"></Monster>
  <Monster monsterid="0" x="350" y="640.0" z="-259.9374"></Monster>
  <Monster monsterid="0" x="290" y="660.0" z="-259.9374"></Monster>
  <Monster monsterid="0" x="240" y="680.0" z="-259.9374"></Monster>
  <Monster monsterid="0" x="-90.0" y="460" z="-259.9374"></Monster>
  <Monster monsterid="0" x="-50" y="290" z="-259.9374"></Monster>
  <Monster monsterid="0" x="-500" y="490" z="-259.9374"></Monster>
  
  <Monster monsterid="0" x="2660.0" y="2830.0" z="-113.303205"></Monster>
  <Monster monsterid="0" x="2840.0" y="2830.0" z="-97.208592"></Monster>
  <Monster monsterid="0" x="2690.0" y="3040.0" z="-124.225439"></Monster>
  <Monster monsterid="0" x="2610.0" y="3170.0" z="-150.124646"></Monster>
  <Monster monsterid="0" x="2810.0" y="3440.0" z="-144.079712"></Monster>
  <Monster monsterid="0" x="2640.0" y="3440.0" z="-161.073312"></Monster>
  
  <Monster monsterid="0" x="5200.0" y="3070.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5350.0" y="3070.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5450.0" y="3070.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5550.0" y="3070.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5650.0" y="3070.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5250.0" y="2900.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5200" y="2900.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5350" y="2900.0" z="-259.937394"></Monster>
  <Monster monsterid="0" x="5550" y="2900.0" z="-259.937394"></Monster>

</Monsters>

 

monsterid값에다가 정해진 몬스터ID값을 넣어줍니다.

현재 저는 0번이 고블린이이고.. 작업된 모델이 고블린밖에없어서... 전부다 0번이네요

반응형

'C++ > 이것저것서버테스트' 카테고리의 다른 글

[푸념] DB연결관련  (0) 2025.04.01
[C++] 개인맵 및 제한시간  (0) 2025.03.27
[C++] 인벤토리 - 3 (장비 장착)  (0) 2025.03.25
[C++] 인벤토리 - 2 (인벤토리 서버저장)  (0) 2025.03.25
[C++] 인벤토리  (1) 2025.03.22