summaryrefslogtreecommitdiffstats
path: root/pacman-c++/common/sceneholder.cpp
blob: d429445d803300fe1315b045314961a24082617a (plain)
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#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);

  m_overlayText = new QGraphicsTextItem();

  visualMap.resize(Constants::map_size.width);
  for (int i = 0; i < visualMap.size(); ++i)
    visualMap[i].resize(Constants::map_size.height);
}

void SceneHolder::reset()
{
  processDelayedItems();
  hideOverlayText();

  /* remove actors from scene so they don't get deleted during clear */
  foreach(Actor *actor, m_actors)
  {
    actor->reset();
    removeItem(actor);
  }

  /* clear our stuff */
  clear();
  m_pointsLeft = 0;
  for (int i = 0; i < visualMap.size(); ++i)
  {
    visualMap[i].clear();
    visualMap[i].resize(Constants::map_size.height);
  }

  /* add actors again */
  foreach(Actor *actor, m_actors)
    addItem(actor);
}

void SceneHolder::processDelayedItems()
{
  /* remove items that got marked for removal from scene */
  foreach(GameEntity *item, m_oldItems)
  {
    removeItem(item);
    delete item;
  }
  m_oldItems.clear();

  /* process death */
  foreach(const Color::Color color, m_death.keys())
  {
    Q_ASSERT(m_death[color] != NULL);
    m_death[color]->onDie(m_actors[color]);
  }
  m_death.clear();
}

void SceneHolder::updateMap(const Transmission::map_t& map)
{
  processDelayedItems();

  /* 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;
      updateMap(map, x, y);
    }
  }

  if (m_pointsLeft == 0)
    emit allPointsRemoved();
}

void SceneHolder::updateMap(const Transmission::map_t& map, const unsigned int x, unsigned int y)
{
  const Transmission::field_t &cur = map[x][y];
  if (cur == Transmission::none)
    return;

  /* we may have multiple colors in one position (e.g during eating another pacman) */
  Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask);
  QList<Color::Color> colors;
  if (color == Color::none)
    colors.append(Color::none);
  foreach(Color::Color col, m_eatingorder.toSet())
  {
    if (color & col)
      colors.append(col);
  }
  Q_ASSERT(colors.count() > 0);

  /* for now complain if there are more colors or it's a Transmission::death packet */
  Q_ASSERT(colors.count() == 1 || cur & Transmission::death);

  if (cur & Transmission::empty)
  {
    GameEntity *oldItem = visualMap[x][y];
    /* special handling for purging field */
    if (oldItem != NULL)
    {
      /* remove item from visualmap and register item for removal in next update */
      visualMap[x][y] = NULL;
      m_oldItems.append(oldItem);

      /* an item must be removed by an actor */
      Actor *actor = m_actors[colors.at(0)];
      Q_ASSERT(actor != NULL);
      oldItem->onDie(actor);
    }
  }

  GameEntity *item = NULL;
  if (cur == Transmission::none)
  {
    /* no update */
  }
  else
  {
    if (cur & Transmission::block)
    {
      unsigned int neighbours = Block::None;
      /* check for old block first */
      if (visualMap[x][y] != NULL)
      {
        Block *oldItem = qgraphicsitem_cast<Block *>(visualMap[x][y]);
        if (oldItem != NULL)
          neighbours = oldItem->neighbours();
      }
      /* 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(colors.at(0), neighbours);
    }

    if (cur & Transmission::bonuspoint)
      item = new BonusPoint();

    if (cur & Transmission::point)
    {
      item = new Point();
      connect(item, SIGNAL(destroyed()), this, SLOT(decrementPoints()));
      ++m_pointsLeft;
    }

    if (cur & Transmission::pacman)
    {
      foreach(color, colors)
      {
        /* WARNING: do NOT add actor to visualMap as visualMap-items may
         * get deleted during update and actors are referenced in_mactors too
         * if you REALLY need that you need to changed updateMap so that all actors
         * will be moved before any new items get allocated (totally untested)
         */
        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));
          actor->hadReset();
        }
        else
        {
          /* check for death */
          if (cur & Transmission::death)
          {
            foreach(Color::Color col, colors)
            {
              if (color == col)
                continue;
              if (m_actors[col]->canEat(actor, m_eatingorder))
              {
                m_death[col] = actor;
                break;
              }
            }
          }

          /* move actor */
          if (actor->hadReset())
            actor->setPos(mapPositionToCoord(x, y));
          else
            actor->move(mapPositionToCoord(x, y));

          /* that's kind a hack but working right now */
          if (!(cur & Transmission::empty))
            actor->stopEating();
          qDebug() << "[SceneUpdate] actor moves: color=" << color
              << "newpos=" << QPoint(x, y);
        }

        if ((cur & Transmission::death) && visualMap[x][y] != NULL)
            m_death[color] = visualMap[x][y];
      }
    }

    if (cur & Transmission::empty)
    {
      /* already handled */
    }
  }

  /* add new created item to scene
   * remove old item on that location if there's one
   */
  if (item != NULL)
  {
    GameEntity *oldItem = visualMap[x][y];
    if (oldItem != NULL)
    {
      removeItem(oldItem);
      delete oldItem;
    }

    addItem(item);
    item->setPos(mapPositionToCoord(x, y));
    visualMap[x][y] = item;
  }
}

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;
}

void SceneHolder::setEatingOrder(QList<Color::Color> &order)
{
  m_eatingorder = order;
}

QList<Color::Color> &SceneHolder::eatingOrder()
{
  return m_eatingorder;
}

void SceneHolder::hideOverlayText()
{
  if (m_overlayText->scene() == this)
    removeItem(m_overlayText);
}

void SceneHolder::centerOverlayText(unsigned int lines)
{
  QFontMetrics metrics(m_overlayText->font());
  m_overlayText->setPos((width() - m_overlayText->textWidth()) / 2, (height() - metrics.height() * lines) / 2);
  m_overlayText->setZValue(100);
}

void SceneHolder::showEatingText()
{
  hideOverlayText();
  m_overlayText->setDefaultTextColor(Qt::black);
  QString text = QString(
      "<div style=\"background-color: gray;\" align=\"center\">"
      "<br />"
  );
  unsigned int lines = 1;

  text = text % QString("<b>Your Pacman: <img src=\"%1\"/></b><br /><br />")
    .arg(m_actors[m_color]->iconStr());
  lines += 2;

  foreach(Actor *actor, m_actors)
  {
    foreach(Actor *other, m_actors)
    {
      if (!actor->canEat(other, m_eatingorder))
        continue;
      text = text % QString("<img src=\"%1\"/> &nbsp;can eat <img src=\"%2\"/><br />")
        .arg(actor->iconStr(), other->iconStr());
      ++lines;
    }
  }
  text = text % "</div>";
  m_overlayText->setHtml(text);
  m_overlayText->setTextWidth(150);
  m_overlayText->setOpacity(0.9);
  centerOverlayText(lines);

  addItem(m_overlayText);
}

void SceneHolder::showWaitingForPlayers()
{
  hideOverlayText();
  m_overlayText->setDefaultTextColor(Qt::black);
  QString text = QString(
      "<div style=\"background-color: gray;\" align=\"center\">"
      "<br />"
      "Waiting for other Players...<br />"
      "</div>"
  );
  unsigned int lines = 3;
  m_overlayText->setHtml(text);
  m_overlayText->setTextWidth(150);
  m_overlayText->setOpacity(0.9);
  centerOverlayText(lines);

  addItem(m_overlayText);
}

void SceneHolder::showWonLost(bool won)
{
  hideOverlayText();
  m_overlayText->setDefaultTextColor(Qt::black);
  QString text = QString(
      "<div style=\"background-color: gray;\" align=\"center\">"
      "<br />"
      "You %1!<br />"
      "</div>"
  ).arg((won) ? "won" : "lost");
  unsigned int lines = 3;
  m_overlayText->setHtml(text);
  m_overlayText->setTextWidth(150);
  m_overlayText->setOpacity(0.9);
  centerOverlayText(lines);

  addItem(m_overlayText);
}

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());
}