From d547dec802f76c346538144f4eacf6d8ca6310c4 Mon Sep 17 00:00:00 2001 From: totycro Date: Sat, 9 Apr 2011 13:54:18 +0200 Subject: Pull up methods from mainwidget to sceneholder for sharing in both client and server --- pacman-c++/mainwidget.cpp | 179 ++-------------------------------------------- 1 file changed, 4 insertions(+), 175 deletions(-) (limited to 'pacman-c++/mainwidget.cpp') diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 63280f2..34ff63e 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -6,89 +6,11 @@ #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; - } - } +#include "util.h" - 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) +MainWidget::MainWidget(QWidget *parent) + : SceneHolder(parent), m_currentKey(0), m_running(false) { - visualMap.resize(Constants::map_size.width); - for (int i=0; isetBackgroundBrush(Qt::black); - QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); window->setAlignment(Qt::AlignLeft | Qt::AlignTop); @@ -172,100 +91,10 @@ void MainWidget::updateScore() 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(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; - } - } - } - } - + SceneHolder::updateMap(map); 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) { -- cgit v1.2.3