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;
}

View File

@@ -10,6 +10,7 @@ class BaseObject;
class GameMapCamera;
class GameMap : public Actor
{
public:
struct BackGroundAni
{
std::string filename;
@@ -37,13 +38,14 @@ class GameMap : public Actor
struct MapMoveArea
{
int town;
int area;
int town = 0;
int area = 0;
};
using MapInfoBody = std::variant<
int,
std::string,
std::map<std::string, bool>,
std::vector<BackGroundAni>,
std::vector<MapAni>,
std::vector<MapNpc>,
@@ -66,6 +68,8 @@ public:
int _MapHeight = 0;
// 可行区域
std::vector<SDL_FRect> _MovableArea;
// 传送区域
std::vector<SDL_FRect> _MoveArea;
// 调试模式
bool _DebugMode = false;
@@ -81,17 +85,36 @@ public:
GameMap(/* args */);
~GameMap();
/**加载地图 (地图路径) */
void LoadMap(std::string mapName);
/**进入地图执行的操作 */
void Enter();
/**重载OnUpdate函数 */
void OnUpdate(float deltaTime) override;
public:
/**读取脚本配置 */
void InitConfiguration(std::string mapName);
/**初始化地板 */
void InitTile();
/**初始化背景动画 */
void InitBackgroundAnimation();
/**初始化地图动画 */
void InitMapAnimation();
/**初始化移动区域 */
void InitVirtualMovableArea();
void Enter(Scene *scene);
void Update(float deltaTime) override;
/**初始化传送区域 */
void InitMoveArea();
/**添加对象 */
void AddObject(RefPtr<BaseObject> object);
public:
// 检查是否可移动
VecFPos3 CheckIsItMovable(VecFPos3 CurPos, VecFPos3 PosOffset);
// 检查是否进入传送区域
MapMoveArea CheckIsItMoveArea(VecFPos3 CurPos);
/**获取移动区域信息 */
std::vector<GameMap::MapMoveArea> &GetMoveAreaInfo();
/**获取移动区域坐标信息 */
SDL_Rect GetMovablePositionArea(int Index);
};

View File

@@ -1,35 +1,47 @@
#include "GameMapLayer.h"
#include "EngineCore/Game.h"
void GameMapLayer::Render()
{
Actor::Render();
// RenderManager *renderer = Game::GetInstance().GetRenderer();
// // 自身坐标
// float Xpos = GetIterationPos().x + GetPos().x;
// float Ypos = GetIterationPos().y + GetPos().y;
// for (auto &info : this->FeasibleAreaInfoList)
// {
// SDL_Rect buf;
// buf.x = info.x + Xpos;
// buf.y = info.y + Ypos;
// buf.w = info.w;
// buf.h = info.h;
// //TODO: 渲染可行区域
// }
// 自身坐标
glm::vec2 Pos = this->ConvertToWorld({0, 0});
// 可行区域
for (auto &info : this->FeasibleAreaInfoList)
{
SDL_Rect buf;
buf.x = info.x + Pos.x;
buf.y = info.y + Pos.y;
buf.w = info.w;
buf.h = info.h;
glm::vec4 red = {1.0f, 0.0f, 0.0f, 0.4f}; // RGBA红色不透明基础色
Game::GetInstance().GetRenderer()->DrawRect(&buf, red);
}
// 移动区域
for (auto &info : this->MoveAreaInfoList)
{
SDL_Rect buf;
buf.x = info.x + Pos.x;
buf.y = info.y + Pos.y;
buf.w = info.w;
buf.h = info.h;
glm::vec4 red = {0.0f, 0.0f, 1.0f, 0.4f}; // RGBA红色不透明基础色
Game::GetInstance().GetRenderer()->DrawRect(&buf, red);
}
}
void GameMapLayer::AddDebugFeasibleAreaInfo(Vec2 pos, VecSize size)
void GameMapLayer::AddDebugFeasibleAreaInfo(Vec2 pos, VecSize size, int Type)
{
SDL_Rect info;
info.x = pos.x;
info.y = pos.y;
info.w = size.width;
info.h = size.height;
this->FeasibleAreaInfoList.push_back(info);
if (Type == 0)
this->FeasibleAreaInfoList.push_back(info);
else if (Type == 1)
this->MoveAreaInfoList.push_back(info);
}
void GameMapLayer::AddObject(RefPtr<Actor> obj)

View File

@@ -9,12 +9,14 @@ class GameMapLayer : public Actor
private:
// 可行区域信息
std::vector<SDL_Rect> FeasibleAreaInfoList;
// 移动区域信息
std::vector<SDL_Rect> MoveAreaInfoList;
public:
// 重载Render以实现绘制可行区域
// 重载OnRender以实现绘制可行区域
void Render() override;
// 添加调试可行区域信息
void AddDebugFeasibleAreaInfo(Vec2 pos, VecSize size);
void AddDebugFeasibleAreaInfo(Vec2 pos, VecSize size, int Type);
public:
void AddObject(RefPtr<Actor> obj); // 添加对象

View File

@@ -0,0 +1,67 @@
#include "GameTown.h"
#include "Global/Global_Game.h"
GameTown::GameTown()
{
}
GameTown::~GameTown()
{
}
void GameTown::Init(int Index)
{
GlobalTownScript::TownConfig *Config = Global_Game::GetInstance().GetTownInfo(Index);
if (Config)
{
this->Name_ = Config->TownName;
this->Entering_Title = Config->Entering_Title;
this->Entering_Cutscene = Config->Entering_Cutscene;
this->NeedLevel = Config->NeedLevel;
this->NeedQuestID = Config->NeedQuestID;
// 构造全部地图
for (auto &it : Config->AreaLst)
{
MapInfo info;
info.Map = new GameMap();
info.Map->LoadMap(it.second.MapPath);
info.Type = it.second.MapType;
MapList_.push_back(info);
if (info.Type == "gate")
{
SariaRoomID = it.first;
SariaRoomPos = {it.second.GenerateXPos, it.second.GenerateYPos, 0};
}
}
}
}
void GameTown::AddCharacter(RefPtr<CharacterObject> Chr, int Index)
{
int TargetMapIndex = Index;
if (Index == -2)
{
TargetMapIndex = SariaRoomID;
}
// 如果有当前调用中的地图
if (CurMapIndex != -1)
{
RemoveChild(MapList_[CurMapIndex].Map);
}
// 如果角色已有父对象
if (Chr->GetParent())
{
Chr->RemoveFromParent();
}
// 调用赛利亚房间地图
AddChild(MapList_[TargetMapIndex].Map);
if(Index == -2)Chr->SetPosition(SariaRoomPos);
MapList_[TargetMapIndex].Map->Enter();
MapList_[TargetMapIndex].Map->AddObject(Chr);
CurMapIndex = TargetMapIndex;
}
void GameTown::OnUpdate(float DeltaTime)
{
Actor::OnUpdate(DeltaTime);
}

View File

@@ -0,0 +1,54 @@
#pragma once
#include "EngineFrame/Base/Actor.h"
#include "Actor/Map/GameMap.h"
#include "Actor/Object/CharacterObject.h"
class GameTown : public Actor
{
public:
struct MapInfo
{
/**地图对象 */
RefPtr<GameMap> Map;
/**地图类型 */
std::string Type;
};
private:
/**城镇名称 */
std::string Name_;
/**进入城镇时的标题Img */
std::string Entering_Title;
/**进入城镇时的过场Img */
std::string Entering_Cutscene;
/**需要的任务进入条件 */
int NeedQuestID;
/**需要的等级进入条件 */
int NeedLevel;
/**赛利亚房间编号 */
int SariaRoomID = -1;
/**赛丽亚房间生成坐标 */
VecFPos3 SariaRoomPos;
/**地图集合 */
std::vector<MapInfo> MapList_;
/**当前所在地图 */
int CurMapIndex = -1;
public:
GameTown(/* args */);
~GameTown();
/**通过城镇ID初始化 */
void Init(int Index);
public:
/**获取当前所在区域 */
int GetCurAreaIndex() { return CurMapIndex; };
/**获取区域对象 */
RefPtr<GameMap> GetArea(int Index) { return MapList_[Index].Map; };
/**添加角色 (无区域编号 上线进入城镇 或 使用传送时进入城镇 有区域编号 移动进入的)*/
void AddCharacter(RefPtr<CharacterObject> Chr, int AreaIndex = -2);
void OnUpdate(float DeltaTime) override;
};

View File

@@ -0,0 +1,100 @@
#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();
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include "EngineFrame/Scene/Scene.h"
#include "Actor/Map/GameTown.h"
class GameWorld : public Scene
{
private:
/**城镇Map */
std::map<int, RefPtr<GameTown>> m_TownMap;
/**我的角色 */
RefPtr<CharacterObject> m_MyCharacter;
/**当前城镇 */
int m_CurTown;
public:
GameWorld();
~GameWorld();
void Enter() override;
void Exit() override;
/**初始化 */
void Init() override;
/**加入角色 */
void AddCharacter(RefPtr<CharacterObject> Chr, int TownId);
/**移动角色 */
void MoveCharacter(RefPtr<CharacterObject> Chr, int TownId,int Area);
public:
static GameWorld *GetWorld();
};

View File

@@ -4,56 +4,29 @@
#include "Actor/Map/GameMap.h"
#include "Global/Global_Game.h"
// 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();
// m_texture->Init(std::get<std::string>(m_data["path"]), std::get<int>(m_data["idx"]));
// Sprite::Init();
// this->imgPath = Path;
// }
Tile::Tile(GameMap *parentMap, std::vector<std::string> normal, std::vector<std::string> ex)
{
m_parentMap = parentMap;
m_texture = new Texture();
// 通过父对象获取地图大小
int Width = m_parentMap->_MapLength;
int Height = m_parentMap->_MapHeight;
// 通过大小纹理大小
m_texture->Init(VecSize(Width, Height));
m_rect = {0, 0, (float)Width, (float)Height};
Actor::Init();
glGenFramebuffers(1, &m_fbo);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
// 将目标纹理附加到FBO的颜色附着点
glFramebufferTexture2D(
GL_FRAMEBUFFER, // 帧缓冲类型
GL_COLOR_ATTACHMENT0, // 颜色附着点(可多个,这里用第一个)
GL_TEXTURE_2D, // 纹理类型
m_texture->getID(), // 目标纹理ID
0 // 多级渐远纹理级别
);
glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
for (size_t i = 0; i < normal.size(); i++)
{
InitTile(normal[i], i);
}
m_tileX = normal.size();
for (size_t i = 0; i < ex.size(); i++)
{
InitExTile(ex[i], i);
}
SetPos(Vec2(0, 80 - 200 - std::get<int>(m_parentMap->_MapInfo["background_pos"])));
// 有可能没写地板
if (m_BasicTileWidth == 0)
m_BasicTileWidth = 224 * normal.size();
// 通过父对象获取地图大小
m_parentMap->_MapLength = m_BasicTileWidth;
m_parentMap->_MapHeight = m_BasicTileHeight + m_ExTileHeight;
Canvas::Init({(int)m_BasicTileWidth, (int)m_BasicTileHeight + (int)m_ExTileHeight + (int)m_tileY});
DrawTile();
}
Tile::~Tile()
@@ -66,6 +39,9 @@ void Tile::InitTile(std::string Path, int Index)
TileInfo data;
InitInfo(Path, data);
// 读取信息
float ImgOffset = std::get<int>(data["pos"]);
if (ImgOffset > m_tileY)
m_tileY = ImgOffset;
std::string path = std::get<std::string>(data["path"]);
int index = std::get<int>(data["idx"]);
// 如果是个空地板 给他默认赋值
@@ -77,18 +53,20 @@ void Tile::InitTile(std::string Path, int Index)
// 判断是否存在标识符对应的纹理,如果不存在则创建
if (m_imgMap.find(key) == m_imgMap.end())
{
RefPtr<Texture> tex = new Texture();
tex->Init(path, index);
RefPtr<Sprite> tex = new Sprite(path, index);
m_imgMap[key] = tex;
}
// 构造绘制信息
// 横向地板数量增加
m_Tile_X_Count++;
// 基础地板宽度增加
m_BasicTileWidth += m_imgMap[key]->GetTexture()->getFrameSize().width;
// 取基础地板最高的高度
if (m_imgMap[key]->GetTexture()->getFrameSize().height > m_BasicTileHeight)
m_BasicTileHeight = m_imgMap[key]->GetTexture()->getFrameSize().height;
DrawInfo info;
info.rect.x = Index * m_imgMap[key]->getSize().width + m_imgMap[key]->getPos().x;
info.rect.y = m_imgMap[key]->getPos().y;
info.rect.w = m_imgMap[key]->getSize().width;
info.rect.h = m_imgMap[key]->getSize().height;
info.texture = key;
// 添加到绘制信息列表
info.sprite_ = key;
info.pos = {Index * 224, ImgOffset};
m_drawQueue.push_back(info);
}
@@ -98,6 +76,9 @@ void Tile::InitExTile(std::string Path, int Index)
TileInfo data;
InitInfo(Path, data);
// 读取信息
float ImgOffset = std::get<int>(data["pos"]);
if (ImgOffset > m_tileY)
m_tileY = ImgOffset;
std::string path = std::get<std::string>(data["path"]);
int index = std::get<int>(data["idx"]);
// 如果是个空地板 给他默认赋值
@@ -109,28 +90,23 @@ void Tile::InitExTile(std::string Path, int Index)
// 判断是否存在标识符对应的纹理,如果不存在则创建
if (m_imgMap.find(key) == m_imgMap.end())
{
RefPtr<Texture> tex = new Texture();
tex->Init(path, index);
RefPtr<Sprite> tex = new Sprite(path, index);
m_imgMap[key] = tex;
}
// 构造绘制信息
// 如果进了这个函数默认有1格
if (m_ExTileHeight == 0)
m_ExTileHeight = 120;
// Ex地板高度120一格
if (((Index / m_Tile_X_Count) * 120) > m_ExTileHeight)
m_ExTileHeight = ((Index / m_Tile_X_Count) * 120);
DrawInfo info;
info.rect.x = Index * m_imgMap[key]->getSize().width + m_imgMap[key]->getPos().x;
info.rect.y = m_imgMap[key]->getPos().y + 560 + ((Index / m_tileX) * 120);
info.rect.w = m_imgMap[key]->getSize().width;
info.rect.h = m_imgMap[key]->getSize().height;
info.texture = key;
// 添加到绘制信息列表
info.sprite_ = key;
info.pos = {Index * 224, m_tileY + m_BasicTileHeight + ((Index / m_Tile_X_Count) * 120)};
m_drawQueue.push_back(info);
}
// void Tile::SetPos(Vec2 pos)
// {
// // pos.y += std::get<int>(m_data["pos"]);
// // Sprite::SetPos(pos);
// }
void Tile::InitInfo(std::string Path, TileInfo &m_data)
{
ScriptData Data = AssetManager::GetInstance().GetScriptInfo(Path);
@@ -151,46 +127,13 @@ void Tile::InitInfo(std::string Path, TileInfo &m_data)
}
}
void Tile::PreRender()
void Tile::DrawTile()
{
m_rect.x = GetWorldPos().x;
m_rect.y = GetWorldPos().y;
//计算摄像机位置 知道自己需要渲染多大的区域
// m_srcRect = {0, 0, m_parentMap->_MapLength, m_parentMap->_MapHeight};
// Vec2 Pos = Global_Game::GetInstance().GetCamera() -> _currentPosition;
// m_srcRect = {Pos.x < 1067 ? 0 : Pos.x - 1067, Pos.y, 1067, 600};
}
void Tile::Render()
{
RenderManager *renderer = Game::GetInstance().GetRenderer();
SDL_FPoint AnchorPos = {0, 0};
if (!m_isBuild)
BeginDraw();
for (auto &info : m_drawQueue)
{
// 保存原始的正交矩阵 设置纹理对应的正交矩阵 和 视口
auto oldOm = Game::GetInstance().GetRenderer()->GetOrthoMatrix();
Game::GetInstance().GetRenderer()->SetOrthoMatrix(glm::ortho(0.0f, (float)m_texture->getSize().width, (float)m_texture->getSize().height, 0.0f, -1.0f, 1.0f));
glViewport(0, 0, m_texture->getSize().width, m_texture->getSize().height);
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo); // 绑定FBO
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (auto &info : m_drawQueue)
{
renderer->DrawTexture(m_imgMap[info.texture], nullptr, &info.rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
}
glBindFramebuffer(GL_FRAMEBUFFER, 0); // 解绑FBO
// 还原原始的正交矩阵 和 视口
Game::GetInstance().GetRenderer()->SetOrthoMatrix(oldOm);
glViewport(0, 0, Game::GetInstance().Screen_W, Game::GetInstance().Screen_H);
m_isBuild = true;
m_imgMap[info.sprite_]->SetPosition(info.pos);
DrawActor(m_imgMap[info.sprite_]);
}
renderer->SetCurrentShaderProgram("flip_y");
renderer->DrawTexture(m_texture, nullptr, &m_rect, 0.f, &AnchorPos, SDL_FLIP_NONE);
renderer->SetCurrentShaderProgram("normal");
EndDraw();
}

View File

@@ -1,9 +1,12 @@
#pragma once
#include "Asset/AssetManager.h"
#include "EngineFrame/Component/Canvas.h"
#include "EngineFrame/Component/Sprite.h"
class GameMap;
class Tile : public Actor
class Tile : public Canvas
{
public:
struct ImgKey
{
std::string img;
@@ -20,46 +23,40 @@ class Tile : public Actor
};
struct DrawInfo
{
ImgKey texture;
SDL_FRect rect = {0, 0, 0, 0};
ImgKey sprite_;
glm::vec2 pos;
};
public:
using TileInfoBody = std::variant<int,std::string>;
using TileInfoBody = std::variant<int, std::string>;
using TileInfo = std::unordered_map<std::string, TileInfoBody>;
private:
/**纹理 */
RefPtr<Texture> m_texture = nullptr;
/**FBO */
GLuint m_fbo = 0;
/**地图父对象 */
GameMap* m_parentMap;
/**绘制纹理集 */
std::map<ImgKey, RefPtr<Texture>> m_imgMap;
/**父对象地图 */
GameMap *m_parentMap;
/**绘制精灵集 */
std::map<ImgKey, RefPtr<Sprite>> m_imgMap;
/**绘制队列 */
std::vector<DrawInfo> m_drawQueue;
/**渲染大小 */
SDL_Rect m_srcRect;
SDL_FRect m_rect;
/**大纹理构造完成Flag */
bool m_isBuild = false;
/**基础地板宽度 */
float m_BasicTileWidth = 0;
/**基础地板高度 */
float m_BasicTileHeight = 0;
/**Ex地板高度 */
float m_ExTileHeight = 0;
/**Tile横轴个数 */
int m_tileX = 0;
/**横向地板数量 */
int m_Tile_X_Count = 0;
/**纵向最大偏移 */
float m_tileY = 0;
public:
Tile(GameMap *parentMap, std::vector<std::string> normal,std::vector<std::string> ex);
Tile(GameMap *parentMap, std::vector<std::string> normal, std::vector<std::string> ex);
~Tile();
void InitTile(std::string Path,int Index);
void InitTile(std::string Path, int Index);
void InitExTile(std::string Path, int Index);
void InitInfo(std::string Path, TileInfo &m_data);
void PreRender() override;
void Render() override;
// void SetPos(Vec2 pos) override;
void DrawTile();
};

View File

@@ -19,7 +19,7 @@ VecSpeed3 ActiveObject::GetSpeed()
return this->Speed;
}
void ActiveObject::Update(float deltaTime)
void ActiveObject::OnUpdate(float deltaTime)
{
VecFPos3 MovePos;
@@ -51,6 +51,4 @@ void ActiveObject::Update(float deltaTime)
}
MoveBy(MovePos);
BaseObject::Update(deltaTime);
}

View File

@@ -11,13 +11,12 @@ public:
int _zRemainderMove = 0;
public:
void
SetPosition(VecFPos3 pos) override;
void SetPosition(VecFPos3 pos) override;
void SetYpos(int y) override;
void SetSpeed(VecSpeed3 speed);
VecSpeed3 GetSpeed();
public:
void Update(float deltaTime) override;
void OnUpdate(float deltaTime) override;
};

View File

@@ -3,7 +3,7 @@
BaseObject::BaseObject()
{
Init(); // 调用了RenderBase的Init函数 对象才会被执行回调
// 对象要使用中心锚点
SetAnchor({0.5f, 0.5f});
}
@@ -11,21 +11,16 @@ BaseObject::~BaseObject()
{
}
void BaseObject::Update(float deltaTime)
{
Actor::Update(deltaTime);
}
void BaseObject::SetPosition(VecFPos3 pos)
{
if(pos == this->Position)
return;
if (pos.y != this->Position.y)
{
SetRenderZOrder(pos.y); // 设置渲染顺序
SetZOrder(pos.y); // 设置渲染顺序
}
this->Position = pos;
SetPos(Vec2{this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition(this->Position.x, this->Position.y - this->Position.z);
}
VecFPos3 BaseObject::GetPosition()
@@ -38,7 +33,7 @@ void BaseObject::SetXpos(int x)
if (x == this->Position.x)
return;
this->Position.x = x;
SetPos({this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition(this->Position.x, this->Position.y - this->Position.z);
}
void BaseObject::SetYpos(int y)
@@ -47,10 +42,10 @@ void BaseObject::SetYpos(int y)
return;
if (y != this->Position.y)
{
SetRenderZOrder(y); // 设置渲染顺序
SetZOrder(y); // 设置渲染顺序
}
this->Position.y = y;
SetPos({this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition(this->Position.x, this->Position.y - this->Position.z);
}
void BaseObject::SetZpos(int z)
@@ -58,7 +53,7 @@ void BaseObject::SetZpos(int z)
if (z == this->Position.z)
return;
this->Position.z = z;
SetPos({this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition(this->Position.x, this->Position.y - this->Position.z);
}
int BaseObject::GetXpos()
@@ -84,11 +79,11 @@ void BaseObject::MoveBy(VecFPos3 pos)
return;
if (RealPos.y != this->Position.y)
{
SetRenderZOrder(RealPos.y); // 设置渲染顺序
SetZOrder(RealPos.y); // 设置渲染顺序
}
if (RealPos != this->Position){
this->Position = RealPos;
SetPos({this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition({this->Position.x, this->Position.y - this->Position.z});
}
}
@@ -100,28 +95,28 @@ void BaseObject::MoveBy(int x, int y, int z)
return;
if (RealPos.y != this->Position.y)
{
SetRenderZOrder(RealPos.y); // 设置渲染顺序
SetZOrder(RealPos.y); // 设置渲染顺序
}
if (RealPos != this->Position)
{
this->Position = RealPos;
SetPos({this->Position.x, this->Position.y - this->Position.z});
Actor::SetPosition({this->Position.x, this->Position.y - this->Position.z});
}
}
void BaseObject::SetDirection(int dir)
{
this->Direction = dir;
Vec2 sc = GetScale();
glm::vec2 sc = GetScale();
// 朝右
if (dir == 0)
{
SetScale(Vec2({SDL_fabsf(sc.x), sc.y}));
SetScale(SDL_fabsf(sc.x), sc.y);
}
// 朝左
else if (dir == 1)
{
SetScale(Vec2({-SDL_fabsf(sc.x), sc.y}));
SetScale(-SDL_fabsf(sc.x), sc.y);
}
}

View File

@@ -16,8 +16,6 @@ public:
BaseObject(/* args */);
~BaseObject();
void Update(float deltaTime) override;
// 数据储存器
ObjectVars _ObjectVars;

View File

@@ -1,5 +1,7 @@
#include "CharacterObject.h"
#include "Asset/Squirrel/SquirrelManager.h"
#include "Actor/Map/GameMap.h"
#include "Actor/Map/GameWorld.h"
CharacterObject::~CharacterObject()
{
@@ -12,9 +14,6 @@ void CharacterObject::Construction(int job)
// 创建装备管理器
_EquipmentManager = new Chr_Equipment();
_EquipmentManager->Init(this);
// // 创建阴影对象
// _Shadow = new Chr_Shadow();
// _Shadow->Init(this);
// 创建动画管理器(一定要先创建装备管理器再创建动画管理器 因为需要读取身上的装备)
_AnimationManager = new Chr_Animation();
_AnimationManager->Init(this);
@@ -51,19 +50,35 @@ void CharacterObject::ControllerMsg(CONTROLLER_MSG_TYPE msgType, void *msgData)
}
}
void CharacterObject::Update(float deltaTime)
void CharacterObject::SetPosition(VecFPos3 pos)
{
ActiveObject::Update(deltaTime);
}
void CharacterObject::SetPos(Vec2 pos)
{
BaseObject::SetPos(pos);
if(_Shadow)_Shadow->SetPos(this->GetPos());
BaseObject::SetPosition(pos);
}
void CharacterObject::SetDirection(int dir)
{
BaseObject::SetDirection(dir);
if(_Shadow)_Shadow->SetDirection(this->GetDirection());
}
void CharacterObject::OnUpdate(float deltaTime)
{
ActiveObject::OnUpdate(deltaTime);
// 判断是否要进行区域移动
if (!IsTeleportArea)
{
GameMap::MapMoveArea Info = this->_AffMap->CheckIsItMoveArea(this->GetPosition());
if (Info.town != -2 && Info.area != -2)
{
IsTeleportArea = true;
// 调用世界类移动自己
GameWorld::GetWorld()->MoveCharacter(this, Info.town, Info.area);
}
}
else
{
GameMap::MapMoveArea Info = this->_AffMap->CheckIsItMoveArea(this->GetPosition());
if (Info.town == -2 && Info.area == -2)
IsTeleportArea = false;
}
}

View File

@@ -1,10 +1,10 @@
#pragma once
#include "EngineFrame/Base/Actor.h"
#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 "Asset/Character/Chr_Shadow.h"
#include "Global/Global_Enum.h"
class CharacterObject : public ActiveObject
{
@@ -18,12 +18,12 @@ public:
// 角色状态机
RefPtr<Chr_StateMachine> _StateMachine = nullptr;
// 角色阴影对象
RefPtr<Chr_Shadow> _Shadow = nullptr;
// 职业
int Job = 0;
// 转职职业 如果是-1则没有转职
int GrowType = -1;
/** 传送区域Flag */
bool IsTeleportArea = false;
public:
~CharacterObject();
@@ -39,7 +39,8 @@ public:
// 控制器信息
void ControllerMsg(CONTROLLER_MSG_TYPE msgType, void* msgData);
void Update(float deltaTime) override;
void SetPos(Vec2 pos) override;
void SetPosition(VecFPos3 pos) override;
void SetDirection(int dir) override;
void OnUpdate(float deltaTime) override;
};

View File

@@ -22,21 +22,20 @@ void Chr_Animation::CreateSkinmationBySlot(std::string actionName, std::string s
if (slotName == "skin_avatar")
{
// 如果穿戴了皮肤时装 读取variation
RefPtr<Equipment> Equip = ((CharacterObject *)m_Parent)->_EquipmentManager->GetEquip(slotName);
RefPtr<Equipment> Equip = ((CharacterObject *)GetParent())->_EquipmentManager->GetEquip(slotName);
if (Equip)
{
Animation::ReplaceData Data(0, 0);
for (auto &Variation : Equip->JobAni[this->chr_parent->Job])
{
Data.Param1 = Variation.ImgFormat[0];
Data.Param2 = Variation.ImgFormat[1];
// 构造Ani以后 统一设置为不可见 然后放入ActionAnis表
// 构造Ani
RefPtr<Animation> Ani = new Animation(path, FormatImgPath, Data);
Ani->SetVisible(false);
Ani->SetRenderZOrder(Variation.Layer);
this->AddChild(Ani);
ActionAnis[actionName].push_back(Ani);
Ani->SetZOrder(Variation.Layer);
// 将构造好的Ani 添加进AniMap
ActionAnis[actionName]->AddAnimation(Ani);
// 构造一下阴影对象 //TODO
// RefPtr<Animation> ShadowAni = new Animation(path, FormatImgPath, Data);
@@ -49,10 +48,9 @@ void Chr_Animation::CreateSkinmationBySlot(std::string actionName, std::string s
}
else
{
RefPtr<Equipment> Equip = ((CharacterObject *)m_Parent)->_EquipmentManager->GetEquip(slotName);
RefPtr<Equipment> Equip = ((CharacterObject *)GetParent())->_EquipmentManager->GetEquip(slotName);
if (Equip)
{
// SDL_Log("CreateSkinmationBySlot %s", slotName.c_str());
Animation::ReplaceData Data(0, 0);
for (auto &Variation : Equip->JobAni[this->chr_parent->Job])
{
@@ -64,12 +62,11 @@ void Chr_Animation::CreateSkinmationBySlot(std::string actionName, std::string s
// 组装动画路径
std::string AniPath = EquipPath + Variation.AnimationGroup + path.substr(path.find_last_of("/"), path.length());
// 构造Ani以后 统一设置为不可见 然后放入ActionAnis表
// 构造Ani
RefPtr<Animation> Ani = new Animation(AniPath, FormatImgPath, Data);
Ani->SetVisible(false);
Ani->SetRenderZOrder(Variation.Layer);
this->AddChild(Ani);
ActionAnis[actionName].push_back(Ani);
Ani->SetZOrder(Variation.Layer);
// 将构造好的Ani 添加进AniMap
ActionAnis[actionName]->AddAnimation(Ani);
}
}
}
@@ -77,24 +74,30 @@ void Chr_Animation::CreateSkinmationBySlot(std::string actionName, std::string s
void Chr_Animation::Init(CharacterObject *parent)
{
// 调用RenderBase类的初始化 才能挂上标签
RenderBase::Init();
parent->AddChild(this);
chr_parent = parent;
GlobalCharacterScript::CharacterConfig Config = Global_Game::GetInstance().CharacterConfigs[parent->Job];
// 遍历所有动作Ani路径
for (const auto &pair : Config.animationPath)
{
std::string ActionName = pair.first;
std::string Path = std::string("character/" + Config.jobTag + "/" + Tool_toLowerCase(pair.second));
//以动作为Key构造AniMap
ActionAnis[ActionName] = new AnimationMap();
ActionAnis[ActionName]->SetVisible(false);
// 遍历所有时装部位构造Ani
for (auto &AvatarPartPair : AvatarPart)
{
std::string AvatarPartName = AvatarPartPair.first;
CreateSkinmationBySlot(ActionName, AvatarPartName, Path);
}
// 将所有时装部位的Ani添加到动作AniMap以后完成构造
ActionAnis[ActionName]->CompleteConstruction();
this->AddChild(ActionAnis[ActionName]);
}
// 设置初始动作
SetAction("rest");
@@ -103,18 +106,9 @@ void Chr_Animation::Init(CharacterObject *parent)
void Chr_Animation::SetAction(std::string actionName)
{
// 先将原动作的Ani设置为不可见
for (auto Ani : ActionAnis[CurrentActionTag])
{
Ani->Reset();
Ani->SetVisible(false);
}
ActionAnis[CurrentActionTag]->Reset();
ActionAnis[CurrentActionTag]->SetVisible(false);
// 再将新动作的Ani设置为可见
for (auto Ani : ActionAnis[actionName])
{
Ani->SetVisible(true);
}
ActionAnis[actionName]->SetVisible(true);
CurrentActionTag = actionName;
// 设置阴影的动作
if(chr_parent->_Shadow)chr_parent->_Shadow->SetAction(actionName);
}

View File

@@ -1,13 +1,14 @@
#pragma once
#include "EngineFrame/Component/Animation.h"
#include "EngineFrame/Base/Actor.h"
#include "EngineFrame/Component/AnimationMap.h"
#include <string>
#include <vector>
#include <unordered_map>
class CharacterObject;
class Chr_Animation : public RenderBase
class Chr_Animation : public Actor
{
// 动作动画集合
using ActionAniList = std::map<std::string, std::vector<RefPtr<Animation>>>;
using ActionAniList = std::map<std::string, RefPtr<AnimationMap>>;
public:
inline static const std::unordered_map<std::string, std::string>

View File

@@ -28,7 +28,6 @@ float Chr_Controller::ConvertAxisValue(Sint16 rawValue)
void Chr_Controller::Init(CharacterObject *pCharacter)
{
addTag(Tag::HANDEL_EVENT);
m_pCharacter = pCharacter;
}

View File

@@ -1,8 +1,8 @@
#pragma once
#include "EngineFrame/Base/BaseNode.h"
#include "EngineFrame/Base/Node.h"
#include "Tool/Common.h"
class CharacterObject;
class Chr_Controller : public BaseNode
class Chr_Controller : public Node
{
private:
CharacterObject *m_pCharacter = nullptr;

View File

@@ -1,44 +0,0 @@
#include "Chr_Shadow.h"
#include "Global/Global_Game.h"
#include "Actor/Object/CharacterObject.h"
#include "Asset/Character/Chr_Equipment.h"
void Chr_Shadow::Init(CharacterObject *parent)
{
// 调用RenderBase类的初始化 才能挂上标签
RenderBase::Init();
chr_parent = parent;
SetRenderZOrder(1000000);
SetPos(parent->GetPos());
}
void Chr_Shadow::SetAction(std::string actionName)
{
// 先将原动作的Ani设置为不可见
for (auto Ani : ActionAnis[CurrentActionTag])
{
Ani->Reset();
Ani->SetVisible(false);
}
// 再将新动作的Ani设置为可见
for (auto Ani : ActionAnis[actionName])
{
Ani->SetVisible(true);
}
CurrentActionTag = actionName;
}
void Chr_Shadow::SetDirection(int dir)
{
Vec2 sc = GetScale();
// 朝右
if (dir == 0)
{
SetScale(Vec2({SDL_fabsf(sc.x), sc.y}));
}
// 朝左
else if (dir == 1)
{
SetScale(Vec2({-SDL_fabsf(sc.x), sc.y}));
}
}

View File

@@ -1,29 +0,0 @@
#pragma once
#include "EngineFrame/Component/Animation.h"
#include <string>
#include <vector>
#include <unordered_map>
class CharacterObject;
class Chr_Shadow : public RenderBase
{
// 动作动画集合
using ActionAniList = std::map<std::string, std::vector<RefPtr<Animation>>>;
private:
/* data */
public:
// 父对象
CharacterObject *chr_parent;
// 时装部位对应的动作动画集合
ActionAniList ActionAnis;
// 当前动作Tag
std::string CurrentActionTag = "waiting";
// 初始化时
void Init(CharacterObject *parent);
// 设置动作
void SetAction(std::string actionName);
// 设置方向
void SetDirection(int dir);
};

View File

@@ -5,7 +5,6 @@
void Chr_StateMachine::Init(CharacterObject *parent)
{
chr_Parent = parent;
addTag(Tag::UPDATE);
parent->AddChild(this);
}
void Chr_StateMachine::ChangeState(int state)

View File

@@ -1,8 +1,8 @@
#pragma once
#include "Global/Global_Enum.h"
#include "EngineFrame/Base/Actor.h"
#include "EngineFrame/Base/Node.h"
class CharacterObject;
class Chr_StateMachine : public Actor
class Chr_StateMachine : public Node
{
private:
// 父对象

View File

@@ -4,7 +4,6 @@
void Mon_Animation::Init(MonsterObject *parent)
{
// 调用RenderBase类的初始化 才能挂上标签
RenderBase::Init();
parent->AddChild(this);
mon_parent = parent;

View File

@@ -4,7 +4,7 @@
#include <vector>
#include <unordered_map>
class MonsterObject;
class Mon_Animation : public RenderBase
class Mon_Animation : public Actor
{
// 动作动画集合
using ActionAniList = std::map<std::string, RefPtr<Animation>>;

View File

@@ -78,7 +78,7 @@ static SQInteger SQR_GetZOrder(HSQUIRRELVM v)
SQUserPointer A_obj;
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
sq_pushinteger(v, Aobj->GetRenderZOrder());
sq_pushinteger(v, Aobj->GetZOrder());
return 1;
}
@@ -89,7 +89,7 @@ static SQInteger SQR_SetZOrder(HSQUIRRELVM v)
SQInteger Value;
sq_getinteger(v, 3, &Value);
Actor *Aobj = (Actor *)A_obj;
Aobj->SetRenderZOrder(Value);
Aobj->SetZOrder(Value);
return 0;
}
@@ -98,7 +98,7 @@ static SQInteger SQR_GetPos(HSQUIRRELVM v)
SQUserPointer A_obj;
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
Vec2 Pos = Aobj->GetPos();
glm::vec2 Pos = Aobj->GetPosition();
sq_newtable(v);
sq_pushstring(v, _SC("x"), -1);
@@ -117,7 +117,7 @@ static SQInteger SQR_SetPos(HSQUIRRELVM v)
if (sq_gettop(v) == 3)
{
Vec2 Pos;
glm::vec2 Pos;
sq_pushnull(v); // null iterator
while (SQ_SUCCEEDED(sq_next(v, 3)))
{
@@ -139,7 +139,7 @@ static SQInteger SQR_SetPos(HSQUIRRELVM v)
sq_pop(v, 1);
Actor *Aobj = (Actor *)A_obj;
Aobj->SetPos(Pos);
Aobj->SetPosition(Pos);
}
else if (sq_gettop(v) == 4)
{
@@ -147,7 +147,7 @@ static SQInteger SQR_SetPos(HSQUIRRELVM v)
sq_getfloat(v, 3, &X);
sq_getfloat(v, 4, &Y);
Actor *Aobj = (Actor *)A_obj;
Aobj->SetPos(Vec2(X, Y));
Aobj->SetPosition(glm::vec2(X, Y));
}
return 0;
}
@@ -157,7 +157,7 @@ static SQInteger SQR_GetWorldPos(HSQUIRRELVM v)
SQUserPointer A_obj;
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
Vec2 Pos = Aobj->GetWorldPos();
glm::vec2 Pos = Aobj->ConvertToWorld(glm::vec2(0, 0));
sq_newtable(v);
sq_pushstring(v, _SC("x"), -1);
@@ -175,7 +175,7 @@ static SQInteger SQR_GetAlpha(HSQUIRRELVM v)
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
sq_pushfloat(v, Aobj->GetAlpha());
sq_pushfloat(v, Aobj->GetOpacity());
return 1;
}
static SQInteger SQR_SetAlpha(HSQUIRRELVM v)
@@ -185,7 +185,7 @@ static SQInteger SQR_SetAlpha(HSQUIRRELVM v)
SQFloat Value;
sq_getfloat(v, 3, &Value);
Actor *Aobj = (Actor *)A_obj;
Aobj->SetAlpha(Value);
Aobj->SetOpacity(Value);
return 0;
}
@@ -194,7 +194,7 @@ static SQInteger SQR_GetScale(HSQUIRRELVM v)
SQUserPointer A_obj;
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
Vec2 Pos = Aobj->GetScale();
glm::vec2 Pos = Aobj->GetScale();
sq_newtable(v);
sq_pushstring(v, _SC("x"), -1);
sq_pushfloat(v, Pos.x);
@@ -212,7 +212,7 @@ static SQInteger SQR_SetScale(HSQUIRRELVM v)
if (sq_gettop(v) == 3)
{
Vec2 Pos;
glm::vec2 Pos;
sq_pushnull(v); // null iterator
while (SQ_SUCCEEDED(sq_next(v, 3)))
{
@@ -242,7 +242,7 @@ static SQInteger SQR_SetScale(HSQUIRRELVM v)
sq_getfloat(v, 3, &X);
sq_getfloat(v, 4, &Y);
Actor *Aobj = (Actor *)A_obj;
Aobj->SetScale(Vec2(X, Y));
Aobj->SetScale(X, Y);
}
return 0;
}
@@ -342,7 +342,7 @@ static SQInteger SQR_GetVisible(HSQUIRRELVM v)
SQUserPointer A_obj;
sq_getuserpointer(v, 2, &A_obj);
Actor *Aobj = (Actor *)A_obj;
sq_pushbool(v, Aobj->GetVisible());
sq_pushbool(v, Aobj->IsVisible());
return 1;
}
@@ -391,7 +391,7 @@ static SQInteger SQR_Canvas_DrawImg(HSQUIRRELVM v)
sq_getinteger(v, 4, &Idx);
sq_GetVec2(v, 5, &Pos);
Canvas *Aobj = (Canvas *)A_obj;
Aobj->DrawImg(ImgPath, Idx, Pos);
// Aobj->DrawImg(ImgPath, Idx, Pos);
return 0;
}
@@ -406,7 +406,7 @@ static SQInteger SQR_Canvas_DrawImgRect(HSQUIRRELVM v)
sq_getinteger(v, 4, &Idx);
sq_GetFRect(v, 5, &Rect);
Canvas *Aobj = (Canvas *)A_obj;
Aobj->DrawImg(ImgPath, Idx, Rect);
// Aobj->DrawImg(ImgPath, Idx, Rect);
return 0;
}

View File

@@ -72,9 +72,12 @@ void Global_Game::InitGame()
EquipmentPathMap = GlobalEquipmentScript::InitEquipmentLst();
// 读取怪物配置文件
MonsterPathMap = GlobalMonsterScript::InitMonsterLst();
// 读取城镇配置文件
TownPathMap = GlobalTownScript::InitTownLst();
// 初始化松鼠脚本管理器
SquirrelManager::GetInstance().Init();
SquirrelManager::GetInstance()
.Init();
// 读取存档
SavaManager::GetInstance().Init();
@@ -105,6 +108,30 @@ GlobalMonsterScript::MonsterConfig Global_Game::GetMonsterInfo(int id)
return MonsterConfig;
}
const std::map<int, std::string> &Global_Game::GetTownMap() const
{
return TownPathMap;
}
GlobalTownScript::TownConfig *Global_Game::GetTownInfo(int id)
{
if (TownInfoMap.count(id))
return TownInfoMap[id];
if (!TownPathMap.count(id))
{
SDL_LogError(0, "城镇ID不存在");
return nullptr;
}
std::string path = TownPathMap[id];
GlobalTownScript::TownConfig *TownConfig = GlobalTownScript::GetTownConfig(path);
if (TownConfig)
{
TownInfoMap[id] = TownConfig;
return TownConfig;
}
return nullptr;
}
RefPtr<GameCamera> Global_Game::GetCamera()
{
return _GameCamera;

View File

@@ -3,6 +3,7 @@
#include "Global/Script/CharacterConfig.h"
#include "Global/Script/EquipmentConfig.h"
#include "Global/Script/MonsterConfig.h"
#include "Global/Script/TownConfig.h"
#include "Global/Save/SavaManager.h"
#include "Global/GameCamera.h"
class Global_Game
@@ -43,6 +44,12 @@ public:
std::map<int, GlobalMonsterScript::MonsterConfig> MonsterInfoMap; // 信息
GlobalMonsterScript::MonsterConfig GetMonsterInfo(int id); // 获取怪物信息
// 城镇相关
std::map<int, std::string> TownPathMap; // 路径
std::map<int, GlobalTownScript::TownConfig *> TownInfoMap; // 信息
const std::map<int, std::string>& GetTownMap() const; // 获取城镇路径
GlobalTownScript::TownConfig *GetTownInfo(int id); // 获取城镇信息
// 游戏摄像机
RefPtr<GameCamera> _GameCamera = nullptr;

View File

@@ -0,0 +1,58 @@
#include "TownConfig.h"
#include "Asset/AssetManager.h"
#include "Tool/Tool_String.h"
namespace GlobalTownScript
{
std::map<int, std::string> InitTownLst()
{
std::map<int, std::string> TownList;
ScriptData Data = AssetManager::GetInstance().GetScriptInfo("town/town.lst");
while (!Data.IsEnd())
{
std::string Index = Data.Get();
std::string MonsterPath = std::string("town/") + Tool_toLowerCase(Data.Get());
TownList[std::stoi(Index)] = MonsterPath;
}
return TownList;
}
TownConfig *GetTownConfig(std::string TownPath)
{
TownConfig *Config = new TownConfig();
ScriptData Data = AssetManager::GetInstance().GetScriptInfo(TownPath);
while (!Data.IsEnd())
{
std::string Segment = Data.Get();
if (Segment == "[entering title]")
{
Config->Entering_Title = "sprite/" + Tool_toLowerCase(Data.Get());
}
else if (Segment == "[cutscene image]")
{
Config->Entering_Cutscene = "sprite/" + Tool_toLowerCase(Data.Get());
}
else if (Segment == "[area]")
{
int Index = std::stoi(Data.Get());
std::string MapPath = "map/" + Tool_toLowerCase(Data.Get());
std::string TypeBuffer = Data.Get();
std::string Type = TypeBuffer.substr(1, TypeBuffer.size() - 2);
int GenerateXPos = -1;
int GenerateYPos = -1;
if (Type == "gate")
{
GenerateXPos = std::stoi(Data.Get());
GenerateYPos = std::stoi(Data.Get());
}
Config->AreaLst[Index] = AreaConfig{MapPath, Type, GenerateXPos, GenerateYPos};
}
else if (Segment == "[name]")
{
Config->TownName = Data.Get();
}
}
return Config;
}
}

View File

@@ -0,0 +1,41 @@
#pragma once
#include <string>
#include <vector>
#include <map>
namespace GlobalTownScript
{
struct AreaConfig
{
/**地图路径 */
std::string MapPath;
/**地图类型 */
std::string MapType;
/**生成坐标X */
int GenerateXPos = 0;
/**生成坐标Y */
int GenerateYPos = 0;
};
/**城镇配置 */
struct TownConfig
{
/**城镇名称 */
std::string TownName;
/**进入城镇时的标题Img */
std::string Entering_Title;
/**进入城镇时的过场Img */
std::string Entering_Cutscene;
/**需要的任务进入条件 */
int NeedQuestID = -1;
/**需要的等级进入条件 */
int NeedLevel = -1;
/**区域 */
std::map<int, AreaConfig> AreaLst;
};
// 读取城镇列表
std::map<int, std::string> InitTownLst();
// 读取城镇配置
TownConfig *GetTownConfig(std::string TownPath);
}

View File

@@ -1,6 +1,6 @@
#include "Scene/Scene_Loading_UI.h"
#include "Scene/Scene_MainUi.h"
#include "Scene/Scene_Test.h"
#include "Actor/Map/GameWorld.h"
#include "Scene_Loading_UI.h"
#include "EngineFrame/Component/Sprite.h"
#include "EngineFrame/Component/Text.h"
@@ -15,6 +15,10 @@ void Scene_Loading_UI::Enter()
{
Mix_PlayMusic(music, 1);
}
else
{
printf("Mix_LoadMUS: %s\n", Mix_GetError());
}
RefPtr<Actor> actor = new Actor;
AddChild(actor);
@@ -22,19 +26,19 @@ void Scene_Loading_UI::Enter()
RefPtr<Sprite> BackGroundSp = new Sprite("ImagePacks2/Loading1.png");
actor->AddChild(BackGroundSp);
RefPtr<Sprite> BackGround2Sp = new Sprite("ImagePacks2/Loading0.png");
BackGround2Sp->SetPos(Vec2{0, 686});
BackGround2Sp->SetPosition(0, 686);
actor->AddChild(BackGround2Sp);
RefPtr<Sprite> LoadCircleSp = new Sprite("ImagePacks2/Loading2.png");
LoadCircleSp->SetName("LoadCircle");
LoadCircleSp->SetPos(Vec2{1280 - 60, 686 - 60});
LoadCircleSp->SetPosition(1280 - 60, 686 - 60);
LoadCircleSp->SetBlendMode(LINEARDODGE);
LoadCircleSp->SetAnchor(Vec2{0.5f, 0.5f});
LoadCircleSp->SetAnchor(0.5f, 0.5f);
actor->AddChild(LoadCircleSp);
actor->SetCallbackOnUpdate("Rotate", [LoadCircleSp](float deltaTime) mutable
{
{
float angle = LoadCircleSp->GetRotation();
LoadCircleSp->SetRotation(angle + 180.0f * deltaTime); });
LoadCircleSp->SetRotation(angle - 180.0f * deltaTime); });
}
void Scene_Loading_UI::Update(float deltaTime)
@@ -43,7 +47,7 @@ void Scene_Loading_UI::Update(float deltaTime)
if (Global_Game::GetInstance().InitFlag)
{
// 设定游戏层场景
RefPtr<Scene_Test> scene = new Scene_Test;
RefPtr<GameWorld> scene = new GameWorld;
Game::GetInstance().ChangeScene(scene);
// 设定UI层场景
RefPtr<Scene_MainUi> sceneUI = new Scene_MainUi;

View File

@@ -1,122 +0,0 @@
#include "Scene_Test.h"
#include <memory>
#include "EngineCore/Game.h"
#include "EngineFrame/Component/AnimationManager.h"
#include "EngineFrame/Component/Text.h"
#include "EngineFrame/Component/Canvas.h"
#include "Global/Global_Game.h"
Scene_Test::Scene_Test()
{
}
Scene_Test::~Scene_Test()
{
SDL_Log("Scene_Test::我的ID是%d --- 我被释放了!", this->MyId);
}
void Scene_Test::Enter()
{
map = new GameMap;
map->LoadMap("map/cataclysm/town/elvengard/new_elvengard.map");
map->Enter(this);
AddChild(map);
map->SetCallbackOnUpdate("csas", [this](float dt)
{
Vec2 pos = map->GetPos();
pos.x -= 10 * dt;
map->SetPos(pos); });
// RefPtr<CharacterObject> obj = new CharacterObject();
// obj->SetPosition({1000, 300, 0});
// obj->Construction(0);
// map->AddObject(obj);
// RefPtr<MonsterObject> monster = new MonsterObject();
// monster->SetPosition({1200, 301, 0});
// monster->Construction(1);
// monster->SetDirection(1);
// map->AddObject(monster);
return;
// // SDL_Log("Scene_Test::进入测试场景!");
RefPtr<Actor> actor = new Actor;
AddChild(actor);
// RefPtr<Sprite> sprite = new Sprite("ImagePacks2/test_white_background.png");
RefPtr<Sprite> chr = new Sprite("sprite/interface2/hud/hud.img", 0);
chr->SetPos(Vec2{200, 100});
// chr->SetAlpha(0.5);
// chr->SetShadow();
// actor->AddComponent(chr);
RefPtr<Canvas> canvas = new Canvas(VecSize{250, 720});
actor->AddChild(canvas);
canvas->DrawImg("sprite/interface2/hud/hud.img", 0, Vec2{0, 0});
// canvas->AddChild(chr);
// RefPtr<AnimationManager> Am = new AnimationManager;
// RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
// Am->AddAnimation(ani4);
// // Am->SetPos(Vec2{200, 100});
// actor->AddChild(Am);
// // sprite2->UnsetClipRect();
// RefPtr<Actor> actor2 = new Actor;
// actor->AddChild(actor2);
// RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
// // ani4->SetScale(Vec2{-1.0, 1.0});
// actor->AddChild(ani4);
// RefPtr<Animation> ani3 = new Animation("map/cataclysm/town/elvengard/animation/obj/serialight01_right.ani");
// actor->AddChild(ani3);
// RefPtr<Animation> ani4 = new Animation("map/cataclysm/town/hendonmyre/animation/object/gateall_02.ani");
// ani4->SetPos(VecPos{300, 500});
// actor->AddChild(ani4);
// for (size_t i = 0; i < 400; i++)
// {
// RefPtr<Actor> actor = new Actor;
// AddChild(actor);
// // RefPtr<Animation> ani3 = new Animation("common/commoneffect/animation/priestslowheal1.ani");
// // actor->AddComponent(ani3);
// // ani3->SetRenderZOrder(1000);
// // RefPtr<Animation> ani = new Animation("common/anton/main.ani");
// // actor->AddComponent(ani);
// // ani->SetRenderZOrder(500);
// RefPtr<Sprite> sprite = new Sprite("sprite/item/avatar/swordman/0sm_acap.img", 0);
// actor->AddComponent(sprite);
// // RefPtr<Animation> ani2 = new Animation("common/anton/face/0/0.ani");
// // actor->AddComponent(ani2);
// // ani2->SetRenderZOrder(1000);
// }
}
void Scene_Test::HandleEvents(SDL_Event *e)
{
Scene::HandleEvents(e);
}
void Scene_Test::Update(float deltaTime)
{
Scene::Update(deltaTime);
}
void Scene_Test::Render()
{
Scene::Render();
}
void Scene_Test::Exit()
{
SDL_Log("Scene_Test::退出测试场景!当前引用计数%d", this->GetRefCount());
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include "EngineFrame/Scene/Scene.h"
#include "Asset/AssetManager.h"
#include "EngineFrame/Component/Animation.h"
#include "Actor/Map/GameMap.h"
#include "Actor/Object/CharacterObject.h"
#include "Actor/Object/MonsterObject.h"
class Scene_Test : public Scene
{
private:
GameMap *map = nullptr;
public:
Scene_Test(/* args */);
~Scene_Test();
public:
void Enter() override;
void HandleEvents(SDL_Event *e) override;
void Update(float deltaTime) override;
void Render() override;
void Exit() override;
public:
int MyId = 0;
};

View File

@@ -16,6 +16,7 @@
#endif
#include "Asset/Asset_ImagePack.h"
#include "Asset/Asset_SoundPack.h"
#include "Asset/Asset_Script.h"
#include "squirrel/SquirrelEx.h"
#include "Tool/Logger.h"
@@ -56,7 +57,7 @@ int main(int argc, char *argv[])
socketInitializeDefault();
nxlinkStdio(); // 启用nxlink调试输出
curl_global_init(CURL_GLOBAL_DEFAULT); // 初始化libcurl
chdir("/switch/Lenheart/");
chdir("/switch/DNF/");
#else
AllocConsole();
SetConsoleTitleA("Yosin & Engine");
@@ -65,8 +66,12 @@ int main(int argc, char *argv[])
// 初始化Image资源系统
Asset_ImagePack::GetInstance();
// 初始化音频资源系统
Asset_SoundPack::GetInstance();
// 初始化脚本资源系统
Asset_Script::GetInstance();
// 初始化线程池
ThreadPool::GetInstance();
// 初始化日志系统