Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
UnitAnimation.h
Go to the documentation of this file.
1/* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_UNITANIMATION
19#define INCLUDED_UNITANIMATION
20
21#include "ps/CStr.h"
23
24#include <vector>
25
26class CUnit;
27class CModel;
28class CSkeletonAnim;
29class CObjectEntry;
30
31/**
32 * Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim)
33 * and the simulation system (via CUnit), providing a simple fire-and-forget API to play animations.
34 * (This is really just a component of CUnit and could probably be merged back into that class.)
35 */
37{
39public:
40 /**
41 * Construct for a given unit, defaulting to the "idle" animation.
42 */
43 CUnitAnimation(entity_id_t ent, CModel* model, CObjectEntry* object);
44
45 /**
46 * Start playing an animation.
47 * The unit's actor defines the available animations, and if more than one is available
48 * then one is picked at random (with a new random choice each loop).
49 * By default, animations start immediately and run at the given speed with no syncing.
50 * Use SetAnimationSync after this to force a specific timing, if it needs to match the
51 * simulation timing.
52 * Alternatively, set @p desync to a non-zero value (e.g. 0.05) to slightly randomise the
53 * offset and speed, so units don't all move in lockstep.
54 * @param name animation's name ("idle", "walk", etc)
55 * @param once if true then the animation freezes on its last frame; otherwise it loops
56 * @param speed fraction of actor-defined speed to play back at (should typically be 1.0)
57 * @param desync maximum fraction of length/speed to randomly adjust timings (or 0.0 for no desyncing)
58 * @param actionSound sound group name to be played at the 'action' point in the animation, or empty string
59 */
60 void SetAnimationState(const CStr& name, bool once, float speed, float desync, const CStrW& actionSound);
61
62 /**
63 * Adjust the speed of the current animation, so that Update(repeatTime) will do a
64 * complete animation loop.
65 * @param repeatTime time for complete loop of animation, in msec
66 */
67 void SetAnimationSyncRepeat(float repeatTime);
68
69 /**
70 * Adjust the offset of the current animation, so that Update(actionTime) will advance it
71 * to the 'action' point defined in the actor.
72 * This must be called after SetAnimationSyncRepeat sets the speed.
73 * @param actionTime time between now and when the action should occur, in msec
74 */
75 void SetAnimationSyncOffset(float actionTime);
76
77 /**
78 * Advance the animation state.
79 * @param time advance time in msec
80 */
81 void Update(float time);
82
83 /**
84 * Regenerate internal animation state from the models in the current unit.
85 * This should be called whenever the unit is changed externally, to keep this in sync.
86 */
87 void ReloadUnit(CModel* model, const CObjectEntry* object);
88
89 /**
90 * Reload animation so any changes take immediate effect.
91 */
92 void ReloadAnimation();
93
94private:
95
96 /**
97 * Picks a new animation ID from our current state
98 */
99 void PickAnimationID();
100
102 {
106 float time;
110 };
111
112 std::vector<SModelAnimState> m_AnimStates;
113
114 /**
115 * True if all the current AnimStates are static, so Update() doesn't need
116 * to do any work at all
117 */
119
120 void AddModel(CModel* model, const CObjectEntry* object);
121
126 CStr m_AnimationID = "";
129 float m_Speed;
131 float m_Desync;
133};
134
135#endif // INCLUDED_UNITANIMATION
Definition: Model.h:39
Definition: ObjectEntry.h:38
Definition: SkeletonAnim.h:33
Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim) and the simulati...
Definition: UnitAnimation.h:37
void Update(float time)
Advance the animation state.
Definition: UnitAnimation.cpp:157
void SetAnimationState(const CStr &name, bool once, float speed, float desync, const CStrW &actionSound)
Start playing an animation.
Definition: UnitAnimation.cpp:102
CModel * m_Model
Definition: UnitAnimation.h:123
NONCOPYABLE(CUnitAnimation)
const CObjectEntry * m_Object
Definition: UnitAnimation.h:124
CStr m_State
Definition: UnitAnimation.h:125
float m_OriginalSpeed
Definition: UnitAnimation.h:128
void SetAnimationSyncRepeat(float repeatTime)
Adjust the speed of the current animation, so that Update(repeatTime) will do a complete animation lo...
Definition: UnitAnimation.cpp:119
bool m_Looping
Definition: UnitAnimation.h:127
CStrW m_ActionSound
Definition: UnitAnimation.h:132
CStr m_AnimationID
Definition: UnitAnimation.h:126
float m_Speed
Definition: UnitAnimation.h:129
void SetAnimationSyncOffset(float actionTime)
Adjust the offset of the current animation, so that Update(actionTime) will advance it to the 'action...
Definition: UnitAnimation.cpp:124
void AddModel(CModel *model, const CObjectEntry *object)
Definition: UnitAnimation.cpp:53
CUnitAnimation(entity_id_t ent, CModel *model, CObjectEntry *object)
Construct for a given unit, defaulting to the "idle" animation.
Definition: UnitAnimation.cpp:46
entity_id_t m_Entity
Definition: UnitAnimation.h:122
float m_Desync
Definition: UnitAnimation.h:131
float m_SyncRepeatTime
Definition: UnitAnimation.h:130
void ReloadAnimation()
Reload animation so any changes take immediate effect.
Definition: UnitAnimation.cpp:86
std::vector< SModelAnimState > m_AnimStates
Definition: UnitAnimation.h:112
void ReloadUnit(CModel *model, const CObjectEntry *object)
Regenerate internal animation state from the models in the current unit.
Definition: UnitAnimation.cpp:91
bool m_AnimStatesAreStatic
True if all the current AnimStates are static, so Update() doesn't need to do any work at all.
Definition: UnitAnimation.h:118
void PickAnimationID()
Picks a new animation ID from our current state.
Definition: UnitAnimation.cpp:280
Definition: Unit.h:38
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
Definition: UnitAnimation.h:102
const CObjectEntry * object
Definition: UnitAnimation.h:105
bool pastSoundPos
Definition: UnitAnimation.h:109
float time
Definition: UnitAnimation.h:106
CSkeletonAnim * anim
Definition: UnitAnimation.h:104
bool pastActionPos
Definition: UnitAnimation.h:108
bool pastLoadPos
Definition: UnitAnimation.h:107
CModel * model
Definition: UnitAnimation.h:103