forked from capocattiveria/Super_mario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoopaTroopa.cpp
204 lines (169 loc) · 4.64 KB
/
KoopaTroopa.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#include "KoopaTroopa.h"
#include <iostream>
#include "Sprites.h"
#include "Sounds.h"
KoopaTroopa::KoopaTroopa(QPoint position, Direction direction) : Enemy()
{
mario = Game::instance()->getMario();
pos_in = position;
dir = direction;
slow = true;
falling = true;
normal = true;
shell = false;
shell_moving = false;
shell_counter = -1;
shell_duration = 400;
harmless = false;
// animation divisor
animation_div = 8;
// durations
death_duration = 400;
// textures
texture_walk[0] = Sprites::instance()->get("Koopa_Troopa-0");
texture_walk[1] = Sprites::instance()->get("Koopa_Troopa-1");
texture_shell = Sprites::instance()->get("Blocked-Shell");
texture_shell_moving[0] = Sprites::instance()->get("Shell-moving-0");
texture_shell_moving[1] = Sprites::instance()->get("Shell-moving-1");
texture_shell_moving[2] = Sprites::instance()->get("Shell-moving-2");
texture_shell_moving[3] = Sprites::instance()->get("Shell-moving-3");
texture_death = Sprites::instance()->get("Blocked-Shell").transformed(QTransform().scale(1, -1));
// set texture and correct y-coordinate w.r.t. texture height
setPixmap(texture_walk[0]);
setPos(position - QPoint(0, pixmap().height()));
setZValue(4);
}
void KoopaTroopa::advance()
{
//bounce and fall in the depth when dying
if (dying)
{
if(death_counter==0)
{
if (pos().x() >= mario->pos().x())
dir = LEFT;
else if (pos().x() <= mario->pos().x())
dir = RIGHT;
}
setX(x() + (dir == RIGHT ? (animation_counter%2) : -(animation_counter%2)));
if (death_counter >= 0 && death_counter <= 20)
setY(y() - 2);
else if (death_counter > 20 && death_counter <= 30)
setY(y() - 3);
else if(death_counter > 30 && death_counter<60)
setY(y() + 2);
else if(death_counter>60)
setY(y() + 3);
return;
}
//normal walking mode
if (normal)
{
moving = true;
slow = true;
}
//shell mode
else if (shell)
moving = false;
//shell and moving mode
else if (shell_moving)
{
harmless = false;
moving_speed = 2;
moving = true;
slow = false;
shell_counter = -1;
}
Entity::advance();
}
void KoopaTroopa::animate()
{
Entity::animate();
if(dying)
{
setPixmap(texture_death);
return;
}
if (shell_counter >= 0) {
shell_counter++;
//returns to walk after a while
if (shell_counter >= shell_duration) {
shell = false;
normal = true;
shell_counter = -1;
}
}
// save current texture height (for later correction)
int prev_h = boundingRect().height();
// set the proper texture
if (dying)
setPixmap(texture_death);
else if (moving && normal)
setPixmap(texture_walk[(animation_counter / animation_div) % 2]);
else if (shell)
setPixmap(texture_shell);
else if (shell_moving && moving)
setPixmap(texture_shell_moving[(animation_counter / animation_div) % 4]);
//correction height from normal to shell mode
int cur_h = boundingRect().height();
if (prev_h != cur_h)
setY(y() - (cur_h - prev_h));
if (dir == RIGHT)
setPixmap(pixmap().transformed(QTransform().scale(-1, 1)));
}
void KoopaTroopa::hit(Object* what, Direction fromDir)
{
Object::hit(what, fromDir);
Mario* mario = dynamic_cast<Mario*>(what);
//change direction if hits inert
if ((dynamic_cast<Inert*>(what) || dynamic_cast<Enemy*>(what))
&& (fromDir == LEFT || fromDir == RIGHT))
dir = inverse(dir);
else if (mario)
{
//change direction if hit by mario
if (mario->x() >= this->x() + (this->boundingRect().width() / 2))
dir = LEFT;
else
dir = RIGHT;
//going to shell mode when is normal and hit by mario from UP
if (fromDir == UP && normal)
{
normal = false;
shell = true;
harmless = true;
shell_counter = 0;
}
//going to shell moving mode when is shell and hit by mario
else if (shell)
{
shell = false;
shell_moving = true;
}
//going to shell mode when is shell moving and hit by mario from UP
else if (fromDir == UP && shell_moving)
{
shell_moving = false;
shell = true;
shell_counter = 0;
}
Sounds::instance()->play("bump");
}
if(dynamic_cast<Inert*>(what) && shell_moving)
Sounds::instance()->play("bump");
}
void KoopaTroopa::hurt()
{
Sounds::instance()->play("stomp");
dying = true;
moving = false;
}
QPainterPath KoopaTroopa::shape() const
{
QPainterPath path;
if (normal)
path.addRect(0, boundingRect().top() + 11, boundingRect().width(), boundingRect().bottom() - 11);
else
path.addRect(0, boundingRect().top(), boundingRect().width(), boundingRect().bottom());
return path;
}