Game Development Reference
In-Depth Information
11
def
update
12
d_x, d_y
=
@camera
.
target_delta_on_screen
atan
=
Math
.
atan2((
$window
.
width
/ 2
)
-
d_x
-
$window
.
mouse_x,
13
(
$window
.
height
/ 2
)
-
d_y
-
$window
.
mouse_y)
14
object
.
gun_angle
= -
atan
* 180 /
Math
::
PI
15
motion_buttons
= [
Gosu
::
KbW
,
Gosu
::
KbS
,
Gosu
::
KbA
,
Gosu
::
KbD
]
16
17
18
if
any_button_down?(
*
motion_buttons)
19
object
.
throttle_down
=
true
20
object
.
direction
=
change_angle(object
.
direction,
*
motion_buttons)
21
else
22
object
.
throttle_down
=
false
23
end
24
25
if
Utils
.
button_down?(
Gosu
::
MsLeft
)
26
object
.
shoot(
*
@camera
.
mouse_coords)
27
end
28
end
29
30
private
31
32
def
any_button_down?
(
*
buttons)
33
buttons
.
each
do
|
b
|
34
return
true
if
Utils
.
button_down?(b)
35
end
36
false
37
end
38
39
def
change_angle
(previous_angle, up, down, right, left)
40
if
Utils
.
button_down?(up)
41
angle
= 0.0
42
angle
+= 45.0
if
Utils
.
button_down?(left)
43
angle
-= 45.0
if
Utils
.
button_down?(right)
44
elsif
Utils
.
button_down?(down)
45
angle
= 180.0
46
angle
-= 45.0
if
Utils
.
button_down?(left)
47
angle
+= 45.0
if
Utils
.
button_down?(right)
48
elsif
Utils
.
button_down?(left)
49
angle
= 90.0
50
angle
+= 45.0
if
Utils
.
button_down?(up)
51
angle
-= 45.0
if
Utils
.
button_down?(down)
52
elsif
Utils
.
button_down?(right)
53
angle
= 270.0
angle
-= 45.0
if
Utils
.
button_down?(up)
54
angle
+= 45.0
if
Utils
.
button_down?(down)
55