feat(角色状态机): 重构角色状态系统为基于类的设计
新增通用状态类和职业专属状态类,将状态逻辑从状态机中解耦 添加状态注册机制,支持按职业配置状态 实现基础状态如待机、移动、跳跃、受击等 为剑士职业实现专属攻击状态 补充状态机开发说明文档
This commit is contained in:
94
Game/include/character/states/CharacterStateBase.h
Normal file
94
Game/include/character/states/CharacterStateBase.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#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 std::string& fallbackTag = "rest") 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
|
||||
Reference in New Issue
Block a user