Game Development Reference
In-Depth Information
Damage tracks it's instances and starts removing old ones when MAX_INSTANCES are
reached. Without this optimization, the game would get increasingly slower every time
somebody shoots.
We have also added a new game object trait - effect? returns true on Damage and
Explosion , false on Tank , Tree , Box and Bullet . That way we can filter out effects
when querying ObjectPool#nearby for collisions or enemies.
09-polishing/entities/object_pool.rb
1 class ObjectPool
2
attr_accessor :objects , :map , :camera
3
4 def initialize
5 @objects = []
6 end
7
8 def nearby (object, max_distance)
9
non_effects . select do | obj |
obj != object &&
10
(obj . x - object . x) . abs < max_distance &&
11
(obj . y - object . y) . abs < max_distance &&
12
Utils . distance_between(
13
obj . x, obj . y, object . x, object . y) < max_distance
14
15 end
16 end
17
18 def non_effects
19 @objects . reject( & :effect? )
20 end
21 end
When it comes to rendering graphics, to make an impression of randomness, we will cycle
through several different damage images and draw them rotated:
09-polishing/entities/components/damage_graphics.rb
1 class DamageGraphics < Component
2 def initialize (object_pool)
3 super
4 @image = images . sample
5 @angle = rand ( 0. . 360 )
6 end
7
8 def draw (viewport)
Search WWH ::




Custom Search