forked from VadzimMychkou/Runners
/
gamescene.cpp
194 lines (151 loc) · 4.51 KB
/
gamescene.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
#include "gamescene.h"
#include <cmath>
#include <QSet>
#include <QGraphicsSceneMouseEvent>
// helper for move animations
class MoveAnim
{
public:
MoveAnim(QPoint start, QPoint end)
: start(start)
, end(end)
, currentPos(start)
, finished(false)
{}
QPointF start;
QPointF end;
QPointF currentPos;
bool finished;
QGraphicsLineItem* line;
void advance()
{
QPointF unit = (end - start)/sqrt(QPointF::dotProduct(end - start, end - start));
currentPos += 4.*unit;
QLineF l = line->line();
l.setP1(currentPos + QPoint(GameScene::tile_width/2, GameScene::tile_height/2));
line->setLine(l);
if ((currentPos - end).manhattanLength() < 4.) {
finished = true;
}
}
};
const qreal GameScene::tile_width = 20;
const qreal GameScene::tile_height = 20;
GameScene::GameScene(const World& world)
: animTicker()
, portal(new QGraphicsPixmapItem(QPixmap(":/portal.png")))
{
hover = this->addRect(0, 0,
tile_width, tile_height,
QColor(255, 64, 255),
QColor(128,128,255,64));
hover->setZValue(100);
player = this->addEllipse(0,
0,
20, 20,
QColor(255, 0, 0),
QColor(128, 32, 32));
player->setZValue(99);
this->player->setPos(worldToScreen(world.playerPosition));
animTicker.setInterval(10);
animTicker.start();
connect(&animTicker, SIGNAL(timeout()), this, SLOT(refreshAnimations()));
portal->setPos(worldToScreen(QPoint(world.landscape.width - 22,
world.landscape.height - 18)));
portal->setZValue(50);
this->addItem(portal);
}
GameScene::~GameScene()
{
}
void GameScene::addTile(QPoint worldPt, Tile tile)
{
this->addRect(tile_width*worldPt.x(), tile_height*worldPt.y(),
tile_width, tile_height,
QColor(0,0,0,64),
tile.brush());
}
void GameScene::setMap(const Landscape& lc)
{
for (int yi = 0; yi < lc.height; yi++)
{
for (int xi = 0; xi < lc.width; xi++)
{
this->addTile(QPoint(xi, yi), lc.getTile(xi, yi));
}
}
}
void GameScene::clearPath()
{
while (moves.count() > 1) {
moves.pop_back();
}
foreach(QGraphicsItem* i, pathItems)
{
i->setVisible(false);
}
pathItems.clear();
}
void GameScene::showPath(QVector<QPoint> newPath)
{
if (!moves.empty()) {
player->setPos(moves.front()->start);
}
this->clearPath();
QPoint prev = newPath.front();
newPath.pop_front();
const QPoint offs(tile_width/2, tile_height/2);
foreach(QPoint cs, newPath) {
QPoint start = worldToScreen(prev);
QPoint end = worldToScreen(cs);
pathItems.append(this->addLine(QLineF(start + offs, end + offs), QPen(QColor("black"))));
prev = cs;
moves.append(new MoveAnim(start, end));
moves.back()->line = pathItems.back();
}
}
QPoint GameScene::screenToWorld(QPoint screenPt) const
{
return QPoint(
screenPt.x()/tile_width,
screenPt.y()/tile_height);
}
QPoint GameScene::worldToScreen(QPoint worldPt) const
{
return QPoint(
worldPt.x()*tile_width,
worldPt.y()*tile_height);
}
// GameScene animations
void GameScene::refreshAnimations()
{
if (moves.empty()) return;
moves.front()->advance();
player->setPos(moves[0]->currentPos.toPoint());
if (moves.front()->finished) {
this->removeItem(moves.front()->line);
pathItems.removeAll(moves.front()->line);
delete moves.front()->line;
QPoint arrivalPt = screenToWorld(moves.front()->end.toPoint());
emit playerArrivedAt(arrivalPt);
delete moves.front();
moves.pop_front();
if (player->collidesWithItem(portal)) {
emit playerCollidesWithPortal();
}
}
}
// GameScene events
void GameScene::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
QPoint worldPos(screenToWorld(event->scenePos().toPoint()));
emit tileClicked(worldPos);
QGraphicsScene::mousePressEvent(event);
}
void GameScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
QPoint worldPos(this->screenToWorld(event->scenePos().toPoint()));
QPoint hoverPos(this->worldToScreen(worldPos));
hover->setPos(hoverPos);
QGraphicsScene::mouseMoveEvent(event);
}