src/proto2/GameState.hh
author terom
Sat, 08 Nov 2008 20:34:14 +0000
changeset 22 b70d30e1b0fe
parent 8 2de58a6d0395
child 24 b81cb670e6b2
permissions -rw-r--r--
all the network code is now there, although it doesn't quite work
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
     1
#ifndef GAMESTATE_HH
5a209a8585c9 proto p2
terom
parents:
diff changeset
     2
#define GAMESTATE_HH
5a209a8585c9 proto p2
terom
parents:
diff changeset
     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
617813994ab1 move proto/p2 -> src/proto2
terom
parents: 4
diff changeset
     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
617813994ab1 move proto/p2 -> src/proto2
terom
parents: 4
diff changeset
     8
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
     9
enum PlayerType {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    10
	PLAYER_LOCAL,
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    11
	PLAYER_REMOTE
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    12
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    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
5a209a8585c9 proto p2
terom
parents:
diff changeset
    19
class Player {
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    20
	protected:
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    21
		Coordinate position;
5a209a8585c9 proto p2
terom
parents:
diff changeset
    22
5a209a8585c9 proto p2
terom
parents:
diff changeset
    23
	public:
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
    24
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    25
		Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
    26
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    27
		PlayerType type;
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    28
		Dimension dimensions;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    29
		bool visible;
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    30
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
    31
		Coordinate getPosition (void) const {
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    32
			return this->position;
5a209a8585c9 proto p2
terom
parents:
diff changeset
    33
		}
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    34
	
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    35
	protected:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    36
		void updatePosition (Coordinate p) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    37
			this->position = p;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    38
		}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    39
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    40
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    41
5a209a8585c9 proto p2
terom
parents:
diff changeset
    42
class LocalPlayer : public Player {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    43
	protected:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    44
		LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { }
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    45
	
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    46
	public:
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    47
		virtual void move (PositionDelta d) {
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    48
			this->position += d;
5a209a8585c9 proto p2
terom
parents:
diff changeset
    49
		}
5a209a8585c9 proto p2
terom
parents:
diff changeset
    50
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    51
5a209a8585c9 proto p2
terom
parents:
diff changeset
    52
class RemotePlayer : public Player {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    53
	protected:
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    54
		RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { }
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    55
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    56
};
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    57
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    58
class GameState {
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    59
	public:
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    60
		Dimension map_dimensions;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    61
		std::list<Player*> player_list;
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    62
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    63
		// only one local player is supported
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    64
		LocalPlayer *local_player;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    65
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    66
		GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    67
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    68
		}
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    69
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    70
		LocalPlayer &getLocalPlayer (void) {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    71
			if (!local_player)
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    72
				throw std::logic_error("getLocalPlayer called with no local player");
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    73
			
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    74
			return *local_player;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    75
		}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    76
		
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    77
		void newLocalPlayer (LocalPlayer *player) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    78
			if (local_player)
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    79
				throw std::logic_error("newLocalPlayer called even though we already have a local player");
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    80
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    81
			player_list.push_back(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    82
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    83
			local_player = player;
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    84
		}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    85
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    86
		void newRemotePlayer (RemotePlayer *player) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    87
			player_list.push_back(player);
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    88
		}
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    89
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    90
		void removePlayer (Player *player) {
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    91
			player_list.remove(player);
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    92
		}
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    93
};
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    94
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    95
#endif