From 979c3b4528da1608404d6e163211280fee5da9c5 Mon Sep 17 00:00:00 2001 From: totycro Date: Sat, 9 Apr 2011 15:08:13 +0200 Subject: added some utility --- pacman-c++/constants.h | 9 ++- pacman-c++/mainwidget.cpp | 40 +--------- pacman-c++/sceneholder.cpp | 33 +++------ pacman-c++/sceneholder.h | 1 + pacman-c++/server.cpp | 40 +++++++++- pacman-c++/server.h | 10 +++ pacman-c++/util.cpp | 177 ++++++++++++++++++++++++++++++--------------- pacman-c++/util.h | 16 +++- 8 files changed, 201 insertions(+), 125 deletions(-) diff --git a/pacman-c++/constants.h b/pacman-c++/constants.h index e7755d4..8977b08 100644 --- a/pacman-c++/constants.h +++ b/pacman-c++/constants.h @@ -39,12 +39,13 @@ namespace Transmission const field_t point = (1 << 4); const field_t bonuspoint = (1 << 5); const field_t pacman = (1 << 6); + const field_t empty = (1 << 7); // explicit empty for update const field_t direction_none = 0; - const field_t direction_left = (1 << 7); - const field_t direction_right = (1 << 8); - const field_t direction_up = (1 << 9); - const field_t direction_down = (1 << 10); + const field_t direction_left = (1 << 8); + const field_t direction_right = (1 << 9); + const field_t direction_up = (1 << 10); + const field_t direction_down = (1 << 11); const mask_t color_mask = Color::none | Color::red | Color::blue | Color::green; const mask_t type_mask = block | bonuspoint; diff --git a/pacman-c++/mainwidget.cpp b/pacman-c++/mainwidget.cpp index 34ff63e..79648c5 100644 --- a/pacman-c++/mainwidget.cpp +++ b/pacman-c++/mainwidget.cpp @@ -12,7 +12,7 @@ MainWidget::MainWidget(QWidget *parent) : SceneHolder(parent), m_currentKey(0), m_running(false) { createGui(); - updateMap(createDummyMap()); + updateMap(Util::createDummyMap()); //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); //AudioPlayer::self()->play(AudioPlayer::Intro); @@ -123,24 +123,7 @@ Transmission::field_t MainWidget::translateKey(int key) void MainWidget::tick() { - Actor::Movement mov = Actor::None; - switch(m_currentKey) - { - case Transmission::direction_up: - mov = Actor::Up; - break; - case Transmission::direction_down: - mov = Actor::Down; - break; - case Transmission::direction_left: - mov = Actor::Left; - break; - case Transmission::direction_right: - mov = Actor::Right; - break; - default: - break; - } + Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); QMapIterator i(m_actors); while (i.hasNext()) @@ -169,24 +152,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event) return; // test stuff - Actor::Movement mov = Actor::None; - switch(m_currentKey) - { - case Transmission::direction_up: - mov = Actor::Up; - break; - case Transmission::direction_down: - mov = Actor::Down; - break; - case Transmission::direction_left: - mov = Actor::Left; - break; - case Transmission::direction_right: - mov = Actor::Right; - break; - default: - break; - } + Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None); QMapIterator i(m_actors); while (i.hasNext()) diff --git a/pacman-c++/sceneholder.cpp b/pacman-c++/sceneholder.cpp index 83025d5..ba9cc97 100644 --- a/pacman-c++/sceneholder.cpp +++ b/pacman-c++/sceneholder.cpp @@ -6,6 +6,7 @@ #include "actor.h" #include "bonuspoint.h" #include "point.h" +#include "util.h" SceneHolder::SceneHolder(QWidget* parent): QWidget(parent) { @@ -66,27 +67,8 @@ void SceneHolder::updateMap(const Transmission::map_t& map) 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::Movement direction = + Util::transmissionMovementToActor(cur & Transmission::direction_mask); //actor->move(direction, mapPositionToCoord(x, y)); } else @@ -112,4 +94,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map) QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) { return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); -} \ No newline at end of file +} + +QPoint SceneHolder::CoordToMapPosition(unsigned int x, unsigned int y) +{ + return QPoint(x / Constants::field_size.width, y / Constants::field_size.height); +} + + diff --git a/pacman-c++/sceneholder.h b/pacman-c++/sceneholder.h index d69e07e..cf6941a 100644 --- a/pacman-c++/sceneholder.h +++ b/pacman-c++/sceneholder.h @@ -23,6 +23,7 @@ protected: // data conversion QPoint mapPositionToCoord(unsigned int x, unsigned int y); + QPoint CoordToMapPosition(unsigned int x, unsigned int y); // map of all pixmap instances QVector< QVector > visualMap; diff --git a/pacman-c++/server.cpp b/pacman-c++/server.cpp index 89cf356..2c16453 100644 --- a/pacman-c++/server.cpp +++ b/pacman-c++/server.cpp @@ -5,7 +5,45 @@ Server::Server(QWidget *parent) : SceneHolder(parent) { - updateMap(createDummyMap()); + updateMap(Util::createDummyMap()); + Transmission::map_t map = calculateUpdates(); + updateMap(map); +} + +QMap< Color::Color, Actor::Movement > Server::getActorDirections() +{ + QMap directions; + directions[Color::red] = Actor::Down; + return directions; +} + + +Transmission::map_t Server::calculateUpdates() +{ + Transmission::map_t map = Util::createEmptyMap(); + + QMap directions = getActorDirections(); + QMapIterator i(directions); + while (i.hasNext()) { + i.next(); + Actor *actor = m_actors.value( i.key() ); + QPoint mapPosition = CoordToMapPosition(actor->x(), actor->y()); + qDebug() << "actor " << i.key() << " is at " << mapPosition; + map[mapPosition.x()][mapPosition.y()] = Transmission::empty; + + QPoint newMapPosition = mapPosition; + switch (i.value()) { + case Actor::Up: newMapPosition += QPoint(0, -1); break; + case Actor::Down: newMapPosition += QPoint(0, 1); break; + case Actor::Left: newMapPosition += QPoint(-1, 0); break; + case Actor::Right: newMapPosition += QPoint(1, 0); break; + case Actor::None: break; + default: Q_ASSERT(false); + } + map[newMapPosition.x()][newMapPosition.y()] = Transmission::pacman | i.key() ; + + } + return map; } int main(int argc, char ** argv) diff --git a/pacman-c++/server.h b/pacman-c++/server.h index 4be0cd8..006b104 100644 --- a/pacman-c++/server.h +++ b/pacman-c++/server.h @@ -2,7 +2,9 @@ #define SERVER_H #include "sceneholder.h" + #include +#include "actor.h" class Server : public SceneHolder @@ -10,6 +12,14 @@ class Server Q_OBJECT public: Server(QWidget *parent = 0); + +protected: + // returns packets read from network until now + // for clients not sending a direction, no change is assumed + QMap getActorDirections(); + + // calculate updates of current tick for sending to client + Transmission::map_t calculateUpdates(); }; #endif // SERVER_H diff --git a/pacman-c++/util.cpp b/pacman-c++/util.cpp index 185b0a6..99a643e 100644 --- a/pacman-c++/util.cpp +++ b/pacman-c++/util.cpp @@ -1,76 +1,137 @@ #include "util.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]; +namespace Util { - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) + Transmission::map_t createEmptyMap() { + 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) { - Transmission::field_t &cur = map[x][y]; - cur = Transmission::none; + for (unsigned int y = 0; y < Constants::map_size.height; ++y) + { + Transmission::field_t &cur = map[x][y]; + cur = Transmission::none; + } } + return map; } + // temporary + Transmission::map_t createDummyMap() + { + Transmission::map_t map = createEmptyMap(); + const char *tmpl[] = { + " # # ", + " #### ###### # #### # # ###### ### ", + " # # ", + " # ##### # # # # # ### # # # ", + " # # # # # # # # # # ## # # ", + " # # # # # # # # ### # # # # ", + " # # # # # # # # # # # # ## # ", + " # # ### ##### # ### # # # ", + " ### # ", + " # # ### #### #### #### ##### ", + " #### # #..# #..# #..# # # ", + " # # ### #..# #..# #### # # # # ", + " # # # #..# #..# # # ", + " # #### # #### #### # # ##### # ", + " # # ", + " #### ###### # ##### # ####### ### ", + " # # " + }; - 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; + } + } - for (unsigned int x = 0; x < Constants::map_size.width; ++x) - { - for (unsigned int y = 0; y < Constants::map_size.height; ++y) + 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) { - 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; + 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; } - 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; + Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def) { + switch (mov) { + case Actor::None: + return Transmission::direction_none; + break; + case Actor::Left: + return Transmission::direction_left; + break; + case Actor::Right: + return Transmission::direction_right; + break; + case Actor::Up: + return Transmission::direction_up; + break; + case Actor::Down: + return Transmission::direction_down; + break; + default: + if (def == static_cast(-1)) { + Q_ASSERT(false); + } else { + return def; + } } + return 0; // for pleasing the compiler } - return map; + Actor::Movement transmissionMovementToActor(Transmission::field_t field, Actor::Movement def) { + switch (field) { + case Transmission::direction_none: + return Actor::None; + break; + case Transmission::direction_left: + return Actor::Left; + break; + case Transmission::direction_right: + return Actor::Right; + break; + case Transmission::direction_up: + return Actor::Up; + break; + case Transmission::direction_down: + return Actor::Down; + break; + default: + if (def == Actor::Movement(-1)) { + Q_ASSERT(false); + } else { + return def; + } + } + return Actor::None; // for pleasing the compiler + } } diff --git a/pacman-c++/util.h b/pacman-c++/util.h index 34ddd31..1754f54 100644 --- a/pacman-c++/util.h +++ b/pacman-c++/util.h @@ -2,7 +2,17 @@ #define UTIL_H #include "constants.h" -// temporary -Transmission::map_t createDummyMap(); +#include "actor.h" -#endif // UTIL_H \ No newline at end of file +namespace Util { + Transmission::map_t createDummyMap(); + + Transmission::map_t createEmptyMap(); + + // default is to assert false with -1 + Transmission::field_t actorMovementToTransmission(Actor::Movement mov, + Transmission::field_t def = -1); + Actor::Movement transmissionMovementToActor(Transmission::field_t field, + Actor::Movement def = Actor::Movement(-1)); +} +#endif // UTIL_H -- cgit v1.2.3