/
bullets.cpp
155 lines (107 loc) · 2.99 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
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
#include <iostream>
#include <cmath>
#include "ioManager.h"
#include "gamedata.h"
#include "bullets.h"
#include "frameFactory.h"
CollisionStrategy* getStrategy(const string& name){
std::string sName = Gamedata::Gamedata::getInstance().getXmlStr(name+"/Strategy");
if (sName == "mid") return new MidPointCollisionStrategy;
if (sName == "rec") return new RectangularCollisionStrategy;
if (sName == "pix") return new PerPixelCollisionStrategy;
throw std::string ("No strategy in getStrategy");
}
Bullets:: ~Bullets(){
std::list<Chunk*>::iterator iter = bulletList.begin();
while(iter!= bulletList.end()){
delete (*iter);
++iter;
}
std::list<Chunk*>::iterator iter1 = freeList.begin();
while(iter1!= freeList.end()){
delete (*iter1);
++iter1;
}
delete strategy;
}
Bullets::Bullets(const std::string& n):
Sprite(n),
name(n),
strategy(getStrategy(name)),
//bulletSurface(IOManager::getInstance().loadAndSet(Gamedata::getInstance().getXmlStr(name+"/File"),true)),
bulletFrame(FrameFactory::getInstance().getFrame(name)),
frameInterval(Gamedata::getInstance().getXmlInt(name+"/Interval")),
timeSinceLastFrame(0),
bulletList(),
freeList()
{}
Bullets::Bullets(const Bullets& b):
Sprite(b),
name(b.name),
strategy(b.strategy),
bulletFrame(b.bulletFrame),
frameInterval(b.frameInterval),
timeSinceLastFrame(b.timeSinceLastFrame),
bulletList(b.bulletList),
freeList(b.freeList)
{}
bool Bullets::collideWith(const Drawable* obj) const{
//std::cout<<"before bullet"<<std::endl;
std::list<Chunk*>::iterator ptr = bulletList.begin();
while(ptr != bulletList.end()){
if(strategy->execute(*(*ptr), *obj)){
freeList.push_back(*ptr);
ptr = bulletList.erase(ptr);
//std::cout<<"bullet"<<std::endl;
return true;
}
++ptr;
}
return false;
}
void Bullets::shoot(const Vector2f&position, const Vector2f& velocity){
if (timeSinceLastFrame>=frameInterval){
if(freeList.empty()){
Frame* newBulletFrame = FrameFactory::getInstance().getFrame(name);
Chunk* b=new Chunk(name,position,velocity,newBulletFrame);
bulletList.push_back(b);
}
else{
Chunk* b = freeList.front();
freeList.pop_front();
b->reset();
b->setVelocity(velocity);
b->setPosition(position);
bulletList.push_back(b);
}
timeSinceLastFrame = 0;
}
}
void Bullets::draw() const{
std::list<Chunk*>::iterator ptr = bulletList.begin();
while(ptr!= bulletList.end()){
(*ptr)->draw();
++ptr;
//std::cout<<"draw"<<std::endl;
}
}
void Bullets::draw(const float scale) {
std::list<Chunk*>::iterator ptr = bulletList.begin();
while(ptr!= bulletList.end()){
(*ptr)->draw(scale);
++ptr;
//std::cout<<"draw"<<std::endl;
}
}
void Bullets::update(Uint32 ticks){
timeSinceLastFrame += ticks;
std::list<Chunk*>::iterator ptr = bulletList.begin();
while(ptr!= bulletList.end()){
(*ptr)->update(ticks);
if((*ptr)->goneTooFar()){
freeList.push_back(*ptr);
ptr = bulletList.erase(ptr);
}
else ++ptr;
}
}