src/proto2/GameState.hh
author terom
Mon, 10 Nov 2008 16:49:09 +0000
branchno-netsession
changeset 31 d0d7489d4e8b
parent 26 5685602aeb9c
child 35 e21cfda0edde
child 42 eb1a93a38cde
permissions -rw-r--r--
add initial code written so far
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
     1
#ifndef GAMESTATE_HH
5a209a8585c9 proto p2
terom
parents:
diff changeset
     2
#define GAMESTATE_HH
5a209a8585c9 proto p2
terom
parents:
diff changeset
     3
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
     4
#include "Dimension.hh"
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
     5
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents: 4
diff changeset
     6
#include <list>
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
     7
#include <stdexcept>
5
617813994ab1 move proto/p2 -> src/proto2
terom
parents: 4
diff changeset
     8
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
     9
enum PlayerType {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    10
    PLAYER_LOCAL,
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    11
    PLAYER_REMOTE
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    12
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    13
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    14
#define PLAYER_DIM_W 10
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    15
#define PLAYER_DIM_H 10
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    16
#define MAP_DIM_W 800
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    17
#define MAP_DIM_H 640
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    18
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    19
// forward-declare GameState
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    20
class GameState;
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    21
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    22
class Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    23
    protected:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    24
        Coordinate position;
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
    25
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    26
        GameState &state;
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    27
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    28
    public:
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    29
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    30
        Player(GameState &state, Coordinate c, bool visible) : position(c), state(state), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    31
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    32
        PlayerType type;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    33
        Dimension dimensions;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    34
        bool visible;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    35
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    36
        Coordinate getPosition (void) const {
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    37
            return position;
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    38
        }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    39
    
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    40
    protected:
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    41
        /*
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    42
         * Update position to the given value.
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    43
         *
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    44
         * Returns true if valid move (not out of bounds), false otherwise (doesn't change position)
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    45
         */
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    46
        bool updatePosition (Coordinate p);
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    47
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    48
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    49
5a209a8585c9 proto p2
terom
parents:
diff changeset
    50
class LocalPlayer : public Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    51
    protected:
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    52
        LocalPlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    53
    
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    54
    public:
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    55
        virtual bool move (PositionDelta d) {
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    56
            return updatePosition(position + d);
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    57
        }
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    58
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    59
5a209a8585c9 proto p2
terom
parents:
diff changeset
    60
class RemotePlayer : public Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    61
    protected:
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    62
        RemotePlayer (GameState &state, Coordinate c, bool visible) : Player(state, c, visible) { }
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    63
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    64
};
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    65
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    66
class GameState {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    67
    public:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    68
        Dimension map_dimensions;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    69
        std::list<Player*> player_list;
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    70
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    71
        // only one local player is supported
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    72
        LocalPlayer *local_player;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    73
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    74
        GameState (void) : map_dimensions(MAP_DIM_W, MAP_DIM_H), local_player(NULL) {
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    75
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    76
        }
26
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    77
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    78
        /*
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    79
         * Check if the given coordinate is valid
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    80
         */
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    81
        bool isValidCoordinate (const Coordinate &p) {
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    82
            // unsigned...
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    83
            return !(p.x > map_dimensions.w || p.y > map_dimensions.h);
5685602aeb9c it works \o/
terom
parents: 25
diff changeset
    84
        }
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    85
        
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    86
        /*
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    87
         * This will return NULL if we don't have a local player - yet
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    88
         */
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    89
        LocalPlayer *getLocalPlayer (void) {
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    90
            return local_player;
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    91
        }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    92
        
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    93
        void newLocalPlayer (LocalPlayer *player) {
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    94
            if (local_player)
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    95
                throw std::logic_error("newLocalPlayer called even though we already have a local player");
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    96
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    97
            player_list.push_back(player);
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    98
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    99
            local_player = player;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   100
        }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   101
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   102
        void newRemotePlayer (RemotePlayer *player) {
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   103
            player_list.push_back(player);
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   104
        }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   105
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   106
        void removePlayer (Player *player) {
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   107
            player_list.remove(player);
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
   108
        }
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
   109
};
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
   110
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
   111
#endif