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