Wolf3D
enemy.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #include "astar.hpp"
14 #include "global.hpp"
15 #include "renderwindow.hpp"
16 #include "sample.hpp"
17 
18 // Forward (Class) Declaration
19 // https://en.cppreference.com/w/cpp/language/class#Forward_declaration
20 class Player;
21 
22 class Enemy {
23  public:
24  //
29  enum state {
30  DIE = 0,
31  PAIN = 4,
32  IDLE = 6,
33  SHOOT = 14,
34  WALK = 17
35  };
36 
48  Enemy(int id, std::string type, double position_x, double position_y, double direction, Player *player, std::vector<std::vector<int>> *world_map);
49 
58  double calculate_rotation(double position_x, double position_y, bool angle = false);
59 
68  void ai(double *position_x, double *position_y, std::vector<std::vector<int>> *world_map, json *sprite);
69 
76  void handle_shot(double *position_x, double *position_y);
77 
87  std::pair<bool, double> line_of_sight(double *position_x, double *position_y, std::vector<std::vector<int>> *world_map);
88 
94  int get_id();
95 
101  std::string get_type();
102 
108  double get_position_x();
109 
115  double get_position_y();
116 
122  int get_direction();
123 
129  void set_rotation(int rotation);
130 
136  double get_health();
137 
144  double remove_health(double health);
145 
151  void set_health(double health);
152 
159  bool is_alive();
160 
166  int get_state();
167 
173  void set_state(int state);
174 
181  void play_sound(int sound, double arg0);
182 
188  void set_tracking_player(bool tracking_player);
189 
195  void init_texture(RenderWindow *window);
196 
202  std::vector<Uint32> get_texture();
203 
208  void update();
209 
210  private:
211  // Field of View
212  std::map<std::string, int> fov{
213  {"mguard", 60}};
214  // Hit chance 1/x
215  std::map<std::string, int> hit_chance{
216  {"mguard", 2}};
217  // Weapon damage (before debuff)
218  std::map<std::string, int> weapon_damage{
219  {"mguard", 30}};
220 
221  // Maximum distance the Enemy can fire
222  int max_fire_distance = 6;
223  // Maximum distance the Enemy can see
224  int max_view_distance = 15;
225  // Used to ensure the sound effect and fire rate don't stack
226  bool is_shooting = false;
227  // Sample for the Enemy's pain sound effect
228  Sample *sound_enemy_pain;
229  // Vector of Samples for the Enemy's death sound effects
230  std::vector<Sample *> sound_enemy_die;
231  // The sounds the Enemy's Weapon can make
232  std::map<std::string, Sample *> sound_enemy_gun;
233 
234  // The Enemy's id
235  int id;
236  // The Enemy's type
237  std::string type;
238  // The Enemy's Position
239  double position_x;
240  double position_y;
241  // The last seen Player's Position from the Enemy's Perspective
242  double last_player_x;
243  double last_player_y;
244  // The Enemy's direction (0-359)
245  int direction = 0;
246 
247  // The Player Class for their Position and dealing damage, @see Player::hit(double)
248  Player *player = nullptr;
249  // Used to prevent stacking of damage applied to the Player
250  bool damaged_player = false;
251 
252  // If the Enemy is tracking the Player's movement
253  bool tracking_player = false;
254  // If the Enemy is actively moving towards the Player's last seen Position
255  bool hunting_player = false;
256  // Pathfinding
257  AStar::Generator *generator;
258  AStar::CoordinateList hunt_path;
259  int hunt_offset = 0;
260 
261  // Enemy's animation state, used for texture index
262  int state = state::IDLE;
263  // Previous state, used to prevent stacking and revertion
264  int previous_state = 0;
265  // Offset of texture index for frame
266  int animation_frame = 0;
267  // Frame for walking
268  int walk_frame = 1;
269  // Rotation for texture 1-8
270  int rotation = 1;
271 
272  // Vector of the Enemy's textures
273  std::vector<Uint32> enemy_texture[49];
274  // Used to map the walking texture file names to their index in the enemy_texture vector
275  std::map<std::string, int> enemy_shoot_textures;
276 
277  // Enemy health
278  double health = 100;
279  bool alive = true;
280 
281  // Timer variables
282  double previous_time = 0.0;
283  double time = 0.0;
284  double timer = 0.0;
285  double shoot_timer = 0.0;
286  double hunt_timer = 0.0;
287  double walk_timer = 0.0;
288 };
Definition: enemy.hpp:22
void set_state(int state)
Set the Enemy's animation state.
Definition: enemy.cpp:400
int get_state()
Get the Enemy's animation state.
Definition: enemy.cpp:399
double get_position_y()
Get the Enemy's position y.
Definition: enemy.cpp:381
int get_id()
Get the Enemy's id.
Definition: enemy.cpp:376
double remove_health(double health)
Remove health from the Enemy's health.
Definition: enemy.cpp:392
double calculate_rotation(double position_x, double position_y, bool angle=false)
Calculate the rotation of the Enemy relative to a target's Postion X/Y.
Definition: enemy.cpp:43
void handle_shot(double *position_x, double *position_y)
A subset of the Enemy's Artificial Intelligence for when the Player shoots within a certain range to ...
Definition: enemy.cpp:255
void play_sound(int sound, double arg0)
Play a sound effect.
Definition: enemy.cpp:412
void set_health(double health)
Set the Enemy's health.
Definition: enemy.cpp:396
void set_tracking_player(bool tracking_player)
Set if the Enemy is tracking the Player.
Definition: enemy.cpp:455
double get_position_x()
Get the Enemy's position x.
Definition: enemy.cpp:380
void init_texture(RenderWindow *window)
Load the textures into memory.
Definition: enemy.cpp:459
std::string get_type()
Get the Enemy's type.
Definition: enemy.cpp:378
void ai(double *position_x, double *position_y, std::vector< std::vector< int >> *world_map, json *sprite)
Perform the Enemy's Artificial Intelligence.
Definition: enemy.cpp:94
bool is_alive()
If the Enemy is alive.
Definition: enemy.cpp:397
std::pair< bool, double > line_of_sight(double *position_x, double *position_y, std::vector< std::vector< int >> *world_map)
Calculating if the Enemy has Line of Sight with the provided coordinates and wall collisions with the...
Definition: enemy.cpp:271
int get_direction()
Get the Enemy's direction.
Definition: enemy.cpp:383
void update()
Update the timer.
Definition: enemy.cpp:638
double get_health()
Get the Enemy's health.
Definition: enemy.cpp:391
std::vector< Uint32 > get_texture()
Get the Enemy's current texture accounting for their state.
Definition: enemy.cpp:526
state
States that the Enemy's animation could be in.
Definition: enemy.hpp:29
void set_rotation(int rotation)
Set the Enemy's rotation.
Definition: enemy.cpp:385
Enemy(int id, std::string type, double position_x, double position_y, double direction, Player *player, std::vector< std::vector< int >> *world_map)
Construct the Enemy object.
Definition: enemy.cpp:5
Definition: player.hpp:19
Definition: renderwindow.hpp:16
Definition: sample.hpp:18
Contains the header functions for the RenderWindow Class.
Contains the header functions for the Enemy Class.