Game Development Reference
In-Depth Information
84
@sand
85
else
# 55% chance
86
@grass
87
end
88
end
89
end
This implementation is very similar to the
Map
we had built in “Generating Random Map
With Perlin Noise”, with some extra additions.
can_move_to?
verifies if tile under
given coordinates is not water. Pretty simple, but it's enough for our prototype.
Also, when we
draw
the map we have to make sure if tiles we are drawing are currently
visible by our camera, otherwise we will end up drawing off screen.
camera.can_view?
handles it. Current implementation will probably be causing a
bottleneck, since it brute forces through all the map rather than cherry-picking the visible
region. We will probably have to get back and change it later.
find_spawn_point
is one more addition. It keeps picking a random point on map and
verifies if it's not water using
can_move_to?
. When solid tile is found, it returns the
coordinates, so our
Tank
will be able to spawn there.
Implementing Floating Camera
If you played the original
Grand Theft Auto
or GTA 2, you should remember how
fascinating the camera was. It backed away when you were driving at high speeds, closed
in when you were walking on foot, and floated around as if a smart drone was following
your protagonist from above.
The following
Camera
implementation is far inferior to the one GTA had nearly two
decades ago, but it's a start:
03-prototype/entities/camera.rb
1
class
Camera
2
attr_accessor
:x
,
:y
,
:zoom
3
4
def
initialize
(target)
5
@target
=
target
6
@x
,
@y
=
target
.
x, target
.
y
7
@zoom
= 1
8
end
9