-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredictenemy.cpp
More file actions
108 lines (104 loc) · 4.1 KB
/
Copy pathpredictenemy.cpp
File metadata and controls
108 lines (104 loc) · 4.1 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
#include "enemy.h"
#include "predictenemy.h"
#include "basicBullet.h"
#include "splitBullet.h"
#include "accBullet.h"
#include "Vector2Basic.h"
#include <iostream>
PredictEnemy::PredictEnemy(float hp, float gentime, float livetime, Vector2 pos, float r, char *filename) : Enemy(hp, gentime, livetime, pos, r, filename)
{
predictLastTime = stateLastTime = accLastTime = gentime;
state = rand() % 4 + 1;
}
std::vector<Bullet *> PredictEnemy::getBullet(float nowTime, BulletManager *creater, Vector2 playerPosition)
{
std::vector<Bullet *> ret;
float dtime = nowTime - gentime;
if (state == 1 && nowTime - stateLastTime >= 2)
{
if (int(dtime * 0.4) - int(dutime * 0.4) >= 1)
{
Bullet *b = new fishBullet(nowTime, 20, creater, PURPLE, 15, pos, 200, PI / 2);
ret.push_back(b);
}
if (int(dtime * 0.2) - int(dutime * 0.2) >= 1)
{
for (float _ang = 0; _ang <= PI; _ang += PI / 6)
{
std::vector<Bullet *> bullets;
for (float ang = 0; ang <= 2 * PI; ang += PI / 16)
{
Vector2 f = {cos(ang), sin(ang)};
Bullet *b = new accBullet(nowTime, 10, creater, RED, 5, {0, 0}, {0, 0}, {200 * f.x, 200 * f.y});
bullets.push_back(b);
}
ret.push_back(new splitBullet(nowTime, rand() % 10, creater, GREEN, 15, pos, {80 * cos(_ang), 80 * sin(_ang)}, bullets));
}
}
if (int(dtime * 2) - int(dutime * 2) >= 1)
{
Vector2 dir = {playerPosition.x - playerLastPosition.x, playerPosition.y - playerLastPosition.y};
Vector2 velocity = {dir.x / (nowTime - predictLastTime), dir.y / (nowTime - predictLastTime)};
float time = sqrt((pos.x - playerPosition.x) * (pos.x - playerPosition.x) + (pos.y - playerPosition.y) * (pos.y - playerPosition.y)) / 800;
Vector2 predict = {playerPosition.x + time * velocity.x, playerPosition.y + time * velocity.y};
float ang = atan2(predict.y - pos.y, predict.x - pos.x);
Vector2 f = {cos(ang), sin(ang)};
Bullet *b = new basicBullet(nowTime, 15, creater, BLUE, 15, pos, {800 * f.x, 800 * f.y});
ret.push_back(b);
}
}
else if (state == 2 && nowTime - stateLastTime >= 2)
{
if (int(dtime * 2) - int(dutime * 2) >= 1)
{
for (float x = 0; x <= 1000; x += (rand() & 1 ? 40 : 80))
{
ret.push_back(new basicBullet(nowTime, 30, creater, RED, 10, {x, pos.y}, {0, 800}));
}
}
}
else if (state == 3 && nowTime - stateLastTime >= 2)
{
if (int(dtime * 4) - int(dutime * 4) >= 1)
{
Vector2 pos;
do
pos = {float(rand() % 1000),
float(rand() % 900)};
while (-200 <= pos.x - playerPosition.x && pos.x - playerPosition.x <= 200 && -200 <= pos.y - playerPosition.y && pos.y - playerPosition.y <= 200);
ret.push_back(new fishBullet(nowTime, 5, creater, ORANGE, 15, pos, 200, PI / 2));
}
}
else if (state == 4 && nowTime - stateLastTime >= 2)
{
if (nowTime - accLastTime >= 0.5 - 0.01 * accCount)
{
for (float ang = 0; ang <= PI * 2; ang += PI / 16)
{
float _ang = ang + (PI / 64) * (accCount % 4);
Vector2 f = {cos(_ang), sin(_ang)};
Bullet *b = new accBullet(nowTime, 15, creater, PURPLE, 30, pos, {100 * f.x, 100 * f.y}, {200 * f.x, 200 * f.y});
ret.push_back(b);
}
accCount++;
accLastTime = nowTime;
}
}
if (nowTime - stateLastTime >= stateLengths[state])
{
accCount = 0;
int new_state;
do
new_state = rand() % 4 + 1;
while (state == new_state);
state = new_state;
stateLastTime = nowTime;
}
if (int(dtime * 10) - int(dutime * 10) >= 1)
{
playerLastPosition = playerPosition;
predictLastTime = nowTime;
}
dutime = dtime;
return ret;
}