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