Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
UnitManager.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2012 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 /*
19  * Container that owns all units
20  */
21 
22 #include "precompiled.h"
23 
24 #include <float.h>
25 
26 #include "Model.h"
27 #include "UnitManager.h"
28 #include "Unit.h"
29 #include "ObjectManager.h"
30 #include "ObjectEntry.h"
31 #include "ps/Game.h"
32 #include "ps/World.h"
33 
34 #include <algorithm>
35 
36 ///////////////////////////////////////////////////////////////////////////////
37 // CUnitManager constructor
39  m_ObjectManager(NULL)
40 {
41 }
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // CUnitManager destructor
46 {
47  DeleteAll();
48 }
49 
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 // AddUnit: add given unit to world
54 {
55  m_Units.push_back(unit);
56 }
57 
58 ///////////////////////////////////////////////////////////////////////////////
59 // RemoveUnit: remove given unit from world, but don't delete it
61 {
62  // find entry in list
63  typedef std::vector<CUnit*>::iterator Iter;
64  Iter i=std::find(m_Units.begin(),m_Units.end(),unit);
65  if (i!=m_Units.end()) {
66  m_Units.erase(i);
67  }
68 }
69 
70 ///////////////////////////////////////////////////////////////////////////////
71 // DeleteUnit: remove given unit from world and delete it
73 {
74  RemoveUnit(unit);
75  delete unit;
76 }
77 
78 ///////////////////////////////////////////////////////////////////////////////
79 // DeleteAll: remove and delete all units
81 {
82  for (size_t i=0;i<m_Units.size();i++) {
83  delete m_Units[i];
84  }
85  m_Units.clear();
86 }
87 
88 ///////////////////////////////////////////////////////////////////////////////
89 // CreateUnit: create a new unit and add it to the world
90 CUnit* CUnitManager::CreateUnit(const CStrW& actorName, uint32_t seed, const std::set<CStr8>& selections)
91 {
92  if (! m_ObjectManager)
93  return NULL;
94 
95  CUnit* unit = CUnit::Create(actorName, seed, selections, *m_ObjectManager);
96  if (unit)
97  AddUnit(unit);
98  return unit;
99 }
void DeleteUnit(CUnit *unit)
Definition: UnitManager.cpp:72
CObjectManager * m_ObjectManager
Definition: UnitManager.h:64
void RemoveUnit(CUnit *unit)
Definition: UnitManager.cpp:60
Definition: Unit.h:35
std::vector< CUnit * > m_Units
Definition: UnitManager.h:62
void AddUnit(CUnit *unit)
Definition: UnitManager.cpp:53
static CUnit * Create(const CStrW &actorName, uint32_t seed, const std::set< CStr > &selections, CObjectManager &objectManager)
Definition: Unit.cpp:47
void DeleteAll()
Definition: UnitManager.cpp:80
unsigned int uint32_t
Definition: wposix_types.h:53
CUnit * CreateUnit(const CStrW &actorName, uint32_t seed, const std::set< CStr8 > &selections)
Definition: UnitManager.cpp:90