summaryrefslogtreecommitdiffstats
path: root/pacman-c++/mainwidget.cpp
blob: b421c3613ee0ffd0c8e4f084adbdeee12538be1b (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
#include "mainwidget.h"
#include "actor.h"
#include "block.h"
#include "bonuspoint.h"
#include "point.h"
#include "constants.h"
#include "audioplayer.h"

// temporary
Transmission::map_t createDummyMap()
{
  Transmission::map_t map;
  map = new Transmission::field_t*[Constants::map_size.width];
  for (unsigned int i = 0; i < Constants::map_size.width; ++i)
    map[i] = new Transmission::field_t[Constants::map_size.height];

  for (unsigned int x = 0; x < Constants::map_size.width; ++x)
  {
    for (unsigned int y = 0; y < Constants::map_size.height; ++y)
    {
      Transmission::field_t &cur = map[x][y];
      cur = Transmission::none;
    }
  }


  const char *tmpl[] = {
    "             #      #              ",
    " #### ###### # #### # # ###### ### ",
    "             #               #     ",
    " # ##### # #   #   # # ### #   # # ",
    " #   #   # # # # # # # #   ##  # # ",
    " #   #   # #   # # # # ### # # # # ",
    " # # # # # # # #   # # #   #  ## # ",
    " #   #   ###   ##### # ### #   # # ",
    " ###                             # ",
    " #    # ### #### #### #### #####   ",
    "   #### #   #..# #..# #..#   #   # ",
    " # #    ### #..# #..# #### # # # # ",
    " #    # #   #..# #..# #          # ",
    " # #### #   #### #### #  # ##### # ",
    "          #              #         ",
    " #### ###### # ##### # ####### ### ",
    "             #       #             "
  };

  for (unsigned int x = 0; x < Constants::map_size.width; ++x)
  {
    for (unsigned int y = 0; y < Constants::map_size.height; ++y)
    {
      Transmission::field_t &cur = map[x][y];
      cur = Transmission::none;
      if (tmpl[y][x] == '#')
        cur |= Color::none | Transmission::block;
      /* this is a simple hack to create areas where no
       * autoplaced points will be placed (see below)
       */
      else if (tmpl[y][x] == '.')
        cur |= Transmission::point;
    }
  }

  map[0][0] |= Transmission::bonuspoint;
  map[1][0] |= Color::red   | Transmission::pacman | Transmission::direction_right;
  map[2][0] |= Color::blue  | Transmission::pacman | Transmission::direction_up;
  map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down;

  /* auto place normal points*/
  for (unsigned int x = 0; x < Constants::map_size.width; ++x)
  {
    for (unsigned int y = 0; y < Constants::map_size.height; ++y)
    {
      Transmission::field_t &cur = map[x][y];
      if (cur == Transmission::none)
        cur |= Transmission::point;
      else if (cur == Transmission::point)
        cur = Transmission::none;
    }
  }

  return map;
}

MainWidget::MainWidget()
  : m_currentKey(0), m_running(false)
{
  visualMap.resize(Constants::map_size.width);
  for (int i=0; i<visualMap.size(); ++i) {
    visualMap[i].resize(Constants::map_size.height);
  }

  createGui();
  updateMap(createDummyMap());

  connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
  AudioPlayer::self()->play(AudioPlayer::Intro);
}

void MainWidget::createGui()
{
  setFocusPolicy(Qt::StrongFocus);

  QVBoxLayout *layout = new QVBoxLayout(this);
  QHBoxLayout *scoreLayout = new QHBoxLayout();

  for (unsigned int i = 1; i < 4; ++i)
  {
    QGroupBox *scoreBox = new QGroupBox(QString("Spieler %1").arg(i), this);
    scoreBox->setObjectName(QString("actor%1").arg(i));
    scoreBox->setCheckable(true);
    connect(scoreBox, SIGNAL(clicked()), this, SLOT(playerScoreClicked()));
    scoreLayout->addWidget(scoreBox);

    QGridLayout *playerLayout = new QGridLayout();
    scoreBox->setLayout(playerLayout);

    playerLayout->addWidget(new QLabel("Rundenpunkte:", this), 0, 0);
    playerLayout->addWidget(new QLabel("Gesamtpunkte:", this), 1, 0);

    playerLayout->addWidget(new QLabel("", this), 0, 1);
    playerLayout->addWidget(new QLabel("", this), 1, 1);

    m_playerScoreLayouts.append(playerLayout);
  }

  m_scene = new QGraphicsScene(0, 0, Constants::map_size_pixel.width, Constants::map_size_pixel.height, this);
  m_scene->setBackgroundBrush(Qt::black);

  QGraphicsView *window = new QGraphicsView(m_scene, this);
  window->setFrameStyle(0);
  window->setAlignment(Qt::AlignLeft | Qt::AlignTop);
  window->setFixedSize(Constants::map_size_pixel.width, Constants::map_size_pixel.height);
  window->setWindowFlags(window->windowFlags() & ~Qt::WindowMaximizeButtonHint);

  layout->addLayout(scoreLayout);
  layout->addWidget(window);

  QFile css(":/stylesheet");
  css.open(QFile::ReadOnly);
  qApp->setStyleSheet(QLatin1String(css.readAll()));

  setLayout(layout);
}

void MainWidget::updateScore()
{
  QMapIterator<Color::Color, Actor*> i(m_actors);
  while (i.hasNext())
  {
    i.next();
    if (i.key() > Color::max)
    {
      /* player #4 isn't supported in score */
      Q_ASSERT(false);
      continue;
    }
    int id = (i.key() >> 1);
    QLabel *turnPointsLbl =
      dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(0,1)->widget());
    QLabel *allPointsLbl =
      dynamic_cast<QLabel*>(m_playerScoreLayouts.at(id)->itemAtPosition(1,1)->widget());
    turnPointsLbl->setText(QString::number(id));
    allPointsLbl->setText(QString::number(id));
  }
}

void MainWidget::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 = Actor::None;
        switch (cur & Transmission::direction_mask)
        {
        case Transmission::direction_none:
          direction = Actor::None;
          break;
        case Transmission::direction_left:
          direction = Actor::Left;
          break;
        case Transmission::direction_right:
          direction = Actor::Right;
          break;
        case Transmission::direction_up:
          direction = Actor::Up;
          break;
        case Transmission::direction_down:
          direction = Actor::Down;
          break;
        default:
          Q_ASSERT(false);
        }
        //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;
        }
      }
    }
  }

  updateScore();
}

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

Transmission::field_t MainWidget::translateKey(int key)
{
  switch(key)
  {
  case Qt::Key_W:
  case Qt::Key_Up:
    return Transmission::direction_up;
    break;
  case Qt::Key_S:
  case Qt::Key_Down:
    return Transmission::direction_down;
    break;
  case Qt::Key_A:
  case Qt::Key_Left:
    return Transmission::direction_left;
    break;
  case Qt::Key_D:
  case Qt::Key_Right:
    return Transmission::direction_right;
    break;
  default:
    return 0;
  }
}

void MainWidget::keyPressEvent(QKeyEvent* event)
{
  if (!m_running)
    return;

  QWidget::keyPressEvent(event);
  m_currentKey = translateKey(event->key());

  // test stuff
  Actor::Movement mov = Actor::None;
  switch(m_currentKey)
  {
  case Transmission::direction_up:
    mov = Actor::Up;
    break;
  case Transmission::direction_down:
    mov = Actor::Down;
    break;
  case Transmission::direction_left:
    mov = Actor::Left;
    break;
  case Transmission::direction_right:
    mov = Actor::Right;
    break;
  default:
    break;
  }

  QMapIterator<Color::Color, Actor*> i(m_actors);
  while (i.hasNext())
  {
    i.next();
    i.value()->move(mov);
  }
}

void MainWidget::keyReleaseEvent(QKeyEvent* event)
{
  if (!m_running)
    return;

  QWidget::keyReleaseEvent(event);
  Transmission::field_t releasedKey = translateKey(event->key());
  if (releasedKey == m_currentKey)
  {
    // current key got released
    // if this is false, a key got released which has already
    // been replaced by a different key, so this case is disregarded
    m_currentKey = Transmission::direction_none;
  }
}

void MainWidget::startGame()
{
  m_running = true;
}

void MainWidget::playerScoreClicked()
{
  QGroupBox *tmp = qobject_cast<QGroupBox *>(sender());
  tmp->setChecked(true);
  return;
}