author | terom |
Sun, 09 Nov 2008 21:51:13 +0000 | |
changeset 25 | af75a1894a32 |
parent 24 | b81cb670e6b2 |
child 26 | 5685602aeb9c |
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 |
|
3 | 19 |
class Player { |
24 | 20 |
protected: |
21 |
Coordinate position; |
|
4
e28b28b8817c
Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents:
3
diff
changeset
|
22 |
|
24 | 23 |
public: |
3 | 24 |
|
24 | 25 |
Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {} |
26 |
||
27 |
PlayerType type; |
|
28 |
Dimension dimensions; |
|
29 |
bool visible; |
|
30 |
||
31 |
Coordinate getPosition (void) const { |
|
25 | 32 |
return position; |
24 | 33 |
} |
34 |
||
35 |
protected: |
|
25 | 36 |
/* |
37 |
* Update position to the given value. |
|
38 |
* |
|
39 |
* Returns true if valid move (not out of bounds), false otherwise (doesn't change position) |
|
40 |
*/ |
|
41 |
bool updatePosition (Coordinate p) { |
|
42 |
// unsigned... |
|
43 |
if (p.x > dimensions.w || p.y > dimensions.h) { |
|
44 |
// out-of-bounds |
|
45 |
return false; |
|
46 |
} |
|
47 |
||
48 |
// valid |
|
49 |
position = p; |
|
50 |
||
51 |
return true; |
|
24 | 52 |
} |
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
53 |
|
3 | 54 |
}; |
55 |
||
56 |
class LocalPlayer : public Player { |
|
24 | 57 |
protected: |
58 |
LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { } |
|
59 |
||
60 |
public: |
|
25 | 61 |
virtual bool move (PositionDelta d) { |
62 |
return true; |
|
63 |
||
64 |
//return updatePosition(position + d); |
|
24 | 65 |
} |
3 | 66 |
}; |
67 |
||
68 |
class RemotePlayer : public Player { |
|
24 | 69 |
protected: |
70 |
RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { } |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
71 |
|
6 | 72 |
}; |
73 |
||
74 |
class GameState { |
|
24 | 75 |
public: |
76 |
Dimension map_dimensions; |
|
77 |
std::list<Player*> player_list; |
|
8 | 78 |
|
24 | 79 |
// only one local player is supported |
80 |
LocalPlayer *local_player; |
|
22
b70d30e1b0fe
all the network code is now there, although it doesn't quite work
terom
parents:
8
diff
changeset
|
81 |
|
24 | 82 |
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
|
83 |
|
24 | 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 |