src/proto2/GameState.hh
author ekku
Wed, 03 Dec 2008 12:09:42 +0000
changeset 180 bfe1077edab3
parent 153 73402d5b778e
child 182 84675387ca74
permissions -rw-r--r--
Ammuksia f:lla
#ifndef GAMESTATE_HH
#define GAMESTATE_HH

#include "Physics.hh"
#include "Input.hh"
#include "Config.hh"

#include <list>
#include <stdexcept>
#include <cmath>

// forward-declare GameState
class GameState;

class Player : public PhysicsObject {
protected:
    GameState &state;
    bool visible;
    
public:
    Player(GameState &state, Vector position, bool visible) : 
        PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
            
        std::vector<Vector> shape(4);
        shape[0] = Vector(0,-9);
        shape[1] = Vector(6,0);
        shape[2] = Vector(0,9); 
        shape[3] = Vector(-6,0);
        // Initialize the shape of the player (salmiakki shape)
        setShape(shape);		
    }
    
    void debugInfo ();
    virtual void handleMove (PlayerInput_Move input);
    void shoot (void);

};

class Shot : public PhysicsObject {
protected:
    GameState &state;
    bool visible;
    uint32_t birth_tick;
    uint32_t death_tick;
public:
    Shot(GameState &state, Vector position, bool visible) :
        PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
        // Looks kind of dumb, for ammunition to have shape
        std::vector<Vector> shape(4);
        shape[0] = Vector(-1, -1);
        shape[1] = Vector(-1, 1);
        shape[2] = Vector(1, 1);
        shape[3] = Vector(1, -1);
        setShape(shape);
    }
private:
    virtual void onCollision();
};

class LocalPlayer : public Player {
protected:
    LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
};

class RemotePlayer : public Player {
protected:
    RemotePlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
};

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

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

    GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {

    }
       
    /*
     * This will return NULL if we don't have a local player - yet
     */
    LocalPlayer *getLocalPlayer (void) {
        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