src/proto2/GameState.hh
author terom
Sun, 09 Nov 2008 21:51:13 +0000
changeset 25 af75a1894a32
parent 24 b81cb670e6b2
child 26 5685602aeb9c
permissions -rw-r--r--
simple proto *almost* works
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
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    19
class Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    20
    protected:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    21
        Coordinate position;
4
e28b28b8817c Drawer lis?tty. Pari metodia gamestateen ja dimensioniin.
ekku
parents: 3
diff changeset
    22
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    23
    public:
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    24
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    25
        Player(Coordinate c, bool visible) : position(c), dimensions(PLAYER_DIM_W, PLAYER_DIM_H), visible(visible) {}
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    26
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    27
        PlayerType type;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    28
        Dimension dimensions;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    29
        bool visible;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    30
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    31
        Coordinate getPosition (void) const {
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    32
            return position;
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    33
        }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    34
    
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    35
    protected:
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    36
        /*
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    37
         * Update position to the given value.
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    38
         *
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    39
         * 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
    40
         */
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    41
        bool updatePosition (Coordinate p) {
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    42
            // unsigned...
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    43
            if (p.x > dimensions.w || p.y > dimensions.h) {
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    44
                // out-of-bounds
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    45
                return false;
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    46
            }
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    47
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    48
            // valid
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    49
            position = p;
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    50
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    51
            return true;
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    52
        }
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    53
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    54
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    55
5a209a8585c9 proto p2
terom
parents:
diff changeset
    56
class LocalPlayer : public Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    57
    protected:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    58
        LocalPlayer (Coordinate c, bool visible) : Player(c, visible) { }
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    59
    
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    60
    public:
25
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    61
        virtual bool move (PositionDelta d) {
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    62
            return true;
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    63
af75a1894a32 simple proto *almost* works
terom
parents: 24
diff changeset
    64
            //return updatePosition(position + d);
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    65
        }
3
5a209a8585c9 proto p2
terom
parents:
diff changeset
    66
};
5a209a8585c9 proto p2
terom
parents:
diff changeset
    67
5a209a8585c9 proto p2
terom
parents:
diff changeset
    68
class RemotePlayer : public Player {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    69
    protected:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    70
        RemotePlayer (Coordinate c, bool visible) : Player(c, visible) { }
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    71
6
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    72
};
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    73
faa4e777cc6e fiddle with cmake, fix some compile errors
terom
parents: 5
diff changeset
    74
class GameState {
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    75
    public:
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    76
        Dimension map_dimensions;
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    77
        std::list<Player*> player_list;
8
2de58a6d0395 fix to mostly compile
terom
parents: 6
diff changeset
    78
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    79
        // only one local player is supported
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    80
        LocalPlayer *local_player;
22
b70d30e1b0fe all the network code is now there, although it doesn't quite work
terom
parents: 8
diff changeset
    81
24
b81cb670e6b2 the great :retab
terom
parents: 22
diff changeset
    82
        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
    83
24
b81cb670e6b2 the great :retab
terom
parents: 22
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