Pyrogenesis trunk
Unit.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_UNIT
19#define INCLUDED_UNIT
20
21#include "ps/CStr.h"
22#include "simulation2/system/Entity.h" // entity_id_t
23
24#include <map>
25#include <memory>
26#include <set>
27
28class CActorDef;
29class CModelAbstract;
30class CObjectEntry;
31class CObjectManager;
32class CUnitAnimation;
33
34
35/////////////////////////////////////////////////////////////////////////////////////////////
36// CUnit: simple "actor" definition - defines a sole object within the world
37class CUnit
38{
40private:
41 // Private constructor. Needs complete list of selections for the variation.
42 CUnit(CObjectManager& objectManager, const CActorDef& actor, const entity_id_t id, const uint32_t seed);
43
44public:
45 // Attempt to create a unit with the given actor.
46 // If specific selections are wanted, call SetEntitySelection or SetActorSelections after creation.
47 // Returns an empty `std::unique_ptr` on failure.
48 static std::unique_ptr<CUnit> Create(const CStrW& actorName, const entity_id_t id,
49 const uint32_t seed, CObjectManager& objectManager);
50
51 ~CUnit();
52
53 // get unit's template object
54 const CObjectEntry& GetObject() const { return *m_Object; }
55 // get unit's model data
56 CModelAbstract& GetModel() const { return *m_Model; }
57
59
60 /**
61 * Update the model's animation.
62 * @param frameTime time in seconds
63 */
64 void UpdateModel(float frameTime);
65
66 // Sets the entity-selection, and updates the unit to use the new
67 // actor variation. Either set one key at a time, or a complete map.
68 void SetEntitySelection(const CStr& key, const CStr& selection);
69 void SetEntitySelection(const std::map<CStr, CStr>& selections);
70
71 // Most units have a hopefully-unique ID number, so they can be referred to
72 // persistently despite saving/loading maps. Default for new units is -1; should
73 // usually be set to CUnitManager::GetNewID() after creation.
74 entity_id_t GetID() const { return m_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
83private:
84 // Actor for the unit
86 // object from which unit was created; never NULL once fully created.
88 // object model representation; never nullptr once fully created.
89 std::unique_ptr<CModelAbstract> m_Model;
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
Represents an actor file.
Definition: ObjectBase.h:215
Abstract base class for graphical objects that are used by units, or as props attached to other CMode...
Definition: ModelAbstract.h:50
Definition: ObjectEntry.h:38
Definition: ObjectManager.h:42
Deals with synchronisation issues between raw animation data (CModel, CSkeletonAnim) and the simulati...
Definition: UnitAnimation.h:37
Definition: Unit.h:38
std::unique_ptr< CModelAbstract > m_Model
Definition: Unit.h:89
std::set< CStr > m_ActorSelections
Definition: Unit.h:103
const std::set< CStr > & GetActorSelections() const
Definition: Unit.h:76
CUnitAnimation * GetAnimation()
Definition: Unit.h:58
~CUnit()
Definition: Unit.cpp:45
entity_id_t GetID() const
Definition: Unit.h:74
void SetActorSelections(const std::set< CStr > &selections)
Overwrite the seed-selected actor selections.
Definition: Unit.cpp:88
void ReloadObject()
Definition: Unit.cpp:94
static std::unique_ptr< CUnit > Create(const CStrW &actorName, const entity_id_t id, const uint32_t seed, CObjectManager &objectManager)
Definition: Unit.cpp:50
CObjectManager & m_ObjectManager
Definition: Unit.h:108
void SetEntitySelection(const CStr &key, const CStr &selection)
Definition: Unit.cpp:69
entity_id_t m_ID
Definition: Unit.h:95
CUnit(CObjectManager &objectManager, const CActorDef &actor, const entity_id_t id, const uint32_t seed)
Definition: Unit.cpp:31
const CObjectEntry & GetObject() const
Definition: Unit.h:54
uint32_t m_Seed
Definition: Unit.h:98
CObjectEntry * m_Object
Definition: Unit.h:87
void UpdateModel(float frameTime)
Update the model's animation.
Definition: Unit.cpp:63
std::map< CStr, CStr > m_EntitySelections
Definition: Unit.h:105
CUnitAnimation * m_Animation
Definition: Unit.h:91
NONCOPYABLE(CUnit)
const CActorDef & m_Actor
Definition: Unit.h:85
CModelAbstract & GetModel() const
Definition: Unit.h:56
u32 entity_id_t
Entity ID type.
Definition: Entity.h:29
unsigned int uint32_t
Definition: wposix_types.h:53
pthread_key_t key
Definition: wpthread.cpp:149