#ifndef KEYBOARDHANDLER_H #define KEYBOARDHANDLER_H #include #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