-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtowerScripts.cpp
More file actions
42 lines (39 loc) · 1013 Bytes
/
towerScripts.cpp
File metadata and controls
42 lines (39 loc) · 1013 Bytes
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
#include"towerScripts.h"
#include<cmath>
AIScript::~AIScript() {
}
void AIScript::onClick() {
}
void AIScript::onPoint() {
}
void AIScript::onStep() {
}
WayPathScript::WayPathScript (WayPath newPath,Entity* ent) {
path = newPath;
actor = ent;
}
AIScript* WayPathScript::copy () {
return AIScript::defaultCopy<WayPathScript>(this);
}
void WayPathScript::onStep () {
if (actor->pos != path.getCurrent()) {
if (distance2(actor->pos,path.getCurrent()) < actor->base->stats[STAT_SPD]) {
actor->pos=path.getCurrent();
} else {
/*create unit vector*/
Vector2 velocity = path.getCurrent();
velocity.x-=actor->pos.x;
velocity.y-=actor->pos.y;
float scale = sqrt(velocity.x*velocity.x+velocity.y*velocity.y);
velocity.x/=scale;
velocity.y/=scale;
/*scale up unit vector by speed*/
velocity.x*=actor->base->stats[STAT_SPD];
velocity.y*=actor->base->stats[STAT_SPD];
actor->pos.x+=velocity.x;
actor->pos.y+=velocity.y;
}
} else if (path.isNext()) {
path.getNext();
}
}