Game Development Reference
In-Depth Information
Respawning Tanks And Removing Dead Ones
To implement respawning we could use Map#find_spawn_point every time we
wanted to respawn, but it may get slow, because it brute forces the map for random spots
that are not water. We don't want our game to start freezing when tanks are respawning, so
we will change how tank spawning works. Instead of looking for a new respawn point all
the time, we will pre-generate several of them for reuse.
class Map
# ...
def spawn_points (max)
@spawn_points = ( 0. .max) . map do
find_spawn_point
end
@spawn_points_pointer = 0
end
def spawn_point
@spawn_points [ ( @spawn_points_pointer += 1 ) % @spawn_points . size ]
end
# ...
end
Here we have spawn_points method that prepares a number of spawn points and stores
them in @spawn_points instance variable, and spawn_point method that cycles
through all @spawn_points and returns them one by one. find_spawn_point can
now become private .
We will use Map#spawn_points when initializing PlayState and pass
ObjectPool to PlayerInput ( AiInput already has it), so that we will be able to
call @object_pool.map.spawn_point when needed.
class PlayState < GameState
# ...
def initialize
# ...
@map = Map . new( @object_pool )
@map . spawn_points( 15 )
@tank = Tank . new( @object_pool ,
PlayerInput . new( 'Player' , @camera , @object_pool ))
# ...
10. times do | i |
Tank . new( @object_pool , AiInput . new(
Search WWH ::




Custom Search