summaryrefslogtreecommitdiff
path: root/source/logic/EnemyCar.h
blob: b56c9d319a03350e20b0584558fe91d3d2acd158 (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
#ifndef ENEMYCAR_H
#define ENEMYCAR_H

#include <cmath>

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

#include "../logic/Car.h"
#include "../logic/PlayerCar.h"
#include "../logic/Rock.h"
#include "../logic/MazeMath.h"

/**
* @brief GameObject that chases the player around the maze.
*
* Attempts to collide with the player, causing the player to lose.
*
* @author Justin Wernick
* @author David Schneider
*/
class EnemyCar: public Car
{
    public:
        /**
        * @brief Creates an EnemyCar at the given coordinates.
        *
        * @param [in] x The x coordinate of the EnemyCar's initial position.
        * @param [in] y The y coordinate of the EnemyCar's initial position.
        */
        EnemyCar(double x, double y);

        //assignment and copy constructors have been left with the compiler generated versions

        /**
        * @brief Processes one frame's worth of activity for the object, called every frame.
        *
        * Primarily adjusts the facing if neccesary and then moves using the inhereted move function.
        *
        * @param maze The maze that confines the EnemyCar's movements.
        * @param players The list of PlayerCars that the EnemyCar can chase.
        * @param rocks The list of Rocks that need to be avoided.
        */
        void update(const Maze& maze, const list<PlayerCar>& players, const list<Rock>& rocks);

        /**
        * @brief Function that is called when an EnemyCar crashes into a PlayerCar.
        */
        void crash();

        /**
        * @brief Function that is called when an EnemyCar drives into a Smokescreen.
        */
        void blind();

    private:
        /**
        * @brief States that define how the EnemyCar's AI should behave.
        *
        * This would need to be expanded to include more states in order to make the enemies appear smarter.
        */
        enum States {
            BLINDED, ///< The EnemyCar can not see, and so does not move.
            CHASING ///< The EnemyCar tries to drive to the block that the player is currently on.
            };

        States _state; ///< The state that the object is currently in.
        double _targetX; ///< The x coordinate that the EnemyCar is driving towards.
        double _targetY; ///< The y coordinate that the EnemyCar is driving towards.

        /**
        * @brief Updates the direction that the EnemyCar is facing, if neccesary.
        *
        * The facing is only changed once the current _targetX and _targetY are reached. After that, a facing is
        * chosen that points into an empty block (no maze walls or rocks) that is closest to the chasing x and y
        * using a straight line. This results in the enemy not always taking the shortest route, but it makes it
        * possible to escape enemies. _targetX and _targetY are updated to one block in the new facing direction.
        * The enemy may only turn around and head backwards if there is no other options, so once the enemy starts
        * driving down narrow a path it will continue to the end of the path.
        *
        * @param [in] maze The maze that confines the EnemyCar's movements.
        * @param [in] chasingX The x coordinate that the EnemyCar is ultimately trying to reach.
        * @param [in] chasingY The y coordinate that the EnemyCar is ultimately trying to reach.
        * @param [in] rocks The Rocks that the EnemyCar needs to avoid.
        */
        void checkFacing(const Maze& maze, double chasingX, double chasingY, const list<Rock>& rocks);

        /**
        * @brief Iterates through a list of Rocks and determines if moving to a given position would result in a collision.
        *
        * @param [in] x The potential new x coordinate.
        * @param [in] y The potential new y coordinate.
        * @param [in] rocks The Rocks that are checked for a collision at x and y.
        */
        bool rockAtLocation(double x, double y, const list<Rock>& rocks);
};

#endif // ENEMYCAR_H