Game Development Reference
In-Depth Information
First, let's recall how Powerup#remove method looks like:
class Powerup < GameObject
# ...
def remove
object_pool . powerup_respawn_queue . enqueue(
respawn_delay,
self . class, x, y)
mark_for_removal
end
# ...
end
Powerup enqueues itself for respawn when picked up, providing it's class and coordinates.
PowerupRespawnQueue holds this data and respawns powerups at right time with help
of ObjectPool :
11-powerups/entities/powerups/powerup_respawn_queue.rb
1 class PowerupRespawnQueue
2 RESPAWN_DELAY = 1000
3 def initialize
4 @respawn_queue = {}
5 @last_respawn = Gosu . milliseconds
6 end
7
8 def enqueue (delay_seconds, type, x, y)
9 respawn_at = Gosu . milliseconds + delay_seconds * 1000
10 @respawn_queue [ respawn_at . to_i ] = [ type, x, y ]
11 end
12
13 def respawn (object_pool)
14 now = Gosu . milliseconds
15 returnif now - @last_respawn < RESPAWN_DELAY
16 @respawn_queue . keys . each do | k |
17 nextif k > now # not yet
18 type, x, y = @respawn_queue . delete(k)
19 type . new(object_pool, x, y)
20 end
21 @last_respawn = now
22 end
23 end
PowerupRespawnQeueue#respawn is called from ObjectPool#update_all ,
but is throttled to run once per second for better performance.
Search WWH ::




Custom Search