Files
DNF_DEV/source_game/Actor/Map/GameWorld.cpp
2026-02-08 17:05:17 +08:00

101 lines
2.6 KiB
C++

#include "GameWorld.h"
#include "Global/Global_Game.h"
#include "EngineCore/Game.h"
#include "EngineFrame/Component/AnimationMap.h"
#include "EngineFrame/Component/Animation.h"
GameWorld::GameWorld()
{
}
GameWorld::~GameWorld()
{
}
void GameWorld::Enter()
{
SetScale(1.2f, 1.2f);
Init();
}
void GameWorld::Exit()
{
}
void GameWorld::Init()
{
// RefPtr<Animation> ani = new Animation("common/tuguan/maineff.ani");
// RefPtr<Sprite> sp = new Sprite("sprite/live/else/chn/2022/0510_danjin_jar/22_danjineft/0510_danjin_body.img",0);
// RefPtr<AnimationMap> am = new AnimationMap();
// // am->AddAnimation(ani);
// am->AddChild(sp);
// am->CompleteConstruction();
// AddChild(am);
// 构造所有城镇
auto &GlobalGame = Global_Game::GetInstance();
for (auto obj : GlobalGame.GetTownMap())
{
RefPtr<GameTown> town = new GameTown;
town->Init(obj.first);
m_TownMap[obj.first] = town;
}
// 构造角色
RefPtr<CharacterObject> obj = new CharacterObject();
obj->Construction(0);
AddCharacter(obj, 1);
}
void GameWorld::AddCharacter(RefPtr<CharacterObject> Chr, int TownId)
{
// 游戏世界类赋值我的角色
m_MyCharacter = Chr;
// 记录当前城镇
m_CurTown = TownId;
// 进入城镇
m_TownMap[TownId]->AddCharacter(Chr);
// 渲染并更新城镇
AddChild(m_TownMap[TownId]);
}
void GameWorld::MoveCharacter(RefPtr<CharacterObject> Chr, int TownId, int Area)
{
// 当前所在城镇
int curTown = m_CurTown;
// 当前所在区域
int curArea = m_TownMap[curTown]->GetCurAreaIndex();
// 获取目标城镇移动信息表
auto MapObj = m_TownMap[TownId]->GetArea(Area);
auto MoveInfo = MapObj->GetMoveAreaInfo();
// 传送后的目标区域
SDL_Rect destArea = {0, 0, 0, 0};
// 遍历移动信息表
for (size_t i = 0; i < MoveInfo.size(); ++i)
{
auto info = MoveInfo[i];
if (info.town == curTown && info.area == curArea)
{
destArea = MapObj->GetMovablePositionArea(i);
}
}
// 读取出应该存在的坐标以后设置角色的坐标
float Xpos = destArea.x + destArea.w / 2;
float Ypos = destArea.y + destArea.h / 2;
Chr->SetPosition({Xpos, Ypos, 0});
// 将角色添加到目标城镇
m_TownMap[TownId]->AddCharacter(Chr, Area);
// 停止渲染更新旧城镇
RemoveChild(m_TownMap[curTown]);
// 记录当前城镇
m_CurTown = TownId;
// 渲染并更新城镇
AddChild(m_TownMap[TownId]);
}
GameWorld *GameWorld::GetWorld()
{
return (GameWorld *)Game::GetInstance().GetScene().Get();
}