-
Notifications
You must be signed in to change notification settings - Fork 1
/
command.py~
90 lines (68 loc) · 2.43 KB
/
command.py~
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
import math
from vector import Vector3
class Command:
def __init__(self):
pass
def tick(self, dt):
pass
class Move(Command):
def __init__(self, ent, dest):
self.ent = ent
self.dest = dest
self.threshold = 0.1
# Orient entity towards the point and move to it
def tick(self, dt):
# Heading
moveDir = self.ent.pos - self.dest
self.ent.desiredHeading = math.pi/2 + math.atan2(moveDir.x, moveDir.z)
# Velocity
# Figure out stopping distance
stopDist = self.ent.speed/(2*self.ent.acceleration*dt)
if self.ent.pos.distance(self.dest) > stopDist:
self.ent.desiredSpeed = self.ent.maxSpeed
else:
self.ent.desiredSpeed = 0
class Follow(Command):
def __init__(self, ent, target):
self.ent = ent
self.target = target
self.stopDist = 100
# Orient entity towards the point and move to it
def tick(self, dt):
self.dest = self.target.pos
# Heading
moveDir = self.ent.pos - self.dest
self.ent.desiredHeading = math.pi/2 + math.atan2(moveDir.x, moveDir.z)
# Velocity
# Figure out stopping distance
stopDist = self.ent.speed/(2*self.ent.acceleration*dt)
if self.ent.pos.distance(self.dest)-self.stopDist > stopDist:
self.ent.desiredSpeed = self.ent.maxSpeed
else:
self.ent.desiredSpeed = 0
class Intercept(Command):
def __init__(self, ent, target):
self.ent = ent
self.target = target
# Orient entity towards the point and move to it
def tick(self, dt):
self.dest = self.target.pos
# Time to intercept
relativeSpeed = (self.target.vel - self.ent.vel).length()
distance = self.ent.pos.distance(self.target.pos)
if relativeSpeed != 0:
time = distance/relativeSpeed
else:
time = 9999999
# Target's projected location
self.dest = self.target.pos + self.target.vel * time
# Heading
moveDir = self.ent.pos - self.dest
self.ent.desiredHeading = math.pi/2 + math.atan2(moveDir.x, moveDir.z)
# Velocity
# Figure out stopping distance
stopDist = self.ent.speed/(2*self.ent.acceleration*dt)
if self.ent.pos.distance(self.dest) > stopDist:
self.ent.desiredSpeed = self.ent.maxSpeed
else:
self.ent.desiredSpeed = 0