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
|
#include "sceneholder.h"
#include "constants.h"
#include "gameentity.h"
#include "block.h"
#include "actor.h"
#include "bonuspoint.h"
#include "point.h"
#include "util.h"
SceneHolder::SceneHolder(QObject *parent)
: QGraphicsScene(parent), m_color(Color::none), m_pointsLeft(0)
{
setSceneRect(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height);
setBackgroundBrush(Qt::black);
visualMap.resize(Constants::map_size.width);
for (int i = 0; i < visualMap.size(); ++i)
visualMap[i].resize(Constants::map_size.height);
}
void SceneHolder::updateMap(const Transmission::map_t& map)
{
/* remove items that got marked for removal from scene */
QMutableListIterator<GameEntity *> i(m_oldItems);
while(i.hasNext())
{
i.next();
GameEntity *item = i.value();
removeItem(item);
i.remove();
delete item;
}
/* process update */
for (unsigned int x = 0; x < Constants::map_size.width; ++x)
{
for (unsigned int y = 0; y < Constants::map_size.height; ++y)
{
const Transmission::field_t &cur = map[x][y];
if (cur == Transmission::none)
continue;
Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask);
GameEntity* item = NULL;
if (cur & Transmission::empty)
{
GameEntity *oldItem = visualMap[x][y];
/* special handling for purging field
* remove elements (in case it's not an actor)
*/
if (oldItem != NULL && dynamic_cast<Actor *>(oldItem) == NULL)
{
visualMap[x][y] = NULL;
Actor *actor = NULL;
foreach (Actor *tmp, m_actors)
{
if (cur & tmp->color())
{
actor = tmp;
break;
}
}
/* an item must be removed by an actor */
if (actor == NULL)
Q_ASSERT(false);
oldItem->onDie(actor);
/* register item for removal in next update */
m_oldItems.append(oldItem);
}
}
if (cur == Transmission::none)
{
// no update
}
else if (cur & Transmission::block)
{
unsigned int neighbours = Block::None;
// check left side
if (x > 0 && map[x - 1][y] & Transmission::block)
neighbours |= Block::Left;
// check right side
if (x < Constants::map_size.width && map[x + 1][y] & Transmission::block)
neighbours |= Block::Right;
// check upside
if (y > 0 && map[x][y - 1] & Transmission::block)
neighbours |= Block::Up;
// check down side
if (y < Constants::map_size.height && map[x][y + 1] & Transmission::block)
neighbours |= Block::Down;
item = new Block(color, neighbours);
}
else if (cur & Transmission::bonuspoint)
item = new BonusPoint();
else if (cur & Transmission::point)
{
item = new Point();
connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints()));
++m_pointsLeft;
}
else if (cur & Transmission::pacman)
{
Actor *actor = m_actors.value(color, NULL);
if (actor == NULL)
{
actor = new Actor(color, (color == m_color));
m_actors[color] = actor;
addItem(actor);
actor->setPos(mapPositionToCoord(x, y));
}
else
{
Actor::Movement direction =
Util::transmissionMovementToActor(cur & Transmission::direction_mask);
actor->move(direction);
qDebug() << "actor " << color << " move " << direction << "to " << x << y;
}
}
else if (cur & Transmission::empty)
{
/* already handled */
}
else
{
qWarning() << "Unknown data value at" << cur;
Q_ASSERT(false);
}
/* add new created item to scene
* remove old item on that location if there's one
*/
if (item != NULL)
{
addItem(item);
item->setPos(mapPositionToCoord(x, y));
GameEntity *oldItem = visualMap[x][y];
visualMap[x][y] = item;
if (oldItem != NULL)
{
removeItem(item);
delete oldItem;
}
}
}
}
}
void SceneHolder::setColor(Color::Color color)
{
m_color = color;
}
Color::Color SceneHolder::color()
{
return m_color;
}
unsigned int SceneHolder::pointsLeft()
{
return m_pointsLeft;
}
void SceneHolder::decrementPoints()
{
--m_pointsLeft;
}
QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y)
{
return QPoint(x * Constants::field_size.width, y * Constants::field_size.height);
}
QPoint SceneHolder::mapPositionToCoord(QPoint point)
{
return mapPositionToCoord(point.x(), point.y());
}
QPoint SceneHolder::CoordToMapPosition(unsigned int x, unsigned int y)
{
return QPoint(x / Constants::field_size.width, y / Constants::field_size.height);
}
QPoint SceneHolder::CoordToMapPosition(QPoint point)
{
return CoordToMapPosition(point.x(), point.y());
}
|