Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UnitAnimation.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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:
96  {
98  std::vector<CSkeletonAnim*> anims;
99  size_t animIdx;
100  float time;
104  };
105 
106  std::vector<SModelAnimState> m_AnimStates;
107 
108  void AddModel(CModel* model, const CObjectEntry* object);
109 
113  CStr m_State;
114  bool m_Looping;
116  float m_Speed;
118  float m_Desync;
120 };
121 
122 #endif // INCLUDED_UNITANIMATION
entity_id_t m_Entity
void AddModel(CModel *model, const CObjectEntry *object)
std::vector< CSkeletonAnim * > anims
Definition: UnitAnimation.h:98
Definition: Unit.h:35
void SetEntityID(entity_id_t ent)
Change the entity ID associated with this animation (currently used for playing locational sound effe...
void Update(float time)
Advance the animation state.
void ReloadUnit(CModel *model, const CObjectEntry *object)
Regenerate internal animation state from the models in the current unit.
void SetAnimationState(const CStr &name, bool once, float speed, float desync, const CStrW &actionSound)
Start playing an animation.
float m_SyncRepeatTime
void SetAnimationSyncRepeat(float repeatTime)
Adjust the speed of the current animation, so that Update(repeatTime) will do a complete animation lo...
NONCOPYABLE(CUnitAnimation)
static size_t model
Definition: x86_x64.cpp:211
CModel * m_Model
Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim) and the simulati...
Definition: UnitAnimation.h:35
std::vector< SModelAnimState > m_AnimStates
Definition: Model.h:50
CUnitAnimation(entity_id_t ent, CModel *model, CObjectEntry *object)
Construct for a given unit, defaulting to the &quot;idle&quot; animation.
u32 entity_id_t
Entity ID type.
Definition: Entity.h:24
void SetAnimationSyncOffset(float actionTime)
Adjust the offset of the current animation, so that Update(actionTime) will advance it to the &#39;action...
const CObjectEntry * m_Object