From 51a3559e5df31018d7de14357f83c24e7e508d7e Mon Sep 17 00:00:00 2001 From: totycro Date: Mon, 11 Apr 2011 12:37:29 +0200 Subject: progress --- pacman-c++/bonuspoint.cpp | 5 ++++- pacman-c++/bonuspoint.h | 2 +- pacman-c++/gameentity.h | 16 +++++++++++++--- pacman-c++/pacman.pro | 3 ++- pacman-c++/pacman.server.pro | 3 ++- pacman-c++/pixmapitem.h | 5 +---- pacman-c++/point.cpp | 4 +++- pacman-c++/point.h | 2 +- pacman-c++/sceneholder.cpp | 21 ++++++++++++++------- pacman-c++/server.cpp | 19 +++++++++++++++---- 10 files changed, 56 insertions(+), 24 deletions(-) (limited to 'pacman-c++') diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index 3a72243..2a772d5 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -20,7 +20,10 @@ BonusPoint::BonusPoint(QGraphicsItem *parent) setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); } -void BonusPoint::enter(Actor* actor) +bool BonusPoint::enter(Actor* actor) { actor->addRoundPoints(Constants::Game::bonus_point_value); + actor->eatingCherry(); + m_eaten = true; + return false; } diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h index 2c5f7ea..dcf561e 100644 --- a/pacman-c++/bonuspoint.h +++ b/pacman-c++/bonuspoint.h @@ -11,7 +11,7 @@ public: virtual ~BonusPoint() {}; - virtual void enter(Actor *actor); + virtual bool enter(Actor *actor); }; #endif // BONUSPOINT_H diff --git a/pacman-c++/gameentity.h b/pacman-c++/gameentity.h index 82df438..2a771ee 100644 --- a/pacman-c++/gameentity.h +++ b/pacman-c++/gameentity.h @@ -1,19 +1,29 @@ #ifndef GAMEENTITY_H #define GAMEENTITY_H +#include + class Actor; /** - * Abstract base class for entities that interact in the game + * Base class for entities that interact in the game */ class GameEntity { +public: + GameEntity(); + virtual ~GameEntity() {}; // returns whether the actor may enter this field - virtual bool checkEnter(Actor *actor) = 0; + virtual bool checkEnter(Actor *actor) { Q_UNUSED(actor); return true; } // default to true // performs action when this actor acctually enters - virtual void enter(Actor *actor) = 0; + // returns whether this entity survives the entering + virtual bool enter(Actor *actor) { Q_UNUSED(actor); return true; } // default to no action/survive + + virtual bool eaten() { return m_eaten; } +protected: + bool m_eaten; }; #endif // GAMEENTITY_H diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index a36ef37..07efc71 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro @@ -14,7 +14,8 @@ SOURCES += pixmapitem.cpp \ audio.cpp \ clicklabel.cpp \ sceneholder.cpp \ - util.cpp + util.cpp \ + gameentity.cpp HEADERS += pixmapitem.h \ actor.h \ animationmanager.h \ diff --git a/pacman-c++/pacman.server.pro b/pacman-c++/pacman.server.pro index 172045d..0b5fd6d 100644 --- a/pacman-c++/pacman.server.pro +++ b/pacman-c++/pacman.server.pro @@ -17,7 +17,8 @@ SOURCES += pixmapitem.cpp \ audio.cpp \ sceneholder.cpp \ util.cpp \ - clicklabel.cpp + clicklabel.cpp \ + gameentity.cpp HEADERS += pixmapitem.h \ actor.h \ animationmanager.h \ diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index 88770d2..4e58a52 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h @@ -7,7 +7,7 @@ #include "gameentity.h" class PixmapItem - : public QGraphicsObject, GameEntity + : public QGraphicsObject, public GameEntity { public: PixmapItem(QGraphicsItem *parent = 0); @@ -25,9 +25,6 @@ public: QPainterPath shape() const; void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); - virtual bool checkEnter(Actor *actor) { Q_UNUSED(actor); return true; } // default to true - virtual void enter(Actor *actor) { Q_UNUSED(actor); } // default to no action - private: QPixmap m_pix; int m_x, m_y; diff --git a/pacman-c++/point.cpp b/pacman-c++/point.cpp index 67a7e5f..05bd277 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp @@ -17,7 +17,9 @@ Point::Point(QGraphicsItem *parent) //setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); } -void Point::enter(Actor* actor) +bool Point::enter(Actor* actor) { actor->addRoundPoints(Constants::Game::point_value); + m_eaten = true; + return false; } diff --git a/pacman-c++/point.h b/pacman-c++/point.h index 0e0152d..033ca56 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h @@ -11,7 +11,7 @@ public: virtual ~Point() {}; - virtual void enter(Actor *actor); + virtual bool enter(Actor *actor); }; #endif // POINT_H diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 0a65d19..1596ba1 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -31,12 +31,9 @@ void SceneHolder::updateMap(const Transmission::map_t& map) Color::Color color = static_cast(cur & Transmission::color_mask); PixmapItem* item = NULL; - if (cur == Transmission::none) - { - // no update - } - else if (cur & Transmission::empty) - { + + if (cur & Transmission::empty) + { // special handling for purging field PixmapItem* oldItem = visualMap[x][y]; // remove elements (in case it's not an actor) if (oldItem != NULL && dynamic_cast(item) == NULL) @@ -44,8 +41,14 @@ void SceneHolder::updateMap(const Transmission::map_t& map) m_scene->removeItem(oldItem); visualMap[x][y] = NULL; delete oldItem; + qDebug() << "deleting " << x << y; } } + + if (cur == Transmission::none) + { + // no update + } else if (cur & Transmission::block) { unsigned int neighbours = Block::None; @@ -81,7 +84,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map) Actor::Movement direction = Util::transmissionMovementToActor(cur & Transmission::direction_mask); actor->move(direction); - qDebug() << "actor move " << direction; + qDebug() << "actor move " << direction << "to " << x << y; + } + else if (cur & Transmission::empty) + { + // already handled } else { diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index b631345..4656238 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -75,20 +75,31 @@ Transmission::map_t Server::calculateUpdates() // // TODO: support actors eating each other + // old item + PixmapItem *oldItem = visualMap[mapPosition.x()][mapPosition.y()]; + if (oldItem != NULL) { + if (oldItem->eaten()) { + map[mapPosition.x()][mapPosition.y()] = Transmission::empty; + } + } + // new item PixmapItem *item = visualMap[newMapPosition.x()][newMapPosition.y()]; - if (item != NULL) { + if (item != NULL && oldItem != item) { if (! item->checkEnter(actor)) { // movement invalid newMapPosition = mapPosition; } else { // apply actions of entering this field - item->enter(actor); + bool survive = item->enter(actor); + if (!survive) { + //map[newMapPosition.x()][newMapPosition.y()] = Transmission::empty; + } } } // if (mapPosition != newMapPosition) { - map[mapPosition.x()][mapPosition.y()] = Transmission::empty; - map[newMapPosition.x()][newMapPosition.y()] = + //map[mapPosition.x()][mapPosition.y()] = Transmission::empty; + map[newMapPosition.x()][newMapPosition.y()] |= Transmission::pacman | i.key() | Util::actorMovementToTransmission(i.value()) ; } -- cgit v1.2.3