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 pathStalker.cpp
92 lines (73 loc) · 1.87 KB
/
Stalker.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
#include "Stalker.h"
Stalker::Stalker(double start_x, double start_y) : Enemy("./gfx/space.bmp")
{
velocity = 2;
SurfaceRectangle.x = start_x;
SurfaceRectangle.y = start_y;
SurfaceRectangle.w = 40;
SurfaceRectangle.h = 40;
}
Stalker::~Stalker()
{ }
void Stalker::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);
}
void Stalker::set_chase(SDL_Rect player)
{
chase = player;
}
std::string Stalker::get_type()
{
return "Stalker";
}
/*bool Stalker::hasCollided(SDL_Rect second)
{
// The sides of the recangle
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = SurfaceRectangle.x;
rightA = SurfaceRectangle.x + SurfaceRectangle.w;
topA = SurfaceRectangle.y;
bottomA = SurfaceRectangle.y + SurfaceRectangle.h;
//Calculate the sides of rect B
leftB = second.x;
rightB = second.x + second.w;
topB = second.y;
bottomB = second.y + second.h;
//If any of the sides from A are outside of B
if(bottomA <= topB) {
return false;
}
if(topA >= bottomB) {
return false;
}
if(rightA <= leftB) {
return false;
}
if(leftA >= rightB) {
return false;
}
delete this;
return true;
} */