Class: ObjectPool
- Inherits:
-
Object
- Object
- ObjectPool
- Defined in:
- lib/entities/object_pool.rb
Instance Attribute Summary collapse
-
#camera ⇒ Object
Returns the value of attribute camera.
-
#map ⇒ Object
Returns the value of attribute map.
-
#objects ⇒ Object
Returns the value of attribute objects.
-
#powerup_respawn_queue ⇒ Object
Returns the value of attribute powerup_respawn_queue.
Instance Method Summary collapse
- #add(object) ⇒ Object
-
#initialize(box) ⇒ ObjectPool
constructor
A new instance of ObjectPool.
- #nearby(object, max_distance) ⇒ Object
- #nearby_point(cx, cy, max_distance, object = nil) ⇒ Object
- #query_range(box) ⇒ Object
- #size ⇒ Object
- #tree_insert(object) ⇒ Object
- #tree_remove(object) ⇒ Object
- #update_all ⇒ Object
Constructor Details
#initialize(box) ⇒ ObjectPool
Returns a new instance of ObjectPool.
8 9 10 11 12 |
# File 'lib/entities/object_pool.rb', line 8 def initialize(box) @tree = QuadTree.new(box) @powerup_respawn_queue = PowerupRespawnQueue.new @objects = [] end |
Instance Attribute Details
#camera ⇒ Object
Returns the value of attribute camera.
2 3 4 |
# File 'lib/entities/object_pool.rb', line 2 def camera @camera end |
#map ⇒ Object
Returns the value of attribute map.
2 3 4 |
# File 'lib/entities/object_pool.rb', line 2 def map @map end |
#objects ⇒ Object
Returns the value of attribute objects.
2 3 4 |
# File 'lib/entities/object_pool.rb', line 2 def objects @objects end |
#powerup_respawn_queue ⇒ Object
Returns the value of attribute powerup_respawn_queue.
2 3 4 |
# File 'lib/entities/object_pool.rb', line 2 def powerup_respawn_queue @powerup_respawn_queue end |
Instance Method Details
#add(object) ⇒ Object
14 15 16 17 |
# File 'lib/entities/object_pool.rb', line 14 def add(object) @objects << object @tree.insert(object) end |
#nearby(object, max_distance) ⇒ Object
51 52 53 54 |
# File 'lib/entities/object_pool.rb', line 51 def nearby(object, max_distance) cx, cy = object.location nearby_point(cx, cy, max_distance, object) end |
#nearby_point(cx, cy, max_distance, object = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/entities/object_pool.rb', line 38 def nearby_point(cx, cy, max_distance, object = nil) hx, hy = cx + max_distance, cy + max_distance # Fast, rough results results = @tree.query_range( AxisAlignedBoundingBox.new([cx, cy], [hx, hy])) # Sift through to select fine-grained results results.select do |o| o != object && Utils.distance_between( o.x, o.y, cx, cy) <= max_distance end end |
#query_range(box) ⇒ Object
56 57 58 |
# File 'lib/entities/object_pool.rb', line 56 def query_range(box) @tree.query_range(box) end |
#size ⇒ Object
4 5 6 |
# File 'lib/entities/object_pool.rb', line 4 def size @objects.size end |
#tree_insert(object) ⇒ Object
23 24 25 |
# File 'lib/entities/object_pool.rb', line 23 def tree_insert(object) @tree.insert(object) end |
#tree_remove(object) ⇒ Object
19 20 21 |
# File 'lib/entities/object_pool.rb', line 19 def tree_remove(object) @tree.remove(object) end |
#update_all ⇒ Object
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/entities/object_pool.rb', line 27 def update_all @objects.each(&:update) @objects.reject! do |o| if o.removable? @tree.remove(o) true end end @powerup_respawn_queue.respawn(self) end |