Game Development Reference
In-Depth Information
56
def
draw_crosshair
57
x
=
$window
.
mouse_x
y
=
$window
.
mouse_y
58
$window
.
draw_line(
59
x
- 10
, y,
Gosu
::
Color
::
RED
,
60
x
+ 10
, y,
Gosu
::
Color
::
RED
,
100
)
61
$window
.
draw_line(
62
x, y
- 10
,
Gosu
::
Color
::
RED
,
63
x, y
+ 10
,
Gosu
::
Color
::
RED
,
100
)
64
65
end
66
67
private
68
69
def
viewport
70
x0
=
@x
-
(
$window
.
width
/ 2
)
/
@zoom
71
x1
=
@x
+
(
$window
.
width
/ 2
)
/
@zoom
72
y0
=
@y
-
(
$window
.
height
/ 2
)
/
@zoom
73
y1
=
@y
+
(
$window
.
height
/ 2
)
/
@zoom
74
[
x0, x1, y0, y1
]
75
end
76
end
Our
Camera
has
@target
that it tries to follow,
@x
and
@y
that it currently is looking at,
and
@zoom
level.
All the magic happens in
update
method. It keeps track of the distance between
@target
and adjust itself to stay nearby. And when
@target.speed
shows some
movement momentum, camera slowly backs away.
Camera
also tels if you
can_view?
an object at some coordinates, so when other entities
draw themselves, they can check if there is a need for that.
Another noteworthy method is
mouse_coords
. It translates mouse position on screen to
mouse position on map, so the game will know where you are targeting your guns.
Implementing The Tank
Most of our tank code will be taken from “Player Movement With Keyboard And Mouse”:
03-prototype/entities/tank.rb
1
class
Tank
2
attr_accessor
:x
,
:y
,
:body_angle
,
:gun_angle
SHOOT_DELAY
= 500
3