feat(角色状态机): 重构角色状态系统为基于类的设计
新增通用状态类和职业专属状态类,将状态逻辑从状态机中解耦 添加状态注册机制,支持按职业配置状态 实现基础状态如待机、移动、跳跃、受击等 为剑士职业实现专属攻击状态 补充状态机开发说明文档
This commit is contained in:
@@ -1,20 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include "character/CharacterActionLibrary.h"
|
||||
#include "character/CharacterDataLoader.h"
|
||||
#include "character/states/CharacterStateBase.h"
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace frostbite2D {
|
||||
|
||||
class CharacterObject;
|
||||
|
||||
/// 角色主状态机。输入先变成意图,再由状态机决定当前大状态和动作推进。
|
||||
class CharacterStateMachine {
|
||||
public:
|
||||
using StateRegistry = std::unordered_map<CharacterStateId, std::unique_ptr<ICharacterStateNode>>;
|
||||
|
||||
CharacterStateMachine() = default;
|
||||
|
||||
void Configure(const character::CharacterConfig& config);
|
||||
void Reset();
|
||||
void Update(CharacterObject& owner,
|
||||
CharacterCommandBuffer& commandBuffer,
|
||||
const CharacterIntent& intent,
|
||||
float deltaTime);
|
||||
void ForceHurt(const CharacterActionDefinition* hurtAction);
|
||||
void ForceHurt(CharacterObject& owner, const CharacterActionDefinition* hurtAction);
|
||||
|
||||
CharacterStateId GetState() const { return currentState_; }
|
||||
const std::string& GetCurrentActionId() const { return currentActionId_; }
|
||||
@@ -27,24 +35,21 @@ public:
|
||||
bool IsMovementLocked() const;
|
||||
bool CanTurn() const;
|
||||
|
||||
void RegisterState(std::unique_ptr<ICharacterStateNode> stateNode);
|
||||
void ClearRegisteredStates();
|
||||
ICharacterStateNode* FindStateNode(CharacterStateId stateId) const;
|
||||
|
||||
private:
|
||||
void ChangeState(CharacterObject& owner, CharacterStateId nextState);
|
||||
void EnterAction(CharacterObject& owner, const CharacterActionDefinition* action);
|
||||
void UpdateGroundState(CharacterObject& owner,
|
||||
CharacterCommandBuffer& commandBuffer,
|
||||
const CharacterIntent& intent);
|
||||
void UpdateAirState(CharacterObject& owner,
|
||||
CharacterCommandBuffer& commandBuffer,
|
||||
const CharacterIntent& intent);
|
||||
void UpdateActionState(CharacterObject& owner,
|
||||
CharacterCommandBuffer& commandBuffer,
|
||||
const CharacterIntent& intent,
|
||||
float deltaTime);
|
||||
void UpdateHurtState(CharacterObject& owner, float deltaTime);
|
||||
void InvokeStateHook(CharacterObject& owner,
|
||||
const CharacterActionDefinition* action,
|
||||
const char* phase) const;
|
||||
bool TryStartAction(CharacterObject& owner,
|
||||
CharacterCommandBuffer& commandBuffer,
|
||||
CharacterCommandType commandType,
|
||||
const std::string& actionId);
|
||||
bool TryStartRegisteredAction(CharacterStateContext& context);
|
||||
void ApplyFrameEvents(CharacterObject& owner, int previousFrame, int currentFrame);
|
||||
|
||||
CharacterStateId currentState_ = CharacterStateId::Idle;
|
||||
@@ -54,6 +59,9 @@ private:
|
||||
float actionFrameProgress_ = 0.0f;
|
||||
int actionFrame_ = 0;
|
||||
float landingTimer_ = 0.0f;
|
||||
StateRegistry stateRegistry_;
|
||||
|
||||
friend class CharacterStateBase;
|
||||
};
|
||||
|
||||
} // namespace frostbite2D
|
||||
|
||||
Reference in New Issue
Block a user