Game Development Reference
In-Depth Information
4 def initialize (object_pool, x, y, seed)
5 super (object_pool)
6 @x , @y = x, y
7 @graphics = TreeGraphics . new( self , seed)
8 @health = Health . new( self , object_pool, 30 , false )
9 @angle = rand ( -15. . 15 )
10 end
11
12 def on_collision (object)
13 @graphics . shake(object . direction)
14 end
15
16 def box
17 [ x, y ]
18 end
19 end
Nothing fancy here, we want it to shake on collision, and it has graphics and health. seed
will used to generate clusters of similar trees. Let's take a look at TreeGraphics :
09-polishing/entities/components/tree_graphics.rb
1 class TreeGraphics < Component
2 SHAKE_TIME = 100
3 SHAKE_COOLDOWN = 200
4 SHAKE_DISTANCE = [2 , 1 , 0 , -1 , -2 , -1 , 0 , 1 , 0 , -1 , 0]
5 def initialize (object, seed)
6 super (object)
7 load_sprite(seed)
8 end
9
10 def shake (direction)
11 now = Gosu . milliseconds
12 returnif @shake_start &&
13 now - @shake_start < SHAKE_TIME + SHAKE_COOLDOWN
14 @shake_start = now
15 @shake_direction = direction
16 @shaking = true
17 end
18
19 def adjust_shake (x, y, shaking_for)
20 elapsed = [ shaking_for, SHAKE_TIME ]. min / SHAKE_TIME . to_f
21 frame = (( SHAKE_DISTANCE . length - 1 ) * elapsed) . floor
22 distance = SHAKE_DISTANCE [ frame ]
23 Utils . point_at_distance(x, y, @shake_direction , distance)
24 end
Search WWH ::




Custom Search