-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnemy.cpp
More file actions
115 lines (101 loc) · 2.26 KB
/
Copy pathEnemy.cpp
File metadata and controls
115 lines (101 loc) · 2.26 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
#include "Enemy.h"
#include "GlobalRendererAndWindow.h"
#include "Tower.h"
#include <cstdlib>
#include <iostream>
Enemy::Enemy(int Cx, int Cy) {
x = Cx;
y = Cy;
targetX = 300;
targetY = 400;
}
void Enemy::Update() {
GetPushed();
if (GRAW::FramesFromStart % speed == 0) {
Move();
}
Attack();
if (x < 100 || x > 500) {
Tower::Points += 4 * (120 - speed);
Die();
}
GRAW::Draw(x, y, 20, 20, 0);
}
int* Enemy::GetCoords() {
int a[2] = { x, y };
return a;
}
void Enemy::Attack() {
if (abs(300 - x) < 25 && abs(400 - y) < 25) {
Tower::Hit();
Die();
}
}
void Enemy::Die() {
x = 200 + (rand() % 200);
y = 5 + 790 * (rand() % 2);
}
void Enemy::GetPushed() {
SDL_Event event;
bool GameRunning = true;
SDL_PollEvent(&event);
if (event.type == SDL_MOUSEMOTION)
{
int xe, ye;
SDL_GetMouseState(&xe, &ye);
mouse::X = xe;
mouse::Y = ye;
int difx = mouse::X - x;
int dify = mouse::Y - y;
if (difx * difx + dify * dify > 900) {
return;
}
MoveTowards(x + difx * ((30.0 - (difx * difx + dify * dify)) / (difx * difx + dify * dify)),
y + dify * ((30.0 - (difx * difx + dify * dify)) / (difx * difx + dify * dify)));
}
}
void Enemy::Move() {
int deltaX = abs(targetX - x);
int deltaY = abs(targetY - y);
int signX = x < targetX ? 1 : -1;
int signY = y < targetY ? 1 : -1;
int error = deltaX - deltaY;
int error2 = error * 2;
if (error2 > -deltaY)
{
error -= deltaY;
x += signX;
}
if (error2 < deltaX)
{
error += deltaX;
y += signY;
}
}
void Enemy::MoveTowards(int _targetX, int _targetY) {
int deltaX = abs(_targetX - x);
int deltaY = abs(_targetY - y);
int signX = x < _targetX ? 1 : -1;
int signY = y < _targetY ? 1 : -1;
int error = deltaX - deltaY;
int error2 = error * 2;
if (error2 > -deltaY)
{
error -= deltaY;
x += signX * 3;
}
if (error2 < deltaX)
{
error += deltaX;
y += signY * 3;
}
}
void Enemy::SetCoords(int xx, int yy) {
x = xx;
y = yy;
}
Enemy::Enemy() {
targetX = 300;
targetY = 400;
}
int Enemy::speed;