修改OpenGl渲染底层之前
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include "EngineFrame/Component/Animation.h"
|
||||
#include "Actor/Map/GameMapCamera.h"
|
||||
#include "Actor/Object/CharacterObject.h"
|
||||
#include "EngineCore/Game.h"
|
||||
|
||||
GameMap::GameMap()
|
||||
{
|
||||
@@ -45,12 +46,23 @@ void GameMap::InitConfiguration(std::string mapName)
|
||||
std::string Segment = Data.Get();
|
||||
if (Segment == "[background pos]")
|
||||
{
|
||||
_MapInfo["background_pos"] = std::stoi(Data.Get());
|
||||
int Offset = std::stoi(Data.Get());
|
||||
MapOffsetY = Offset;
|
||||
_MapInfo["background_pos"] = Offset;
|
||||
}
|
||||
else if (Segment == "[map name]")
|
||||
{
|
||||
_MapInfo["name"] = Data.Get();
|
||||
}
|
||||
else if (Segment == "[limit map camera move]")
|
||||
{
|
||||
std::vector<int> limit;
|
||||
limit.push_back(std::stoi(Data.Get()));
|
||||
limit.push_back(std::stoi(Data.Get()));
|
||||
limit.push_back(std::stoi(Data.Get()));
|
||||
limit.push_back(std::stoi(Data.Get()));
|
||||
_MapInfo["limit_map_camera_move"] = limit;
|
||||
}
|
||||
else if (Segment == "[wide mode camera vertical correction]")
|
||||
{
|
||||
_MapInfo["wide_mode_camer_vertical_correction"] = std::stoi(Data.Get());
|
||||
@@ -196,7 +208,10 @@ void GameMap::InitTile()
|
||||
{
|
||||
if (!_MapInfo.count("tile"))
|
||||
return;
|
||||
// 普通地板数量
|
||||
int NormalTileCount = 0;
|
||||
// 普通地板高度
|
||||
int NormalTileHeight = 560;
|
||||
std::vector<std::string> tileArr = std::get<std::vector<std::string>>(_MapInfo["tile"]);
|
||||
if (tileArr.size() > 0)
|
||||
{
|
||||
@@ -224,7 +239,7 @@ void GameMap::InitTile()
|
||||
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);
|
||||
int ybuf = -200 - std::get<int>(_MapInfo["background_pos"]) + NormalTileHeight + 120 * (i / NormalTileCount);
|
||||
tile->SetPos(VecFPos{xbuf, ybuf});
|
||||
_LayerMap["bottom"]->AddComponent(tile);
|
||||
}
|
||||
@@ -325,6 +340,7 @@ void GameMap::LoadMap(std::string mapName)
|
||||
|
||||
void GameMap::Enter(Scene *scene)
|
||||
{
|
||||
_Scene = scene;
|
||||
scene->AddChild(_LayerMap["contact"]);
|
||||
scene->AddChild(_LayerMap["distantback"]);
|
||||
scene->AddChild(_LayerMap["middleback"]);
|
||||
@@ -334,18 +350,6 @@ void GameMap::Enter(Scene *scene)
|
||||
scene->AddChild(_LayerMap["close"]);
|
||||
scene->AddChild(_LayerMap["cover"]);
|
||||
scene->AddChild(_LayerMap["max"]);
|
||||
|
||||
// TODO
|
||||
int HSU = 0;
|
||||
_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)
|
||||
@@ -354,45 +358,68 @@ void GameMap::HandleEvents(SDL_Event *e)
|
||||
|
||||
void GameMap::Update(float deltaTime)
|
||||
{
|
||||
// this->_Camera->Update(deltaTime);
|
||||
if (_Scene->GetCamera() == nullptr)
|
||||
return;
|
||||
GameMapCamera *Cam = (GameMapCamera *)(_Scene->GetCamera().Get());
|
||||
int targetX = Cam->_currentPosition.x;
|
||||
int targetY = Cam->_currentPosition.y;
|
||||
// 屏幕中心
|
||||
int width_Separate = Game::GetInstance().Screen_W / 2;
|
||||
int height_Separate = Game::GetInstance().Screen_H / 2;
|
||||
|
||||
// if (this->_Camera)
|
||||
// {
|
||||
// int CamearXpos = this->_Camera->X;
|
||||
// int CamearYpos = this->_Camera->Y;
|
||||
// int CamearZpos = this->_Camera->Z;
|
||||
// 获取摄像机可行区域限制
|
||||
auto limitIt = _MapInfo.find("limit_map_camera_move");
|
||||
if (limitIt != _MapInfo.end())
|
||||
{
|
||||
std::vector<int> limit = std::get<std::vector<int>>(_MapInfo["limit_map_camera_move"]);
|
||||
int X_Limit_Min = limit[0];
|
||||
int X_Limit_Max = limit[1];
|
||||
int Y_Limit_Min = limit[2];
|
||||
int Y_Limit_Max = limit[3];
|
||||
// 应用地图边界限制
|
||||
targetX = std::clamp(targetX, width_Separate, _MapLength - width_Separate);
|
||||
targetY = std::clamp(targetY, height_Separate, _MapHeight - height_Separate);
|
||||
// 应用自定义摄像机移动限制
|
||||
if (X_Limit_Min != -1)
|
||||
targetX = std::max(targetX, X_Limit_Min);
|
||||
if (X_Limit_Max != -1)
|
||||
targetX = std::min(targetX, X_Limit_Max);
|
||||
if (Y_Limit_Min != -1)
|
||||
targetY = std::max(targetY, Y_Limit_Min);
|
||||
if (Y_Limit_Max != -1)
|
||||
targetY = std::min(targetY, Y_Limit_Max);
|
||||
}
|
||||
|
||||
// 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)});
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// 更新图层位置
|
||||
for (auto &Layer : _LayerMap)
|
||||
{
|
||||
int posX = -targetX + width_Separate;
|
||||
int posY = -targetY + height_Separate + MapOffsetY;
|
||||
if (Layer.first == "distantback")
|
||||
{
|
||||
posX *= BackgroundMoveSpeed;
|
||||
posX /= 100;
|
||||
}
|
||||
Layer.second->SetPos(VecFPos(posX, posY));
|
||||
}
|
||||
}
|
||||
|
||||
void GameMap::AddObject(RefPtr<BaseObject> object)
|
||||
{
|
||||
object->_AffMap = this;
|
||||
_LayerMap["normal"]->AddObject(object);
|
||||
// 如果是角色对象
|
||||
if (object->m_objecttype == ObjectType::CHARACTER)
|
||||
{
|
||||
CharacterObject *chr = (CharacterObject *)(object.Get());
|
||||
_LayerMap["bottom"]->AddComponent(chr->_Shadow);
|
||||
}
|
||||
}
|
||||
|
||||
VecFPos3 GameMap::CheckIsItMovable(VecFPos3 CurPos, VecFPos3 PosOffset)
|
||||
VecPos3 GameMap::CheckIsItMovable(VecPos3 CurPos, VecPos3 PosOffset)
|
||||
{
|
||||
// 初始化结果为原坐标(默认不移动)
|
||||
VecFPos3 result = CurPos;
|
||||
VecPos3 result = CurPos;
|
||||
|
||||
// 如果没有可移动区域限制,直接全量位移
|
||||
if (_MovableArea.empty())
|
||||
|
||||
@@ -65,15 +65,18 @@ public:
|
||||
// 可行区域
|
||||
std::vector<SDL_FRect> _MovableArea;
|
||||
// 调试模式
|
||||
bool _DebugMode = true;
|
||||
bool _DebugMode = false;
|
||||
|
||||
public:
|
||||
// 图层Map 图层类型 显示对象
|
||||
std::unordered_map<std::string, RefPtr<GameMapLayer>> _LayerMap;
|
||||
// 摄像机对象
|
||||
GameMapCamera *_Camera;
|
||||
|
||||
// 所属场景
|
||||
Scene *_Scene = nullptr;
|
||||
|
||||
// 背景层移动速率
|
||||
int BackgroundMoveSpeed = 103;
|
||||
// 地图Y轴偏移量
|
||||
int MapOffsetY = 0;
|
||||
public:
|
||||
GameMap(/* args */);
|
||||
~GameMap();
|
||||
@@ -91,5 +94,5 @@ public:
|
||||
|
||||
public:
|
||||
// 检查是否可移动
|
||||
VecFPos3 CheckIsItMovable(VecFPos3 CurPos, VecFPos3 PosOffset);
|
||||
VecPos3 CheckIsItMovable(VecPos3 CurPos, VecPos3 PosOffset);
|
||||
};
|
||||
|
||||
@@ -12,15 +12,10 @@ GameMapCamera::~GameMapCamera()
|
||||
{
|
||||
}
|
||||
|
||||
void GameMapCamera::SetParentMap(GameMap *map)
|
||||
{
|
||||
this->_ParentMap = map;
|
||||
_ParentMap->_Camera = this;
|
||||
}
|
||||
|
||||
void GameMapCamera::SetFromActor(BaseObject *actor)
|
||||
{
|
||||
this->_FromActor = actor;
|
||||
_FromActor->_AffCamera = this;
|
||||
}
|
||||
|
||||
void GameMapCamera::Update(float deltaTime)
|
||||
@@ -28,41 +23,105 @@ void GameMapCamera::Update(float deltaTime)
|
||||
SyncPosByFromParent(deltaTime);
|
||||
}
|
||||
|
||||
void GameMapCamera::SyncPos(float deltaTime)
|
||||
{
|
||||
}
|
||||
|
||||
void GameMapCamera::SetPos(int x, int y, int z)
|
||||
{
|
||||
this->X = x;
|
||||
this->Y = y;
|
||||
this->Z = 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;
|
||||
// this->X += x;
|
||||
// this->Y += y;
|
||||
// this->Z += z;
|
||||
}
|
||||
|
||||
void GameMapCamera::SyncPosByFromParent(float deltaTime)
|
||||
{
|
||||
if (this->_FromActor != nullptr)
|
||||
if (this->_FromActor == nullptr)
|
||||
{
|
||||
float width_Separate = Game::GetInstance().Screen_W / 2;
|
||||
float height_Separate = Game::GetInstance().Screen_H / 2;
|
||||
int R_X, R_Y, R_Z;
|
||||
R_X = std::fmin(std::fmax(this->_FromActor->Position.x, width_Separate), _ParentMap->_MapLength - width_Separate);
|
||||
R_Y = std::fmin(std::fmax(this->_FromActor->Position.y, height_Separate), _ParentMap->_MapHeight - height_Separate);
|
||||
R_Z = 0;
|
||||
// SDL_Log("R_X: %d, R_Y: %d, R_Z: %d", R_X, R_Y, R_Z);
|
||||
// SetPos(R_X, R_Y, R_Z);
|
||||
return;
|
||||
}
|
||||
int targetX = _FromActor->Position.x;
|
||||
int targetY = _FromActor->Position.y;
|
||||
|
||||
for (auto Layer : _ParentMap->_LayerMap)
|
||||
{
|
||||
if (Layer.first == "distantback")
|
||||
{
|
||||
Layer.second->SetPos(VecFPos((-R_X + width_Separate) * BackgroundMoveSpeed, -R_Y + R_Z + height_Separate + 120 + BackgroundOffset));
|
||||
}
|
||||
else
|
||||
Layer.second->SetPos(VecFPos((-R_X + width_Separate), -R_Y + R_Z + height_Separate + 120 + BackgroundOffset));
|
||||
}
|
||||
if (_isSmoothMoving)
|
||||
{
|
||||
// 平滑移动计算
|
||||
VecPos targetPosition(targetX, targetY);
|
||||
_currentPosition = SmoothDamp(_currentPosition, targetPosition, _velocity, _smoothTime, _maxSpeed, deltaTime);
|
||||
}
|
||||
else
|
||||
{
|
||||
_currentPosition.x = targetX;
|
||||
_currentPosition.y = targetY;
|
||||
}
|
||||
}
|
||||
|
||||
VecPos GameMapCamera::SmoothDamp(const VecPos ¤t, const VecPos &target, VecPos ¤tVelocity, float smoothTime, int maxSpeed, float deltaTime)
|
||||
{
|
||||
// 平滑时间为0时直接返回目标位置并重置速度
|
||||
if (smoothTime <= 0.f)
|
||||
{
|
||||
currentVelocity = VecPos(0, 0);
|
||||
return target;
|
||||
}
|
||||
|
||||
// 临界阻尼系数计算(避免过冲的核心参数)
|
||||
const float omega = 2.0f / smoothTime;
|
||||
const float x = omega * deltaTime;
|
||||
const float expTerm = 1.0f / (1.0f + x + 0.48f * x * x + 0.235f * x * x * x);
|
||||
|
||||
// 计算当前位置与目标的差值
|
||||
VecPos delta = target - current;
|
||||
|
||||
// 计算目标速度(基于阻尼公式和当前差值)
|
||||
// 注意:这里先通过浮点计算保持精度,最后转换为整数
|
||||
const float tx = delta.x * (omega * omega * deltaTime) - currentVelocity.x * (1.0f + 0.48f * x) * x;
|
||||
const float ty = delta.y * (omega * omega * deltaTime) - currentVelocity.y * (1.0f + 0.48f * x) * x;
|
||||
VecPos targetVelocity(static_cast<int>(tx), static_cast<int>(ty));
|
||||
|
||||
// 应用速度衰减(指数平滑)
|
||||
currentVelocity += targetVelocity;
|
||||
currentVelocity = VecPos(
|
||||
static_cast<int>(currentVelocity.x * expTerm),
|
||||
static_cast<int>(currentVelocity.y * expTerm));
|
||||
|
||||
// 限制最大速度(如果设置了maxSpeed)
|
||||
if (maxSpeed > 0)
|
||||
{
|
||||
// 计算当前速度的模长(浮点精度)
|
||||
const float speedSq = static_cast<float>(currentVelocity.x * currentVelocity.x + currentVelocity.y * currentVelocity.y);
|
||||
const float maxSpeedSq = static_cast<float>(maxSpeed * maxSpeed);
|
||||
|
||||
if (speedSq > maxSpeedSq)
|
||||
{
|
||||
// 速度超限,按比例缩放至最大速度
|
||||
const float scale = maxSpeed / sqrtf(speedSq);
|
||||
currentVelocity = VecPos(
|
||||
static_cast<int>(currentVelocity.x * scale),
|
||||
static_cast<int>(currentVelocity.y * scale));
|
||||
}
|
||||
}
|
||||
|
||||
// 计算新位置(当前位置 + 速度 * 时间)
|
||||
VecPos newPos = current + VecPos(
|
||||
static_cast<int>(currentVelocity.x * deltaTime),
|
||||
static_cast<int>(currentVelocity.y * deltaTime));
|
||||
|
||||
// 检测过冲:如果新位置已越过目标,直接锁定目标并重置速度
|
||||
// 点积 > 0 表示方向相反(已超过目标)
|
||||
const int dot = delta.x * (newPos.x - target.x) + delta.y * (newPos.y - target.y);
|
||||
if (dot > 0)
|
||||
{
|
||||
newPos = target;
|
||||
currentVelocity = VecPos(0, 0);
|
||||
}
|
||||
|
||||
return newPos;
|
||||
}
|
||||
@@ -5,41 +5,37 @@ 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.03;
|
||||
// 人物中线长度
|
||||
int CharacterLineLength = 0;
|
||||
// 摄像机朝向
|
||||
int Direction = 1;
|
||||
// 摄像机朝向时间
|
||||
float DirectionTime = 0.f;
|
||||
// 摄像机记录的跟随对象坐标
|
||||
VecPos3 FromActorPos = {0, 0, 0};
|
||||
|
||||
// 缩放比率
|
||||
float CameraRate = 1.0;
|
||||
|
||||
// 添加平滑移动相关的成员变量
|
||||
bool _isSmoothMoving = false;
|
||||
VecPos _currentPosition; // 当前摄像机位置
|
||||
VecPos _velocity; // 当前移动速度(用于平滑移动)
|
||||
float _smoothTime = 0.1f; // 平滑时间(秒),可调整
|
||||
int _maxSpeed = 2000; // 最大移动速度,防止过快
|
||||
|
||||
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 SyncPos(float deltaTime);
|
||||
void SyncPosByFromParent(float deltaTime);
|
||||
|
||||
// 平滑阻尼函数(类似Unity的Mathf.SmoothDamp)
|
||||
VecPos SmoothDamp(const VecPos ¤t, const VecPos &target, VecPos ¤tVelocity, float smoothTime, int maxSpeed, float deltaTime);
|
||||
};
|
||||
|
||||
@@ -28,6 +28,7 @@ void GameMapLayer::Render()
|
||||
// 绘制填充矩形
|
||||
SDL_RenderFillRect(renderer, &buf);
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 250);
|
||||
}
|
||||
|
||||
void GameMapLayer::AddDebugFeasibleAreaInfo(VecFPos pos, VecSize size)
|
||||
|
||||
@@ -7,8 +7,6 @@ class BaseObject;
|
||||
class GameMapLayer : public Actor
|
||||
{
|
||||
private:
|
||||
// 地图对象
|
||||
std::vector<RefPtr<Component>> ObjectManager;
|
||||
// 可行区域信息
|
||||
std::vector<SDL_Rect> FeasibleAreaInfoList;
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "Tile.h"
|
||||
#include "Tool/Tool_String.h"
|
||||
#include "EngineCore/Game.h"
|
||||
Tile::Tile()
|
||||
{
|
||||
}
|
||||
@@ -12,6 +13,7 @@ Tile::Tile(std::string Path) : Sprite()
|
||||
|
||||
m_texture = new Texture(std::get<std::string>(m_data["path"]), std::get<int>(m_data["idx"]));
|
||||
Sprite::Init();
|
||||
this->imgPath = Path;
|
||||
}
|
||||
|
||||
Tile::~Tile()
|
||||
@@ -42,4 +44,4 @@ void Tile::InitInfo(std::string Path)
|
||||
m_data["pos"] = std::stoi(Data.Get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
#include "ActiveObject.h"
|
||||
|
||||
void ActiveObject::SetPosition(VecFPos3 pos)
|
||||
void ActiveObject::SetPosition(VecPos3 pos)
|
||||
{
|
||||
BaseObject::SetPosition(pos);
|
||||
BaseObject::SetRenderZOrder(this->Position.y);
|
||||
}
|
||||
|
||||
void ActiveObject::SetYpos(float y)
|
||||
void ActiveObject::SetYpos(int y)
|
||||
{
|
||||
BaseObject::SetYpos(y);
|
||||
BaseObject::SetRenderZOrder(this->Position.y);
|
||||
}
|
||||
|
||||
void ActiveObject::SetSpeed(VecSpeed3 speed)
|
||||
@@ -24,27 +21,49 @@ VecSpeed3 ActiveObject::GetSpeed()
|
||||
|
||||
void ActiveObject::Update(float deltaTime)
|
||||
{
|
||||
// X Y 轴方向的加速度计算
|
||||
if (Speed.x != 0 || Speed.y != 0)
|
||||
int IntegerDelta = static_cast<int>(deltaTime * 1000);
|
||||
const int Gravity = 1020;
|
||||
|
||||
// X轴移动(含余数补偿)
|
||||
if (Speed.x != 0)
|
||||
{
|
||||
MoveBy(Speed.x * deltaTime, Speed.y * deltaTime, 0);
|
||||
int totalX = Speed.x * IntegerDelta + Remainder.x; // 加上上次余数
|
||||
int moveX = totalX / 1000; // 整数部分为实际移动
|
||||
Remainder.x = totalX % 1000; // 保留余数(-999~999)
|
||||
MoveBy(moveX, 0, 0);
|
||||
}
|
||||
// Z轴只在Z轴大于0时 或者 Z轴速度向上时才会有重力
|
||||
|
||||
// Y轴移动(同上)
|
||||
if (Speed.y != 0)
|
||||
{
|
||||
int totalY = Speed.y * IntegerDelta + Remainder.y;
|
||||
int moveY = totalY / 1000;
|
||||
Remainder.y = totalY % 1000;
|
||||
MoveBy(0, moveY, 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);
|
||||
// 重力对速度的影响(先更新Speed.z,含余数)
|
||||
int speedZTotal = -Gravity * IntegerDelta + Remainder.z; // 注意负号(减速)
|
||||
Speed.z += speedZTotal / 1000; // 速度的整数部分
|
||||
Remainder.z = speedZTotal % 1000; // 速度的余数
|
||||
|
||||
// 基于更新后的Speed.z计算移动量(同样含余数)
|
||||
int moveZTotal = Speed.z * IntegerDelta + _zRemainderMove; // 新增_zRemainderMove记录移动余数
|
||||
int moveZ = moveZTotal / 1000;
|
||||
_zRemainderMove = moveZTotal % 1000;
|
||||
MoveBy(0, 0, moveZ);
|
||||
}
|
||||
// Z轴小于0时要修正
|
||||
else if (Position.z < 0)
|
||||
{
|
||||
Position.z = 0;
|
||||
Speed.z = 0;
|
||||
Remainder.z = 0; // 重置余数
|
||||
_zRemainderMove = 0;
|
||||
SetPosition(Position);
|
||||
SDL_LogError(0, "修正Z轴");
|
||||
}
|
||||
// 执行父类的更新
|
||||
|
||||
BaseObject::Update(deltaTime);
|
||||
}
|
||||
|
||||
@@ -5,11 +5,16 @@ class ActiveObject : public BaseObject
|
||||
{
|
||||
|
||||
public:
|
||||
// 三轴速度
|
||||
VecSpeed3 Speed;
|
||||
// 三轴移动余数
|
||||
VecPos3 Remainder;
|
||||
int _zRemainderMove = 0;
|
||||
|
||||
public:
|
||||
void SetPosition(VecFPos3 pos) override;
|
||||
void SetYpos(float y) override;
|
||||
void
|
||||
SetPosition(VecPos3 pos) override;
|
||||
void SetYpos(int y) override;
|
||||
|
||||
void SetSpeed(VecSpeed3 speed);
|
||||
VecSpeed3 GetSpeed();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "BaseObject.h"
|
||||
#include "Actor/Map/GameMap.h"
|
||||
#include "Actor/Map/GameMapCamera.h"
|
||||
|
||||
BaseObject::BaseObject()
|
||||
{
|
||||
Init(); // 调用了RenderBase的Init函数 对象才会被执行回调
|
||||
@@ -10,41 +12,50 @@ BaseObject::~BaseObject()
|
||||
{
|
||||
}
|
||||
|
||||
void BaseObject::SetPosition(VecFPos3 pos)
|
||||
void BaseObject::Update(float deltaTime)
|
||||
{
|
||||
if (_AffCamera != nullptr)
|
||||
{
|
||||
_AffCamera->SyncPos(deltaTime);
|
||||
}
|
||||
Actor::Update(deltaTime);
|
||||
}
|
||||
|
||||
void BaseObject::SetPosition(VecPos3 pos)
|
||||
{
|
||||
if (pos.y != this->Position.y)
|
||||
{
|
||||
SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
|
||||
SetRenderZOrder(pos.y); // 设置渲染顺序
|
||||
}
|
||||
this->Position = pos;
|
||||
Actor::SetPos(VecFPos{this->Position.x, this->Position.y - this->Position.z});
|
||||
SetPos(VecFPos{this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
VecFPos3 BaseObject::GetPosition()
|
||||
VecPos3 BaseObject::GetPosition()
|
||||
{
|
||||
return this->Position;
|
||||
}
|
||||
|
||||
void BaseObject::SetXpos(float x)
|
||||
void BaseObject::SetXpos(int x)
|
||||
{
|
||||
this->Position.x = x;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetYpos(float y)
|
||||
void BaseObject::SetYpos(int y)
|
||||
{
|
||||
if (y != this->Position.y)
|
||||
{
|
||||
SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
|
||||
SetRenderZOrder(y); // 设置渲染顺序
|
||||
}
|
||||
this->Position.y = y;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetZpos(float z)
|
||||
void BaseObject::SetZpos(int z)
|
||||
{
|
||||
this->Position.z = z;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
int BaseObject::GetXpos()
|
||||
@@ -62,28 +73,33 @@ int BaseObject::GetZpos()
|
||||
return this->Position.z;
|
||||
}
|
||||
|
||||
void BaseObject::MoveBy(VecFPos3 pos)
|
||||
void BaseObject::MoveBy(VecPos3 pos)
|
||||
{
|
||||
// 只有moveby移动时判断所在地图中是否能够这样移动
|
||||
VecFPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), pos);
|
||||
if (pos.y != 0)
|
||||
VecPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), pos);
|
||||
if (RealPos.y != this->Position.y)
|
||||
{
|
||||
SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
|
||||
SetRenderZOrder(RealPos.y); // 设置渲染顺序
|
||||
}
|
||||
if (RealPos != this->Position){
|
||||
this->Position = RealPos;
|
||||
SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
this->Position = RealPos;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::MoveBy(float x, float y, float z)
|
||||
void BaseObject::MoveBy(int x, int y, int z)
|
||||
{
|
||||
// 只有moveby移动时判断所在地图中是否能够这样移动
|
||||
VecFPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), VecFPos3({x, y, z}));
|
||||
if (y != 0)
|
||||
VecPos3 RealPos = this->_AffMap->CheckIsItMovable(GetPosition(), VecPos3({x, y, z}));
|
||||
if (RealPos.y != this->Position.y)
|
||||
{
|
||||
SetRenderZOrder((int)(this->Position.y)); // 设置渲染顺序
|
||||
SetRenderZOrder(RealPos.y); // 设置渲染顺序
|
||||
}
|
||||
if (RealPos != this->Position)
|
||||
{
|
||||
this->Position = RealPos;
|
||||
SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
this->Position = RealPos;
|
||||
Actor::SetPos({this->Position.x, this->Position.y - this->Position.z});
|
||||
}
|
||||
|
||||
void BaseObject::SetDirection(int dir)
|
||||
|
||||
@@ -1,38 +1,43 @@
|
||||
#pragma once
|
||||
#include "EngineFrame/Actor/Actor.h"
|
||||
#include "Asset/Common/ObjectVars.h"
|
||||
#include "Global/Global_Enum.h"
|
||||
class GameMap;
|
||||
class GameMapCamera;
|
||||
class BaseObject : public Actor
|
||||
{
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
VecFPos3 Position; // 位置
|
||||
public:
|
||||
ObjectType m_objecttype; // 对象类型
|
||||
VecPos3 Position; // 位置
|
||||
int Direction = 0; // 方向
|
||||
GameMap *_AffMap = nullptr; // 所在地图
|
||||
GameMapCamera *_AffCamera = nullptr; // 跟随相机
|
||||
|
||||
public:
|
||||
BaseObject(/* args */);
|
||||
~BaseObject();
|
||||
|
||||
void Update(float deltaTime) override;
|
||||
|
||||
// 数据储存器
|
||||
ObjectVars _ObjectVars;
|
||||
|
||||
public:
|
||||
virtual void SetPosition(VecFPos3 pos);
|
||||
virtual void SetXpos(float x);
|
||||
virtual void SetYpos(float y);
|
||||
virtual void SetZpos(float z);
|
||||
virtual void SetPosition(VecPos3 pos);
|
||||
virtual void SetXpos(int x);
|
||||
virtual void SetYpos(int y);
|
||||
virtual void SetZpos(int z);
|
||||
|
||||
VecFPos3 GetPosition();
|
||||
VecPos3 GetPosition();
|
||||
int GetXpos();
|
||||
int GetYpos();
|
||||
int GetZpos();
|
||||
|
||||
void MoveBy(VecFPos3 pos);
|
||||
void MoveBy(float x, float y, float z);
|
||||
|
||||
void SetDirection(int dir);
|
||||
virtual void MoveBy(VecPos3 pos);
|
||||
virtual void MoveBy(int x, int y, int z);
|
||||
|
||||
virtual void SetDirection(int dir);
|
||||
int GetDirection();
|
||||
|
||||
ObjectVars &GetObjectVars();
|
||||
|
||||
@@ -5,21 +5,16 @@ CharacterObject::~CharacterObject()
|
||||
{
|
||||
}
|
||||
|
||||
void CharacterObject::Update(float deltaTime)
|
||||
{
|
||||
ActiveObject::Update(deltaTime);
|
||||
}
|
||||
|
||||
void CharacterObject::Render()
|
||||
{
|
||||
ActiveObject::Render();
|
||||
}
|
||||
|
||||
void CharacterObject::Construction(int job)
|
||||
{
|
||||
m_objecttype = ObjectType::CHARACTER;
|
||||
this->Job = job;
|
||||
// 创建装备管理器
|
||||
_EquipmentManager = new Chr_Equipment();
|
||||
_EquipmentManager->Init(this);
|
||||
// 创建阴影对象
|
||||
_Shadow = new Chr_Shadow();
|
||||
_Shadow->Init(this);
|
||||
// 创建动画管理器(一定要先创建装备管理器再创建动画管理器 因为需要读取身上的装备)
|
||||
_AnimationManager = new Chr_Animation();
|
||||
_AnimationManager->Init(this);
|
||||
@@ -55,3 +50,20 @@ void CharacterObject::ControllerMsg(CONTROLLER_MSG_TYPE msgType, void *msgData)
|
||||
this->_StateMachine->ChangeState(BASE_STATE::MOVE);
|
||||
}
|
||||
}
|
||||
|
||||
void CharacterObject::Update(float deltaTime)
|
||||
{
|
||||
ActiveObject::Update(deltaTime);
|
||||
}
|
||||
|
||||
void CharacterObject::SetPos(VecFPos pos)
|
||||
{
|
||||
BaseObject::SetPos(pos);
|
||||
if(_Shadow)_Shadow->SetPos(this->GetPos());
|
||||
}
|
||||
|
||||
void CharacterObject::SetDirection(int dir)
|
||||
{
|
||||
BaseObject::SetDirection(dir);
|
||||
if(_Shadow)_Shadow->SetDirection(this->GetDirection());
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#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
|
||||
{
|
||||
@@ -16,6 +17,9 @@ public:
|
||||
RefPtr<Chr_Controller> _Controller = nullptr;
|
||||
// 角色状态机
|
||||
RefPtr<Chr_StateMachine> _StateMachine = nullptr;
|
||||
|
||||
// 角色阴影对象
|
||||
RefPtr<Chr_Shadow> _Shadow = nullptr;
|
||||
// 职业
|
||||
int Job = 0;
|
||||
// 转职职业 如果是-1则没有转职
|
||||
@@ -25,8 +29,6 @@ public:
|
||||
~CharacterObject();
|
||||
|
||||
public:
|
||||
void Update(float deltaTime) override;
|
||||
void Render() override;
|
||||
|
||||
// 通过职业创建角色
|
||||
void Construction(int job);
|
||||
@@ -36,4 +38,8 @@ public:
|
||||
void DisableController();
|
||||
// 控制器信息
|
||||
void ControllerMsg(CONTROLLER_MSG_TYPE msgType, void* msgData);
|
||||
|
||||
void Update(float deltaTime) override;
|
||||
void SetPos(VecFPos pos) override;
|
||||
void SetDirection(int dir) override;
|
||||
};
|
||||
|
||||
10
source_game/Actor/Object/MonsterObject.cpp
Normal file
10
source_game/Actor/Object/MonsterObject.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "MonsterObject.h"
|
||||
#include "Global/Global_Game.h"
|
||||
void MonsterObject::Construction(int MonsterID)
|
||||
{
|
||||
this->Id = MonsterID;
|
||||
|
||||
// 创建动画管理器(一定要先创建装备管理器再创建动画管理器 因为需要读取身上的装备)
|
||||
_AnimationManager = new Mon_Animation();
|
||||
_AnimationManager->Init(this);
|
||||
}
|
||||
18
source_game/Actor/Object/MonsterObject.h
Normal file
18
source_game/Actor/Object/MonsterObject.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include "Actor/Object/ActiveObject.h"
|
||||
#include "Global/Global_Enum.h"
|
||||
#include "Asset/Monster/Mon_Animation.h"
|
||||
class MonsterObject : public ActiveObject
|
||||
{
|
||||
public:
|
||||
// 怪物动画管理器
|
||||
RefPtr<Mon_Animation> _AnimationManager = nullptr;
|
||||
|
||||
// 怪物ID
|
||||
int Id = 0;
|
||||
|
||||
public:
|
||||
// 通过ID创建怪物
|
||||
void Construction(int MonsterID);
|
||||
|
||||
};
|
||||
Reference in New Issue
Block a user