From b35be096423e89746d441cbf2333800f853c92fd Mon Sep 17 00:00:00 2001 From: totycro Date: Mon, 4 Apr 2011 22:55:41 +0200 Subject: Support actor in map --- pacman-c++/block.cpp | 3 ++- pacman-c++/block.h | 2 +- pacman-c++/bonuspoint.cpp | 3 ++- pacman-c++/bonuspoint.h | 2 +- pacman-c++/constants.h | 13 ++++++++--- pacman-c++/mainwidget.cpp | 56 ++++++++++++++++++++++++++++++++--------------- pacman-c++/mainwidget.h | 9 +++++++- 7 files changed, 62 insertions(+), 26 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/block.cpp b/pacman-c++/block.cpp index 4240dd4..9d57578 100644 --- a/pacman-c++/block.cpp +++ b/pacman-c++/block.cpp @@ -6,7 +6,8 @@ std::map Block::m_pixmaps; -Block::Block(Color color) +Block::Block(Color color, QGraphicsItem *parent) + : PixmapItem(parent) { if (m_pixmaps.find(color) == m_pixmaps.end()) { QString pixmapName = ":/" + QString("block%1").arg(color); diff --git a/pacman-c++/block.h b/pacman-c++/block.h index f19b590..25f4f08 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -12,7 +12,7 @@ class Block : public PixmapItem { public: - Block(Color color); + Block(Color color, QGraphicsItem *parent=0); private: // map for saving QPixmaps for reuse diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index 67c8250..efff263 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -4,7 +4,8 @@ namespace { QPixmap *pixmap = 0; } -BonusPoint::BonusPoint() +BonusPoint::BonusPoint(QGraphicsItem *parent) + : PixmapItem(parent) { if (pixmap == 0) { pixmap = new QPixmap(":/cherry"); diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h index cfae4b4..ebd1615 100644 --- a/pacman-c++/bonuspoint.h +++ b/pacman-c++/bonuspoint.h @@ -7,7 +7,7 @@ class BonusPoint : public PixmapItem { public: - BonusPoint(); + BonusPoint(QGraphicsItem *parent=0); private: }; diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index a8907b1..088fcb8 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h @@ -17,13 +17,20 @@ namespace transmission { typedef unsigned int field_t; typedef unsigned int mask_t; - const field_t block = (1 << 3); - const field_t bonuspoint = (1 << 4); + const field_t block = (1 << 3); + const field_t bonuspoint = (1 << 4); + const field_t pacman = (1 << 5); + + const field_t direction_left = (1 << 6); + const field_t direction_right = (1 << 7); + const field_t direction_up = (1 << 8); + const field_t direction_down = (1 << 9); const mask_t color_mask = noColor | red | blue | green; const mask_t type_mask = block | bonuspoint; + const mask_t direction_mask = direction_left | direction_right | direction_up | direction_down; typedef field_t** map_t; } -#endif // CONSTANTS_H \ No newline at end of file +#endif // CONSTANTS_H diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 72d9c8a..8dd1875 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -12,10 +12,10 @@ MainWidget::MainWidget() { QLabel *lbl = new QLabel("da kommt da spielstand hin", this); layout->addWidget(lbl); - scene = new QGraphicsScene(0, 0, 500, 500, this); - scene->setBackgroundBrush(Qt::black); + m_scene = new QGraphicsScene(0, 0, 500, 500, this); + m_scene->setBackgroundBrush(Qt::black); - QGraphicsView *window = new QGraphicsView(scene, this); + QGraphicsView *window = new QGraphicsView(m_scene, this); window->setFrameStyle(0); window->setAlignment(Qt::AlignLeft | Qt::AlignTop); window->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -54,6 +54,9 @@ transmission::map_t createDummyMap() { map[7][5] |= transmission::bonuspoint; + map[5][5] |= blue; + map[5][5] |= transmission::pacman; + return map; } void MainWidget::loadDummyMap() @@ -76,27 +79,44 @@ void MainWidget::loadDummyMap() item = new Block(color); } else if (cur & transmission::bonuspoint) { item = new BonusPoint(); + } else if (cur & transmission::pacman) { + Actor *actor = 0; + ActorMap::iterator it = m_actors.find(color); + if (it != m_actors.end()) { + actor = it->second; + } else { + qDebug() << "new actor of col" << color; + actor = new Actor(color); + m_actors[color] = actor; + m_scene->addItem(actor); + actor->setPos( mapPositionToCoord(x, y) ); + } + Actor::Movement direction; + switch (cur & transmission::direction_mask) { + 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 != 0) { + m_scene->addItem(item); + item->setPos( mapPositionToCoord(x, y) ); } - Q_ASSERT(item != 0); - scene->addItem(item); - item->setPos( mapPositionToCoord(x, y) ); } } - - /* - Actor *actor3 = new Actor(Actor::Player3); - scene->addItem(actor3); - actor3->setPos(140, 100); - - for (unsigned int i=0; i<20; ++i) { - Block *b = new Block(Actor::Player1); - scene->addItem(b); - b->setPos( 100 + i*16, 200); - } - */ } QPoint MainWidget::mapPositionToCoord(unsigned int x, unsigned int y) { return QPoint(x * field_size[0], y * field_size[1]); } + +Actor* MainWidget::getActor(Color color) +{ + +} + diff --git a/pacman-c++/mainwidget.h b/pacman-c++/mainwidget.h index 4cdb3b8..93af229 100644 --- a/pacman-c++/mainwidget.h +++ b/pacman-c++/mainwidget.h @@ -2,9 +2,12 @@ #define MAINWIDGET_H #include +#include #include "constants.h" +class Actor; + class MainWidget : public QWidget { @@ -19,7 +22,11 @@ private: // data conversion QPoint mapPositionToCoord(unsigned int x, unsigned int y); - QGraphicsScene *scene; + QGraphicsScene *m_scene; + + Actor *getActor(Color); + typedef std::map ActorMap; + ActorMap m_actors; }; #endif // MAINWIDGET_H \ No newline at end of file -- cgit v1.2.3