Game Development Reference
In-Depth Information
4
5 def initialize (map)
6 @map = map
7 @units = Gosu :: TexturePacker . load_json(
8 $window , Game . media_path( 'ground_units.json' ), :precise )
9 @body = @units . frame( 'tank1_body.png' )
10 @shadow = @units . frame( 'tank1_body_shadow.png' )
11 @gun = @units . frame( 'tank1_dualgun.png' )
12 @x , @y = @map . find_spawn_point
13 @body_angle = 0.0
14 @gun_angle = 0.0
15 @last_shot = 0
16 sound . volume = 0.3
17 end
18
19 def sound
20
@@sound ||= Gosu :: Song . new(
$window , Game . media_path( 'tank_driving.mp3' ))
21
22 end
23
24 def shoot (target_x, target_y)
25 if Gosu . milliseconds - @last_shot > SHOOT_DELAY
26 @last_shot = Gosu . milliseconds
27 Bullet . new( @x , @y , target_x, target_y) . fire( 100 )
28 end
29 end
30
31 def update (camera)
32 d_x, d_y = camera . target_delta_on_screen
33 atan = Math . atan2(( $window . width / 2 ) - d_x - $window . mouse_x,
34 ( $window . height / 2 ) - d_y - $window . mouse_y)
35 @gun_angle = - atan * 180 / Math :: PI
36 new_x, new_y = @x , @y
37 new_x -= speed if $window . button_down?( Gosu :: KbA )
38 new_x += speed if $window . button_down?( Gosu :: KbD )
39 new_y -= speed if $window . button_down?( Gosu :: KbW )
40 new_y += speed if $window . button_down?( Gosu :: KbS )
41 if @map . can_move_to?(new_x, new_y)
42 @x , @y = new_x, new_y
43 else
44 @speed = 1.0
45 end
46
@body_angle = change_angle( @body_angle ,
Gosu :: KbW , Gosu :: KbS , Gosu :: KbA , Gosu :: KbD )
47
48
49 if moving?
Search WWH ::




Custom Search