建档
This commit is contained in:
369
source_game/Actor/Map/GameMap.cpp
Normal file
369
source_game/Actor/Map/GameMap.cpp
Normal file
@@ -0,0 +1,369 @@
|
||||
#include "GameMap.h"
|
||||
#include "Asset/AssetManager.h"
|
||||
#include "EngineFrame/Scene/Scene.h"
|
||||
#include "EngineFrame/Component/Animation.h"
|
||||
#include "Actor/Map/GameMapCamera.h"
|
||||
#include "Actor/Object/CharacterObject.h"
|
||||
|
||||
GameMap::GameMap()
|
||||
{
|
||||
_LayerMap["contact"] = new GameMapLayer();
|
||||
_LayerMap["distantback"] = new GameMapLayer();
|
||||
_LayerMap["middleback"] = new GameMapLayer();
|
||||
_LayerMap["bottom"] = new GameMapLayer();
|
||||
_LayerMap["closeback"] = new GameMapLayer();
|
||||
_LayerMap["normal"] = new GameMapLayer();
|
||||
_LayerMap["close"] = new GameMapLayer();
|
||||
_LayerMap["cover"] = new GameMapLayer();
|
||||
_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);
|
||||
}
|
||||
|
||||
GameMap::~GameMap()
|
||||
{
|
||||
}
|
||||
|
||||
void GameMap::InitConfiguration(std::string mapName)
|
||||
{
|
||||
ScriptData Data = AssetManager::GetInstance().GetScriptInfo(mapName);
|
||||
_MapInfo["wide_mode_camer_vertical_correction"] = 0;
|
||||
_MapInfo["background_pos"] = 0;
|
||||
_MapPath = mapName;
|
||||
_MapDir = mapName.substr(0, mapName.find_last_of("/") + 1);
|
||||
while (!Data.IsEnd())
|
||||
{
|
||||
std::string Segment = Data.Get();
|
||||
if (Segment == "[background pos]")
|
||||
{
|
||||
_MapInfo["background_pos"] = std::stoi(Data.Get());
|
||||
}
|
||||
else if (Segment == "[map name]")
|
||||
{
|
||||
_MapInfo["name"] = Data.Get();
|
||||
}
|
||||
else if (Segment == "[wide mode camera vertical correction]")
|
||||
{
|
||||
_MapInfo["wide_mode_camer_vertical_correction"] = std::stoi(Data.Get());
|
||||
}
|
||||
else if (Segment == "[tile]")
|
||||
{
|
||||
std::vector<std::string> tileArry;
|
||||
while (true)
|
||||
{
|
||||
auto tiledata = Data.Get();
|
||||
if (tiledata == "[/tile]")
|
||||
break;
|
||||
tileArry.push_back(_MapDir + Tool_toLowerCase(tiledata));
|
||||
}
|
||||
_MapInfo["tile"] = tileArry;
|
||||
}
|
||||
else if (Segment == "[extended tile]")
|
||||
{
|
||||
std::vector<std::string> tileArry;
|
||||
while (true)
|
||||
{
|
||||
auto tiledata = Data.Get();
|
||||
if (tiledata == "[/extended tile]")
|
||||
break;
|
||||
tileArry.push_back(_MapDir + Tool_toLowerCase(tiledata));
|
||||
}
|
||||
_MapInfo["extended_tile"] = tileArry;
|
||||
}
|
||||
else if (Segment == "[far sight scroll]" || Segment == "[middle sight scroll]" || Segment == "[near sight scroll]")
|
||||
{
|
||||
_MapInfo[Segment.substr(1, Segment.length() - 2)] = std::stoi(Data.Get());
|
||||
}
|
||||
else if (Segment == "[background animation]")
|
||||
{
|
||||
std::vector<BackGroundAni> Arr;
|
||||
while (true)
|
||||
{
|
||||
std::string aniData = Data.Get();
|
||||
if (aniData == "[/background animation]")
|
||||
break;
|
||||
if (aniData == "[ani info]")
|
||||
{
|
||||
BackGroundAni ani;
|
||||
Data.Get();
|
||||
ani.filename = _MapDir + Tool_toLowerCase(Data.Get());
|
||||
Data.Get();
|
||||
ani.layer = Data.Get();
|
||||
Data.Get();
|
||||
ani.order = Data.Get();
|
||||
Arr.push_back(ani);
|
||||
}
|
||||
}
|
||||
_MapInfo["background_animation"] = Arr;
|
||||
}
|
||||
else if (Segment == "[sound]")
|
||||
{
|
||||
std::vector<std::string> soundArry;
|
||||
while (true)
|
||||
{
|
||||
std::string sounddata = Data.Get();
|
||||
if (sounddata == "[/sound]")
|
||||
break;
|
||||
soundArry.push_back(sounddata);
|
||||
}
|
||||
_MapInfo["sound"] = soundArry;
|
||||
}
|
||||
else if (Segment == "[animation]")
|
||||
{
|
||||
std::vector<MapAni> aniArry;
|
||||
while (true)
|
||||
{
|
||||
std::string aniData = Data.Get();
|
||||
if (aniData == "[/animation]")
|
||||
break;
|
||||
MapAni info;
|
||||
info.filename = Tool_RegRealPath(_MapDir + Tool_toLowerCase(aniData));
|
||||
info.layer = Data.Get();
|
||||
info.XPos = std::stoi(Data.Get());
|
||||
info.YPos = std::stoi(Data.Get());
|
||||
info.ZPos = std::stoi(Data.Get());
|
||||
aniArry.push_back(info);
|
||||
}
|
||||
_MapInfo["animation"] = aniArry;
|
||||
}
|
||||
else if (Segment == "[NPC]")
|
||||
{
|
||||
std::vector<MapNpc> npcArry;
|
||||
while (true)
|
||||
{
|
||||
std::string npcData = Data.Get();
|
||||
if (npcData == "[/NPC]")
|
||||
break;
|
||||
MapNpc info;
|
||||
info.id = std::stoi(npcData);
|
||||
info.direction = Data.Get();
|
||||
info.XPos = std::stoi(Data.Get());
|
||||
info.YPos = std::stoi(Data.Get());
|
||||
info.ZPos = std::stoi(Data.Get());
|
||||
npcArry.push_back(info);
|
||||
}
|
||||
_MapInfo["NPC"] = npcArry;
|
||||
}
|
||||
else if (Segment == "[town movable area]")
|
||||
{
|
||||
std::vector<int> town_movable_area;
|
||||
std::vector<MapMoveArea> town_movable_area_info;
|
||||
while (true)
|
||||
{
|
||||
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));
|
||||
MapMoveArea T;
|
||||
T.town = std::stoi(areadata);
|
||||
T.area = std::stoi(areadata);
|
||||
town_movable_area_info.push_back(T);
|
||||
}
|
||||
_MapInfo["town_movable_area"] = town_movable_area;
|
||||
_MapInfo["town_movable_area_info"] = town_movable_area_info;
|
||||
}
|
||||
else if (Segment == "[virtual movable area]")
|
||||
{
|
||||
std::vector<int> virtual_movable_area;
|
||||
while (true)
|
||||
{
|
||||
std::string areadata = Data.Get();
|
||||
if (areadata == "[/virtual movable area]")
|
||||
break;
|
||||
virtual_movable_area.push_back(std::stoi(areadata));
|
||||
virtual_movable_area.push_back(std::stoi(Data.Get()));
|
||||
virtual_movable_area.push_back(std::stoi(Data.Get()));
|
||||
virtual_movable_area.push_back(std::stoi(Data.Get()));
|
||||
}
|
||||
_MapInfo["virtual_movable_area"] = virtual_movable_area;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameMap::InitTile()
|
||||
{
|
||||
if (!_MapInfo.count("tile"))
|
||||
return;
|
||||
int NormalTileCount = 0;
|
||||
std::vector<std::string> tileArr = std::get<std::vector<std::string>>(_MapInfo["tile"]);
|
||||
if (tileArr.size() > 0)
|
||||
{
|
||||
NormalTileCount = tileArr.size();
|
||||
_MapLength = NormalTileCount * 224;
|
||||
for (int i = 0; i < NormalTileCount; i++)
|
||||
{
|
||||
std::string path = tileArr[i];
|
||||
RefPtr<Tile> tile = new Tile(path);
|
||||
tile->SetPos(VecFPos{i * 224, -200 - std::get<int>(_MapInfo["background_pos"])});
|
||||
_LayerMap["bottom"]->AddComponent(tile);
|
||||
}
|
||||
_MapHeight = 560;
|
||||
}
|
||||
if (_MapInfo.count("extended_tile"))
|
||||
{
|
||||
std::vector<std::string> extileArr = std::get<std::vector<std::string>>(_MapInfo["extended_tile"]);
|
||||
if (extileArr.size() > 0)
|
||||
{
|
||||
int ExTileCount = extileArr.size();
|
||||
int Buffer = (ExTileCount / NormalTileCount);
|
||||
_MapHeight += (Buffer < 1 ? 1 : Buffer) * 40;
|
||||
for (int i = 0; i < ExTileCount; i++)
|
||||
{
|
||||
std::string path = extileArr[i];
|
||||
RefPtr<Tile> tile = new Tile(path);
|
||||
int xbuf = i % NormalTileCount * 224;
|
||||
int ybuf = 560 - 200 - std::get<int>(_MapInfo["background_pos"]) + 40 * (i / NormalTileCount);
|
||||
tile->SetPos(VecFPos{xbuf, ybuf});
|
||||
_LayerMap["bottom"]->AddComponent(tile);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameMap::InitBackgroundAnimation()
|
||||
{
|
||||
float Rate = _MapInfo.count("far_sight_scroll") ? std::get<int>(_MapInfo["far_sight_scroll"]) / 100.0f : 1.0f;
|
||||
(void)Rate;
|
||||
// TODO 摄像机
|
||||
if (_MapInfo.count("background_animation"))
|
||||
{
|
||||
std::vector<BackGroundAni> aniArr = std::get<std::vector<BackGroundAni>>(_MapInfo["background_animation"]);
|
||||
for (auto &ani : aniArr)
|
||||
{
|
||||
std::string path = ani.filename;
|
||||
std::vector<RefPtr<Animation>> AniList;
|
||||
RefPtr<Animation> AniObj = new Animation(path);
|
||||
int width = AniObj->GetSize().width;
|
||||
AniList.push_back(AniObj);
|
||||
for (int i = 0; i < (_MapLength * Rate) / width; i++)
|
||||
{
|
||||
RefPtr<Animation> AniObj = new Animation(path);
|
||||
AniList.push_back(AniObj);
|
||||
}
|
||||
for (int i = 0; i < (int)AniList.size(); i++)
|
||||
{
|
||||
AniList[i]->SetPos(VecFPos{i * width, -120});
|
||||
AniList[i]->SetRenderZOrder(-1000000);
|
||||
std::string layer = ani.layer;
|
||||
layer = layer.substr(1, layer.length() - 2);
|
||||
if (_LayerMap.count(layer))
|
||||
{
|
||||
_LayerMap[layer]->AddChild(AniList[i]);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameMap::InitMapAnimation()
|
||||
{
|
||||
if (_MapInfo.count("animation"))
|
||||
{
|
||||
std::vector<MapAni> aniArr = std::get<std::vector<MapAni>>(_MapInfo["animation"]);
|
||||
for (auto &ani : aniArr)
|
||||
{
|
||||
std::string path = ani.filename;
|
||||
RefPtr<Animation> AniObj = new Animation(path);
|
||||
AniObj->SetPos(VecFPos{ani.XPos, ani.YPos - ani.ZPos});
|
||||
AniObj->SetRenderZOrder(ani.YPos);
|
||||
std::string layer = ani.layer;
|
||||
layer = layer.substr(1, layer.length() - 2);
|
||||
if (_LayerMap.count(layer))
|
||||
{
|
||||
_LayerMap[layer]->AddChild(AniObj);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GameMap::LoadMap(std::string mapName)
|
||||
{
|
||||
// 读取脚本配置
|
||||
InitConfiguration(mapName);
|
||||
// 初始化地板
|
||||
InitTile();
|
||||
// 初始化背景动画
|
||||
InitBackgroundAnimation();
|
||||
// 初始化场景Ani
|
||||
InitMapAnimation();
|
||||
|
||||
RefPtr<CharacterObject> obj = new CharacterObject();
|
||||
obj->SetPosition({620, 200, 0});
|
||||
obj->Construction(0);
|
||||
_LayerMap["normal"]->AddObject(obj);
|
||||
}
|
||||
|
||||
void GameMap::Enter(Scene *scene)
|
||||
{
|
||||
scene->AddChild(_LayerMap["contact"]);
|
||||
scene->AddChild(_LayerMap["distantback"]);
|
||||
scene->AddChild(_LayerMap["middleback"]);
|
||||
scene->AddChild(_LayerMap["bottom"]);
|
||||
scene->AddChild(_LayerMap["closeback"]);
|
||||
scene->AddChild(_LayerMap["normal"]);
|
||||
scene->AddChild(_LayerMap["close"]);
|
||||
scene->AddChild(_LayerMap["cover"]);
|
||||
scene->AddChild(_LayerMap["max"]);
|
||||
|
||||
// TODO
|
||||
int HSU = 230;
|
||||
_LayerMap["contact"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["distantback"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["middleback"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["bottom"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["closeback"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["normal"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["close"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["cover"]->SetPos(VecFPos{0, HSU});
|
||||
_LayerMap["max"]->SetPos(VecFPos{0, HSU});
|
||||
}
|
||||
|
||||
void GameMap::HandleEvents(SDL_Event *e)
|
||||
{
|
||||
}
|
||||
|
||||
void GameMap::Update(float deltaTime)
|
||||
{
|
||||
// this->_Camera->Update(deltaTime);
|
||||
|
||||
// if (this->_Camera)
|
||||
// {
|
||||
// int CamearXpos = this->_Camera->X;
|
||||
// int CamearYpos = this->_Camera->Y;
|
||||
// int CamearZpos = this->_Camera->Z;
|
||||
|
||||
// SDL_Log("Camera Pos: %d, %d", CamearXpos, CamearYpos);
|
||||
// // 遍历
|
||||
// for (auto &pair : _LayerMap)
|
||||
// {
|
||||
// // pair.first 是 key(std::string)
|
||||
// std::string key = pair.first;
|
||||
// // pair.second 是 value(RefPtr<GameMapLayer>)
|
||||
// RefPtr<GameMapLayer> &value = pair.second;
|
||||
|
||||
// if (key == "distantback")
|
||||
// {
|
||||
// value->SetPos(VecPos{(-CamearXpos + 553), (-CamearYpos + CamearZpos + 300 + 120 + 80)});
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// value->SetPos(VecPos{(-CamearXpos + 553), (-CamearYpos + CamearZpos + 300 + 120 + 80)});
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
87
source_game/Actor/Map/GameMap.h
Normal file
87
source_game/Actor/Map/GameMap.h
Normal file
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
#include "Actor/Map/GameMapLayer.h"
|
||||
#include "Asset/Asset_Script.h"
|
||||
#include "Actor/Map/Tile.h"
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <variant>
|
||||
|
||||
class BaseObject;
|
||||
class GameMapCamera;
|
||||
class GameMap
|
||||
{
|
||||
struct BackGroundAni
|
||||
{
|
||||
std::string filename;
|
||||
std::string layer;
|
||||
std::string order;
|
||||
};
|
||||
|
||||
struct MapAni
|
||||
{
|
||||
std::string filename;
|
||||
std::string layer;
|
||||
int XPos;
|
||||
int YPos;
|
||||
int ZPos;
|
||||
};
|
||||
|
||||
struct MapNpc
|
||||
{
|
||||
int id;
|
||||
std::string direction;
|
||||
int XPos;
|
||||
int YPos;
|
||||
int ZPos;
|
||||
};
|
||||
|
||||
struct MapMoveArea
|
||||
{
|
||||
int town;
|
||||
int area;
|
||||
};
|
||||
|
||||
using MapInfoBody = std::variant<
|
||||
int,
|
||||
std::string,
|
||||
std::vector<BackGroundAni>,
|
||||
std::vector<MapAni>,
|
||||
std::vector<MapNpc>,
|
||||
std::vector<MapMoveArea>,
|
||||
std::vector<int>,
|
||||
std::vector<std::string>>;
|
||||
|
||||
public:
|
||||
// 地图信息
|
||||
std::unordered_map<std::string, MapInfoBody> _MapInfo;
|
||||
// 地图路径
|
||||
std::string _MapPath;
|
||||
// 地图文件夹
|
||||
std::string _MapDir;
|
||||
// 地图宽度
|
||||
int _MapLength = 0;
|
||||
// 地图高度
|
||||
int _MapHeight = 0;
|
||||
|
||||
public:
|
||||
// 图层Map 图层类型 显示对象
|
||||
std::unordered_map<std::string, RefPtr<GameMapLayer>> _LayerMap;
|
||||
// 摄像机对象
|
||||
GameMapCamera *_Camera;
|
||||
|
||||
|
||||
public:
|
||||
GameMap(/* args */);
|
||||
~GameMap();
|
||||
|
||||
void LoadMap(std::string mapName);
|
||||
void InitConfiguration(std::string mapName);
|
||||
void InitTile();
|
||||
void InitBackgroundAnimation();
|
||||
void InitMapAnimation();
|
||||
void Enter(Scene *scene);
|
||||
void HandleEvents(SDL_Event *e);
|
||||
void Update(float deltaTime);
|
||||
|
||||
public:
|
||||
};
|
||||
54
source_game/Actor/Map/GameMapCamera.cpp
Normal file
54
source_game/Actor/Map/GameMapCamera.cpp
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "GameMapCamera.h"
|
||||
#include "Actor/Map/GameMap.h"
|
||||
#include "Actor/Object/BaseObject.h"
|
||||
#include <algorithm>
|
||||
|
||||
GameMapCamera::GameMapCamera()
|
||||
{
|
||||
}
|
||||
|
||||
GameMapCamera::~GameMapCamera()
|
||||
{
|
||||
}
|
||||
|
||||
void GameMapCamera::SetParentMap(GameMap *map)
|
||||
{
|
||||
this->_ParentMap = map;
|
||||
_ParentMap->_Camera = this;
|
||||
}
|
||||
|
||||
void GameMapCamera::SetFromActor(BaseObject *actor)
|
||||
{
|
||||
this->_FromActor = actor;
|
||||
}
|
||||
|
||||
void GameMapCamera::Update(float deltaTime)
|
||||
{
|
||||
SyncPosByFromParent(deltaTime);
|
||||
}
|
||||
|
||||
void GameMapCamera::SetPos(int x, int y, int z)
|
||||
{
|
||||
this->X = x;
|
||||
this->Y = y;
|
||||
this->Z = z;
|
||||
}
|
||||
|
||||
void GameMapCamera::AddPos(int x, int y, int z)
|
||||
{
|
||||
this->X += x;
|
||||
this->Y += y;
|
||||
this->Z += z;
|
||||
}
|
||||
|
||||
void GameMapCamera::SyncPosByFromParent(float deltaTime)
|
||||
{
|
||||
if (this->_FromActor != nullptr)
|
||||
{
|
||||
// int R_X, R_Y, R_Z;
|
||||
// R_X = std::min(std::max(this->_FromActor->X, 533), MovableAreaX - 533);
|
||||
// R_Y = std::min(std::max(this->_FromActor->Y, 300), MovableAreaY - 300);
|
||||
// R_Z = 0;
|
||||
// SetPos(R_X, R_Y, R_Z);
|
||||
}
|
||||
}
|
||||
45
source_game/Actor/Map/GameMapCamera.h
Normal file
45
source_game/Actor/Map/GameMapCamera.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
class GameMap;
|
||||
class BaseObject;
|
||||
class GameMapCamera : public Actor
|
||||
{
|
||||
private:
|
||||
GameMap *_ParentMap = nullptr;
|
||||
// 跟随对象
|
||||
BaseObject *_FromActor = nullptr;
|
||||
|
||||
public:
|
||||
// 摄像机坐标
|
||||
int X = 0;
|
||||
int Y = 0;
|
||||
int Z = 0;
|
||||
// 镜头可行坐标
|
||||
int MovableAreaX = 0;
|
||||
int MovableAreaY = 0;
|
||||
// 背景偏移量
|
||||
int BackgroundOffset = 0;
|
||||
// 背景层移动速率
|
||||
int BackgroundMoveSpeed = 1.0;
|
||||
// 人物中线长度
|
||||
int CharacterLineLength = 0;
|
||||
// 摄像机朝向
|
||||
int Direction = 1;
|
||||
// 摄像机朝向时间
|
||||
float DirectionTime = 0.f;
|
||||
// 缩放比率
|
||||
float CameraRate = 1.0;
|
||||
|
||||
public:
|
||||
GameMapCamera();
|
||||
~GameMapCamera();
|
||||
|
||||
void SetParentMap(GameMap *map);
|
||||
void SetFromActor(BaseObject *actor);
|
||||
void Update(float deltaTime);
|
||||
|
||||
void SetPos(int x, int y, int z);
|
||||
void AddPos(int x, int y, int z);
|
||||
|
||||
void SyncPosByFromParent(float deltaTime);
|
||||
};
|
||||
14
source_game/Actor/Map/GameMapLayer.cpp
Normal file
14
source_game/Actor/Map/GameMapLayer.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "GameMapLayer.h"
|
||||
|
||||
GameMapLayer::GameMapLayer()
|
||||
{
|
||||
}
|
||||
|
||||
GameMapLayer::~GameMapLayer()
|
||||
{
|
||||
}
|
||||
|
||||
void GameMapLayer::AddObject(RefPtr<Actor> obj)
|
||||
{
|
||||
this->AddChild(obj);
|
||||
}
|
||||
19
source_game/Actor/Map/GameMapLayer.h
Normal file
19
source_game/Actor/Map/GameMapLayer.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "EngineFrame/Component/Sprite.h"
|
||||
|
||||
class BaseObject;
|
||||
class GameMapLayer : public Actor
|
||||
{
|
||||
private:
|
||||
// 地图对象
|
||||
std::vector<RefPtr<Component>> ObjectManager;
|
||||
|
||||
public:
|
||||
GameMapLayer(/* args */);
|
||||
~GameMapLayer();
|
||||
|
||||
public:
|
||||
void AddObject(RefPtr<Actor> obj); // 添加对象
|
||||
};
|
||||
45
source_game/Actor/Map/Tile.cpp
Normal file
45
source_game/Actor/Map/Tile.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "Tile.h"
|
||||
#include "Tool/Tool_String.h"
|
||||
Tile::Tile()
|
||||
{
|
||||
}
|
||||
|
||||
Tile::Tile(std::string Path) : Sprite()
|
||||
{
|
||||
InitInfo(Path);
|
||||
if (std::get<std::string>(m_data["path"]) == "")
|
||||
m_data["path"] = "sprite/character/common/circlecooltime.img";
|
||||
|
||||
m_texture = new Texture(std::get<std::string>(m_data["path"]), std::get<int>(m_data["idx"]));
|
||||
Sprite::Init();
|
||||
}
|
||||
|
||||
Tile::~Tile()
|
||||
{
|
||||
}
|
||||
|
||||
void Tile::SetPos(VecFPos pos)
|
||||
{
|
||||
pos.y += std::get<int>(m_data["pos"]);
|
||||
Sprite::SetPos(pos);
|
||||
}
|
||||
|
||||
void Tile::InitInfo(std::string Path)
|
||||
{
|
||||
ScriptData Data = AssetManager::GetInstance().GetScriptInfo(Path);
|
||||
m_data["pos"] = 0;
|
||||
while (!Data.IsEnd())
|
||||
{
|
||||
std::string Segment = Data.Get();
|
||||
if (Segment == "[IMAGE]")
|
||||
{
|
||||
std::string PathBuf = Tool_toLowerCase(Data.Get());
|
||||
m_data["path"] = "sprite/" + PathBuf;
|
||||
m_data["idx"] = std::stoi(Data.Get());
|
||||
}
|
||||
else if (Segment == "[img pos]")
|
||||
{
|
||||
m_data["pos"] = std::stoi(Data.Get());
|
||||
}
|
||||
}
|
||||
}
|
||||
22
source_game/Actor/Map/Tile.h
Normal file
22
source_game/Actor/Map/Tile.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "Asset/AssetManager.h"
|
||||
#include "EngineFrame/Component/Sprite.h"
|
||||
class Tile : public Sprite
|
||||
{
|
||||
|
||||
using TileInfoBody = std::variant<
|
||||
int,
|
||||
std::string>;
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, TileInfoBody> m_data;
|
||||
|
||||
public:
|
||||
Tile(/* args */);
|
||||
Tile(std::string Path);
|
||||
~Tile();
|
||||
|
||||
void SetPos(VecFPos pos) override;
|
||||
|
||||
void InitInfo(std::string Path);
|
||||
};
|
||||
50
source_game/Actor/Object/ActiveObject.cpp
Normal file
50
source_game/Actor/Object/ActiveObject.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
#include "ActiveObject.h"
|
||||
|
||||
void ActiveObject::SetPosition(VecFPos3 pos)
|
||||
{
|
||||
BaseObject::SetPosition(pos);
|
||||
BaseObject::SetRenderZOrder(this->Position.y);
|
||||
}
|
||||
|
||||
void ActiveObject::SetYpos(float y)
|
||||
{
|
||||
BaseObject::SetYpos(y);
|
||||
BaseObject::SetRenderZOrder(this->Position.y);
|
||||
}
|
||||
|
||||
void ActiveObject::SetSpeed(VecSpeed3 speed)
|
||||
{
|
||||
this->Speed = speed;
|
||||
}
|
||||
|
||||
VecSpeed3 ActiveObject::GetSpeed()
|
||||
{
|
||||
return this->Speed;
|
||||
}
|
||||
|
||||
void ActiveObject::Update(float deltaTime)
|
||||
{
|
||||
// X Y 轴方向的加速度计算
|
||||
if (Speed.x != 0 || Speed.y != 0)
|
||||
{
|
||||
MoveBy(Speed.x * deltaTime, Speed.y * deltaTime, 0);
|
||||
}
|
||||
// Z轴只在Z轴大于0时 或者 Z轴速度向上时才会有重力
|
||||
if (Position.z > 0 || Speed.z > 0)
|
||||
{
|
||||
// TODO 还没有写角色属性 要读了角色属性以后才是正确的
|
||||
float Gravity = 68000.0 / 1000.0 * 15.0;
|
||||
Speed.z -= Gravity * deltaTime;
|
||||
MoveBy(0, 0, Speed.z * deltaTime);
|
||||
}
|
||||
// Z轴小于0时要修正
|
||||
else if (Position.z < 0)
|
||||
{
|
||||
Position.z = 0;
|
||||
Speed.z = 0;
|
||||
SetPosition(Position);
|
||||
SDL_LogError(0, "修正Z轴");
|
||||
}
|
||||
// 执行父对象的更新
|
||||
BaseObject::Update(deltaTime);
|
||||
}
|
||||
19
source_game/Actor/Object/ActiveObject.h
Normal file
19
source_game/Actor/Object/ActiveObject.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
#include "Actor/Object/BaseObject.h"
|
||||
|
||||
class ActiveObject : public BaseObject
|
||||
{
|
||||
|
||||
public:
|
||||
VecSpeed3 Speed;
|
||||
|
||||
public:
|
||||
void SetPosition(VecFPos3 pos) override;
|
||||
void SetYpos(float y) override;
|
||||
|
||||
void SetSpeed(VecSpeed3 speed);
|
||||
VecSpeed3 GetSpeed();
|
||||
|
||||
public:
|
||||
void Update(float deltaTime) override;
|
||||
};
|
||||
97
source_game/Actor/Object/BaseObject.cpp
Normal file
97
source_game/Actor/Object/BaseObject.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
#include "BaseObject.h"
|
||||
|
||||
BaseObject::BaseObject()
|
||||
{
|
||||
Init(); // 调用了RenderBase的Init函数 对象才会被执行回调
|
||||
SetAnchor({0.5f, 0.5f});
|
||||
}
|
||||
|
||||
BaseObject::~BaseObject()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseObject::SetPosition(VecFPos3 pos)
|
||||
{
|
||||
this->Position = pos;
|
||||
Actor::SetPos(VecFPos{this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
VecFPos3 BaseObject::GetPosition()
|
||||
{
|
||||
return this->Position;
|
||||
}
|
||||
|
||||
void BaseObject::SetXpos(float x)
|
||||
{
|
||||
this->Position.x = x;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetYpos(float y)
|
||||
{
|
||||
this->Position.y = y;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetZpos(float z)
|
||||
{
|
||||
this->Position.z = z;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
int BaseObject::GetXpos()
|
||||
{
|
||||
return this->Position.x;
|
||||
}
|
||||
|
||||
int BaseObject::GetYpos()
|
||||
{
|
||||
return this->Position.y;
|
||||
}
|
||||
|
||||
int BaseObject::GetZpos()
|
||||
{
|
||||
return this->Position.z;
|
||||
}
|
||||
|
||||
void BaseObject::MoveBy(VecFPos3 pos)
|
||||
{
|
||||
this->Position.x += pos.x;
|
||||
this->Position.y += pos.y;
|
||||
this->Position.z += pos.z;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::MoveBy(float x, float y, float z)
|
||||
{
|
||||
this->Position.x += x;
|
||||
this->Position.y += y;
|
||||
this->Position.z += z;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetDirection(int dir)
|
||||
{
|
||||
this->Direction = dir;
|
||||
VecFPos sc = GetScale();
|
||||
// 朝右
|
||||
if (dir == 0)
|
||||
{
|
||||
SetScale(VecFPos({SDL_fabsf(sc.x), sc.y}));
|
||||
}
|
||||
// 朝左
|
||||
else if (dir == 1)
|
||||
{
|
||||
SetScale(VecFPos({-SDL_fabsf(sc.x), sc.y}));
|
||||
}
|
||||
}
|
||||
|
||||
int BaseObject::GetDirection()
|
||||
{
|
||||
return this->Direction;
|
||||
}
|
||||
|
||||
ObjectVars &BaseObject::GetObjectVars()
|
||||
{
|
||||
return _ObjectVars;
|
||||
}
|
||||
37
source_game/Actor/Object/BaseObject.h
Normal file
37
source_game/Actor/Object/BaseObject.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "Asset/Common/ObjectVars.h"
|
||||
class BaseObject : public Actor
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
VecFPos3 Position; // 位置
|
||||
int Direction = 0; // 方向
|
||||
|
||||
public:
|
||||
BaseObject(/* args */);
|
||||
~BaseObject();
|
||||
|
||||
// 数据储存器
|
||||
ObjectVars _ObjectVars;
|
||||
|
||||
public:
|
||||
virtual void SetPosition(VecFPos3 pos);
|
||||
virtual void SetXpos(float x);
|
||||
virtual void SetYpos(float y);
|
||||
virtual void SetZpos(float z);
|
||||
|
||||
VecFPos3 GetPosition();
|
||||
int GetXpos();
|
||||
int GetYpos();
|
||||
int GetZpos();
|
||||
|
||||
void MoveBy(VecFPos3 pos);
|
||||
void MoveBy(float x, float y, float z);
|
||||
|
||||
void SetDirection(int dir);
|
||||
int GetDirection();
|
||||
|
||||
ObjectVars &GetObjectVars();
|
||||
};
|
||||
57
source_game/Actor/Object/CharacterObject.cpp
Normal file
57
source_game/Actor/Object/CharacterObject.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "CharacterObject.h"
|
||||
#include "Asset/Squirrel/SquirrelManager.h"
|
||||
|
||||
CharacterObject::~CharacterObject()
|
||||
{
|
||||
}
|
||||
|
||||
void CharacterObject::Update(float deltaTime)
|
||||
{
|
||||
ActiveObject::Update(deltaTime);
|
||||
}
|
||||
|
||||
void CharacterObject::Render()
|
||||
{
|
||||
ActiveObject::Render();
|
||||
}
|
||||
|
||||
void CharacterObject::Construction(int job)
|
||||
{
|
||||
// 创建装备管理器
|
||||
_EquipmentManager = new Chr_Equipment();
|
||||
_EquipmentManager->Init(this);
|
||||
// 创建动画管理器(一定要先创建装备管理器再创建动画管理器 因为需要读取身上的装备)
|
||||
_AnimationManager = new Chr_Animation();
|
||||
_AnimationManager->Init(this);
|
||||
// 创建状态机
|
||||
_StateMachine = new Chr_StateMachine();
|
||||
_StateMachine->Init(this);
|
||||
|
||||
// 开启控制器
|
||||
EnableController();
|
||||
}
|
||||
|
||||
void CharacterObject::EnableController()
|
||||
{
|
||||
this->_Controller = new Chr_Controller();
|
||||
_Controller->Init(this);
|
||||
AddChild(_Controller);
|
||||
}
|
||||
|
||||
void CharacterObject::DisableController()
|
||||
{
|
||||
this->RemoveChild(_Controller);
|
||||
this->_Controller = nullptr;
|
||||
}
|
||||
|
||||
void CharacterObject::ControllerMsg(CONTROLLER_MSG_TYPE msgType, void *msgData)
|
||||
{
|
||||
// 摇杆移动(左)
|
||||
if (msgType == CONTROLLER_MSG_TYPE::CONTROLLER_MSG_TYPE_LEFT_JOYSTICK_MOVE)
|
||||
{
|
||||
VecFPos *pos = (VecFPos *)msgData;
|
||||
std::vector<float> movedata = {pos->x, pos->y};
|
||||
this->GetObjectVars().SetArray("_move_data_", movedata);
|
||||
this->_StateMachine->ChangeState(BASE_STATE::MOVE);
|
||||
}
|
||||
}
|
||||
39
source_game/Actor/Object/CharacterObject.h
Normal file
39
source_game/Actor/Object/CharacterObject.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "Actor/Object/ActiveObject.h"
|
||||
#include "Asset/Character/Chr_Animation.h"
|
||||
#include "Asset/Character/Chr_Equipment.h"
|
||||
#include "Asset/Character/Chr_Controller.h"
|
||||
#include "Asset/Character/Chr_StateMachine.h"
|
||||
#include "Global/Global_Enum.h"
|
||||
class CharacterObject : public ActiveObject
|
||||
{
|
||||
public:
|
||||
// 角色动画管理器
|
||||
RefPtr<Chr_Animation> _AnimationManager = nullptr;
|
||||
// 装备管理器
|
||||
RefPtr<Chr_Equipment> _EquipmentManager = nullptr;
|
||||
// 角色控制器
|
||||
RefPtr<Chr_Controller> _Controller = nullptr;
|
||||
// 角色状态机
|
||||
RefPtr<Chr_StateMachine> _StateMachine = nullptr;
|
||||
// 职业
|
||||
int Job = 0;
|
||||
// 转职职业 如果是-1则没有转职
|
||||
int GrowType = -1;
|
||||
|
||||
public:
|
||||
~CharacterObject();
|
||||
|
||||
public:
|
||||
void Update(float deltaTime) override;
|
||||
void Render() override;
|
||||
|
||||
// 通过职业创建角色
|
||||
void Construction(int job);
|
||||
// 开启控制器
|
||||
void EnableController();
|
||||
// 关闭控制器
|
||||
void DisableController();
|
||||
// 控制器信息
|
||||
void ControllerMsg(CONTROLLER_MSG_TYPE msgType, void* msgData);
|
||||
};
|
||||
Reference in New Issue
Block a user