5 |
5 |
6 #include <list> |
6 #include <list> |
7 #include <stdexcept> |
7 #include <stdexcept> |
8 |
8 |
9 enum PlayerType { |
9 enum PlayerType { |
10 PLAYER_LOCAL, |
10 PLAYER_LOCAL, |
11 PLAYER_REMOTE |
11 PLAYER_REMOTE |
12 }; |
12 }; |
13 |
13 |
14 #define PLAYER_DIM_W 10 |
14 #define PLAYER_DIM_W 10 |
15 #define PLAYER_DIM_H 10 |
15 #define PLAYER_DIM_H 10 |
16 #define MAP_DIM_W 800 |
16 #define MAP_DIM_W 800 |
17 #define MAP_DIM_H 640 |
17 #define MAP_DIM_H 640 |
18 |
18 |
19 class Player { |
19 class Player { |
20 protected: |
20 protected: |
21 Coordinate position; |
21 Coordinate position; |
22 |
22 |
23 public: |
23 public: |
24 |
24 |
25 Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {} |
25 Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {} |
26 |
26 |
27 PlayerType type; |
27 PlayerType type; |
28 Dimension dimensions; |
28 Dimension dimensions; |
29 bool visible; |
29 bool visible; |
30 |
30 |
31 Coordinate getPosition (void) const { |
31 Coordinate getPosition (void) const { |
32 return this->position; |
32 return this->position; |
33 } |
33 } |
34 |
34 |
35 protected: |
35 protected: |
36 void updatePosition (Coordinate p) { |
36 void updatePosition (Coordinate p) { |
37 this->position = p; |
37 this->position = p; |
38 } |
38 } |
39 |
39 |
40 }; |
40 }; |
41 |
41 |
42 class LocalPlayer : public Player { |
42 class LocalPlayer : public Player { |
43 protected: |
43 protected: |
44 LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { } |
44 LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { } |
45 |
45 |
46 public: |
46 public: |
47 virtual void move (PositionDelta d) { |
47 virtual void move (PositionDelta d) { |
48 this->position += d; |
48 this->position += d; |
49 } |
49 } |
50 }; |
50 }; |
51 |
51 |
52 class RemotePlayer : public Player { |
52 class RemotePlayer : public Player { |
53 protected: |
53 protected: |
54 RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { } |
54 RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { } |
55 |
55 |
56 }; |
56 }; |
57 |
57 |
58 class GameState { |
58 class GameState { |
59 public: |
59 public: |
60 Dimension map_dimensions; |
60 Dimension map_dimensions; |
61 std::list<Player*> player_list; |
61 std::list<Player*> player_list; |
62 |
62 |
63 // only one local player is supported |
63 // only one local player is supported |
64 LocalPlayer *local_player; |
64 LocalPlayer *local_player; |
65 |
65 |
66 GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) { |
66 GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) { |
67 |
67 |
68 } |
68 } |
69 |
69 |
70 LocalPlayer &getLocalPlayer (void) { |
70 LocalPlayer &getLocalPlayer (void) { |
71 if (!local_player) |
71 if (!local_player) |
72 throw std::logic_error("getLocalPlayer called with no local player"); |
72 throw std::logic_error("getLocalPlayer called with no local player"); |
73 |
73 |
74 return *local_player; |
74 return *local_player; |
75 } |
75 } |
76 |
76 |
77 void newLocalPlayer (LocalPlayer *player) { |
77 void newLocalPlayer (LocalPlayer *player) { |
78 if (local_player) |
78 if (local_player) |
79 throw std::logic_error("newLocalPlayer called even though we already have a local player"); |
79 throw std::logic_error("newLocalPlayer called even though we already have a local player"); |
80 |
80 |
81 player_list.push_back(player); |
81 player_list.push_back(player); |
82 |
82 |
83 local_player = player; |
83 local_player = player; |
84 } |
84 } |
85 |
85 |
86 void newRemotePlayer (RemotePlayer *player) { |
86 void newRemotePlayer (RemotePlayer *player) { |
87 player_list.push_back(player); |
87 player_list.push_back(player); |
88 } |
88 } |
89 |
89 |
90 void removePlayer (Player *player) { |
90 void removePlayer (Player *player) { |
91 player_list.remove(player); |
91 player_list.remove(player); |
92 } |
92 } |
93 }; |
93 }; |
94 |
94 |
95 #endif |
95 #endif |