-
Notifications
You must be signed in to change notification settings - Fork 0
/
bullets.cpp
117 lines (106 loc) · 2.98 KB
/
bullets.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
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
#include "bullets.h"
Bullets::~Bullets(){
std::list<Sprite*>::iterator it;
for(it=sprites.begin(); it!=sprites.end(); ++it){
delete (*it);
}
for(it=sprites_exploded.begin(); it!=sprites_exploded.end(); ++it){
delete (*it);
}
}
void Bullets::draw() const {
std::list<Sprite*>::const_iterator it;
for(it=sprites.begin(); it!=sprites.end(); ++it){
(*it)->draw();
}
for(it=sprites_exploded.begin(); it!=sprites_exploded.end(); ++it){
(*it)->draw();
}
}
void Bullets::update(Uint32 ticks) {
std::list<Sprite*>::iterator it;
for(it=sprites.begin(); it!=sprites.end(); ++it){
(*it)->update(ticks);
if(((*it)->getControls())==0xFF){
delete (*it);
it = sprites.erase(it);
}
}
for(it=sprites_exploded.begin(); it!=sprites_exploded.end(); ++it){
(*it)->update(ticks);
if(static_cast<ExplodingSprite*>(*it)->chunkCount() == 0 ){
delete (*it);
it = sprites.erase(it);
}
}
}
void Bullets::add(Sprite* ele){
sprites.push_back(ele);
}
void Bullets::remove(Sprite* ele){
std::list<Sprite*>::iterator it;
if(it!=sprites.end() || ele){
delete (*it);
sprites.erase(it);
}
}
bool Bullets::checkCollision(std::list<Sprite*>& enemy){
std::list<Sprite*>::iterator it_enemy = enemy.begin();
bool status = true;
while(it_enemy!=enemy.end()){
std::list<Sprite*>::iterator it_sprites;
for(it_sprites=sprites.begin(); it_sprites!=sprites.end(); ++it_sprites){
if(strat.execute(*(*it_sprites),*(*it_enemy)) && (*it_sprites)->getNumber()=="1"){
delete (*it_sprites);
it_sprites = sprites.erase(it_sprites);
(*it_enemy)->setControls(0xff);
(*it_enemy)->update(0);
if((*it_enemy)->getControls()==0xff){
sprites_exploded.push_back(new ExplodingSprite(*(*it_enemy)));
delete (*it_enemy);
it_enemy = enemy.erase(it_enemy);
status = false;
}
--it_sprites;
break;
}
}
++it_enemy;
}
return status;
}
bool Bullets::checkCollisionPlayer(Sprite* player, std::list<Sprite*>& enemy){
std::list<Sprite*>::iterator it_enemy = enemy.begin();
while(it_enemy!=enemy.end()){
if(strat.execute(*(player),*(*it_enemy))){
(*it_enemy)->setControls(0xff);
(*it_enemy)->update(0);
if((*it_enemy)->getControls()==0xff){
sprites_exploded.push_back(new ExplodingSprite(*(*it_enemy)));
delete (*it_enemy);
it_enemy = enemy.erase(it_enemy);
}
(player)->setControls(0xff);
(player)->update(0);
if((player)->getControls()==0xff){
sprites_exploded.push_back(new ExplodingSprite(*(player)));
return false;
}
}
++it_enemy;
}
std::list<Sprite*>::iterator it_sprites;
for(it_sprites=sprites.begin(); it_sprites!=sprites.end(); ++it_sprites){
if(strat.execute(*(*it_sprites),*(player)) && (*it_sprites)->getNumber()!="1"){
delete (*it_sprites);
it_sprites = sprites.erase(it_sprites);
(player)->setControls(0xff);
(player)->update(0);
if((player)->getControls()==0xff){
sprites_exploded.push_back(new ExplodingSprite(*(player)));
return false;
}
}
}
return true;
}