terom@3: #ifndef GAMESTATE_HH terom@3: #define GAMESTATE_HH terom@3: ekku@4: #include "Dimension.hh" ekku@4: terom@5: #include terom@22: #include terom@5: terom@3: enum PlayerType { terom@24: PLAYER_LOCAL, terom@24: PLAYER_REMOTE terom@3: }; terom@3: terom@22: #define PLAYER_DIM_W 10 terom@22: #define PLAYER_DIM_H 10 terom@22: #define MAP_DIM_W 800 terom@22: #define MAP_DIM_H 640 terom@22: terom@26: // forward-declare GameState terom@26: class GameState; terom@26: terom@3: class Player { terom@24: protected: terom@24: Coordinate position; ekku@4: terom@26: GameState &state; terom@26: terom@24: public: terom@3: terom@26: Player(GameState &state, Coordinate c, bool visible) : position(c), state(state), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {} terom@24: terom@24: PlayerType type; terom@24: Dimension dimensions; terom@24: bool visible; terom@24: terom@24: Coordinate getPosition (void) const { terom@25: return position; terom@24: } terom@24: terom@24: protected: terom@25: /* terom@25: * Update position to the given value. terom@25: * terom@25: * Returns true if valid move (not out of bounds), false otherwise (doesn't change position) terom@25: */ terom@26: bool updatePosition (Coordinate p); terom@22: terom@3: }; terom@3: terom@3: class LocalPlayer : public Player { terom@24: protected: terom@26: LocalPlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { } terom@24: terom@24: public: terom@25: virtual bool move (PositionDelta d) { terom@26: return updatePosition(position + d); terom@24: } terom@3: }; terom@3: terom@3: class RemotePlayer : public Player { terom@24: protected: terom@26: RemotePlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { } terom@22: terom@6: }; terom@6: terom@6: class GameState { terom@24: public: terom@24: Dimension map_dimensions; terom@24: std::list player_list; terom@8: terom@24: // only one local player is supported terom@24: LocalPlayer *local_player; terom@22: terom@24: GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) { terom@22: terom@24: } terom@26: terom@26: /* terom@26: * Check if the given coordinate is valid terom@26: */ terom@26: bool isValidCoordinate (const Coordinate &p) { terom@26: // unsigned... terom@26: return !(p.x > map_dimensions.w || p.y > map_dimensions.h); terom@26: } terom@25: terom@25: /* terom@25: * This will return NULL if we don't have a local player - yet terom@25: */ terom@25: LocalPlayer *getLocalPlayer (void) { terom@25: return local_player; terom@24: } terom@24: terom@24: void newLocalPlayer (LocalPlayer *player) { terom@24: if (local_player) terom@24: throw std::logic_error("newLocalPlayer called even though we already have a local player"); terom@24: terom@24: player_list.push_back(player); terom@24: terom@24: local_player = player; terom@24: } terom@24: terom@24: void newRemotePlayer (RemotePlayer *player) { terom@24: player_list.push_back(player); terom@24: } terom@24: terom@24: void removePlayer (Player *player) { terom@24: player_list.remove(player); terom@24: } terom@6: }; terom@6: terom@3: #endif