载入中...
搜索中...
未找到
AnimStateMachine.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
7#include "common/StateValue.h"
8#include "common/Time.h"
9
10#include <string>
11#include <unordered_map>
12#include <vector>
13
14namespace eve::animation {
15
16class AnimClip;
17class AnimSkeleton;
18
24public:
26 explicit AnimStateMachine(AnimSkeleton *skeleton);
28 ~AnimStateMachine() override;
29
32
37 AnimSkeleton *getSkeleton() const override { return skeleton_; }
38
40 void addState(const std::string &name, AnimClip *clip);
42 void setEntry(const std::string &name);
44 bool hasState(const std::string &name) const;
46 std::string getCurrentState() const { return currentState_; }
48 int getStateCount() const { return static_cast<int>(states_.size()); }
49
54 int addTransition(const std::string &from, const std::string &to, float blendSeconds);
55
57 void addFloatCondition(int transitionId, const std::string &param, const std::string &op,
58 float threshold);
60 void addBoolCondition(int transitionId, const std::string &param, bool value);
62 void addTriggerCondition(int transitionId, const std::string &param);
64 void setExitTime(int transitionId, float normalizedTime);
66 void setHasExitTime(int transitionId, bool enabled);
67
69 void setFloat(const std::string &name, float value);
71 float getFloat(const std::string &name) const;
73 void setBool(const std::string &name, bool value);
75 bool getBool(const std::string &name) const;
77 void setTrigger(const std::string &name);
79 void resetTrigger(const std::string &name);
81 bool getTrigger(const std::string& name) const {
82 auto it = triggers_.find(name);
83 return it != triggers_.end() && it->second;
84 }
85
90 AnimPose *getPose() override;
92 float getStateTime() const { return stateTime_; }
94 bool isBlending() const { return blending_; }
95
97 [[nodiscard]] eve::Result<void> advance(const eve::SimulationStep &step) override;
98
100 [[nodiscard]] eve::SimulationTick currentTick() const noexcept override { return lastTick_; }
102 [[nodiscard]] bool hasCurrentTick() const noexcept override { return hasLastTick_; }
103
105 void update(float dt);
106
108 bool captureState(StateValue& out) const;
109
115 bool restoreState(const StateValue& in, std::string* err = nullptr);
116
118 bool resetToDefaults();
119
120private:
121 enum class CondKind { Float, Bool, Trigger };
122 enum class FloatOp { Gt, Ge, Lt, Le, Eq, Ne };
123
124 struct Condition {
125 CondKind kind = CondKind::Float;
126 std::string param;
127 FloatOp op = FloatOp::Gt;
128 float threshold = 0.f;
129 bool boolValue = true;
130 };
131
132 struct Transition {
133 std::string from;
134 std::string to;
135 float blendSeconds = 0.2f;
136 bool hasExitTime = false;
137 float exitTime = 0.f;
138 std::vector<Condition> conditions;
139 };
140
141 struct State {
142 std::string name;
143 AnimClip *clip = nullptr;
144 };
145
146 void requireState(const std::string &name) const;
147 Transition &requireTransition(int id);
148 static FloatOp parseFloatOp(const std::string &op);
149 bool conditionsMet(const Transition &tr) const;
150 bool tryTransition();
151 void updateUnchecked(float dt);
152
153 AnimSkeleton *skeleton_ = nullptr;
154 AnimPose pose_;
155 AnimPose fromPose_;
156 AnimPose toPose_;
157
158 std::unordered_map<std::string, State> states_;
159 std::vector<std::string> stateOrder_;
160 std::vector<Transition> transitions_;
161
162 std::unordered_map<std::string, float> floats_;
163 std::unordered_map<std::string, bool> bools_;
164 std::unordered_map<std::string, bool> triggers_;
165
166 std::string currentState_;
167 std::string nextState_;
168 float stateTime_ = 0.f;
169 float blendDuration_ = 0.f;
170 float blendElapsed_ = 0.f;
171 bool blending_ = false;
172 bool started_ = false;
174 bool hasLastTick_ = false;
175};
176
177} // namespace eve::animation
double value
std::string from
#define EVENGINE_API_WORLD
Definition Export.h:109
glm::vec4 clip
HexCoordinates to
Cell the unit walks towards on this segment.
Definition HexUnits.cpp:64
TokenKind kind
std::string name
float tr
Common time values and an injectable deterministic simulation clock.
float step
Definition TreeMesh.cpp:314
Move-only operation result carrying either a value or Status.
Definition Result.h:155
JSON-compatible state value tree used by state hot reload.
Definition StateValue.h:20
Keyframed skeletal animation clip (local TRS tracks per bone). Script type: AnimClip.
Definition AnimClip.h:145
Evaluated local (and optional world) pose for an AnimSkeleton. Script type: AnimPose.
Definition AnimPose.h:17
3D bone hierarchy + bind-pose local TRS for skeletal animation. Independent of ik::Skeleton3D (FABRIK...
Clip-driven animation state machine with float/bool/trigger parameters and cross-fade transitions....
bool hasCurrentTick() const noexcept override
Whether advance has consumed at least one checked step.
std::string getCurrentState() const
Returns the current state.
eve::SimulationTick currentTick() const noexcept override
Last scheduler tick consumed by the checked state-machine API.
bool getTrigger(const std::string &name) const
Query a trigger's current state.
float getStateTime() const
Returns the state time.
int getStateCount() const
Returns the state count.
AnimStateMachine(const AnimStateMachine &)=delete
AnimStateMachine & operator=(const AnimStateMachine &)=delete
AnimSkeleton * getSkeleton() const override
Borrowed pointer accessor. @ownership Borrowed @lifetime Valid while the owning animation object rema...
bool isBlending() const
True when blending.
Borrowed pose provider consumed by AnimLayerMixer.
static constexpr StrongUint64 zero() noexcept
Returns the zero value for this strong type.
State
Lifecycle state of a generic transaction plan.
enum EVENGINE_API_FOUNDATION Float
OT_FLOAT.
Definition Runtime.h:118
enum EVENGINE_API_FOUNDATION Bool
OT_BOOL.
Definition Runtime.h:116
bool enabled
One deterministic fixed-step emitted by SimulationClock.
Definition Time.h:158