Game Development Reference
In-Depth Information
8 @input . control( self )
9 @physics = TankPhysics . new( self , object_pool)
10 @graphics = TankGraphics . new( self )
11 @sounds = TankSounds . new( self )
12 @direction = @gun_angle = 0.0
13 end
14
15 def shoot (target_x, target_y)
16 if Gosu . milliseconds - ( @last_shot || 0 ) > SHOOT_DELAY
17 @last_shot = Gosu . milliseconds
18 Bullet . new(object_pool, @x , @y , target_x, target_y) . fire( 100 )
19 end
20 end
21 end
Tank class was reduced over 5 times. We could go further and extract Gun component,
but for now it's simple enough already. Now, the components.
05-refactor/entities/components/tank_physics.rb
1 class TankPhysics < Component
2
attr_accessor :speed
3
4 def initialize (game_object, object_pool)
5 super (game_object)
6 @object_pool = object_pool
7 @map = object_pool . map
8 game_object . x, game_object . y = @map . find_spawn_point
9 @speed = 0.0
10 end
11
12 def can_move_to? (x, y)
13 @map . can_move_to?(x, y)
14 end
15
16 def moving?
17 @speed > 0
18 end
19
20 def update
21 if object . throttle_down
22 accelerate
23 else
24 decelerate
25 end
26 if @speed > 0
Search WWH ::




Custom Search