refactor(character): 使用整数坐标优化角色位置同步

- 将CharacterWorldPosition改为使用整数坐标,避免浮点精度问题
- 添加位置余数处理,确保移动平滑性
- 统一角色位置同步逻辑到SyncActorPositionFromWorld方法
- 修改地图移动检测使用整数坐标判断
This commit is contained in:
2026-04-05 11:42:39 +08:00
parent c4eefab70c
commit 2b0cfc6ce5
5 changed files with 122 additions and 37 deletions

View File

@@ -1,9 +1,20 @@
#include "world/GameTown.h"
#include "character/CharacterObject.h"
#include <SDL2/SDL.h>
#include <algorithm>
#include <cmath>
namespace frostbite2D {
namespace {
Vec2 RoundWorldPoint(const Vec2& pos) {
return Vec2(static_cast<float>(std::lround(pos.x)),
static_cast<float>(std::lround(pos.y)));
}
} // namespace
GameTown::GameTown() = default;
bool GameTown::Init(int index, const std::string& townPath) {
@@ -96,7 +107,12 @@ void GameTown::AddCharacter(RefPtr<Actor> actor, int areaIndex) {
if (actor) {
cameraController_.SetDebugEnabled(false);
if (areaIndex == -2 && sariaRoomPos_.x >= 0.0f && sariaRoomPos_.y >= 0.0f) {
actor->SetPosition(sariaRoomPos_);
Vec2 spawnPos = RoundWorldPoint(sariaRoomPos_);
if (auto* character = dynamic_cast<CharacterObject*>(actor.Get())) {
character->SetCharacterPosition(spawnPos);
} else {
actor->SetPosition(spawnPos);
}
}
mapIt->map->AddObject(actor);
cameraController_.SetFocus(actor->GetPosition());