-
Notifications
You must be signed in to change notification settings - Fork 47
/
movement.py
72 lines (58 loc) · 2.03 KB
/
movement.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
"""
MovementPlugin provides a centralized plugin for controlling client
movement so the client doesn't try to pull itself in a dozen
directions.
"""
from spockbot.plugins.base import PluginBase, pl_announce
from spockbot.plugins.tools.event import EVENT_UNREGISTER
from spockbot.vector import Vector3
class MovementCore(object):
def __init__(self, plug):
self.__plug = plug
self.move_to = plug.new_path
def stop(self):
self.__plug.path_nodes = None
@property
def is_moving(self):
return self.__plug.path_nodes is not None
@property
def current_path(self):
return self.__plug.path_nodes
@property
def current_target(self):
p = self.current_path
return p[0] if p else None
@property
def final_target(self):
p = self.current_path
return p[len(p)-1] if p else None
@pl_announce('Movement')
class MovementPlugin(PluginBase):
requires = ('ClientInfo', 'Event', 'Net', 'Pathfinding', 'Physics')
def __init__(self, ploader, settings):
super(MovementPlugin, self).__init__(ploader, settings)
self.movement = MovementCore(self)
self.path_nodes = None
ploader.provides('Movement', self.movement)
def new_path(self, *xyz):
target = Vector3(*xyz)
self.pathfinding.pathfind(
self.clientinfo.position, target, self.path_cb
)
def path_cb(self, result):
self.path_nodes = result
self.event.emit('movement_path_done')
self.event.reg_event_handler('action_tick', self.follow_path)
def follow_path(self, _, __):
if not self.path_nodes:
self.movement.stop()
return EVENT_UNREGISTER
target = self.path_nodes[0]
jumped = False
if target.is_jump and self.clientinfo.position.on_ground:
self.physics.jump()
jumped = True
if self.physics.move_target(target) or jumped:
self.path_nodes.popleft()
if not self.path_nodes:
self.movement.stop()