Game Development Reference
In-Depth Information
56 end
57 angle = (angle + 360 ) % 360 if angle && angle < 0
58 (angle || previous_angle)
59 end
60 end
We finally come to a place where keyboard and mouse input is handled and converted
to Tank commands. We could have used Command pattern to decouple everything even
further.
Refactoring PlayState
05-refactor/game_states/play_state.rb
1 require 'ruby-prof' if ENV [ 'ENABLE_PROFILING' ]
2 class PlayState < GameState
3
attr_accessor :update_interval
4
5 def initialize
6 @map = Map . new
7 @camera = Camera . new
8 @object_pool = ObjectPool . new( @map )
9 @tank = Tank . new( @object_pool , PlayerInput . new( @camera ))
10 @camera . target = @tank
11 end
12
13 def enter
14 RubyProf . start if ENV [ 'ENABLE_PROFILING' ]
15 end
16
17 def leave
18 if ENV [ 'ENABLE_PROFILING' ]
19 result = RubyProf . stop
20 printer = RubyProf :: FlatPrinter . new(result)
21 printer . print( STDOUT )
22 end
23 end
24
25 def update
26 @object_pool . objects . map( & :update )
27 @object_pool . objects . reject!( & :removable? )
28 @camera . update
29 update_caption
30 end
31
32 def draw
Search WWH ::




Custom Search