-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworld.cpp
More file actions
104 lines (87 loc) · 1.92 KB
/
world.cpp
File metadata and controls
104 lines (87 loc) · 1.92 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
103
104
#include "world.h"
#include "player.h"
#include "spawn.h"
#include "item.h"
World::World()
{
srand(time(nullptr));
player = new Player();
entities[0] = player;
entities[1] = new Spawn();
nbEntities = 2;
}
World::~World(){
for(int i = 0;i<nbEntities;i++)
{
delete entities[i];
}
}
Player* World::getPlayer()
{
return player;
}
bool World::isFree(const IntRect &r, Entity *exeption) const
{
for(int i = 0; i < nbEntities; i++){
if(entities[i] == exeption || entities[i]->getType()==Entity::Type_Item) continue;
if(entities[i]->getRect().intersects(r))
return false;
}
return true;
}
std::vector<Player *> World::collision(const IntRect &r) const{
assert(false);
}
int World::areaEffect(const IntRect &r, EntityCallback callback){
int ans = 0;
for(int i = 0; i < nbEntities; i++){
if(entities[i]->getRect().intersects(r))
ans += callback(entities[i]);
}
return ans;
}
int World::areaEffect(const IntRect &r, EntityGenericCallback callback, const void *data){
int ans = 0;
for(int i = 0; i < nbEntities; i++){
if(entities[i]->getRect().intersects(r))
ans += callback(entities[i], data);
}
return ans;
}
void World::frame()
{
//add some chests
if(rand() % 180 == 0)
addEntity(new Item());
for(int i = 0;i<nbEntities;i++)
{
entities[i]->frame();
}
cleanEntities();
}
void World::draw()
{
for(int i = 0;i<nbEntities;i++)
{
entities[i]->draw();
}
}
void World::addEntity(Entity *e)
{
if(nbEntities>=max_entities)return;
entities[nbEntities] = e;
nbEntities ++;
}
void World::cleanEntities()
{
for(int i = 0;i<nbEntities;i++)
{
if(entities[i]->mustRemove())
{
nbEntities--;
delete entities[i];
entities[i] = entities[nbEntities];
i--;
}
}
}