From 98f4a31e1a359a69dbcc0fa4055f36cefb6d4e02 Mon Sep 17 00:00:00 2001 From: totycro Date: Mon, 11 Apr 2011 12:09:04 +0200 Subject: Added basic game logic --- pacman-c++/block.h | 2 ++ pacman-c++/bonuspoint.cpp | 6 ++++++ pacman-c++/bonuspoint.h | 2 ++ pacman-c++/constants.h | 11 +++++++++-- pacman-c++/gameentity.h | 19 +++++++++++++++++++ pacman-c++/mainwidget.cpp | 4 ++-- pacman-c++/pacman.pro | 3 ++- pacman-c++/pacman.server.pro | 3 ++- pacman-c++/pixmapitem.h | 7 ++++++- pacman-c++/point.cpp | 6 ++++++ pacman-c++/point.h | 2 ++ pacman-c++/server.cpp | 13 ++++++++++--- 12 files changed, 68 insertions(+), 10 deletions(-) create mode 100644 pacman-c++/gameentity.h diff --git a/pacman-c++/block.h b/pacman-c++/block.h index 17f1813..b5a4bf3 100644 --- a/pacman-c++/block.h +++ b/pacman-c++/block.h @@ -24,6 +24,8 @@ public: void setNeighbours(unsigned int neighbours); + virtual bool checkEnter(Actor *actor) { Q_UNUSED(actor); return false; } // TODO: colored blocks + private: // map for saving QPixmaps for reuse static QMap m_pixmaps; diff --git a/pacman-c++/bonuspoint.cpp b/pacman-c++/bonuspoint.cpp index eedb796..3a72243 100644 --- a/pacman-c++/bonuspoint.cpp +++ b/pacman-c++/bonuspoint.cpp @@ -1,5 +1,6 @@ #include "bonuspoint.h" #include "constants.h" +#include "actor.h" #define BONUSPOINTS_NUM_SPRITES 4 @@ -18,3 +19,8 @@ BonusPoint::BonusPoint(QGraphicsItem *parent) int rand = (int) (BONUSPOINTS_NUM_SPRITES * (qrand() / (RAND_MAX + 1.0))); setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); } + +void BonusPoint::enter(Actor* actor) +{ + actor->addRoundPoints(Constants::Game::bonus_point_value); +} diff --git a/pacman-c++/bonuspoint.h b/pacman-c++/bonuspoint.h index d204f72..2c5f7ea 100644 --- a/pacman-c++/bonuspoint.h +++ b/pacman-c++/bonuspoint.h @@ -10,6 +10,8 @@ public: BonusPoint(QGraphicsItem *parent=0); virtual ~BonusPoint() {}; + + virtual void enter(Actor *actor); }; #endif // BONUSPOINT_H diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index e7330ae..aacc964 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h @@ -16,8 +16,15 @@ namespace Constants { const unsigned int sprite_offset = 20; const unsigned int tick = 250; // ms - const unsigned int port = 7321; - const unsigned int connection_timeout = 3000; + namespace Networking { + const unsigned int port = 7321; + const unsigned int connection_timeout = 3000; + } + + namespace Game { + const unsigned int bonus_point_value = 100; + const unsigned int point_value = 10; + } } namespace Color diff --git a/pacman-c++/gameentity.h b/pacman-c++/gameentity.h new file mode 100644 index 0000000..82df438 --- /dev/null +++ b/pacman-c++/gameentity.h @@ -0,0 +1,19 @@ +#ifndef GAMEENTITY_H +#define GAMEENTITY_H + +class Actor; + +/** + * Abstract base class for entities that interact in the game + */ +class GameEntity { + + // returns whether the actor may enter this field + virtual bool checkEnter(Actor *actor) = 0; + + // performs action when this actor acctually enters + virtual void enter(Actor *actor) = 0; + +}; + +#endif // GAMEENTITY_H diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 6c4012e..df624da 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -237,8 +237,8 @@ Color::Color MainWidget::connectToServer() // connect to server m_socket = new QTcpSocket(this); - m_socket->connectToHost(srv, Constants::port); - bool worked = m_socket->waitForConnected(Constants::connection_timeout); + m_socket->connectToHost(srv, Constants::Networking::port); + bool worked = m_socket->waitForConnected(Constants::Networking::connection_timeout); if (worked) { // additional init diff --git a/pacman-c++/pacman.pro b/pacman-c++/pacman.pro index 45592c5..a36ef37 100644 --- a/pacman-c++/pacman.pro +++ b/pacman-c++/pacman.pro @@ -27,7 +27,8 @@ HEADERS += pixmapitem.h \ audio.h \ clicklabel.h \ sceneholder.h \ - util.h + util.h \ + gameentity.h RESOURCES += pacman.qrc OBJECTS_DIR = .obj diff --git a/pacman-c++/pacman.server.pro b/pacman-c++/pacman.server.pro index 4284286..172045d 100644 --- a/pacman-c++/pacman.server.pro +++ b/pacman-c++/pacman.server.pro @@ -30,7 +30,8 @@ HEADERS += pixmapitem.h \ audio.h \ sceneholder.h \ util.h \ - clicklabel.h + clicklabel.h \ + gameentity.h RESOURCES += pacman.qrc OBJECTS_DIR = .obj diff --git a/pacman-c++/pixmapitem.h b/pacman-c++/pixmapitem.h index f57c22a..88770d2 100644 --- a/pacman-c++/pixmapitem.h +++ b/pacman-c++/pixmapitem.h @@ -4,8 +4,10 @@ #include #include +#include "gameentity.h" + class PixmapItem - : public QGraphicsObject + : public QGraphicsObject, GameEntity { public: PixmapItem(QGraphicsItem *parent = 0); @@ -23,6 +25,9 @@ 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 8837bba..67a7e5f 100644 --- a/pacman-c++/point.cpp +++ b/pacman-c++/point.cpp @@ -1,5 +1,6 @@ #include "point.h" #include "constants.h" +#include "actor.h" namespace { @@ -15,3 +16,8 @@ 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) +{ + actor->addRoundPoints(Constants::Game::point_value); +} diff --git a/pacman-c++/point.h b/pacman-c++/point.h index ad1cf77..0e0152d 100644 --- a/pacman-c++/point.h +++ b/pacman-c++/point.h @@ -10,6 +10,8 @@ public: Point(QGraphicsItem *parent=0); virtual ~Point() {}; + + virtual void enter(Actor *actor); }; #endif // POINT_H diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index ba2b3c9..b631345 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -74,8 +74,15 @@ Transmission::map_t Server::calculateUpdates() newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); // - if(dynamic_cast(visualMap[newMapPosition.x()][newMapPosition.y()]) != 0) - continue; + // TODO: support actors eating each other + PixmapItem *item = visualMap[newMapPosition.x()][newMapPosition.y()]; + if (item != NULL) { + if (! item->checkEnter(actor)) { // movement invalid + newMapPosition = mapPosition; + } else { // apply actions of entering this field + item->enter(actor); + } + } // if (mapPosition != newMapPosition) @@ -114,7 +121,7 @@ void Server::waitForClientConnections() { QTcpServer *tcpSrv = new QTcpServer(this); // server must stay alive as long as sockets (qt parent mem mechanism) - tcpSrv->listen(QHostAddress::Any, Constants::port); + tcpSrv->listen(QHostAddress::Any, Constants::Networking::port); #define SINGLE -- cgit v1.2.3