src/proto2/GameState.hh
author terom
Sun, 09 Nov 2008 20:40:46 +0000
changeset 24 b81cb670e6b2
parent 22 b70d30e1b0fe
child 25 af75a1894a32
permissions -rw-r--r--
the great :retab
#ifndef GAMESTATE_HH
#define GAMESTATE_HH

#include "Dimension.hh"

#include <list>
#include <stdexcept>

enum PlayerType {
    PLAYER_LOCAL,
    PLAYER_REMOTE
};

#define PLAYER_DIM_W 10
#define PLAYER_DIM_H 10
#define MAP_DIM_W 800
#define MAP_DIM_H 640

class Player {
    protected:
        Coordinate position;

    public:

        Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}

        PlayerType type;
        Dimension dimensions;
        bool visible;

        Coordinate getPosition (void) const {
            return this->position;
        }
    
    protected:
        void updatePosition (Coordinate p) {
            this->position = p;
        }

};

class LocalPlayer : public Player {
    protected:
        LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { }
    
    public:
        virtual void move (PositionDelta d) {
            this->position += d;
        }
};

class RemotePlayer : public Player {
    protected:
        RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { }

};

class GameState {
    public:
        Dimension map_dimensions;
        std::list<Player*> player_list;

        // only one local player is supported
        LocalPlayer *local_player;

        GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {

        }

        LocalPlayer &getLocalPlayer (void) {
            if (!local_player)
                throw std::logic_error("getLocalPlayer called with no local player");
            
            return *local_player;
        }
        
        void newLocalPlayer (LocalPlayer *player) {
            if (local_player)
                throw std::logic_error("newLocalPlayer called even though we already have a local player");

            player_list.push_back(player);

            local_player = player;
        }

        void newRemotePlayer (RemotePlayer *player) {
            player_list.push_back(player);
        }

        void removePlayer (Player *player) {
            player_list.remove(player);
        }
};

#endif