-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.cpp
146 lines (125 loc) · 4.2 KB
/
player.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
#include <QKeyEvent>
#include <QTimer>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QDebug>
#include "player.h"
#include "bomb.h"
#include "settings.h"
Player::Player(int x, int y, QObject *parent) : QObject (parent) {
_x = x;
_y = y;
_health = sizes::Health;
_maxBombs = sizes::Bombs;
_explosionSize = sizes::ExplosionRange;
setPos(x * sizes::FieldSize, y * sizes::FieldSize);
setPixmap(QPixmap(":/img/img/player_front.png"));
}
Player::~Player() {
scene()->removeItem(this);
}
void Player::move(direction dir, std::vector<std::vector<Field *> > &fields) {
if(dir == Left) {
if (_x > 0 && getField(_x - 1, _y, fields)->isClear()) {
getField(_x, _y, fields)->playerOut(this);
getField(_x - 1, _y, fields)->playerOn(this);
--_x;
setPos(_x * sizes::FieldSize, _y * sizes::FieldSize);
getField(_x, _y, fields)->getPowerUp(this);
}
}
else if (dir == Right) {
if (_x < sizes::Columns - 1 && getField(_x + 1, _y, fields)->isClear()) {
getField(_x, _y, fields)->playerOut(this);
getField(_x + 1, _y, fields)->playerOn(this);
++_x;
setPos(_x * sizes::FieldSize, _y * sizes::FieldSize);
getField(_x, _y, fields)->getPowerUp(this);
}
}
else if (dir == Down) {
if (_y < sizes::Rows - 1 && getField(_x, _y + 1, fields)->isClear()) {
getField(_x, _y, fields)->playerOut(this);
getField(_x, _y + 1, fields)->playerOn(this);
++_y;
setPos(_x * sizes::FieldSize, _y * sizes::FieldSize);
getField(_x, _y, fields)->getPowerUp(this);
}
}
else if (dir == Up) {
if (_y > 0 && getField(_x, _y - 1, fields)->isClear()) {
getField(_x, _y, fields)->playerOut(this);
getField(_x, _y - 1, fields)->playerOn(this);
--_y;
setPos(_x * sizes::FieldSize, _y * sizes::FieldSize);
getField(_x, _y, fields)->getPowerUp(this);
}
}
}
void Player::plantBomb(std::vector<std::vector<Field *> >& fields) {
if(_maxBombs > 0) {
if(getField(_x, _y, fields)->isBomb() == false) {
Bomb *bomb = new Bomb(&_maxBombs);
getField(_x, _y, fields)->setBomb(bomb);
connect(bomb, SIGNAL(explode()), getField(_x, _y, fields), SLOT(explosion())); //connect always
bool flagLeft = true, flagRight = true, flagUp = true, flagDown = true;
for(int i = 1; i <= _explosionSize; i++) {
if (flagRight && _x + i < sizes::Columns) {
connectBomb(_x + i, _y, bomb, fields, flagRight);
}
if (flagLeft && _x - i >= 0) {
connectBomb(_x - i, _y, bomb, fields, flagLeft);
}
if (flagUp && _y - i >= 0) {
connectBomb(_x, _y - i, bomb, fields, flagUp);
}
if (flagDown && _y + i < sizes::Rows) {
connectBomb(_x, _y + i, bomb, fields, flagDown);
}
}
_maxBombs--;
QTimer::singleShot(2000, bomb, &Bomb::emitExplode);
}
}
}
int Player::getX() const {
return _x;
}
int Player::getY() const {
return _y;
}
void Player::setX(int x) {
_x = x;
}
void Player::setY(int y) {
_y = y;
}
void Player::decreaseHP(Field * field)
{
_health--;
if(_health == 0) {
field->playerOut(this);
emit playerDied();
delete this;
}
}
void Player::addBomb() {
_maxBombs++;
}
void Player::increaseExplosionSize() {
_explosionSize++;
}
void Player::increaseHP() {
_health++;
}
Field *Player::getField(int x, int y, std::vector<std::vector<Field *> > &fields) const {
return fields[static_cast<size_t>(y)][static_cast<size_t>(x)];
}
void Player::connectBomb(int x, int y, Bomb *bomb, std::vector<std::vector<Field *> >& fields, bool& flag) {
if (getField(x, y, fields)->isBlockOnField() != Undestroyable)
connect(bomb, SIGNAL(explode()), getField(x, y, fields), SLOT(explosion()));
else
flag = false;
if (getField(x, y, fields)->isBlockOnField() == Destroyable)
flag = false;
}