-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
128 lines (103 loc) · 2.89 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
#include "player.h"
#include <QGraphicsScene>
Player::Player(int playerNumber, int monsterNumber, QGraphicsScene* scene, QGraphicsItem *parent)
: QObject(), QGraphicsPixmapItem(parent), playerNumber(playerNumber)
{
QPixmap playerImage(":/img/player" + QString::number(monsterNumber) + ".png");
setPixmap(playerImage.scaled(50, 50));
setFlag(QGraphicsItem::ItemIsFocusable);
setFlag(QGraphicsItem::ItemIsMovable);
setFocus();
if (scene) scene->addItem(this);
}
QString Player::getPlayerName()
{
return "Player" + QString::number(playerNumber);
}
void Player::keyPressEvent(QKeyEvent *event)
{
int dx = 0;
int dy = 0;
if (playerNumber == 1) {
switch (event->key()) {
case Qt::Key_J:
dx = -50;
break;
case Qt::Key_L:
dx = 50;
break;
case Qt::Key_I:
dy = -50;
break;
case Qt::Key_K:
dy = 50;
break;
case Qt::Key_O:
placeBomb();
break;
}
}
else if (playerNumber == 2) {
switch (event->key()) {
case Qt::Key_A:
dx = -50;
break;
case Qt::Key_D:
dx = 50;
break;
case Qt::Key_W:
dy = -50;
break;
case Qt::Key_S:
dy = 50;
break;
case Qt::Key_E:
placeBomb();
break;
}
}
movePlayer(dx, dy);
}
void Player::placeBomb()
{
Bomb* bomb = new Bomb(scene(), nullptr);
bomb->setPos(x(), y());
emit bombPlaced(bomb);
}
void Player::movePlayer(int dx, int dy)
{
int newX = pos().x() + dx;
int newY = pos().y() + dy;
int minX = 0;
int minY = 0;
int maxX = 600;
int maxY = 500;
if (newX < minX || newX > maxX || newY < minY || newY > maxY) {
return;
}
QList<QGraphicsItem*> itemsList = scene()->items();
foreach (QGraphicsItem* item, itemsList) {
Player* player = dynamic_cast<Player*>(item);
if (player && player != this && player->pos() == QPointF(newX, newY)) {
return;
}
IndestructibleWall* indestructibleWall = dynamic_cast<IndestructibleWall*>(item);
if (indestructibleWall && indestructibleWall->pos() == QPointF(newX, newY)) {
return;
}
DestructibleWall* destructibleWall = dynamic_cast<DestructibleWall*>(item);
if (destructibleWall && destructibleWall->pos() == QPointF(newX, newY)) {
return;
}
Bomb* bomb = dynamic_cast<Bomb*>(item);
if (bomb && bomb->pos() == QPointF(newX, newY)) {
return;
}
Explosion* explosion = dynamic_cast<Explosion*>(item);
if (explosion && explosion->pos() == QPointF(newX, newY)) {
return;
}
}
setPos(newX, newY);
emit playerMoved(newX, newY, playerNumber);
}