-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeyboard.py
More file actions
160 lines (115 loc) · 4.15 KB
/
Copy pathkeyboard.py
File metadata and controls
160 lines (115 loc) · 4.15 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""
Written By:
Chris Humphreys
Email: < chris (--AT--) habitualcoder [--DOT--] com >
All python source code copyright Chris Humphreys 2010
Level data, Obliteration name and some media copyright
Flash Jester Punk 2007
Alien Breed name, concept and media copyright Team17 1992
This file is part of alien breed obliteration wii edition
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import mygame
from unit import *
from movement import *
from utils import *
class PlayerKeyHandler:
BACKWARDS_DELAY = 200
def __init__(self, unit, my_game):
self._unit = unit
self._running_and_firing_last_time = None
self._running_and_firing = None
self._firing = False
self._moving = False
self._running = False
self._quit = False
self._change_weapons = False
self.my_game = my_game
self.key_set = KeySet.load_key_set()
self.previous_movement = Movement()
def handle(self, time):
movement = self.previous_movement
movement.none()
keystate = self.my_game.get_key_pressed()
if keystate & self.key_set.left > 0:
if keystate & self.key_set.up > 0:
movement.from_state('moveupleft')
elif keystate & self.key_set.down >0:
movement.from_state('movedownleft')
else:
movement.from_state('moveleft')
elif keystate & self.key_set.right > 0:
if keystate & self.key_set.up > 0:
movement.from_state('moveupright')
elif keystate & self.key_set.down >0:
movement.from_state('movedownright')
else:
movement.from_state('moveright')
elif keystate & self.key_set.up > 0:
movement.from_state('moveup')
elif keystate & self.key_set.down >0:
movement.from_state('movedown')
if keystate & self.key_set.walls >0:
if Cheats.getCheatButtonEnabled():
Cheats.toggleWalkThroughWalls()
#Cheats.toggleNoCollisions()
#Cheats.toggleNoAudio()
#Cheats.toggleNoCountdown()
#Cheats.toggleLogCollisionsInfo()
self._change_weapons = keystate & self.key_set.change_weapon >0
self._firing = keystate & self.key_set.fire >0
self._intex = keystate & self.key_set.intex >0
self._quit = keystate & self.key_set.escape >0
self._running = keystate & self.key_set.run >0
if self._running:
movement.run()
self._moving = not movement.is_none()
if self._firing and self._moving:
self._running_and_firing_last_time = time
if not self._running_and_firing:
if self._firing and self._moving:
#remember which direction we were running and firing in...
self._running_and_firing = movement
else:
if not self._firing:
self._running_and_firing = None
else:
if not self._moving and self._running_and_firing_last_time:
if time - self._running_and_firing_last_time > PlayerKeyHandler.BACKWARDS_DELAY:
self._running_and_firing = None
backwards = False
if self._running_and_firing:
if self._direction_opposite(self._running_and_firing, movement):
backwards = True
if not self._moving:
#if not dying then stop animating
if self._unit.current_state() != 'dying':
self._unit.stop()
#apply movement animation
if not movement.is_none():
if not backwards:
state = movement.to_state_name()
else:
state = movement.reverse().to_state_name()
self._unit.enter_state(state, time)
return movement
def _direction_opposite(self, movement1, movement2):
reverse = movement1.reverse()
return movement2 == reverse
def is_firing(self):
return self._firing
def change_weapons(self):
return self._change_weapons
def is_intex(self):
return self._intex
def is_quit(self):
return self._quit