summaryrefslogtreecommitdiffstats
path: root/pacman-c++/sceneholder.cpp
blob: ba9cc9767e6d4d684da6584643198a26168a6e8c (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
#include "sceneholder.h"

#include "constants.h"
#include "pixmapitem.h"
#include "block.h"
#include "actor.h"
#include "bonuspoint.h"
#include "point.h"
#include "util.h"

SceneHolder::SceneHolder(QWidget* parent): QWidget(parent)
{
  m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this);
  m_scene->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)
{
  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;
      //qDebug() << "not 0 at x=" << x << ", y=" << y << ", val=" << cur;

      Color::Color color = static_cast<Color::Color>(cur & Transmission::color_mask);
      //qDebug() << "col=" << color;

      PixmapItem* item = NULL;
      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();
      else if (cur & Transmission::pacman)
      {
        Actor *actor = m_actors.value(color, NULL);
        if (actor == NULL)
        {
          //qDebug() << "new actor of col" << color;
          actor = new Actor(color, (color == Color::red)); //TODO: red = local for testing
          m_actors[color] = actor;
          m_scene->addItem(actor);
          actor->setPos(mapPositionToCoord(x, y));
        }

        Actor::Movement direction =
          Util::transmissionMovementToActor(cur & Transmission::direction_mask);
        //actor->move(direction, mapPositionToCoord(x, y));
      }
      else
        Q_ASSERT(false);

      if (item != NULL)
      {
        m_scene->addItem(item);
        item->setPos(mapPositionToCoord(x, y));
        PixmapItem* oldItem = visualMap[x][y];
        visualMap[x][y] = item;
        if (oldItem != NULL)
        {
          m_scene->removeItem(item);
          delete oldItem;
        }
      }
    }
  }

}

QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y)
{
  return QPoint(x * Constants::field_size.width, y * Constants::field_size.height);
}

QPoint SceneHolder::CoordToMapPosition(unsigned int x, unsigned int y)
{
  return QPoint(x / Constants::field_size.width, y / Constants::field_size.height);
}