Line data Source code
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" 23 : #include "simulation2/system/Entity.h" 24 : 25 : #include <cstdint> 26 : #include <map> 27 : #include <memory> 28 : #include <set> 29 : 30 : class CActorDef; 31 : class CModelAbstract; 32 : class CObjectEntry; 33 : class CObjectManager; 34 : class CUnitAnimation; 35 : 36 : 37 : ///////////////////////////////////////////////////////////////////////////////////////////// 38 : // CUnit: simple "actor" definition - defines a sole object within the world 39 : class CUnit 40 : { 41 : NONCOPYABLE(CUnit); 42 : private: 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 : 46 : public: 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 0 : const CObjectEntry& GetObject() const { return *m_Object; } 57 : // get unit's model data 58 2 : CModelAbstract& GetModel() const { return *m_Model; } 59 : 60 0 : CUnitAnimation* GetAnimation() { return m_Animation; } 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 0 : 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 : 85 : private: 86 : // Actor for the unit 87 : const CActorDef& m_Actor; 88 : // object from which unit was created; never NULL once fully created. 89 : CObjectEntry* m_Object = nullptr; 90 : // object model representation; never nullptr once fully created. 91 : std::unique_ptr<CModelAbstract> m_Model; 92 : 93 : CUnitAnimation* m_Animation = nullptr; 94 : 95 : // unique (per map) ID number for units created in the editor, as a 96 : // permanent way of referencing them. 97 : entity_id_t m_ID; 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 110 : CObjectManager& m_ObjectManager; 111 : 112 : void ReloadObject(); 113 : 114 : friend class CUnitAnimation; 115 : }; 116 : 117 : #endif