This commit is contained in:
2026-02-08 16:20:50 +08:00
parent 0ae47e5d6a
commit 8b88904ef7
72 changed files with 5963 additions and 2038 deletions

View File

@@ -1,5 +1,6 @@
#include "GameMap.h"
#include "Asset/AssetManager.h"
#include "Asset/Asset_SoundPack.h"
#include "EngineCore/Game.h"
#include "EngineFrame/Scene/Scene.h"
#include "EngineFrame/Component/Animation.h"
@@ -19,15 +20,25 @@ GameMap::GameMap()
_LayerMap["max"] = new GameMapLayer();
// 设置图层渲染顺序
_LayerMap["contact"]->SetRenderZOrder(10000);
_LayerMap["distantback"]->SetRenderZOrder(50000);
_LayerMap["middleback"]->SetRenderZOrder(100000);
_LayerMap["bottom"]->SetRenderZOrder(150000);
_LayerMap["closeback"]->SetRenderZOrder(200000);
_LayerMap["normal"]->SetRenderZOrder(250000);
_LayerMap["close"]->SetRenderZOrder(300000);
_LayerMap["cover"]->SetRenderZOrder(350000);
_LayerMap["max"]->SetRenderZOrder(400000);
_LayerMap["contact"]->SetZOrder(10000);
_LayerMap["distantback"]->SetZOrder(50000);
_LayerMap["middleback"]->SetZOrder(100000);
_LayerMap["bottom"]->SetZOrder(150000);
_LayerMap["closeback"]->SetZOrder(200000);
_LayerMap["normal"]->SetZOrder(250000);
_LayerMap["close"]->SetZOrder(300000);
_LayerMap["cover"]->SetZOrder(350000);
_LayerMap["max"]->SetZOrder(400000);
AddChild(_LayerMap["contact"]);
AddChild(_LayerMap["distantback"]);
AddChild(_LayerMap["middleback"]);
AddChild(_LayerMap["bottom"]);
AddChild(_LayerMap["closeback"]);
AddChild(_LayerMap["normal"]);
AddChild(_LayerMap["close"]);
AddChild(_LayerMap["cover"]);
AddChild(_LayerMap["max"]);
}
GameMap::~GameMap()
@@ -107,7 +118,7 @@ void GameMap::InitConfiguration(std::string mapName)
{
BackGroundAni ani;
Data.Get();
ani.filename = _MapDir + Tool_toLowerCase(Data.Get());
ani.filename = Tool_RegRealPath(_MapDir + Tool_toLowerCase(Data.Get()));
Data.Get();
ani.layer = Data.Get();
Data.Get();
@@ -119,13 +130,13 @@ void GameMap::InitConfiguration(std::string mapName)
}
else if (Segment == "[sound]")
{
std::vector<std::string> soundArry;
std::map<std::string, bool> soundArry;
while (true)
{
std::string sounddata = Data.Get();
if (sounddata == "[/sound]")
break;
soundArry.push_back(sounddata);
soundArry[sounddata] = true;
}
_MapInfo["sound"] = soundArry;
}
@@ -174,13 +185,17 @@ void GameMap::InitConfiguration(std::string mapName)
std::string areadata = Data.Get();
if (areadata == "[/town movable area]")
break;
town_movable_area.push_back(std::stoi(areadata));
town_movable_area.push_back(std::stoi(areadata));
town_movable_area.push_back(std::stoi(areadata));
town_movable_area.push_back(std::stoi(areadata));
int x1 = std::stoi(areadata);
int y1 = std::stoi(Data.Get());
int x2 = std::stoi(Data.Get());
int y2 = std::stoi(Data.Get());
town_movable_area.push_back(x1);
town_movable_area.push_back(y1);
town_movable_area.push_back(x2);
town_movable_area.push_back(y2);
MapMoveArea T;
T.town = std::stoi(areadata);
T.area = std::stoi(areadata);
T.town = std::stoi(Data.Get());
T.area = std::stoi(Data.Get());
town_movable_area_info.push_back(T);
}
_MapInfo["town_movable_area"] = town_movable_area;
@@ -212,25 +227,19 @@ void GameMap::InitTile()
int NormalTileCount = 0;
// 普通地板高度
int NormalTileHeight = 560;
std::vector<std::string> tileArr = std::get<std::vector<std::string>>(_MapInfo["tile"]);
if (tileArr.size() > 0)
{
NormalTileCount = tileArr.size();
_MapLength = NormalTileCount * 224;
_MapHeight = 560;
}
if (!_MapInfo.count("extended_tile"))
return;
std::vector<std::string> tileArr;
std::vector<std::string> extileArr;
std::vector<std::string> extileArr = std::get<std::vector<std::string>>(_MapInfo["extended_tile"]);
if (extileArr.size() > 0)
tileArr = std::get<std::vector<std::string>>(_MapInfo["tile"]);
// 拓展地板不一定有
if (_MapInfo.count("extended_tile"))
{
int ExTileCount = extileArr.size();
int Buffer = (ExTileCount / NormalTileCount);
_MapHeight += (Buffer < 1 ? 1 : Buffer) * 40;
extileArr = std::get<std::vector<std::string>>(_MapInfo["extended_tile"]);
}
_Tile = new Tile(this, tileArr, extileArr);
_Tile->SetPosition(0, -200 - std::get<int>(_MapInfo["background_pos"]));
_LayerMap["bottom"]->AddChild(_Tile);
}
@@ -256,8 +265,8 @@ void GameMap::InitBackgroundAnimation()
}
for (int i = 0; i < (int)AniList.size(); i++)
{
AniList[i]->SetPos(Vec2{i * width, -120});
AniList[i]->SetRenderZOrder(-1000000);
AniList[i]->SetPosition(i * width, -120);
AniList[i]->SetZOrder(-1000000);
std::string layer = ani.layer;
layer = layer.substr(1, layer.length() - 2);
if (_LayerMap.count(layer))
@@ -281,8 +290,8 @@ void GameMap::InitMapAnimation()
{
std::string path = ani.filename;
RefPtr<Animation> AniObj = new Animation(path);
AniObj->SetPos(Vec2{ani.XPos, ani.YPos - ani.ZPos});
AniObj->SetRenderZOrder(ani.YPos);
AniObj->SetPosition(ani.XPos, ani.YPos - ani.ZPos);
AniObj->SetZOrder(ani.YPos);
std::string layer = ani.layer;
layer = layer.substr(1, layer.length() - 2);
if (_LayerMap.count(layer))
@@ -305,12 +314,30 @@ void GameMap::InitVirtualMovableArea()
float w = Info[i + 2];
float h = Info[i + 3];
if (_DebugMode)
_LayerMap["max"]->AddDebugFeasibleAreaInfo(Vec2(x, y), VecSize(w, h));
_LayerMap["max"]->AddDebugFeasibleAreaInfo(Vec2(x, y), VecSize(w, h), 0);
_MovableArea.push_back(SDL_FRect{x, y, w, h});
}
}
}
void GameMap::InitMoveArea()
{
std::vector<int> Info = std::get<std::vector<int>>(_MapInfo["town_movable_area"]);
if (Info.size() > 0)
{
for (int i = 0; i < Info.size(); i += 4)
{
float x = Info[i];
float y = Info[i + 1];
float w = Info[i + 2];
float h = Info[i + 3];
if (_DebugMode)
_LayerMap["max"]->AddDebugFeasibleAreaInfo(Vec2(x, y), VecSize(w, h), 1);
_MoveArea.push_back(SDL_FRect{x, y, w, h});
}
}
}
void GameMap::LoadMap(std::string mapName)
{
// 读取脚本配置
@@ -323,24 +350,41 @@ void GameMap::LoadMap(std::string mapName)
InitMapAnimation();
// 初始化可行区域
InitVirtualMovableArea();
// 初始化传送区域
InitMoveArea();
}
void GameMap::Enter(Scene *scene)
void GameMap::Enter()
{
AddChild(_LayerMap["contact"]);
AddChild(_LayerMap["distantback"]);
AddChild(_LayerMap["middleback"]);
AddChild(_LayerMap["bottom"]);
AddChild(_LayerMap["closeback"]);
AddChild(_LayerMap["normal"]);
AddChild(_LayerMap["close"]);
AddChild(_LayerMap["cover"]);
AddChild(_LayerMap["max"]);
auto &Audio = Asset_SoundPack::GetInstance();
// 获取背景音乐
auto bgmIt = _MapInfo.find("sound");
if (bgmIt != _MapInfo.end())
{
std::map<std::string, bool> bgmlist = std::get<std::map<std::string, bool>>(_MapInfo["sound"]);
// 获取正在播放的背景音乐
std::map<std::string, int> curmap = Audio.GetBackgroudMusicVector();
for (auto &it : curmap)
{
std::string name = it.first;
// 判断正在播放的背景音乐是否与当前地图背景音乐相同
if (!bgmlist.count(name))
{
Audio.StopAudio(name);
}
}
// 播放当前地图背景音乐
for (auto &it : bgmlist)
{
std::string name = it.first;
if (!curmap.count(name))
Audio.PlayAudio(name);
}
}
}
void GameMap::Update(float deltaTime)
void GameMap::OnUpdate(float deltaTime)
{
Actor::Update(deltaTime);
RefPtr<GameCamera> Cam = Global_Game::GetInstance().GetCamera();
if (Cam == nullptr)
@@ -384,8 +428,11 @@ void GameMap::Update(float deltaTime)
posX *= BackgroundMoveSpeed;
posX /= 100.0f;
}
Layer.second->SetPos(Vec2(posX, posY));
Layer.second->SetPosition(posX, posY);
}
Actor::OnUpdate(deltaTime);
}
void GameMap::AddObject(RefPtr<BaseObject> object)
@@ -397,9 +444,6 @@ void GameMap::AddObject(RefPtr<BaseObject> object)
{
CharacterObject *chr = (CharacterObject *)(object.Get());
Global_Game::GetInstance().GetCamera()->SetFromActor(chr);
if (chr->_Shadow)
_LayerMap["bottom"]->AddChild(chr->_Shadow);
}
}
@@ -459,4 +503,42 @@ VecFPos3 GameMap::CheckIsItMovable(VecFPos3 CurPos, VecFPos3 PosOffset)
result.z += PosOffset.z;
return result;
}
}
GameMap::MapMoveArea GameMap::CheckIsItMoveArea(VecFPos3 ChrPos)
{
MapMoveArea invalidArea;
invalidArea.town = -2;
invalidArea.area = -2;
if (_MoveArea.size() && _MapInfo.count("town_movable_area_info"))
{
SDL_FPoint CurPos = {ChrPos.x, ChrPos.y};
for (int i = 0; i < _MoveArea.size(); i++)
{
if (SDL_PointInFRect(&CurPos, &_MoveArea[i]))
{
const std::vector<MapMoveArea> &tma = std::get<std::vector<MapMoveArea>>(_MapInfo["town_movable_area_info"]);
return tma[i];
}
}
}
// 所有矩形都不命中,返回无效值
return invalidArea;
}
std::vector<GameMap::MapMoveArea> &GameMap::GetMoveAreaInfo()
{
return std::get<std::vector<GameMap::MapMoveArea>>(_MapInfo["town_movable_area_info"]);
}
SDL_Rect GameMap::GetMovablePositionArea(int Index)
{
auto Vertex = std::get<std::vector<int>>(_MapInfo["town_movable_area"]);
int FindIndex = Index * 4;
SDL_Rect Buf = {Vertex[FindIndex], Vertex[FindIndex + 1], Vertex[FindIndex + 2], Vertex[FindIndex + 3]};
return Buf;
}