forked from capocattiveria/Super_mario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBabyCheep.cpp
186 lines (152 loc) · 4.35 KB
/
BabyCheep.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
#include "BabyCheep.h"
#include "Sprites.h"
#include "Sounds.h"
#include "Mario.h"
#include "Game.h"
BabyCheep::BabyCheep(QPoint position,Direction direction) : Enemy()
{
// set attribute
dir = direction;
slow = true;
falling = false;
script_move = false;
baby_free =false;
script_duration = 55;
// animation divisor
animation_div = 8 ;
script_counter = 0 ;
moving_start_counter = 0;
// durations
death_duration = 300;
// textures
texture_swim[0] = Sprites::instance()->get("baby-cheep-0");
texture_swim[1] = Sprites::instance()->get("baby-cheep-1");
texture_death = Sprites::instance()->get("baby-cheep-0").transformed(QTransform().scale(1, -1));
// set texture and correct y-coordinate w.r.t. texture height
setPixmap(texture_swim[0]);
setPos (position);
setZValue(3);
}
void BabyCheep::setScript_Move(bool _script_move, Direction _dir)
{
dir = _dir;
script_counter = 0;
moving_start_counter = 0;
script_move = _script_move;
}
void BabyCheep::advance()
{
//baby cheep is based with two fondamental movements
//when script_move, baby is launched from big bertha
//and when is inside of the mouth, and the position is the same of big bertha
//dying when go out the scene
if (pos().x() >= 150 * 16 || pos().x() <= 0)
{
dying = true;
}
//bounce and fall when dying
if (dying)
{
if (death_counter >= 0 && death_counter <= 20)
setY(y() - (animation_counter % 2));
else
setY(y() + 1);
return;
}
//stop to move with the script when the script_duration is reached
if (script_move && script_counter >script_duration)
{
moving_start_counter =0;
script_move = false;
}
//baby go away when the mother is death
if(baby_free)
{
script_move = false;
moving_speed = moving_start_counter%2;
if(dir==RIGHT)
setX(x()+moving_speed);
else
setX(x()-moving_speed);
moving_start_counter++;
return;
}
//manage the moving_speed when is launched from the big bertha's mouth
if (script_move)
{
if(moving_start_counter>=0)
moving_start_counter++;
if (script_counter < 8 )
{
moving_speed=moving_start_counter % 3 ==0; //0.33 speed
script_counter += moving_speed;
}
else if (script_counter >= 8 && script_counter < 16 )
{
moving_speed=moving_start_counter % 2; //0.5 speed
script_counter+=moving_speed;
}
else if(script_counter >= 16 && script_counter < 32)
{
moving_speed=1;
script_counter+=moving_speed;
}
else if(script_counter >= 32 && script_counter<40)
{
moving_speed = moving_start_counter%2; //0.5 speed
script_counter += moving_speed;
}
else if(script_counter>=40 && script_counter<=48 )
{
moving_speed=moving_start_counter % 3 ==0; //0.33 speed
script_counter+=moving_speed;
}
else
{
moving_speed = -(moving_start_counter%2);
script_counter++;
}
if(dir==RIGHT)
setX(x()+moving_speed);
else
setX(x()-moving_speed);
}
solveCollisions();
}
void BabyCheep::animate()
{
Entity::animate();
// save current texture height (for later correction) // non credo serve per baby cheep
int prev_h = boundingRect().height();
// set the proper texture
if (dying)
setPixmap(texture_death);
else if (moving)
setPixmap(texture_swim[(animation_counter / animation_div) % 2]);
if (dir == RIGHT)
setPixmap(pixmap().transformed(QTransform().scale(-1, 1)));
}
void BabyCheep::hit(Object* what, Direction fromDir)
{
Object::hit(what, fromDir);
}
void BabyCheep::hurt()
{
Mario* mario = Game::instance()->getMario();
Sounds::instance()->play("stomp");
if(script_move || baby_free)
{
mario->updateScore(100, pos().toPoint());
death_duration = 100;
}
else
death_duration=1;
dying = true;
moving_start_counter =-1;
}
void BabyCheep::setBabyFree(bool _baby_free)
{
moving_start_counter = 0;
script_move = false;
baby_free = _baby_free;
}