summaryrefslogtreecommitdiffstats
path: root/pacman-c++
diff options
context:
space:
mode:
Diffstat (limited to 'pacman-c++')
-rw-r--r--pacman-c++/constants.h9
-rw-r--r--pacman-c++/mainwidget.cpp40
-rw-r--r--pacman-c++/sceneholder.cpp33
-rw-r--r--pacman-c++/sceneholder.h1
-rw-r--r--pacman-c++/server.cpp40
-rw-r--r--pacman-c++/server.h10
-rw-r--r--pacman-c++/util.cpp177
-rw-r--r--pacman-c++/util.h16
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
39 const field_t point = (1 << 4); 39 const field_t point = (1 << 4);
40 const field_t bonuspoint = (1 << 5); 40 const field_t bonuspoint = (1 << 5);
41 const field_t pacman = (1 << 6); 41 const field_t pacman = (1 << 6);
42 const field_t empty = (1 << 7); // explicit empty for update
42 43
43 const field_t direction_none = 0; 44 const field_t direction_none = 0;
44 const field_t direction_left = (1 << 7); 45 const field_t direction_left = (1 << 8);
45 const field_t direction_right = (1 << 8); 46 const field_t direction_right = (1 << 9);
46 const field_t direction_up = (1 << 9); 47 const field_t direction_up = (1 << 10);
47 const field_t direction_down = (1 << 10); 48 const field_t direction_down = (1 << 11);
48 49
49 const mask_t color_mask = Color::none | Color::red | Color::blue | Color::green; 50 const mask_t color_mask = Color::none | Color::red | Color::blue | Color::green;
50 const mask_t type_mask = block | bonuspoint; 51 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)
12 : SceneHolder(parent), m_currentKey(0), m_running(false) 12 : SceneHolder(parent), m_currentKey(0), m_running(false)
13{ 13{
14 createGui(); 14 createGui();
15 updateMap(createDummyMap()); 15 updateMap(Util::createDummyMap());
16 16
17 //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame())); 17 //connect(AudioPlayer::self(), SIGNAL(finished()), this, SLOT(startGame()));
18 //AudioPlayer::self()->play(AudioPlayer::Intro); 18 //AudioPlayer::self()->play(AudioPlayer::Intro);
@@ -123,24 +123,7 @@ Transmission::field_t MainWidget::translateKey(int key)
123 123
124void MainWidget::tick() 124void MainWidget::tick()
125{ 125{
126 Actor::Movement mov = Actor::None; 126 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);
127 switch(m_currentKey)
128 {
129 case Transmission::direction_up:
130 mov = Actor::Up;
131 break;
132 case Transmission::direction_down:
133 mov = Actor::Down;
134 break;
135 case Transmission::direction_left:
136 mov = Actor::Left;
137 break;
138 case Transmission::direction_right:
139 mov = Actor::Right;
140 break;
141 default:
142 break;
143 }
144 127
145 QMapIterator<Color::Color, Actor*> i(m_actors); 128 QMapIterator<Color::Color, Actor*> i(m_actors);
146 while (i.hasNext()) 129 while (i.hasNext())
@@ -169,24 +152,7 @@ void MainWidget::keyPressEvent(QKeyEvent* event)
169 return; 152 return;
170 153
171 // test stuff 154 // test stuff
172 Actor::Movement mov = Actor::None; 155 Actor::Movement mov = Util::transmissionMovementToActor(m_currentKey, Actor::None);
173 switch(m_currentKey)
174 {
175 case Transmission::direction_up:
176 mov = Actor::Up;
177 break;
178 case Transmission::direction_down:
179 mov = Actor::Down;
180 break;
181 case Transmission::direction_left:
182 mov = Actor::Left;
183 break;
184 case Transmission::direction_right:
185 mov = Actor::Right;
186 break;
187 default:
188 break;
189 }
190 156
191 QMapIterator<Color::Color, Actor*> i(m_actors); 157 QMapIterator<Color::Color, Actor*> i(m_actors);
192 while (i.hasNext()) 158 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 @@
6#include "actor.h" 6#include "actor.h"
7#include "bonuspoint.h" 7#include "bonuspoint.h"
8#include "point.h" 8#include "point.h"
9#include "util.h"
9 10
10SceneHolder::SceneHolder(QWidget* parent): QWidget(parent) 11SceneHolder::SceneHolder(QWidget* parent): QWidget(parent)
11{ 12{
@@ -66,27 +67,8 @@ void SceneHolder::updateMap(const Transmission::map_t& map)
66 actor->setPos(mapPositionToCoord(x, y)); 67 actor->setPos(mapPositionToCoord(x, y));
67 } 68 }
68 69
69 Actor::Movement direction = Actor::None; 70 Actor::Movement direction =
70 switch (cur & Transmission::direction_mask) 71 Util::transmissionMovementToActor(cur & Transmission::direction_mask);
71 {
72 case Transmission::direction_none:
73 direction = Actor::None;
74 break;
75 case Transmission::direction_left:
76 direction = Actor::Left;
77 break;
78 case Transmission::direction_right:
79 direction = Actor::Right;
80 break;
81 case Transmission::direction_up:
82 direction = Actor::Up;
83 break;
84 case Transmission::direction_down:
85 direction = Actor::Down;
86 break;
87 default:
88 Q_ASSERT(false);
89 }
90 //actor->move(direction, mapPositionToCoord(x, y)); 72 //actor->move(direction, mapPositionToCoord(x, y));
91 } 73 }
92 else 74 else
@@ -112,4 +94,11 @@ void SceneHolder::updateMap(const Transmission::map_t& map)
112QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y) 94QPoint SceneHolder::mapPositionToCoord(unsigned int x, unsigned int y)
113{ 95{
114 return QPoint(x * Constants::field_size.width, y * Constants::field_size.height); 96 return QPoint(x * Constants::field_size.width, y * Constants::field_size.height);
115} \ No newline at end of file 97}
98
99QPoint SceneHolder::CoordToMapPosition(unsigned int x, unsigned int y)
100{
101 return QPoint(x / Constants::field_size.width, y / Constants::field_size.height);
102}
103
104
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:
23 23
24 // data conversion 24 // data conversion
25 QPoint mapPositionToCoord(unsigned int x, unsigned int y); 25 QPoint mapPositionToCoord(unsigned int x, unsigned int y);
26 QPoint CoordToMapPosition(unsigned int x, unsigned int y);
26 27
27 // map of all pixmap instances 28 // map of all pixmap instances
28 QVector< QVector<PixmapItem *> > visualMap; 29 QVector< QVector<PixmapItem *> > 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 @@
5Server::Server(QWidget *parent) 5Server::Server(QWidget *parent)
6 : SceneHolder(parent) 6 : SceneHolder(parent)
7{ 7{
8 updateMap(createDummyMap()); 8 updateMap(Util::createDummyMap());
9 Transmission::map_t map = calculateUpdates();
10 updateMap(map);
11}
12
13QMap< Color::Color, Actor::Movement > Server::getActorDirections()
14{
15 QMap<Color::Color, Actor::Movement> directions;
16 directions[Color::red] = Actor::Down;
17 return directions;
18}
19
20
21Transmission::map_t Server::calculateUpdates()
22{
23 Transmission::map_t map = Util::createEmptyMap();
24
25 QMap<Color::Color, Actor::Movement> directions = getActorDirections();
26 QMapIterator<Color::Color, Actor::Movement> i(directions);
27 while (i.hasNext()) {
28 i.next();
29 Actor *actor = m_actors.value( i.key() );
30 QPoint mapPosition = CoordToMapPosition(actor->x(), actor->y());
31 qDebug() << "actor " << i.key() << " is at " << mapPosition;
32 map[mapPosition.x()][mapPosition.y()] = Transmission::empty;
33
34 QPoint newMapPosition = mapPosition;
35 switch (i.value()) {
36 case Actor::Up: newMapPosition += QPoint(0, -1); break;
37 case Actor::Down: newMapPosition += QPoint(0, 1); break;
38 case Actor::Left: newMapPosition += QPoint(-1, 0); break;
39 case Actor::Right: newMapPosition += QPoint(1, 0); break;
40 case Actor::None: break;
41 default: Q_ASSERT(false);
42 }
43 map[newMapPosition.x()][newMapPosition.y()] = Transmission::pacman | i.key() ;
44
45 }
46 return map;
9} 47}
10 48
11int main(int argc, char ** argv) 49int 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 @@
2#define SERVER_H 2#define SERVER_H
3 3
4#include "sceneholder.h" 4#include "sceneholder.h"
5
5#include <QtGui> 6#include <QtGui>
7#include "actor.h"
6 8
7class Server 9class Server
8 : public SceneHolder 10 : public SceneHolder
@@ -10,6 +12,14 @@ class Server
10 Q_OBJECT 12 Q_OBJECT
11public: 13public:
12 Server(QWidget *parent = 0); 14 Server(QWidget *parent = 0);
15
16protected:
17 // returns packets read from network until now
18 // for clients not sending a direction, no change is assumed
19 QMap<Color::Color, Actor::Movement> getActorDirections();
20
21 // calculate updates of current tick for sending to client
22 Transmission::map_t calculateUpdates();
13}; 23};
14 24
15#endif // SERVER_H 25#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 @@
1#include "util.h" 1#include "util.h"
2 2
3// temporary 3namespace Util {
4Transmission::map_t createDummyMap()
5{
6 Transmission::map_t map;
7 map = new Transmission::field_t*[Constants::map_size.width];
8 for (unsigned int i = 0; i < Constants::map_size.width; ++i)
9 map[i] = new Transmission::field_t[Constants::map_size.height];
10 4
11 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 5 Transmission::map_t createEmptyMap() {
12 { 6 Transmission::map_t map;
13 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 7 map = new Transmission::field_t*[Constants::map_size.width];
8 for (unsigned int i = 0; i < Constants::map_size.width; ++i)
9 map[i] = new Transmission::field_t[Constants::map_size.height];
10
11 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
14 { 12 {
15 Transmission::field_t &cur = map[x][y]; 13 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
16 cur = Transmission::none; 14 {
15 Transmission::field_t &cur = map[x][y];
16 cur = Transmission::none;
17 }
17 } 18 }
19 return map;
18 } 20 }
21 // temporary
22 Transmission::map_t createDummyMap()
23 {
24 Transmission::map_t map = createEmptyMap();
19 25
26 const char *tmpl[] = {
27 " # # ",
28 " #### ###### # #### # # ###### ### ",
29 " # # ",
30 " # ##### # # # # # ### # # # ",
31 " # # # # # # # # # # ## # # ",
32 " # # # # # # # # ### # # # # ",
33 " # # # # # # # # # # # # ## # ",
34 " # # ### ##### # ### # # # ",
35 " ### # ",
36 " # # ### #### #### #### ##### ",
37 " #### # #..# #..# #..# # # ",
38 " # # ### #..# #..# #### # # # # ",
39 " # # # #..# #..# # # ",
40 " # #### # #### #### # # ##### # ",
41 " # # ",
42 " #### ###### # ##### # ####### ### ",
43 " # # "
44 };
20 45
21 const char *tmpl[] = { 46 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
22 " # # ", 47 {
23 " #### ###### # #### # # ###### ### ", 48 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
24 " # # ", 49 {
25 " # ##### # # # # # ### # # # ", 50 Transmission::field_t &cur = map[x][y];
26 " # # # # # # # # # # ## # # ", 51 cur = Transmission::none;
27 " # # # # # # # # ### # # # # ", 52 if (tmpl[y][x] == '#')
28 " # # # # # # # # # # # # ## # ", 53 cur |= Color::none | Transmission::block;
29 " # # ### ##### # ### # # # ", 54 /* this is a simple hack to create areas where no
30 " ### # ", 55 * autoplaced points will be placed (see below)
31 " # # ### #### #### #### ##### ", 56 */
32 " #### # #..# #..# #..# # # ", 57 else if (tmpl[y][x] == '.')
33 " # # ### #..# #..# #### # # # # ", 58 cur |= Transmission::point;
34 " # # # #..# #..# # # ", 59 }
35 " # #### # #### #### # # ##### # ", 60 }
36 " # # ",
37 " #### ###### # ##### # ####### ### ",
38 " # # "
39 };
40 61
41 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 62 map[0][0] |= Transmission::bonuspoint;
42 { 63 map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right;
43 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 64 //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up;
65 //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down;
66
67 /* auto place normal points*/
68 for (unsigned int x = 0; x < Constants::map_size.width; ++x)
44 { 69 {
45 Transmission::field_t &cur = map[x][y]; 70 for (unsigned int y = 0; y < Constants::map_size.height; ++y)
46 cur = Transmission::none; 71 {
47 if (tmpl[y][x] == '#') 72 Transmission::field_t &cur = map[x][y];
48 cur |= Color::none | Transmission::block; 73 if (cur == Transmission::none)
49 /* this is a simple hack to create areas where no 74 cur |= Transmission::point;
50 * autoplaced points will be placed (see below) 75 else if (cur == Transmission::point)
51 */ 76 cur = Transmission::none;
52 else if (tmpl[y][x] == '.') 77 }
53 cur |= Transmission::point;
54 } 78 }
79
80 return map;
55 } 81 }
56 82
57 map[0][0] |= Transmission::bonuspoint;
58 map[1][0] |= Color::red | Transmission::pacman | Transmission::direction_right;
59 //map[2][0] |= Color::blue | Transmission::pacman | Transmission::direction_up;
60 //map[3][0] |= Color::green | Transmission::pacman | Transmission::direction_down;
61 83
62 /* auto place normal points*/ 84 Transmission::field_t actorMovementToTransmission(Actor::Movement mov, Transmission::field_t def) {
63 for (unsigned int x = 0; x < Constants::map_size.width; ++x) 85 switch (mov) {
64 { 86 case Actor::None:
65 for (unsigned int y = 0; y < Constants::map_size.height; ++y) 87 return Transmission::direction_none;
66 { 88 break;
67 Transmission::field_t &cur = map[x][y]; 89 case Actor::Left:
68 if (cur == Transmission::none) 90 return Transmission::direction_left;
69 cur |= Transmission::point; 91 break;
70 else if (cur == Transmission::point) 92 case Actor::Right:
71 cur = Transmission::none; 93 return Transmission::direction_right;
94 break;
95 case Actor::Up:
96 return Transmission::direction_up;
97 break;
98 case Actor::Down:
99 return Transmission::direction_down;
100 break;
101 default:
102 if (def == static_cast<Transmission::field_t>(-1)) {
103 Q_ASSERT(false);
104 } else {
105 return def;
106 }
72 } 107 }
108 return 0; // for pleasing the compiler
73 } 109 }
74 110
75 return map; 111 Actor::Movement transmissionMovementToActor(Transmission::field_t field, Actor::Movement def) {
112 switch (field) {
113 case Transmission::direction_none:
114 return Actor::None;
115 break;
116 case Transmission::direction_left:
117 return Actor::Left;
118 break;
119 case Transmission::direction_right:
120 return Actor::Right;
121 break;
122 case Transmission::direction_up:
123 return Actor::Up;
124 break;
125 case Transmission::direction_down:
126 return Actor::Down;
127 break;
128 default:
129 if (def == Actor::Movement(-1)) {
130 Q_ASSERT(false);
131 } else {
132 return def;
133 }
134 }
135 return Actor::None; // for pleasing the compiler
136 }
76} 137}
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 @@
2#define UTIL_H 2#define UTIL_H
3 3
4#include "constants.h" 4#include "constants.h"
5// temporary 5#include "actor.h"
6Transmission::map_t createDummyMap();
7 6
8#endif // UTIL_H \ No newline at end of file 7namespace Util {
8 Transmission::map_t createDummyMap();
9
10 Transmission::map_t createEmptyMap();
11
12 // default is to assert false with -1
13 Transmission::field_t actorMovementToTransmission(Actor::Movement mov,
14 Transmission::field_t def = -1);
15 Actor::Movement transmissionMovementToActor(Transmission::field_t field,
16 Actor::Movement def = Actor::Movement(-1));
17}
18#endif // UTIL_H