summaryrefslogtreecommitdiff
path: root/source/logic/Game.h
blob: 9adff76f90e1f3efd1fe17d70a61070e90963787 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef GAME_H
#define GAME_H

#include <list>
#include <algorithm>
using namespace std;

#include <allegro5/allegro.h>

#include "../presentation/Screen.h"
#include "../presentation/BitmapStore.h"

#include "../logic/Maze.h"
#include "../logic/PlayerCar.h"
#include "../logic/EnemyCar.h"
#include "../logic/Checkpoint.h"
#include "../logic/Rock.h"
#include "../logic/Smokescreen.h"
#include "../logic/DestroyedObjectPopup.h"
#include "../logic/AllegroWrappers.h"
#include "../logic/CollisionDetector.h"

#include "../data/LevelReader.h"
#include "../data/Config.h"

/**
* @brief The object that controls the flow of the game, and the launch point of the game.
*
* Game contains the various components, including the screen, the maze, and all of the
* objects in the maze. The timing of the gameloop also falls under Game's control.
* Essencially, Game is the central point that everything connects to.
*
* @author Justin Wernick
* @author David Schneider
*/
class Game
{
    public:
        static const unsigned int FPS = 30; ///< Frames per second, the number of times the gameloop is run every second.

        /**
        * @brief Constructor, that creates the relevant Allegro entities.
        */
        Game();

        /**
        * @brief Constructor, that destroys the relevant Allegro entities.
        */
        ~Game();

        /**
        * @brief Entry point for the program. This should be called from main.
        */
        void start();

    private:
        /**
        * @brief Unimplemented copy constructor, prevents copying of Game objects.
        *
        * Copying a Game is unneccesary as there should only be a single Game object.
        */
        Game(const Game& ref);
        /**
        * @brief Unimplemented assignment operator.
        *
        * @see Game::Game(const Game& ref)
        */
        Game& operator=(const Game& rhs);

        /**
        * @brief Initialises all of the GameObject lists using a file.
        *
        * @param [in] levelFile The path of the file that contains the level layout.
        */
        void initLevel(const string& levelFile);

        /**
        * @brief Main part of the game, performs the actions in each frame FPS times per second until the game is over.
        *
        * Each frame runs the update methods of each of the GameObjects in the lists. The CollisionDetector
        * then checks for collisions between objects. Any GameObjects that have been destroyed are then removed
        * from their lists. Finally, the Screen is called to draw all of the GameObjects that still exist in
        * their new positions.
        *
        * Before the next iteration begins, a check is done for the victory and loss conditions. The loop is
        * ended if either of these are met, or if the player has quit the game.
        */
        void runloop();

        /**
        * @brief Calls the update method on each of the GameObjects in the game.
        */
        void update();
        /**
        * @brief Removes any GameObjects that have been destroyed from their lists.
        */
        void cleanup();

        /**
        * @brief Destroys all GameObjects in the game, resetting the lists for a new level to be loaded.
        *
        * This should always be called before a new level is loaded.
        */
        void clearLists();

        AllegroInit _allegro; ///< Handles dependencies on Allegro being installed.

        Config _config; ///< Loads configuration from file on construction, used to set resolution of screen.
        Screen _screen; ///< Handles all drawing operations.
        ALLEGRO_TIMER* _timer; ///< Creates FPS events per second, that are put into _timerEvents.
        ALLEGRO_EVENT_QUEUE* _timerEvents; ///< Catches events from _timer, used to regulate speed of runloop.

        Maze _maze; ///< The environment that confines the movements of GameObjects, specifically Cars.
        /**
        * @brief Typically a single PlayerCar, controlled by the person playing the game.
        *
        * A list was used for _players to allow the Game object to be constructed without needing to initialise
        * a meaningless PlayerCar object. This also allows the PlayerCar to be destroyed by Rocks or EnemyCars.
        * An added benefit is that it adds the ease of extending the game to allow multiple players. To add
        * multiplayer, the KeyboardHandler would need to be modified to allow different sets of input keys,
        * and the Screen would need to be modified to keep all players visible, but the Game class would be
        * able to remain largely unchanged.
        */
        list<PlayerCar> _players;

        list<EnemyCar> _enemies; ///< List of all EnemyCars chasing the player.
        list<Checkpoint> _checkpoints; ///< List of checkpoints that the player needs to collect.
        list<Rock> _rocks; ///< List of rocks that the player and EnemyCars need to avoid.
        list<Smokescreen> _smokescreens; ///< List of Smokescreen objects that are currently able to delay EnemyCars.
        list<DestroyedObjectPopup> _popups; ///< List of purely visual DestroyedObjectPopups that need to be drawn.

        CollisionDetector _collisionDetector; ///< Object that checks for collisions each frame.
};

#endif // GAME_H