-
Notifications
You must be signed in to change notification settings - Fork 9
/
World.cpp
91 lines (90 loc) · 2.15 KB
/
World.cpp
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
#include "World.h"
#include "Tank.h"
#include "Land.h"
#include <iostream>
World::World() : timer(0) { }
//WorldObject* World::addObj(WorldObjectptr obj)
//{
// objects.push_back(std::move(obj));
// return objects.back().get();
//}
WorldObject* World::addObj(WorldObject* optr)
{
objects.push_back(WorldObjectptr(optr));
return optr;
}
WorldObject* World::addObj(WorldObject::Type t)
{
WorldObjectptr obj;
bool playNice = true;
switch(t)
{
case WorldObject::TankType:
obj.reset(new Tank());
break;
case WorldObject::LandType:
obj.reset(new Land());
break;
default:
playNice = false;
}
if(playNice)
{
objects.push_back(std::move(obj));
return objects.back().get();
}
return nullptr;
}
void World::removeObj(std::list<WorldObjectptr>::iterator o)
{
objects.erase(o);
}
void World::drawAll(sf::RenderWindow &win)
{
for(auto &objit : objects)
objit->draw(win);
}
void World::crudeStepAll(float dt)
{
for(auto objit = objects.begin();objit != objects.end();)
{
if(!((*objit)->selfDestruct))
{
for(auto j = std::next(objit) ; j != objects.end() ; ++j)
(*objit)->handleCollision(*(*j).get());
/* ^
dereference the iterator , get the raw pointer and dereference it*/
(*objit)->step(dt);
++objit;
}
else
objit = objects.erase(objit);
}
}
void World::stepAll(float dt)
{
// crudeStepAll(dt);
timer += dt;
while(timer > updateRate)
{
timer -= updateRate;
crudeStepAll(updateRate);
}
}
void World::play()
{
for(auto &objit : objects)
objit->reset();
timer = 0;
}
void World::traceAll()
{
int i = 1;
for(auto obj = objects.begin() ; obj != objects.end() ; ++obj,++i)
{
std::cout << "Object " << i << std::endl;
std::cout << "\tobject.type = " << (*obj)->type << std::endl;
std::cout << "\tobject.selfDestruct = " << (*obj)->selfDestruct << std::endl;
std::cout << "\tAddress of object = " << (*obj).get() << std::endl;
}
}