-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathentity.cpp
More file actions
165 lines (153 loc) · 4.11 KB
/
entity.cpp
File metadata and controls
165 lines (153 loc) · 4.11 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "entity.h"
int idCounter = 0;
/**
* Super constructor for Entities that assignes them an ID
*/
Entity::Entity(){
ID = idCounter;
idCounter++;
}
/*
EntityManager::~EntityManager(){
//Delete all alocated entities from the entity list
for(int i=0;i<entityList.size();i++){
delete entityList[i];
}
}*/ // Don't think I acutally need to delete all entities if I pass them by &entity to the list
/**
* Loop through all entities, update them if they are ready to update
* Kill the entities if they are not alive anymore
*/
void EntityManager::updateEntities(int framecount, float dTime){
//Loop through all entities
for(int i=0;i<entityList.size();i++){
//Call entities update funciton only if it is ready to update
if(entityList[i]->readyToUpdate){
entityList[i]->update(framecount, dTime);
}
//if entity is dead kill it! possibly with fire
if(entityList[i]->alive == false){
std::cout << "removing entity because dead. ID=" << entityList[i]->ID << " TYPE=" << entityList[i]->type << std::endl;
//removeByID(entityList[i]->ID);
removeByRef(entityList[i]);
}
}
}
/**
* Loop through all entities, and if they are collidable check if they had collided
*/
void EntityManager::collideEntities(){
for(int i=0;i<entityList.size();i++){
if(entityList[i]->type == "map"){
continue;
}
for(int j=0;j<entityList.size();j++){
if(j == i){
continue;
}
/*if(entityList[i]->type == "player" &&
entityList[j]->type == "player"){
continue;
}*/
if(entityList[i]->collides && entityList[j]->collides){
for(int k=0;k<entityList[i]->collisionBoxes.size();k++){
for(int l=0;l<entityList[j]->collisionBoxes.size();l++){
if(entityList[i]->collisionBoxes[k].intersects(
entityList[j]->collisionBoxes[l])){
//std::cout << "COLLISON on " << entityList[i]->type <<
// " WITH " << entityList[j]->type << std::endl;
if(!ready){
return;
}
entityList[i]->onCollision(entityList[j],entityList[j]->collisionBoxes[l]);
entityList[j]->onCollision(entityList[i],entityList[i]->collisionBoxes[k]);
}
}
}
}
}
}
}
/**
* Loop Through all entities and draw them if they are drawable
*/
void EntityManager::drawEntities(sf::RenderWindow *screen,int screenx,int screeny){
for(int i=0;i<entityList.size();i++){
if(entityList[i]->drawable){
entityList[i]->draw(screen,screenx,screeny);
}
}
}
/**
* Loop through all entities and if the entitiy has the same ID delete it
*/
void EntityManager::removeByID(int ID){
for(int i=0;i<entityList.size();i++){
if(entityList[i]->ID == ID){
delete entityList[i];
entityList.erase(entityList.begin() + i);
break;
}
}
}
void EntityManager::removeByRef(Entity *ent){
for(int i=0;i<entityList.size();i++){
if(entityList[i] == ent){
delete entityList[i];
entityList.erase(entityList.begin() + i);
break;
}
}
}
Entity* EntityManager::getByID(int ID){
for(int i=0;i<entityList.size();i++){
if(entityList[i]->ID == ID){
std::cout << "GOT " << entityList[i]->type << std::endl;
return entityList[i];
}
}
return NULL;
}
void Entity::collideWall(sf::FloatRect otherBox){
int xMag = (collisionBoxes[0].left+collisionBoxes[0].width) - (otherBox.left+otherBox.width);
int yMag = (collisionBoxes[0].top+collisionBoxes[0].height) - (otherBox.top+otherBox.height);
/*if(xMag == yMag){
xMag--;
}*/
//Because fuck logic
if(xMag == 26 && yMag == 28){
xMag = 29;
}
if(xMag == 27 && yMag == 30){
yMag = 26;
}
int xx = 0;
int yy = 0;
if(xMag != 0){
xx = xMag/abs(xMag);
}
if(yMag != 0){
yy = yMag/abs(yMag);
}
//std::cout << xx << " " << yy << std::endl;
//std::cout << "VOLS: " << xVel << " " << yVel << std::endl;
//std::cout << "MAGS: " << xMag << " " << yMag << std::endl;
while(collisionBoxes[0].intersects(otherBox)){
if(abs(xMag) > abs(yMag)){
//std::cout << "DOIN X" << "\n";
x += xx;
} else {
//std::cout << "DOIN Y" << "\n";
y += yy;
}
collisionBoxes[0].left = x;
collisionBoxes[0].top = y;
}
if(abs(xMag) >= abs(yMag)){
x += xx;
} else {
y += yy;
}
collisionBoxes[0].left = x;
collisionBoxes[0].top = y;
}