src/proto2/GameState.hh
author terom
Mon, 24 Nov 2008 17:14:29 +0000
changeset 96 4a801210096c
parent 94 08bebac3d0c2
child 108 1b93045a5b0a
permissions -rw-r--r--
fix movement physics+network code to some degree, jumping is now buggy?
#ifndef GAMESTATE_HH
#define GAMESTATE_HH

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

#include <list>
#include <stdexcept>

// in cells/kg
const uint16_t MAP_WIDTH = 800;
const uint16_t MAP_HEIGHT = 600;
const float MAP_GRAVITY = 1200.0;
const float PLAYER_MASS = 10.0;
const float PLAYER_MOVE_FORCE = 5000.0;
const float PLAYER_INITIAL_X = 400.0;
const float PLAYER_INITIAL_Y = 300.0;

// 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,-18);
			shape[1] = Vector(6,-9);
			shape[2] = Vector(0,0); 
			shape[3] = Vector(-6,-9);
			// Initialize the shape of the player (salmiakki shape)
			setShape(shape);		
		}

		void debugInfo ();
    
    public:
        virtual void handleMove (PlayerInput_Move input);
};

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