-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcontroller.py
More file actions
110 lines (84 loc) · 3.63 KB
/
controller.py
File metadata and controls
110 lines (84 loc) · 3.63 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
""" Keyboard manager """
__author__ = "Peter Bennett"
__copyright__ = "Copyright 2012, Peter A Bennett"
__license__ = "LGPL"
__version__ = "0.1"
__maintainer__ = "Peter Bennett"
__email__ = "pab850@googlemail.com"
__contact__ = "www.bytebash.com"
from pyglet.window import key, mouse
from pyglet import clock
""" Statename strings """
flight = "Flight Mode"
wireframe = "Wireframe Mode"
class Controller(object):
def __init__(self, window, player, console):
self.keys = key.KeyStateHandler()
self.player = player
# Console
self.console = console
# States
self.states = {}
self.states[wireframe] = False
self.states[flight] = False
self.mouse_speed = 0.0014
# Key Press Events
self.keyPressEvents = {}
clock.schedule(self.update)
window.push_handlers(self.on_key_press)
window.push_handlers(self.on_key_release)
window.push_handlers(self.keys)
window.push_handlers(self.on_mouse_motion)
window.push_handlers(self.on_mouse_drag)
window.push_handlers(self.on_mouse_press)
window.push_handlers(self.on_mouse_release)
def on_mouse_motion(self, x, y, dx, dy):
self.player.orient(-dy*self.mouse_speed, dx*self.mouse_speed)
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self.player.orient(-dy*self.mouse_speed, dx*self.mouse_speed)
def keyPressed(self, symbol):
if symbol in self.keyPressEvents:
return self.keyPressEvents[symbol]
return False
def update(self, dt):
# Handle events that must occur while a key is pressed.
self.player.update(dt,
self.keyPressed(key.W),
self.keyPressed(key.S),
self.keyPressed(key.A),
self.keyPressed(key.D))
def on_mouse_press(self, x, y, button, modifiers):
if button == mouse.LEFT:
if not self.keyPressed(mouse.LEFT):
self.player.fire()
if button == mouse.RIGHT:
if not self.keyPressed(mouse.RIGHT):
self.player.altFire()
def on_mouse_release(self, x, y, button, modifiers):
self.keyPressEvents[button] = False
def on_key_press(self, symbol, modifiers):
# Handle events that only require one action per keypress here.
# Player jump
if symbol == key.SPACE:
if not self.keyPressed(key.SPACE):
self.player.jump()
# Player jump
if symbol == key.R:
if not self.keyPressed(key.R):
self.player.fire()
# Enable or disable flight mode.
if symbol == key.F:
if not self.keyPressed(key.F):
self.states[flight] = not self.states[flight]
self.console.updateConsole(flight + ": " +
str(self.states[flight]))
self.player.toggleFlightMode(self.states[flight])
# Enable or disable wireframe mode.
if symbol == key.T:
if not self.keyPressed(key.T):
self.states[wireframe] = not self.states[wireframe]
self.console.updateConsole(wireframe + ": " +
str(self.states[wireframe]))
self.keyPressEvents[symbol] = True
def on_key_release(self, symbol, modifiers):
self.keyPressEvents[symbol] = False