-
Notifications
You must be signed in to change notification settings - Fork 0
/
follower.cpp
75 lines (65 loc) · 1.76 KB
/
follower.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
#include "follower.h"
#include "behaviour.h"
#include "ship.h"
#include <iostream>
#include <cstdlib>
using namespace std;
Follower::Follower(Ship *ship) : Behaviour(ship) {
common_init();
}
Follower::Follower(Ship *ship, list<Object *> *targets) : Behaviour(ship), targets(targets) {
common_init();
}
Follower::~Follower() {
ship->rotate_right(false);
ship->thrust(false);
delete targets;
}
void Follower::common_init() {
time_until_next_lock = 2500.0 + rand()%500;
time_between_locks = 900 + rand()%1000;
target = NULL;
done = false;
}
void Follower::step(int delta) {
if(ship->is_alive()) {
lock_step(delta);
if(target) {
if (target->is_alive()) {
ship->thrust(true);
WrappedPoint target_point = target->position;
float angle = (ship->heading() - (ship->position.closest_to(target_point) - target_point).normalized().direction());
angle = (angle < 0.0) ? (360.0 + angle) : angle;
if (angle >= 0 && angle < 180) {
ship->rotate_left(true);
} else {
ship->rotate_right(true);
}
} else {
target = NULL;
time_until_next_lock = time_between_locks;
ship->rotate_right(false);
}
}
}
}
void Follower::lock_nearest_target() {
if(target && !target->is_alive()) {
target = NULL;
}
list<Object *>::iterator s;
for(s = targets->begin(); s != targets->end(); s++) {
if((*s)->is_alive()) {
if(target == NULL || (*s)->position.distance_to(ship->position) < target->position.distance_to(ship->position)) {
target = (*s);
}
}
}
}
void Follower::lock_step(int delta) {
time_until_next_lock -= delta;
if(time_until_next_lock <= 0) {
lock_nearest_target();
time_until_next_lock += time_between_locks;
}
}