src/GameState.hh
changeset 185 25becd2cb026
child 190 12fea0d2d0df
equal deleted inserted replaced
184:561892e2a30e 185:25becd2cb026
       
     1 #ifndef GAMESTATE_HH
       
     2 #define GAMESTATE_HH
       
     3 
       
     4 #include "Physics.hh"
       
     5 #include "Input.hh"
       
     6 #include "Config.hh"
       
     7 
       
     8 #include <list>
       
     9 #include <stdexcept>
       
    10 #include <cmath>
       
    11 
       
    12 // forward-declare GameState
       
    13 class GameState;
       
    14 
       
    15 class Player : public PhysicsObject {
       
    16 protected:
       
    17     GameState &state;
       
    18     bool visible;
       
    19     
       
    20 public:
       
    21     Player(GameState &state, Vector position, bool visible) : 
       
    22         PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, Vector(0, 0)), state(state), visible(visible) {
       
    23             
       
    24         std::vector<Vector> shape(4);
       
    25         shape[0] = Vector(0,-9);
       
    26         shape[1] = Vector(6,0);
       
    27         shape[2] = Vector(0,9); 
       
    28         shape[3] = Vector(-6,0);
       
    29         // Initialize the shape of the player (salmiakki shape)
       
    30         setShape(shape);
       
    31         collision_elasticity = PLAYER_COLLISION_ELASTICITY;
       
    32         world.addPlayerObject(this);
       
    33     }
       
    34     
       
    35     void debugInfo ();
       
    36     virtual void handleMove (PlayerInput_Move input);
       
    37     void shoot (void);
       
    38 
       
    39 };
       
    40 
       
    41 class Shot : public PhysicsObject {
       
    42 protected:
       
    43     GameState &state;
       
    44     bool visible;
       
    45     uint32_t birth_tick;
       
    46     uint32_t death_tick;
       
    47     bool target_visible;
       
    48 public:
       
    49     Shot(GameState &state, Vector position, Vector velocity, bool visible) :
       
    50         PhysicsObject((PhysicsWorld &) state, PLAYER_MASS, position, velocity), state(state), visible(visible) {
       
    51         // Looks kind of dumb, for ammunition to have shape
       
    52         std::vector<Vector> shape(4);
       
    53         shape[0] = Vector(-1, -1);
       
    54         shape[1] = Vector(-1, 1);
       
    55         shape[2] = Vector(1, 1);
       
    56         shape[3] = Vector(1, -1);
       
    57         setShape(shape);
       
    58         target_visible = false;
       
    59         collision_elasticity = 0.9; // = shotType.elasticity
       
    60         world.addProjectile(this);
       
    61     }
       
    62 private:
       
    63     virtual void onCollision();
       
    64     virtual void draw(CL_GraphicContext *gc);
       
    65 };
       
    66 
       
    67 class LocalPlayer : public Player {
       
    68 protected:
       
    69     LocalPlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
       
    70 };
       
    71 
       
    72 class RemotePlayer : public Player {
       
    73 protected:
       
    74     RemotePlayer (GameState &state, Vector pos, bool visible) : Player(state, pos, visible) { }
       
    75 };
       
    76 
       
    77 class GameState : public PhysicsWorld {
       
    78 public:
       
    79     std::list<Player*> player_list;
       
    80 
       
    81     // only one local player is supported
       
    82     LocalPlayer *local_player;
       
    83 
       
    84     GameState (void) : PhysicsWorld(Vector(0, MAP_GRAVITY), Vector(MAP_WIDTH, MAP_HEIGHT)), local_player(NULL) {
       
    85 
       
    86     }
       
    87        
       
    88     /*
       
    89      * This will return NULL if we don't have a local player - yet
       
    90      */
       
    91     LocalPlayer *getLocalPlayer (void) {
       
    92         return local_player;
       
    93     }
       
    94         
       
    95     void newLocalPlayer (LocalPlayer *player) {
       
    96         if (local_player)
       
    97             throw std::logic_error("newLocalPlayer called even though we already have a local player");
       
    98 
       
    99         player_list.push_back(player);
       
   100 
       
   101         local_player = player;
       
   102     }
       
   103 
       
   104     void newRemotePlayer (RemotePlayer *player) {
       
   105         player_list.push_back(player);
       
   106     }
       
   107 
       
   108     void removePlayer (Player *player) {
       
   109         player_list.remove(player);
       
   110     }
       
   111 };
       
   112 
       
   113 #endif