-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameObjectManager.cpp
More file actions
102 lines (88 loc) · 2.45 KB
/
GameObjectManager.cpp
File metadata and controls
102 lines (88 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "stdafx.h"
#include "GameObjectManager.h"
#include "Game.h" //For debugging Only!!
GameObjectManager::GameObjectManager()
{}
//Free Memory
GameObjectManager::~GameObjectManager()
{
for (std::map<int, BaseObject*>::iterator itr = m_mGameObjects.begin(); itr != m_mGameObjects.end(); ++itr)
{
delete itr->second;
}
}
//Add new objects
void GameObjectManager::Add(int key, BaseObject* object)
{
m_mGameObjects.insert(std::pair<int, BaseObject*>(key, object));
//If wall object that may need to be collided with
if (object->GetType() == dlb::WALL)
m_vWall.push_back(object);
//If non wall collidable objects (essentially anything that is solid)
if (object->GetType() != dlb::WALL && object->IsSolid() == true)
m_vCollidable.push_back(object);
}
//Remove object based on integer key
void GameObjectManager::Remove(int key)
{
std::map<int, BaseObject*>::const_iterator result = m_mGameObjects.find(key);
if (result != m_mGameObjects.end())
{
delete result->second;
m_mGameObjects.erase(key);
}
}
//Return an ptr to object specified by integer key
BaseObject* GameObjectManager::Get(int key)
{
std::map<int, BaseObject*>::const_iterator result = m_mGameObjects.find(key);
if (result == m_mGameObjects.end())
return nullptr;
return result->second;
}
//Reset so that can go to next level, start over, etc.
void GameObjectManager::ClearObjectManager()
{
for (std::map<int, BaseObject*>::iterator itr = m_mGameObjects.begin(); itr != m_mGameObjects.end(); ++itr)
{
delete itr->second;
}
m_mGameObjects.clear();
m_vWall.clear();
m_vCollidable.clear();
}
//Return number of objects
int GameObjectManager::GetObjectCount() const
{
return m_mGameObjects.size();
}
//Update all objects
void GameObjectManager::UpdateAll()
{
//vector of objects that should be deleted based on their logic
std::vector<int> toDelete;
//Update all
std::map<int, BaseObject*>::iterator itr = m_mGameObjects.begin();
while (itr != m_mGameObjects.end())
{
//2 is return value for object that should be deleted
if (itr->second->Update() == 2)
toDelete.push_back(itr->second->GetId());
++itr;
}
//Delete objects that need to be deleted
for (std::vector<int>::iterator itr = toDelete.begin(); itr != toDelete.end(); ++itr)
{
Remove(*itr);
}
}
//Draw All Objects
void GameObjectManager::DrawAll(sf::RenderWindow& window)
{
std::map<int, BaseObject*>::iterator itr = m_mGameObjects.begin();
while (itr != m_mGameObjects.end())
{
itr->second->Draw(window);
++itr;
}
}