Game Development Reference
In-Depth Information
While we had to rip player input away from it's movement, we got ourselves a benefit -
tank now both accelerates and decelerates. When directional buttons are no longer pressed,
tank keeps moving in last direction, but quickly decelerates and stops. Another addition
that would have been more difficult to implement on previous Tank is collision sound.
When Tank abruptly stops by hitting something (for now it's only water), collision sound
is played. We will have to fix that, because metal bang is not appropriate when you stop on
the edge of a river, but we now did it for the sake of science.
05-refactor/entities/components/tank_graphics.rb
1 class TankGraphics < Component
2 def initialize (game_object)
3 super (game_object)
4 @body = units . frame( 'tank1_body.png' )
5 @shadow = units . frame( 'tank1_body_shadow.png' )
6 @gun = units . frame( 'tank1_dualgun.png' )
7 end
8
9 def draw (viewport)
10 @shadow . draw_rot(x - 1 , y - 1 , 0 , object . direction)
11 @body . draw_rot(x, y, 1 , object . direction)
12 @gun . draw_rot(x, y, 2 , object . gun_angle)
13 end
14
15
private
16
17 def units
18
@@units = Gosu :: TexturePacker . load_json(
$window , Utils . media_path( 'ground_units.json' ), :precise )
19
20 end
21 end
Again, graphics are neatly packed and separated from everything else. Eventually we
should optimize draw to take viewport into consideration, but it's good enough for now,
especially when we have only one tank in the game.
05-refactor/entities/components/tank_sounds.rb
1 class TankSounds < Component
2 def update
3 if object . physics . moving?
4 if @driving && @driving . paused?
5 @driving . resume
6 elsif @driving . nil?
7
@driving = driving_sound . play( 1 , 1 , true )
Search WWH ::




Custom Search