diff options
| author | totycro <totycro@unknown-horizons.org> | 2011-04-11 12:09:04 +0200 |
|---|---|---|
| committer | totycro <totycro@unknown-horizons.org> | 2011-04-11 12:09:04 +0200 |
| commit | 98f4a31e1a359a69dbcc0fa4055f36cefb6d4e02 (patch) | |
| tree | ddd164d3339a067116f02f88314e2f57457bb1df /pacman-c++ | |
| parent | 30c36b7549f8ae411f8eca4de144627331895a24 (diff) | |
| download | foop-98f4a31e1a359a69dbcc0fa4055f36cefb6d4e02.tar.gz foop-98f4a31e1a359a69dbcc0fa4055f36cefb6d4e02.tar.bz2 foop-98f4a31e1a359a69dbcc0fa4055f36cefb6d4e02.zip | |
Added basic game logic
Diffstat (limited to 'pacman-c++')
| -rw-r--r-- | pacman-c++/block.h | 2 | ||||
| -rw-r--r-- | pacman-c++/bonuspoint.cpp | 6 | ||||
| -rw-r--r-- | pacman-c++/bonuspoint.h | 2 | ||||
| -rw-r--r-- | pacman-c++/constants.h | 11 | ||||
| -rw-r--r-- | pacman-c++/gameentity.h | 19 | ||||
| -rw-r--r-- | pacman-c++/mainwidget.cpp | 4 | ||||
| -rw-r--r-- | pacman-c++/pacman.pro | 3 | ||||
| -rw-r--r-- | pacman-c++/pacman.server.pro | 3 | ||||
| -rw-r--r-- | pacman-c++/pixmapitem.h | 7 | ||||
| -rw-r--r-- | pacman-c++/point.cpp | 6 | ||||
| -rw-r--r-- | pacman-c++/point.h | 2 | ||||
| -rw-r--r-- | pacman-c++/server.cpp | 13 |
12 files changed, 68 insertions, 10 deletions
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: | |||
| 24 | 24 | ||
| 25 | void setNeighbours(unsigned int neighbours); | 25 | void setNeighbours(unsigned int neighbours); |
| 26 | 26 | ||
| 27 | virtual bool checkEnter(Actor *actor) { Q_UNUSED(actor); return false; } // TODO: colored blocks | ||
| 28 | |||
| 27 | private: | 29 | private: |
| 28 | // map for saving QPixmaps for reuse | 30 | // map for saving QPixmaps for reuse |
| 29 | static QMap<Color::Color, QPixmap> m_pixmaps; | 31 | static QMap<Color::Color, QPixmap> 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 @@ | |||
| 1 | #include "bonuspoint.h" | 1 | #include "bonuspoint.h" |
| 2 | #include "constants.h" | 2 | #include "constants.h" |
| 3 | #include "actor.h" | ||
| 3 | 4 | ||
| 4 | #define BONUSPOINTS_NUM_SPRITES 4 | 5 | #define BONUSPOINTS_NUM_SPRITES 4 |
| 5 | 6 | ||
| @@ -18,3 +19,8 @@ BonusPoint::BonusPoint(QGraphicsItem *parent) | |||
| 18 | int rand = (int) (BONUSPOINTS_NUM_SPRITES * (qrand() / (RAND_MAX + 1.0))); | 19 | int rand = (int) (BONUSPOINTS_NUM_SPRITES * (qrand() / (RAND_MAX + 1.0))); |
| 19 | setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | 20 | setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); |
| 20 | } | 21 | } |
| 22 | |||
| 23 | void BonusPoint::enter(Actor* actor) | ||
| 24 | { | ||
| 25 | actor->addRoundPoints(Constants::Game::bonus_point_value); | ||
| 26 | } | ||
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: | |||
| 10 | BonusPoint(QGraphicsItem *parent=0); | 10 | BonusPoint(QGraphicsItem *parent=0); |
| 11 | virtual ~BonusPoint() | 11 | virtual ~BonusPoint() |
| 12 | {}; | 12 | {}; |
| 13 | |||
| 14 | virtual void enter(Actor *actor); | ||
| 13 | }; | 15 | }; |
| 14 | 16 | ||
| 15 | #endif // BONUSPOINT_H | 17 | #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 { | |||
| 16 | const unsigned int sprite_offset = 20; | 16 | const unsigned int sprite_offset = 20; |
| 17 | const unsigned int tick = 250; // ms | 17 | const unsigned int tick = 250; // ms |
| 18 | 18 | ||
| 19 | const unsigned int port = 7321; | 19 | namespace Networking { |
| 20 | const unsigned int connection_timeout = 3000; | 20 | const unsigned int port = 7321; |
| 21 | const unsigned int connection_timeout = 3000; | ||
| 22 | } | ||
| 23 | |||
| 24 | namespace Game { | ||
| 25 | const unsigned int bonus_point_value = 100; | ||
| 26 | const unsigned int point_value = 10; | ||
| 27 | } | ||
| 21 | } | 28 | } |
| 22 | 29 | ||
| 23 | namespace Color | 30 | 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 @@ | |||
| 1 | #ifndef GAMEENTITY_H | ||
| 2 | #define GAMEENTITY_H | ||
| 3 | |||
| 4 | class Actor; | ||
| 5 | |||
| 6 | /** | ||
| 7 | * Abstract base class for entities that interact in the game | ||
| 8 | */ | ||
| 9 | class GameEntity { | ||
| 10 | |||
| 11 | // returns whether the actor may enter this field | ||
| 12 | virtual bool checkEnter(Actor *actor) = 0; | ||
| 13 | |||
| 14 | // performs action when this actor acctually enters | ||
| 15 | virtual void enter(Actor *actor) = 0; | ||
| 16 | |||
| 17 | }; | ||
| 18 | |||
| 19 | #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() | |||
| 237 | 237 | ||
| 238 | // connect to server | 238 | // connect to server |
| 239 | m_socket = new QTcpSocket(this); | 239 | m_socket = new QTcpSocket(this); |
| 240 | m_socket->connectToHost(srv, Constants::port); | 240 | m_socket->connectToHost(srv, Constants::Networking::port); |
| 241 | bool worked = m_socket->waitForConnected(Constants::connection_timeout); | 241 | bool worked = m_socket->waitForConnected(Constants::Networking::connection_timeout); |
| 242 | if (worked) | 242 | if (worked) |
| 243 | { | 243 | { |
| 244 | // additional init | 244 | // 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 \ | |||
| 27 | audio.h \ | 27 | audio.h \ |
| 28 | clicklabel.h \ | 28 | clicklabel.h \ |
| 29 | sceneholder.h \ | 29 | sceneholder.h \ |
| 30 | util.h | 30 | util.h \ |
| 31 | gameentity.h | ||
| 31 | RESOURCES += pacman.qrc | 32 | RESOURCES += pacman.qrc |
| 32 | 33 | ||
| 33 | OBJECTS_DIR = .obj | 34 | 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 \ | |||
| 30 | audio.h \ | 30 | audio.h \ |
| 31 | sceneholder.h \ | 31 | sceneholder.h \ |
| 32 | util.h \ | 32 | util.h \ |
| 33 | clicklabel.h | 33 | clicklabel.h \ |
| 34 | gameentity.h | ||
| 34 | RESOURCES += pacman.qrc | 35 | RESOURCES += pacman.qrc |
| 35 | 36 | ||
| 36 | OBJECTS_DIR = .obj | 37 | 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 @@ | |||
| 4 | #include <QtGui/QGraphicsObject> | 4 | #include <QtGui/QGraphicsObject> |
| 5 | #include <QtGui/QGraphicsScene> | 5 | #include <QtGui/QGraphicsScene> |
| 6 | 6 | ||
| 7 | #include "gameentity.h" | ||
| 8 | |||
| 7 | class PixmapItem | 9 | class PixmapItem |
| 8 | : public QGraphicsObject | 10 | : public QGraphicsObject, GameEntity |
| 9 | { | 11 | { |
| 10 | public: | 12 | public: |
| 11 | PixmapItem(QGraphicsItem *parent = 0); | 13 | PixmapItem(QGraphicsItem *parent = 0); |
| @@ -23,6 +25,9 @@ public: | |||
| 23 | QPainterPath shape() const; | 25 | QPainterPath shape() const; |
| 24 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); | 26 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); |
| 25 | 27 | ||
| 28 | virtual bool checkEnter(Actor *actor) { Q_UNUSED(actor); return true; } // default to true | ||
| 29 | virtual void enter(Actor *actor) { Q_UNUSED(actor); } // default to no action | ||
| 30 | |||
| 26 | private: | 31 | private: |
| 27 | QPixmap m_pix; | 32 | QPixmap m_pix; |
| 28 | int m_x, m_y; | 33 | 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 @@ | |||
| 1 | #include "point.h" | 1 | #include "point.h" |
| 2 | #include "constants.h" | 2 | #include "constants.h" |
| 3 | #include "actor.h" | ||
| 3 | 4 | ||
| 4 | namespace | 5 | namespace |
| 5 | { | 6 | { |
| @@ -15,3 +16,8 @@ Point::Point(QGraphicsItem *parent) | |||
| 15 | 16 | ||
| 16 | //setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); | 17 | //setSprite(rand * 20 + Constants::sprite_margin, Constants::sprite_margin, Constants::field_size.width, Constants::field_size.height); |
| 17 | } | 18 | } |
| 19 | |||
| 20 | void Point::enter(Actor* actor) | ||
| 21 | { | ||
| 22 | actor->addRoundPoints(Constants::Game::point_value); | ||
| 23 | } | ||
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: | |||
| 10 | Point(QGraphicsItem *parent=0); | 10 | Point(QGraphicsItem *parent=0); |
| 11 | virtual ~Point() | 11 | virtual ~Point() |
| 12 | {}; | 12 | {}; |
| 13 | |||
| 14 | virtual void enter(Actor *actor); | ||
| 13 | }; | 15 | }; |
| 14 | 16 | ||
| 15 | #endif // POINT_H | 17 | #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() | |||
| 74 | newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); | 74 | newMapPosition.setY(visualMap[newMapPosition.x()].size() - 1); |
| 75 | 75 | ||
| 76 | // <t3h g4m3 10gic> | 76 | // <t3h g4m3 10gic> |
| 77 | if(dynamic_cast<Block*>(visualMap[newMapPosition.x()][newMapPosition.y()]) != 0) | 77 | // TODO: support actors eating each other |
| 78 | continue; | 78 | PixmapItem *item = visualMap[newMapPosition.x()][newMapPosition.y()]; |
| 79 | if (item != NULL) { | ||
| 80 | if (! item->checkEnter(actor)) { // movement invalid | ||
| 81 | newMapPosition = mapPosition; | ||
| 82 | } else { // apply actions of entering this field | ||
| 83 | item->enter(actor); | ||
| 84 | } | ||
| 85 | } | ||
| 79 | // </t3h g4m2 10gic> | 86 | // </t3h g4m2 10gic> |
| 80 | 87 | ||
| 81 | if (mapPosition != newMapPosition) | 88 | if (mapPosition != newMapPosition) |
| @@ -114,7 +121,7 @@ void Server::waitForClientConnections() | |||
| 114 | { | 121 | { |
| 115 | QTcpServer *tcpSrv = new QTcpServer(this); | 122 | QTcpServer *tcpSrv = new QTcpServer(this); |
| 116 | // server must stay alive as long as sockets (qt parent mem mechanism) | 123 | // server must stay alive as long as sockets (qt parent mem mechanism) |
| 117 | tcpSrv->listen(QHostAddress::Any, Constants::port); | 124 | tcpSrv->listen(QHostAddress::Any, Constants::Networking::port); |
| 118 | 125 | ||
| 119 | #define SINGLE | 126 | #define SINGLE |
| 120 | 127 | ||
