refactor(character): 重构角色动作与动画系统

- 移除自动回退动作生成逻辑,改为严格检查动作定义
- 增加动作资源缺失时的详细错误报告机制
- 统一输入事件处理接口,优化角色对象生命周期管理
- 改进动画标签管理,移除隐式回退逻辑
- 增强状态机对无效动作的处理能力
This commit is contained in:
2026-04-04 05:53:07 +08:00
parent f64180ebed
commit 1200cf0181
13 changed files with 380 additions and 202 deletions

View File

@@ -3,6 +3,7 @@
#include <SDL2/SDL.h>
#include <array>
#include <cstdio>
#include <sstream>
namespace frostbite2D {
namespace {
@@ -51,12 +52,6 @@ bool CharacterAnimation::Init(CharacterObject* parent,
return false;
}
if (actionAnimations_.count("rest") > 0) {
SetAction("rest");
} else {
SetAction(actionAnimations_.begin()->first);
}
return true;
}
@@ -129,13 +124,37 @@ void CharacterAnimation::CreateAnimationBySlot(
}
}
void CharacterAnimation::SetAction(const std::string& actionName) {
std::string CharacterAnimation::DescribeAvailableActions() const {
if (actionAnimations_.empty()) {
return "<none>";
}
std::ostringstream stream;
bool first = true;
for (const auto& [actionName, animations] : actionAnimations_) {
(void)animations;
if (!first) {
stream << ", ";
}
stream << actionName;
first = false;
}
return stream.str();
}
bool CharacterAnimation::SetAction(const std::string& actionName) {
auto nextIt = actionAnimations_.find(actionName);
if (nextIt == actionAnimations_.end()) {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"CharacterAnimation: action %s missing, keep %s", actionName.c_str(),
currentActionTag_.c_str());
return;
if (parent_) {
parent_->ReportFatalCharacterError("CharacterAnimation::SetAction",
"requested animation tag is not loaded",
std::string(), actionName);
} else {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"CharacterAnimation: action %s missing and parent is null",
actionName.c_str());
}
return false;
}
if (!currentActionTag_.empty()) {
@@ -154,6 +173,7 @@ void CharacterAnimation::SetAction(const std::string& actionName) {
}
currentActionTag_ = actionName;
SetDirection(direction_);
return true;
}
void CharacterAnimation::SetDirection(int direction) {