Files
Frostbite2D/Game/include/character/states/CharacterStateBase.h
Lenheart 1200cf0181 refactor(character): 重构角色动作与动画系统
- 移除自动回退动作生成逻辑,改为严格检查动作定义
- 增加动作资源缺失时的详细错误报告机制
- 统一输入事件处理接口,优化角色对象生命周期管理
- 改进动画标签管理,移除隐式回退逻辑
- 增强状态机对无效动作的处理能力
2026-04-04 05:53:07 +08:00

93 lines
3.2 KiB
C++

#pragma once
#include "character/CharacterActionTypes.h"
#include <string>
namespace frostbite2D {
class CharacterObject;
class CharacterStateMachine;
struct CharacterActionDefinition;
struct CharacterStateContext {
CharacterObject& owner;
CharacterCommandBuffer& commandBuffer;
const CharacterIntent& intent;
float deltaTime = 0.0f;
};
class ICharacterStateNode {
public:
virtual ~ICharacterStateNode() = default;
virtual CharacterStateId GetId() const = 0;
virtual void OnEnter(CharacterStateMachine& machine,
CharacterStateContext& context,
CharacterStateId previousState) {
(void)machine;
(void)context;
(void)previousState;
}
virtual void OnUpdate(CharacterStateMachine& machine,
CharacterStateContext& context) = 0;
virtual void OnExit(CharacterStateMachine& machine,
CharacterStateContext& context,
CharacterStateId nextState) {
(void)machine;
(void)context;
(void)nextState;
}
};
class ICharacterActionStateNode {
public:
virtual ~ICharacterActionStateNode() = default;
virtual bool TryEnter(CharacterStateMachine& machine,
CharacterStateContext& context) = 0;
};
class CharacterStateBase : public ICharacterStateNode {
public:
explicit CharacterStateBase(CharacterStateId stateId);
~CharacterStateBase() override = default;
CharacterStateId GetId() const override;
protected:
void ChangeState(CharacterStateMachine& machine,
CharacterStateContext& context,
CharacterStateId nextState) const;
void PlayActionById(CharacterObject& owner, const std::string& actionId) const;
void InvokeHook(CharacterStateMachine& machine,
CharacterObject& owner,
const CharacterActionDefinition* action,
const char* phase) const;
bool TryStartAction(CharacterStateMachine& machine,
CharacterStateContext& context,
CharacterCommandType commandType,
const std::string& actionId) const;
bool TryStartRegisteredAction(CharacterStateMachine& machine,
CharacterStateContext& context) const;
void ApplyFrameEvents(CharacterStateMachine& machine,
CharacterObject& owner,
int previousFrame,
int currentFrame) const;
const CharacterActionDefinition* GetCurrentAction(const CharacterStateMachine& machine) const;
const std::string& GetCurrentActionId(const CharacterStateMachine& machine) const;
void SetCurrentAction(CharacterStateMachine& machine,
const CharacterActionDefinition* action,
const std::string& actionId) const;
void ClearCurrentAction(CharacterStateMachine& machine) const;
float& StateTime(CharacterStateMachine& machine) const;
float& ActionFrameProgress(CharacterStateMachine& machine) const;
int& ActionFrame(CharacterStateMachine& machine) const;
float& LandingTimer(CharacterStateMachine& machine) const;
private:
CharacterStateId stateId_ = CharacterStateId::Idle;
};
} // namespace frostbite2D