From b7ef2f2d72171f4f8200585a3b4fc22d1d9f0c5b Mon Sep 17 00:00:00 2001 From: vladislavbelov Date: Mon, 28 Aug 2023 18:01:44 +0000 Subject: [PATCH] Provides entity ID for unit in its constructor. Refs 2eac4af3a4, refs 56bb858802, 91ad17c685, 72bd886f80. Differential Revision: https://code.wildfiregames.com/D5110 This was SVN commit r27818. --- source/graphics/Unit.cpp | 31 +++++++------------ source/graphics/Unit.h | 14 ++++----- source/graphics/UnitAnimation.cpp | 7 +---- source/graphics/UnitAnimation.h | 9 +----- source/graphics/UnitManager.cpp | 22 +++++-------- source/graphics/UnitManager.h | 7 +++-- .../components/CCmpProjectileManager.cpp | 6 ++-- .../components/CCmpVisualActor.cpp | 6 ++-- 8 files changed, 37 insertions(+), 65 deletions(-) diff --git a/source/graphics/Unit.cpp b/source/graphics/Unit.cpp index fb444887f7..bc68b9e92d 100644 --- a/source/graphics/Unit.cpp +++ b/source/graphics/Unit.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -18,18 +18,18 @@ #include "precompiled.h" #include "Unit.h" -#include "Model.h" -#include "ObjectBase.h" -#include "ObjectEntry.h" -#include "ObjectManager.h" -#include "SkeletonAnim.h" -#include "SkeletonAnimDef.h" -#include "UnitAnimation.h" +#include "graphics/Model.h" +#include "graphics/ObjectBase.h" +#include "graphics/ObjectEntry.h" +#include "graphics/ObjectManager.h" +#include "graphics/SkeletonAnim.h" +#include "graphics/SkeletonAnimDef.h" +#include "graphics/UnitAnimation.h" #include "ps/CLogger.h" -CUnit::CUnit(CObjectManager& objectManager, const CActorDef& actor, uint32_t seed) -: m_ID(INVALID_ENTITY), m_ObjectManager(objectManager), m_Actor(actor), m_Seed(seed), m_Animation(nullptr) +CUnit::CUnit(CObjectManager& objectManager, const CActorDef& actor, const entity_id_t id, const uint32_t seed) + : m_ObjectManager(objectManager), m_Actor(actor), m_ID(id), m_Seed(seed), m_Animation(nullptr) { /** * When entity selections change, we might end up with a different layout in terms of variants/groups, @@ -48,13 +48,13 @@ CUnit::~CUnit() delete m_Model; } -CUnit* CUnit::Create(const CStrW& actorName, uint32_t seed, CObjectManager& objectManager) +CUnit* CUnit::Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager) { auto [success, actor] = objectManager.FindActorDef(actorName); UNUSED2(success); - CUnit* unit = new CUnit(objectManager, actor, seed); + CUnit* unit = new CUnit(objectManager, actor, id, seed); if (!unit->m_Model) { delete unit; @@ -69,13 +69,6 @@ void CUnit::UpdateModel(float frameTime) m_Animation->Update(frameTime*1000.0f); } -void CUnit::SetID(entity_id_t id) -{ - m_ID = id; - if (m_Animation) - m_Animation->SetEntityID(id); -} - void CUnit::SetEntitySelection(const CStr& key, const CStr& selection) { CStr selection_lc = selection.LowerCase(); diff --git a/source/graphics/Unit.h b/source/graphics/Unit.h index be5f0ce16c..f66c56909d 100644 --- a/source/graphics/Unit.h +++ b/source/graphics/Unit.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -18,12 +18,12 @@ #ifndef INCLUDED_UNIT #define INCLUDED_UNIT -#include -#include - #include "ps/CStr.h" #include "simulation2/system/Entity.h" // entity_id_t +#include +#include + class CActorDef; class CModelAbstract; class CObjectEntry; @@ -38,15 +38,14 @@ class CUnit NONCOPYABLE(CUnit); private: // Private constructor. Needs complete list of selections for the variation. - CUnit(CObjectManager& objectManager, const CActorDef& actor, uint32_t seed); + CUnit(CObjectManager& objectManager, const CActorDef& actor, const entity_id_t id, const uint32_t seed); public: // Attempt to create a unit with the given actor. // If specific selections are wanted, call SetEntitySelection or SetActorSelections after creation. // Returns NULL on failure. - static CUnit* Create(const CStrW& actorName, uint32_t seed, CObjectManager& objectManager); + static CUnit* Create(const CStrW& actorName, const entity_id_t id, const uint32_t seed, CObjectManager& objectManager); - // destructor ~CUnit(); // get unit's template object @@ -71,7 +70,6 @@ public: // persistently despite saving/loading maps. Default for new units is -1; should // usually be set to CUnitManager::GetNewID() after creation. entity_id_t GetID() const { return m_ID; } - void SetID(entity_id_t id); const std::set& GetActorSelections() const { return m_ActorSelections; } diff --git a/source/graphics/UnitAnimation.cpp b/source/graphics/UnitAnimation.cpp index e98db5b02d..d729993401 100644 --- a/source/graphics/UnitAnimation.cpp +++ b/source/graphics/UnitAnimation.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -50,11 +50,6 @@ CUnitAnimation::CUnitAnimation(entity_id_t ent, CModel* model, CObjectEntry* obj ReloadUnit(model, object); } -void CUnitAnimation::SetEntityID(entity_id_t ent) -{ - m_Entity = ent; -} - void CUnitAnimation::AddModel(CModel* model, const CObjectEntry* object) { SModelAnimState state; diff --git a/source/graphics/UnitAnimation.h b/source/graphics/UnitAnimation.h index 1607e4535b..00e4db7839 100644 --- a/source/graphics/UnitAnimation.h +++ b/source/graphics/UnitAnimation.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2021 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -19,7 +19,6 @@ #define INCLUDED_UNITANIMATION #include "ps/CStr.h" - #include "simulation2/system/Entity.h" #include @@ -43,12 +42,6 @@ public: */ CUnitAnimation(entity_id_t ent, CModel* model, CObjectEntry* object); - /** - * Change the entity ID associated with this animation - * (currently used for playing locational sound effects). - */ - void SetEntityID(entity_id_t ent); - /** * Start playing an animation. * The unit's actor defines the available animations, and if more than one is available diff --git a/source/graphics/UnitManager.cpp b/source/graphics/UnitManager.cpp index e627598441..fab7dfdd6f 100644 --- a/source/graphics/UnitManager.cpp +++ b/source/graphics/UnitManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -15,19 +15,13 @@ * along with 0 A.D. If not, see . */ -/* - * Container that owns all units - */ - #include "precompiled.h" -#include - -#include "Model.h" -#include "UnitManager.h" -#include "Unit.h" -#include "ObjectManager.h" -#include "ObjectEntry.h" +#include "graphics/Model.h" +#include "graphics/ObjectEntry.h" +#include "graphics/ObjectManager.h" +#include "graphics/Unit.h" +#include "graphics/UnitManager.h" #include "ps/Game.h" #include "ps/World.h" @@ -87,12 +81,12 @@ void CUnitManager::DeleteAll() /////////////////////////////////////////////////////////////////////////////// // CreateUnit: create a new unit and add it to the world -CUnit* CUnitManager::CreateUnit(const CStrW& actorName, uint32_t seed) +CUnit* CUnitManager::CreateUnit(const CStrW& actorName, const entity_id_t id, const uint32_t seed) { if (! m_ObjectManager) return NULL; - CUnit* unit = CUnit::Create(actorName, seed, *m_ObjectManager); + CUnit* unit = CUnit::Create(actorName, id, seed, *m_ObjectManager); if (unit) AddUnit(unit); return unit; diff --git a/source/graphics/UnitManager.h b/source/graphics/UnitManager.h index d673498442..eed884aebe 100644 --- a/source/graphics/UnitManager.h +++ b/source/graphics/UnitManager.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -23,6 +23,7 @@ #define INCLUDED_UNITMANAGER #include "ps/CStrForward.h" +#include "simulation2/system/Entity.h" #include #include @@ -49,7 +50,7 @@ public: void DeleteAll(); // creates a new unit and adds it to the world - CUnit* CreateUnit(const CStrW& actorName, uint32_t seed); + CUnit* CreateUnit(const CStrW& actorName, const entity_id_t id, const uint32_t seed); // return the units const std::vector& GetUnits() const { return m_Units; } @@ -69,4 +70,4 @@ private: CObjectManager* m_ObjectManager; }; -#endif +#endif // INCLUDED_UNITMANAGER diff --git a/source/simulation2/components/CCmpProjectileManager.cpp b/source/simulation2/components/CCmpProjectileManager.cpp index d235dbae55..08055e6aca 100644 --- a/source/simulation2/components/CCmpProjectileManager.cpp +++ b/source/simulation2/components/CCmpProjectileManager.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -202,7 +202,7 @@ uint32_t CCmpProjectileManager::LaunchProjectile(CFixedVector3D launchPoint, CFi projectile.origin = launchPoint; - projectile.unit = GetSimContext().GetUnitManager().CreateUnit(actorName, m_ActorSeed++); + projectile.unit = GetSimContext().GetUnitManager().CreateUnit(actorName, INVALID_ENTITY, m_ActorSeed++); if (!projectile.unit) // The error will have already been logged return currentId; @@ -301,7 +301,7 @@ void CCmpProjectileManager::Interpolate(float frameTime) quat.ToMatrix(transform); transform.Translate(m_Projectiles[i].pos); - CUnit* unit = GetSimContext().GetUnitManager().CreateUnit(m_Projectiles[i].impactActorName, m_ActorSeed++); + CUnit* unit = GetSimContext().GetUnitManager().CreateUnit(m_Projectiles[i].impactActorName, INVALID_ENTITY, m_ActorSeed++); unit->GetModel().SetTransform(transform); ProjectileImpactAnimation projectileImpactAnimation; diff --git a/source/simulation2/components/CCmpVisualActor.cpp b/source/simulation2/components/CCmpVisualActor.cpp index 3e69de8885..0c337fa509 100644 --- a/source/simulation2/components/CCmpVisualActor.cpp +++ b/source/simulation2/components/CCmpVisualActor.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2022 Wildfire Games. +/* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -591,7 +591,7 @@ void CCmpVisualActor::InitModel() std::wstring actorName = m_ActorName; if (actorName.find(L".xml") == std::wstring::npos) actorName += L".xml"; - m_Unit = GetSimContext().GetUnitManager().CreateUnit(actorName, GetActorSeed()); + m_Unit = GetSimContext().GetUnitManager().CreateUnit(actorName, GetEntityId(), GetActorSeed()); if (!m_Unit) return; @@ -621,8 +621,6 @@ void CCmpVisualActor::InitModel() model.ToCModelDecal()->RemoveShadows(); } - m_Unit->SetID(GetEntityId()); - bool floating = m_Unit->GetObject().m_Base->m_Properties.m_FloatOnWater; CmpPtr cmpPosition(GetEntityHandle()); if (cmpPosition)