src/proto2/GameState.hh
branchno-netsession
changeset 35 e21cfda0edde
parent 26 5685602aeb9c
child 41 ca80cd67785d
equal deleted inserted replaced
34:1ea6554d703e 35:e21cfda0edde
     1 #ifndef GAMESTATE_HH
     1 #ifndef GAMESTATE_HH
     2 #define GAMESTATE_HH
     2 #define GAMESTATE_HH
     3 
     3 
     4 #include "Dimension.hh"
     4 #include "Physics.hh"
       
     5 #include "Input.hh"
     5 
     6 
     6 #include <list>
     7 #include <list>
     7 #include <stdexcept>
     8 #include <stdexcept>
     8 
     9 
     9 enum PlayerType {
    10 // in meters/kg
    10     PLAYER_LOCAL,
    11 const float MAP_WIDTH = 100.0;
    11     PLAYER_REMOTE
    12 const float MAP_HEIGHT = 100.0;
    12 };
    13 const float MAP_GRAVITY = 9.81;
    13 
    14 const float PLAYER_MASS = 10.0;
    14 #define PLAYER_DIM_W 10
    15 const float PLAYER_MOVE_FORCE = 500.0;
    15 #define PLAYER_DIM_H 10
    16 const float PLAYER_INITIAL_X = 50.0;
    16 #define MAP_DIM_W 800
    17 const float PLAYER_INITIAL_Y = 40.0;
    17 #define MAP_DIM_H 640
       
    18 
    18 
    19 // forward-declare GameState
    19 // forward-declare GameState
    20 class GameState;
    20 class GameState;
    21 
    21 
    22 class Player {
    22 class Player : public PhysicsObject {
    23     protected:
    23     protected:
    24         Coordinate position;
       
    25 
       
    26         GameState &state;
    24         GameState &state;
       
    25         bool visible;
    27 
    26 
    28     public:
    27     public:
    29 
    28 
    30         Player(GameState &state, Coordinate c, bool visible) : position(c), state(state), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
    29         Player(GameState &state, Vector position, bool visible) : 
    31 
    30             PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) { }
    32         PlayerType type;
       
    33         Dimension dimensions;
       
    34         bool visible;
       
    35 
       
    36         Coordinate getPosition (void) const {
       
    37             return position;
       
    38         }
       
    39     
       
    40     protected:
       
    41         /*
       
    42          * Update position to the given value.
       
    43          *
       
    44          * Returns true if valid move (not out of bounds), false otherwise (doesn't change position)
       
    45          */
       
    46         bool updatePosition (Coordinate p);
       
    47 
    31 
    48 };
    32 };
    49 
    33 
    50 class LocalPlayer : public Player {
    34 class LocalPlayer : public Player {
    51     protected:
    35     protected:
    52         LocalPlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
    36         LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
    53     
    37     
    54     public:
    38     public:
    55         virtual bool move (PositionDelta d) {
    39         virtual void handleMove (PlayerInput_Move input);
    56             return updatePosition(position + d);
       
    57         }
       
    58 };
    40 };
    59 
    41 
    60 class RemotePlayer : public Player {
    42 class RemotePlayer : public Player {
    61     protected:
    43     protected:
    62         RemotePlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
    44         RemotePlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
    63 
       
    64 };
    45 };
    65 
    46 
    66 class GameState {
    47 class GameState : public PhysicsWorld {
    67     public:
    48     public:
    68         Dimension map_dimensions;
       
    69         std::list<Player*> player_list;
    49         std::list<Player*> player_list;
    70 
    50 
    71         // only one local player is supported
    51         // only one local player is supported
    72         LocalPlayer *local_player;
    52         LocalPlayer *local_player;
    73 
    53 
    74         GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {
    54         GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {
    75 
    55 
    76         }
    56         }
    77 
    57        
    78         /*
       
    79          * Check if the given coordinate is valid
       
    80          */
       
    81         bool isValidCoordinate (const Coordinate &p) {
       
    82             // unsigned...
       
    83             return !(p.x > map_dimensions.w || p.y > map_dimensions.h);
       
    84         }
       
    85         
       
    86         /*
    58         /*
    87          * This will return NULL if we don't have a local player - yet
    59          * This will return NULL if we don't have a local player - yet
    88          */
    60          */
    89         LocalPlayer *getLocalPlayer (void) {
    61         LocalPlayer *getLocalPlayer (void) {
    90             return local_player;
    62             return local_player;