Pyrogenesis  trunk
Unit.h
Go to the documentation of this file.
1 /* Copyright (C) 2021 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_UNIT
19 #define INCLUDED_UNIT
20 
21 #include <map>
22 #include <set>
23 
24 #include "ps/CStr.h"
25 #include "simulation2/system/Entity.h" // entity_id_t
26 
27 class CActorDef;
28 class CModelAbstract;
29 class CObjectEntry;
30 class CObjectManager;
31 class CUnitAnimation;
32 
33 
34 /////////////////////////////////////////////////////////////////////////////////////////////
35 // CUnit: simple "actor" definition - defines a sole object within the world
36 class CUnit
37 {
39 private:
40  // Private constructor. Needs complete list of selections for the variation.
41  CUnit(CObjectManager& objectManager, const CActorDef& actor, uint32_t seed);
42 
43 public:
44  // Attempt to create a unit with the given actor.
45  // If specific selections are wanted, call SetEntitySelection or SetActorSelections after creation.
46  // Returns NULL on failure.
47  static CUnit* Create(const CStrW& actorName, uint32_t seed, CObjectManager& objectManager);
48 
49  // destructor
50  ~CUnit();
51 
52  // get unit's template object
53  const CObjectEntry& GetObject() const { return *m_Object; }
54  // get unit's model data
55  CModelAbstract& GetModel() const { return *m_Model; }
56 
58 
59  /**
60  * Update the model's animation.
61  * @param frameTime time in seconds
62  */
63  void UpdateModel(float frameTime);
64 
65  // Sets the entity-selection, and updates the unit to use the new
66  // actor variation. Either set one key at a time, or a complete map.
67  void SetEntitySelection(const CStr& key, const CStr& selection);
68  void SetEntitySelection(const std::map<CStr, CStr>& selections);
69 
70  // Most units have a hopefully-unique ID number, so they can be referred to
71  // persistently despite saving/loading maps. Default for new units is -1; should
72  // usually be set to CUnitManager::GetNewID() after creation.
73  entity_id_t GetID() const { return m_ID; }
74  void SetID(entity_id_t id);
75 
76  const std::set<CStr>& GetActorSelections() const { return m_ActorSelections; }
77 
78  /**
79  * Overwrite the seed-selected actor selections. Likely only useful for Atlas or debugging.
80  */
81  void SetActorSelections(const std::set<CStr>& selections);
82 
83 private:
84  // Actor for the unit
86  // object from which unit was created; never NULL once fully created.
87  CObjectEntry* m_Object = nullptr;
88  // object model representation; never NULL once fully created.
89  CModelAbstract* m_Model = nullptr;
90 
92 
93  // unique (per map) ID number for units created in the editor, as a
94  // permanent way of referencing them.
96 
97  // seed used when creating unit
99 
100  // Actor-level selections for this unit. This is normally set at init time,
101  // so that we always re-use the same aesthetic variants.
102  // These have lower priority than entity-level selections.
103  std::set<CStr> m_ActorSelections;
104  // Entity-level selections for this unit (used for e.g. animation variants).
105  std::map<CStr, CStr> m_EntitySelections;
106 
107  // object manager which looks after this unit's objectentry
109 
110  void ReloadObject();
111 
112  friend class CUnitAnimation;
113 };
114 
115 #endif
~CUnit()
Definition: Unit.cpp:45
std::set< CStr > m_ActorSelections
Definition: Unit.h:103
void ReloadObject()
Definition: Unit.cpp:104
std::map< CStr, CStr > m_EntitySelections
Definition: Unit.h:105
Definition: ObjectManager.h:41
entity_id_t GetID() const
Definition: Unit.h:73
Definition: Unit.h:36
Represents an actor file.
Definition: ObjectBase.h:214
uint32_t m_Seed
Definition: Unit.h:98
NONCOPYABLE(CUnit)
CModelAbstract & GetModel() const
Definition: Unit.h:55
static CUnit * Create(const CStrW &actorName, uint32_t seed, CObjectManager &objectManager)
Definition: Unit.cpp:51
const CActorDef & m_Actor
Definition: Unit.h:85
entity_id_t m_ID
Definition: Unit.h:95
CObjectManager & m_ObjectManager
Definition: Unit.h:108
void SetActorSelections(const std::set< CStr > &selections)
Overwrite the seed-selected actor selections.
Definition: Unit.cpp:98
pthread_key_t key
Definition: wpthread.cpp:140
const std::set< CStr > & GetActorSelections() const
Definition: Unit.h:76
CModelAbstract * m_Model
Definition: Unit.h:89
CObjectEntry * m_Object
Definition: Unit.h:87
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:37
const CObjectEntry & GetObject() const
Definition: Unit.h:53
unsigned int uint32_t
Definition: wposix_types.h:53
Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim) and the simulati...
Definition: UnitAnimation.h:37
void SetEntitySelection(const CStr &key, const CStr &selection)
Definition: Unit.cpp:79
CUnitAnimation * GetAnimation()
Definition: Unit.h:57
void SetID(entity_id_t id)
Definition: Unit.cpp:72
CUnitAnimation * m_Animation
Definition: Unit.h:91
u32 entity_id_t
Entity ID type.
Definition: Entity.h:23
void UpdateModel(float frameTime)
Update the model&#39;s animation.
Definition: Unit.cpp:66
Definition: ObjectEntry.h:38
CUnit(CObjectManager &objectManager, const CActorDef &actor, uint32_t seed)
Definition: Unit.cpp:31