Pyrogenesis  trunk
UnitAnimation.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 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"
22 
24 
25 class CUnit;
26 class CModel;
27 class CSkeletonAnim;
28 class CObjectEntry;
29 
30 /**
31  * Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim)
32  * and the simulation system (via CUnit), providing a simple fire-and-forget API to play animations.
33  * (This is really just a component of CUnit and could probably be merged back into that class.)
34  */
36 {
38 public:
39  /**
40  * Construct for a given unit, defaulting to the "idle" animation.
41  */
43 
44  /**
45  * Change the entity ID associated with this animation
46  * (currently used for playing locational sound effects).
47  */
48  void SetEntityID(entity_id_t ent);
49 
50  /**
51  * Start playing an animation.
52  * The unit's actor defines the available animations, and if more than one is available
53  * then one is picked at random (with a new random choice each loop).
54  * By default, animations start immediately and run at the given speed with no syncing.
55  * Use SetAnimationSync after this to force a specific timing, if it needs to match the
56  * simulation timing.
57  * Alternatively, set @p desync to a non-zero value (e.g. 0.05) to slightly randomise the
58  * offset and speed, so units don't all move in lockstep.
59  * @param name animation's name ("idle", "walk", etc)
60  * @param once if true then the animation freezes on its last frame; otherwise it loops
61  * @param speed fraction of actor-defined speed to play back at (should typically be 1.0)
62  * @param desync maximum fraction of length/speed to randomly adjust timings (or 0.0 for no desyncing)
63  * @param actionSound sound group name to be played at the 'action' point in the animation, or empty string
64  */
65  void SetAnimationState(const CStr& name, bool once, float speed, float desync, const CStrW& actionSound);
66 
67  /**
68  * Adjust the speed of the current animation, so that Update(repeatTime) will do a
69  * complete animation loop.
70  * @param repeatTime time for complete loop of animation, in msec
71  */
72  void SetAnimationSyncRepeat(float repeatTime);
73 
74  /**
75  * Adjust the offset of the current animation, so that Update(actionTime) will advance it
76  * to the 'action' point defined in the actor.
77  * This must be called after SetAnimationSyncRepeat sets the speed.
78  * @param actionTime time between now and when the action should occur, in msec
79  */
80  void SetAnimationSyncOffset(float actionTime);
81 
82  /**
83  * Advance the animation state.
84  * @param time advance time in msec
85  */
86  void Update(float time);
87 
88  /**
89  * Regenerate internal animation state from the models in the current unit.
90  * This should be called whenever the unit is changed externally, to keep this in sync.
91  */
92  void ReloadUnit(CModel* model, const CObjectEntry* object);
93 
94 private:
95 
96  /**
97  * Picks a new animation ID from our current state
98  */
99  void UpdateAnimationID();
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 
125  CStr m_State;
126  CStr m_AnimationID = "";
127  bool m_Looping;
129  float m_Speed;
131  float m_Desync;
133 };
134 
135 #endif // INCLUDED_UNITANIMATION
entity_id_t m_Entity
Definition: UnitAnimation.h:122
void AddModel(CModel *model, const CObjectEntry *object)
Definition: UnitAnimation.cpp:55
bool pastSoundPos
Definition: UnitAnimation.h:109
bool m_Looping
Definition: UnitAnimation.h:127
CStr m_State
Definition: UnitAnimation.h:125
CModel * model
Definition: UnitAnimation.h:103
CStrW m_ActionSound
Definition: UnitAnimation.h:132
Definition: Unit.h:36
void SetEntityID(entity_id_t ent)
Change the entity ID associated with this animation (currently used for playing locational sound effe...
Definition: UnitAnimation.cpp:50
float time
Definition: UnitAnimation.h:106
bool m_AnimStatesAreStatic
True if all the current AnimStates are static, so Update() doesn&#39;t need to do any work at all...
Definition: UnitAnimation.h:118
CSkeletonAnim * anim
Definition: UnitAnimation.h:104
void Update(float time)
Advance the animation state.
Definition: UnitAnimation.cpp:155
void UpdateAnimationID()
Picks a new animation ID from our current state.
Definition: UnitAnimation.cpp:278
float m_Speed
Definition: UnitAnimation.h:129
CStr m_AnimationID
Definition: UnitAnimation.h:126
void ReloadUnit(CModel *model, const CObjectEntry *object)
Regenerate internal animation state from the models in the current unit.
Definition: UnitAnimation.cpp:88
float m_Desync
Definition: UnitAnimation.h:131
void SetAnimationState(const CStr &name, bool once, float speed, float desync, const CStrW &actionSound)
Start playing an animation.
Definition: UnitAnimation.cpp:98
float m_SyncRepeatTime
Definition: UnitAnimation.h:130
void SetAnimationSyncRepeat(float repeatTime)
Adjust the speed of the current animation, so that Update(repeatTime) will do a complete animation lo...
Definition: UnitAnimation.cpp:117
NONCOPYABLE(CUnitAnimation)
const CObjectEntry * object
Definition: UnitAnimation.h:105
Definition: UnitAnimation.h:101
static size_t model
Definition: x86_x64.cpp:221
CModel * m_Model
Definition: UnitAnimation.h:123
Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim) and the simulati...
Definition: UnitAnimation.h:35
std::vector< SModelAnimState > m_AnimStates
Definition: UnitAnimation.h:112
float m_OriginalSpeed
Definition: UnitAnimation.h:128
Definition: Model.h:48
CUnitAnimation(entity_id_t ent, CModel *model, CObjectEntry *object)
Construct for a given unit, defaulting to the "idle" animation.
Definition: UnitAnimation.cpp:43
bool pastLoadPos
Definition: UnitAnimation.h:107
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
void SetAnimationSyncOffset(float actionTime)
Adjust the offset of the current animation, so that Update(actionTime) will advance it to the &#39;action...
Definition: UnitAnimation.cpp:122
Definition: ObjectEntry.h:38
bool pastActionPos
Definition: UnitAnimation.h:108
const CObjectEntry * m_Object
Definition: UnitAnimation.h:124
Definition: SkeletonAnim.h:32