Class: Asteroids::ObjectPool

Inherits:
Object
  • Object
show all
Defined in:
lib/asteroids/componenets/game_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObjectPool

Returns a new instance of ObjectPool.



5
6
7
# File 'lib/asteroids/componenets/game_pool.rb', line 5

def initialize
  @objects = []
end

Instance Attribute Details

#objectsObject

Returns the value of attribute objects.



3
4
5
# File 'lib/asteroids/componenets/game_pool.rb', line 3

def objects
  @objects
end

Instance Method Details

#find_empty_spaceObject



14
15
16
17
18
19
20
21
22
# File 'lib/asteroids/componenets/game_pool.rb', line 14

def find_empty_space
  x = Gosu::random(0, 800)
  y = Gosu::random(0, 600)
  while is_not_empty(x, y)
    x = Gosu::random(0, 800)
    y = Gosu::random(0, 600)
  end
  {x: x, y: y}
end

#is_not_empty(x, y) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/asteroids/componenets/game_pool.rb', line 24

def is_not_empty(x, y)
  @objects.each do |object|
    if object.is_a? Asteroids::Asteroid and object.x == x and object.y == y
      return true
    end
  end
  false
end

#update_allObject



9
10
11
12
# File 'lib/asteroids/componenets/game_pool.rb', line 9

def update_all
  @objects.map(&:update)
  @objects.delete_if { |object| object.removable?}
end