| author | terom |
| Mon, 10 Nov 2008 16:49:09 +0000 | |
| branch | no-netsession |
| changeset 31 | d0d7489d4e8b |
| parent 26 | 5685602aeb9c |
| child 35 | e21cfda0edde |
| child 42 | eb1a93a38cde |
| permissions | -rw-r--r-- |
| 3 | 1 |
#ifndef GAMESTATE_HH |
2 |
#define GAMESTATE_HH |
|
3 |
||
|
4
e28b28b8817c
Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents:
3
diff
changeset
|
4 |
#include "Dimension.hh" |
|
e28b28b8817c
Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents:
3
diff
changeset
|
5 |
|
| 5 | 6 |
#include <list> |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
7 |
#include <stdexcept> |
| 5 | 8 |
|
| 3 | 9 |
enum PlayerType {
|
| 24 | 10 |
PLAYER_LOCAL, |
11 |
PLAYER_REMOTE |
|
| 3 | 12 |
}; |
13 |
||
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
14 |
#define PLAYER_DIM_W 10 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
15 |
#define PLAYER_DIM_H 10 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
16 |
#define MAP_DIM_W 800 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
17 |
#define MAP_DIM_H 640 |
|
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
18 |
|
| 26 | 19 |
// forward-declare GameState |
20 |
class GameState; |
|
21 |
||
| 3 | 22 |
class Player {
|
| 24 | 23 |
protected: |
24 |
Coordinate position; |
|
|
4
e28b28b8817c
Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents:
3
diff
changeset
|
25 |
|
| 26 | 26 |
GameState &state; |
27 |
||
| 24 | 28 |
public: |
| 3 | 29 |
|
| 26 | 30 |
Player(GameState &state, Coordinate c, bool visible) : position(c), state(state), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
|
| 24 | 31 |
|
32 |
PlayerType type; |
|
33 |
Dimension dimensions; |
|
34 |
bool visible; |
|
35 |
||
36 |
Coordinate getPosition (void) const {
|
|
| 25 | 37 |
return position; |
| 24 | 38 |
} |
39 |
||
40 |
protected: |
|
| 25 | 41 |
/* |
42 |
* Update position to the given value. |
|
43 |
* |
|
44 |
* Returns true if valid move (not out of bounds), false otherwise (doesn't change position) |
|
45 |
*/ |
|
| 26 | 46 |
bool updatePosition (Coordinate p); |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
47 |
|
| 3 | 48 |
}; |
49 |
||
50 |
class LocalPlayer : public Player {
|
|
| 24 | 51 |
protected: |
| 26 | 52 |
LocalPlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
|
| 24 | 53 |
|
54 |
public: |
|
| 25 | 55 |
virtual bool move (PositionDelta d) {
|
| 26 | 56 |
return updatePosition(position + d); |
| 24 | 57 |
} |
| 3 | 58 |
}; |
59 |
||
60 |
class RemotePlayer : public Player {
|
|
| 24 | 61 |
protected: |
| 26 | 62 |
RemotePlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
63 |
|
| 6 | 64 |
}; |
65 |
||
66 |
class GameState {
|
|
| 24 | 67 |
public: |
68 |
Dimension map_dimensions; |
|
69 |
std::list<Player*> player_list; |
|
| 8 | 70 |
|
| 24 | 71 |
// only one local player is supported |
72 |
LocalPlayer *local_player; |
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
73 |
|
| 24 | 74 |
GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {
|
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
75 |
|
| 24 | 76 |
} |
| 26 | 77 |
|
78 |
/* |
|
79 |
* Check if the given coordinate is valid |
|
80 |
*/ |
|
81 |
bool isValidCoordinate (const Coordinate &p) {
|
|
82 |
// unsigned... |
|
83 |
return !(p.x > map_dimensions.w || p.y > map_dimensions.h); |
|
84 |
} |
|
| 25 | 85 |
|
86 |
/* |
|
87 |
* This will return NULL if we don't have a local player - yet |
|
88 |
*/ |
|
89 |
LocalPlayer *getLocalPlayer (void) {
|
|
90 |
return local_player; |
|
| 24 | 91 |
} |
92 |
||
93 |
void newLocalPlayer (LocalPlayer *player) {
|
|
94 |
if (local_player) |
|
95 |
throw std::logic_error("newLocalPlayer called even though we already have a local player");
|
|
96 |
||
97 |
player_list.push_back(player); |
|
98 |
||
99 |
local_player = player; |
|
100 |
} |
|
101 |
||
102 |
void newRemotePlayer (RemotePlayer *player) {
|
|
103 |
player_list.push_back(player); |
|
104 |
} |
|
105 |
||
106 |
void removePlayer (Player *player) {
|
|
107 |
player_list.remove(player); |
|
108 |
} |
|
| 6 | 109 |
}; |
110 |
||
| 3 | 111 |
#endif |