forked from capocattiveria/Super_mario
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMushroom.cpp
134 lines (113 loc) · 2.6 KB
/
Mushroom.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
#include "Mushroom.h"
#include "Mario.h"
#include "Sounds.h"
#include "Sprites.h"
#include <iostream>
#include "Iceberg.h"
Mushroom::Mushroom(QPoint position, Direction _dir, bool _red) : Collectable(position)
{
// set attributes
red = _red;
dir = _dir;
if (red)
type = MUSHROOM;
else
type = LIFE;
moving_counter = 0;
falling_speed = 2;
// set texture and position
setPixmap(Sprites::instance()->get(red ? "mushroom-red" : "mushroom-green"));
setPos(position);
setZValue(1);
// play mushroom sound
Sounds::instance()->play("spawn");
}
void Mushroom::animate()
{
Entity::animate();
}
void Mushroom::hit(Object* what, Direction fromDir)
{
Object::hit(what, fromDir);
//walking phase on uphill or downhill
if (dynamic_cast<Iceberg*>(what) && dynamic_cast<Iceberg*>(what)->type() == "downhill")
{
falling = false;
script_move = true;
downhill = true;
}
else if (dynamic_cast<Iceberg*>(what) && dynamic_cast<Iceberg*>(what)->type() == "uphill")
{
falling = false;
script_move = true;
downhill = false;
}
else
script_move = false;
// if hit by Mario, Mario eats mushroom and mushroom dies
Mario* mario = dynamic_cast<Mario*>(what);
if (mario)
{
//if mushroom is red mario becomes big
if(red)
mario->powerUp(type);
//if mushroom is green mario gains a new life
if(type==LIFE)
{
mario->updateLives(1, pos().toPoint());
Sounds::instance()->play("1up");
}
else
mario->updateScore(1000,pos().toPoint());
die();
return;
}
Inert* inert_obj = dynamic_cast<Inert*>(what);
if (inert_obj && fromDir == DOWN)
walkable_object = inert_obj;
// if hit from its left or right side, it
// has to move to the opposite direction w.r.t. the one
// is he currently moving
if (fromDir == RIGHT || fromDir == LEFT)
setDirection(inverse(dir));
}
void Mushroom::advance()
{
//raising phase from the block
if (dir == UP)
{
collidable = true;
if (y() >= spawned_position.y() - pixmap().height())
{
setY(y() - moving_speed);
}
if (y() < spawned_position.y() - pixmap().height())
{
if (type == LIFE)
{
jumping = true;
jumping_duration = 60;
}
else
falling = true;
slow = false;
moving_speed = 1;
dir = RIGHT;
}
}
//falling phase from the block
else if (dir == DOWN) {
slow = false;
falling = true;
if (y() >= spawned_position.y() + pixmap().height() + 16) {
collidable = true;
moving_speed = 1;
}
if (walkable_object)
{
dir = RIGHT;
}
}
else
Entity::advance();
}