-
Notifications
You must be signed in to change notification settings - Fork 0
/
mob.py
50 lines (34 loc) · 1.22 KB
/
mob.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
import random
import math
import entity
class Mob(entity.Entity):
def __init__(self, world, entity_type):
super().__init__(world, entity_type)
self.heading = 0
self.select_target()
self.reset()
def select_target(self):
self.target = [x + random.randint(-5, 5) for x in self.position]
def update(self, delta_time):
if not random.randint(0, int(3 / delta_time)): # change target every 3 seconds on average
self.select_target()
delta_x = self.position[0] - self.target[0]
delta_y = self.position[2] - self.target[2]
self.heading = -math.atan2(delta_y, delta_x) + math.tau / 4
self.heading += math.modf((self.rotation[0] - math.tau / 4) / math.tau)[1] * math.tau
if delta_time * 5 > 1:
self.rotation[0] = self.heading
else:
self.rotation[0] += (self.heading - self.rotation[0]) * delta_time * 5
target_dist = math.sqrt(delta_x ** 2 + delta_y ** 2)
if target_dist > 1:
self.accel[0] = math.cos(self.rotation[0] + math.tau / 4) * 3
self.accel[2] = -math.sin(self.rotation[0] + math.tau / 4) * 3
super().update(delta_time)
if self.wall and self.grounded:
if random.randint(0, 3):
self.jump()
else:
self.select_target()
if self.position[1] < 0:
self.teleport((0, 80, 0))