加入 Node节点类 还未测试新框架

This commit is contained in:
2025-10-27 23:12:56 +08:00
parent 80d088316b
commit 0ae47e5d6a
52 changed files with 1642 additions and 458 deletions

View File

@@ -0,0 +1,120 @@
#include "GameCamera.h"
#include "EngineCore/Game.h"
#include "Actor/Map/GameMap.h"
#include "Actor/Object/BaseObject.h"
#include "Global/Global_Game.h"
#include <algorithm>
void GameCamera::SetFromActor(BaseObject *actor)
{
// 如果原来有跟随对象
if (this->_FromActor)
{
this->_FromActor->RemoveCallbackOnUpdate("Camear Update");
}
this->_FromActor = actor;
actor->SetCallbackOnUpdate("Camear Update", [](float deltaTime) mutable
{ Global_Game::GetInstance().GetCamera()->Update(deltaTime); });
}
void GameCamera::Update(float deltaTime)
{
SyncPosByFromParent(deltaTime);
}
void GameCamera::SyncPos(float deltaTime)
{
}
void GameCamera::SetPos(float x, float y, float z)
{
// this->X = x;
// this->Y = y;
// this->Z = z;
}
void GameCamera::AddPos(float x, float y, float z)
{
// this->X += x;
// this->Y += y;
// this->Z += z;
}
void GameCamera::SyncPosByFromParent(float deltaTime)
{
if (this->_FromActor == nullptr)
{
return;
}
float targetX = _FromActor->Position.x;
float targetY = _FromActor->Position.y;
if (_isSmoothMoving)
{
// 平滑移动计算
Vec2 targetPosition(targetX, targetY);
_currentPosition = SmoothDamp(_currentPosition, targetPosition, _velocity, _smoothTime, _maxSpeed, deltaTime);
}
else
{
_currentPosition.x = targetX;
_currentPosition.y = targetY;
}
}
Vec2 GameCamera::SmoothDamp(const Vec2 &current, const Vec2 &target, Vec2 &currentVelocity, float smoothTime, float maxSpeed, float deltaTime)
{
// 平滑时间为0时直接返回目标位置并重置速度
if (smoothTime <= 0.f)
{
currentVelocity = Vec2(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);
// 计算当前位置与目标的差值
Vec2 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;
Vec2 targetVelocity(tx, ty);
// 应用速度衰减(指数平滑)
currentVelocity += targetVelocity;
currentVelocity = Vec2(currentVelocity.x * expTerm, currentVelocity.y * expTerm);
// 限制最大速度如果设置了maxSpeed
if (maxSpeed > 0)
{
// 计算当前速度的模长(浮点精度)
const float speedSq = currentVelocity.x * currentVelocity.x + currentVelocity.y * currentVelocity.y;
const float maxSpeedSq = maxSpeed * maxSpeed;
if (speedSq > maxSpeedSq)
{
// 速度超限,按比例缩放至最大速度
const float scale = maxSpeed / sqrtf(speedSq);
currentVelocity = Vec2(currentVelocity.x * scale, currentVelocity.y * scale);
}
}
// 计算新位置(当前位置 + 速度 * 时间)
Vec2 newPos = current + Vec2(currentVelocity.x * deltaTime, currentVelocity.y * deltaTime);
// 检测过冲:如果新位置已越过目标,直接锁定目标并重置速度
// 点积 > 0 表示方向相反(已超过目标)
const float dot = delta.x * (newPos.x - target.x) + delta.y * (newPos.y - target.y);
if (dot > 0)
{
newPos = target;
currentVelocity = Vec2(0, 0);
}
return newPos;
}

View File

@@ -0,0 +1,35 @@
#pragma once
#include "EngineFrame/Base/Actor.h"
class GameMap;
class BaseObject;
class GameCamera : public Actor
{
private:
// 跟随对象
BaseObject *_FromActor = nullptr;
public:
// 缩放比率
float CameraRate = 1.0;
// 添加平滑移动相关的成员变量
bool _isSmoothMoving = false;
Vec2 _currentPosition; // 当前摄像机位置
Vec2 _velocity; // 当前移动速度(用于平滑移动)
float _smoothTime = 0.1f; // 平滑时间(秒),可调整
float _maxSpeed = 2000; // 最大移动速度,防止过快
public:
void SetFromActor(BaseObject *actor);
void Update(float deltaTime);
void SetPos(float x, float y, float z);
void AddPos(float x, float y, float z);
// 同步坐标相关
void SyncPos(float deltaTime);
void SyncPosByFromParent(float deltaTime);
// 平滑阻尼函数类似Unity的Mathf.SmoothDamp
Vec2 SmoothDamp(const Vec2 &current, const Vec2 &target, Vec2 &currentVelocity, float smoothTime, float maxSpeed, float deltaTime);
};

View File

@@ -4,9 +4,6 @@
Global_Game::Global_Game()
{
}
Global_Game::~Global_Game()
{
}
void Global_Game::InitFont()
{
@@ -65,11 +62,6 @@ void Global_Game::InitFont()
void Global_Game::Init()
{
InitFont();
// TTF_Font *FontBuf = TTF_OpenFont("Fonts/NotoSansSC-Regular.otf", 24);
// TTF_Font *FontBuf = TTF_OpenFont("Fonts/calibri.ttf", 24);
}
void Global_Game::InitGame()
@@ -83,9 +75,11 @@ void Global_Game::InitGame()
// 初始化松鼠脚本管理器
SquirrelManager::GetInstance().Init();
// 读取存档
SavaManager::GetInstance().Init();
//创建摄像机
_GameCamera = new GameCamera();
// 游戏初始化完成标志
InitFlag = true;
}
@@ -109,4 +103,9 @@ GlobalMonsterScript::MonsterConfig Global_Game::GetMonsterInfo(int id)
MonsterConfig.id = id;
MonsterInfoMap[id] = MonsterConfig;
return MonsterConfig;
}
}
RefPtr<GameCamera> Global_Game::GetCamera()
{
return _GameCamera;
}

View File

@@ -4,6 +4,7 @@
#include "Global/Script/EquipmentConfig.h"
#include "Global/Script/MonsterConfig.h"
#include "Global/Save/SavaManager.h"
#include "Global/GameCamera.h"
class Global_Game
{
@@ -19,14 +20,15 @@ public:
return instance;
}
Global_Game();
// 游戏资源加载之前的初始化
void Init();
// 游戏资源加载之后的初始化
void InitGame();
public:
// 当前游戏状态 0未初始化
int game_state = 0;
private:
void InitFont();
public:
// 字体资源
@@ -39,14 +41,14 @@ public:
// 怪物相关
std::map<int, std::string> MonsterPathMap; // 路径
std::map<int, GlobalMonsterScript::MonsterConfig> MonsterInfoMap; // 信息
GlobalMonsterScript::MonsterConfig GetMonsterInfo(int id); // 获取怪物信息
GlobalMonsterScript::MonsterConfig GetMonsterInfo(int id); // 获取怪物信息
// 游戏资源初始化标志
// 游戏摄像机
RefPtr<GameCamera> _GameCamera = nullptr;
// 游戏资源初始化标志
bool InitFlag = false;
private:
Global_Game(/* args */);
~Global_Game();
void InitFont();
public:
RefPtr<GameCamera> GetCamera();
};