-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBullet.cpp
137 lines (125 loc) · 2.36 KB
/
Bullet.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
#include "include.h"
Bullet::Bullet(int x, int y, int speed, int atk, int color, BULLET id) :
pos_x(x), pos_y(y), speed(speed), atk(atk), color(color), id(id), valid(1),status_count(0) {}
bool Bullet::move()
{
if (status_count >= speed)
{
int x = pos_x - 1;
int y = pos_y - ROW_HIGH / 2;
int i = 0;
pos_x++;
Zombie* zombie = zlist.getZombie(x - 5, y);
while (zombie == NULL && i < 6)
zombie = zlist.getZombie(x + (i++) - 5, y);
if (zombie == NULL)
{
status_count = 0;
if (pos_x >= WINDOWS_WIDTH - 1)
valid = 0;
}
else
{
zombie->beHit(atk);
valid = 0;
}
}
else
status_count++;
return valid;
}
BULLET Bullet::getID() const
{
return id;
}
void BulletList::reinit()
{
for (auto i = bullet_list.begin(); i != bullet_list.end();)
{
delete (*i);
i = bullet_list.erase(i);
}
}
BulletList::BulletList() {}
BulletList::~BulletList()
{
reinit();
}
void BulletList::addBullet(Bullet* blt)
{
bullet_list.push_back(blt);
}
void BulletList::bulletOperate()
{
for (std::vector<Bullet*>::iterator i = bullet_list.begin();
i != bullet_list.end();)
{
if (!(*i)->move())
{
fixBullet(**i);
delete (*i);
i = bullet_list.erase(i);
}
else
{
showBullet(**i);
i++;
}
}
}
IceBullet::IceBullet(int x, int y, int speed, int atk, int color, BULLET id, int ice_time) :
Bullet(x, y, speed, atk, color, id), ice_time(ice_time)
{}
bool IceBullet::move()
{
if (status_count >= speed)
{
int x = pos_x - 1;
int y = pos_y - 3;
int i = 0;
pos_x++;
Zombie* zombie = zlist.getZombie(x - 5, y);
while (zombie == NULL && i < 6)
zombie = zlist.getZombie(x + (i++) - 5, y);
if (zombie == NULL)
{
status_count = 0;
if (pos_x >= WINDOWS_WIDTH - 1)
valid = 0;
}
else
{
zombie->beHit(atk);
zombie->beSlowed(ice_time);
valid = 0;
}
}
else
status_count++;
return valid;
}
Basketball::Basketball(int x, int y, int speed, int atk, int color, BULLET id, int target_col) :
Bullet(x, y, speed, atk, color, id), target_col(target_col)
{}
bool Basketball::move()
{
if (status_count >= speed)
{
status_count = 0;
pos_x--;
if (pos_x <= 0)
valid = 0;
int r = (pos_y - 1- TOP_HIGH) / (ROW_HIGH + 1);
int c = (pos_x + COL_WIDTH / 2) / (COL_WIDTH + 1);
if (c == target_col)
{
Plant* p = plist.getPlant(r, c);
if (p)
p->beEaten(atk);
valid = 0;
}
}
else
status_count++;
return valid;
}