-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInClassChallenge4.2.2.rb
More file actions
97 lines (71 loc) · 2.16 KB
/
InClassChallenge4.2.2.rb
File metadata and controls
97 lines (71 loc) · 2.16 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
# To add a keyboard, I would assign two keys to be the far left and right x axes, and two keys to be the far top and bottom
# y axes, keeping it wrapped in another class. Another key would be assigned the fire lasers button.
# The original author of the code was short-sighted in that they only thought about the code working with one specific kind of controller,
# when in reality many controllers should be able to be used for games. It also relied on the original API not changing, and if it did
# the author would've had to fix the code to work with the updated API.
# You can't modify this code
class SidewinderJoystick
BUTTON_UP = 2
BUTTON_DOWN = 3
def get_x_axis(stick_id) # returns -1.0 to 1.0
def get_y_axis(stick_id) # returns -1.0 to 1.0
def button(button_id) # Returns BUTTON_UP or BUTTON_DOWN
end
# You can't modify this code
class XboxGamepad
def get_x_axis(stick_id) # returns -128 to 127
def get_y_axis(stick_id) # returns -128 to 127
def button_status(button_id) # float from 0.0 (up) to 1.0 (down)
end
class WrappedSidewinderJoystick
def x(stick_id)
SideWinderJoystick.get_x_axis(stick_id)
end
def y(stick_id)
SideWinderJoystick.get_y_axis(stick_id)
end
def lasers_fired?
if SideWinderJoystick.button(4) == BUTTON_DOWN
true
else
false
end
end
class WrappedXboxGamepad
def x(stick_id)
XboxGamepad.get_x_axis(stick_id) / 128
end
def y(stick_id)
XboxGamepad.get_y_axis(stick_id) / 128
end
def lasers_fired?
if XboxGamepad.button_status(4) == 1.0
true
else
false
end
end
end
class Player
def initialize(joystick)
@x = 0
@y = 0
@joystick = joystick
end
def update() # called every game frame
@x += joystick.x(1)
@y += joystick.y(1)
if joystick.lasers_fired?
self.fire_lasers()
end
self.update_graphics()
end
# ...
end
if config.joystick == SIDEWINDER
joystick = WrappedSidewinderJoystick.new
elsif config.joystick == XBOX
joystick = WrappedXboxController.new
end
p = Player.new(joystick)
# ... Game code continues ...