Class: Asteroids::AsteroidPhysics
- Defined in:
- lib/asteroids/asteroid/asteroid_physics.rb
Instance Method Summary collapse
- #detect_collision? ⇒ Boolean
-
#initialize(game_object, object_pool) ⇒ AsteroidPhysics
constructor
A new instance of AsteroidPhysics.
- #update ⇒ Object
Methods inherited from Component
Constructor Details
#initialize(game_object, object_pool) ⇒ AsteroidPhysics
Returns a new instance of AsteroidPhysics.
4 5 6 7 |
# File 'lib/asteroids/asteroid/asteroid_physics.rb', line 4 def initialize(game_object, object_pool) super(game_object) @object_pool = object_pool end |
Instance Method Details
#detect_collision? ⇒ Boolean
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/asteroids/asteroid/asteroid_physics.rb', line 39 def detect_collision? @object_pool.objects.each do |other_object| if other_object.is_a? Asteroids::Missile or other_object.is_a? Asteroids::Ship and Utils.collide(object, other_object) return true end end return false end |
#update ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/asteroids/asteroid/asteroid_physics.rb', line 9 def update if detect_collision? @explosion_time = Gosu.milliseconds object.explode end if @explosion_time if Gosu.milliseconds - @explosion_time > 100 object.mark_for_removal if object.radius == 45 2.times do |n| Asteroid.new(@object_pool, object.x + rand(20), object.y + rand(20), rand * (-1.5), rand() * 1.5, rand * 3, (object.radius - 15)) end end if object.radius == 30 2.times do |n| Asteroid.new(@object_pool, object.x + rand(20), object.y + rand(20), rand() * (-2.2), rand() * 2.2, rand * 3, (object.radius - 12.5)) end end end end object.x += Gosu::offset_x(object.angle, 0.0001) + object.vel_x object.y += Gosu::offset_y(object.angle, 0.0001) + object.vel_y object.x %= 800 object.y %= 600 end |