This repository has been archived by the owner on Apr 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDodger.cpp
103 lines (83 loc) · 2.38 KB
/
Dodger.cpp
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
#include "Dodger.h"
Dodger::Dodger(double start_x, double start_y) : Enemy("./gfx/skull.bmp")
{
Velocity = 2;
SurfaceRectangle.x = start_x;
SurfaceRectangle.y = start_y;
SurfaceRectangle.w = 40;
SurfaceRectangle.h = 40;
DodgeProjectile = false;
}
Dodger::~Dodger()
{ }
void Dodger::set_dodge(bool dodge)
{
DodgeProjectile = dodge;
}
void Dodger::move()
{
if (DodgeProjectile)
dodge_move();
else
chase_move();
}
void Dodger::chase_move()
{
double delta_y = (Chase.y - SurfaceRectangle.y);
double delta_x = (Chase.x - SurfaceRectangle.x);
double angle = 0;
const double PI = 3.141592;
// Calculate the angle (in radians)
if (delta_y < 0 && delta_x > 0)
angle = -(atan(delta_y / delta_x));
else if (delta_y < 0 && delta_x < 0)
angle = -(atan(delta_y / delta_x) + PI);
else if (delta_y > 0 && delta_x < 0)
angle = -(atan(delta_y / delta_x)) + PI;
else
angle = -((atan(delta_y / delta_x)) - PI/2) + (PI * 1.5);
// Move the enemy
if (SurfaceRectangle.x < 800)
SurfaceRectangle.x += (cos(angle) * Velocity);
if (!SurfaceRectangle.y < 600)
SurfaceRectangle.y -= (sin(angle) * Velocity);
// Don't make it able to leave the screen
if (SurfaceRectangle.x > 800 - SurfaceRectangle.w)
SurfaceRectangle.x = 800 - SurfaceRectangle.w;
if (SurfaceRectangle.y > 600 - SurfaceRectangle.h)
SurfaceRectangle.y = 600 - SurfaceRectangle.h;
}
void Dodger::dodge_move()
{
double delta_y = (Chase.y - SurfaceRectangle.y);
double delta_x = (Chase.x - SurfaceRectangle.x);
double angle = 0;
const double PI = 3.141592;
// Calculate the angle (in radians)
if (delta_y < 0 && delta_x > 0)
angle = -(atan(delta_y / delta_x));
else if (delta_y < 0 && delta_x < 0)
angle = -(atan(delta_y / delta_x) + PI);
else if (delta_y > 0 && delta_x < 0)
angle = -(atan(delta_y / delta_x)) + PI;
else
angle = -((atan(delta_y / delta_x)) - PI/2) + (PI * 1.5);
// Move the enemy
if (SurfaceRectangle.x < 800)
SurfaceRectangle.x -= (cos(angle) * Velocity);
if (!SurfaceRectangle.y < 600)
SurfaceRectangle.y += (sin(angle) * Velocity);
// Don't make it able to leave the screen
if (SurfaceRectangle.x > 800 - SurfaceRectangle.w)
SurfaceRectangle.x = 800 - SurfaceRectangle.w;
if (SurfaceRectangle.y > 600 - SurfaceRectangle.h)
SurfaceRectangle.y = 600 - SurfaceRectangle.h;
}
void Dodger::set_chase(SDL_Rect player)
{
Chase = player;
}
std::string Dodger::get_type()
{
return "Dodger";
}