summaryrefslogtreecommitdiff
path: root/source/presentation/KeyboardHandler.h
blob: 00d7821499142725eb8474215b29a0c9fe479a01 (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
#ifndef KEYBOARDHANDLER_H
#define KEYBOARDHANDLER_H

#include <allegro5/allegro.h>

#include "../logic/Maze.h"
#include "../logic/AllegroWrappers.h"

/**
* @brief Class for handling keyboard related game inputs from the player.
*
* Written with controlling a PlayerCar in mind. The handler keeps track of the last direction
* pushed and responds to requests from the PlayerCar for which direction it should face next,
* and whether the player is pressing the Smokescreen button.
*
* @author Justin Wernick
* @author David Schneider
*/
class KeyboardHandler
{
    public:
        /**
        * @brief Creates a KeyboardHandler with a given initial state.
        *
        * @param [in] currentFacing The initial value for the previous facing of the object being controlled.
        */
        KeyboardHandler(Maze::Direction currentFacing);
        /**
        * @brief Copy constructor that ensures that a copy of a KeyboardHandler will have its own event queue.
        */
        KeyboardHandler(const KeyboardHandler& ref);
        /**
        * @brief Assignment operator that ensures that an assigned KeyboardHandler will have its own event queue.
        */
        KeyboardHandler& operator=(const KeyboardHandler& rhs);
        /**
        * @brief Cleans up the keyboard event queue
        */
        ~KeyboardHandler();

        /**
        * @brief Gives the last direction that the player entered on the keyboard.
        *
        * All pending keyboard events are processed, then a key out of those currently depressed is returned.
        * The precendence for keys held down (up, down, left, then right) is arbitrary, since the player
        * should not be holding down more than one arrow key at a time.
        * If no keys are currently depressed, the value returned on the last call is returned again.
        *
        * @return The direction that the player has chosen through pressing arrow keys.
        */
        Maze::Direction getFacing();

        /**
        * @brief Gives whether or not the key for creating a Smokescreen is currently pressed.
        *
        * @return True if a Smokescreen should be created.
        */
        bool getSmokescreen();

    private:
        AllegroKeyboardInit _keyboard; ///< Ensures that dependencies on the Allegro keyboard library are installed.

        /**
        * @brief Processes all pending keyboard inputs, and updates flags as appropriate.
        */
        void updateFlags();

        bool _up; ///< True if the up arrow key is depressed.
        bool _down; ///< True if the down arrow key is depressed.
        bool _left; ///< True if the left arrow key is depressed.
        bool _right; ///< True if the right arrow key is depressed.
        bool _smokescreen; ///< True if the smokescreen key is depressed.
        Maze::Direction _previousFacing; ///< The direction that was returned on the last call of getFacing.
        ALLEGRO_EVENT_QUEUE* _keyboardEvents; ///< Queue for all keyboard events.

        static const int UP_KEY = ALLEGRO_KEY_UP; ///< Key that must be pressed to turn up.
        static const int DOWN_KEY = ALLEGRO_KEY_DOWN; ///< Key that must be pressed to turn down.
        static const int LEFT_KEY = ALLEGRO_KEY_LEFT; ///< Key that must be pressed to turn left.
        static const int RIGHT_KEY = ALLEGRO_KEY_RIGHT; ///< Key that must be pressed to turn right.
        static const int SMOKESCREEN_KEY = ALLEGRO_KEY_SPACE; ///< Key that must be pressed to create a smokescreen.
};

#endif // KEYBOARDHANDLER_H