Skip to content

Instantly share code, notes, and snippets.

@Sephi-Chan
Created November 8, 2012 18:35
Show Gist options
  • Save Sephi-Chan/4040637 to your computer and use it in GitHub Desktop.
Save Sephi-Chan/4040637 to your computer and use it in GitHub Desktop.
frequency = 50/1000 # 50 ms
loop do
moving_objects = Ship.all + Projectile.all
moving_objects.each do |moving_object|
moving_object.move
moving_object.collisions.each do |collision|
collision.object.collides_with(collision.other_object)
end
end
sleep(frequency)
end
class MovingObject < ActiveRecord::Base
def move
# Move the object.
end
def collisions
# Find things that collides with me.
end
end
class Ship < MovingObject
def collides_with(other_object)
if other_object.is_a(Ship)
explode
other_object.explode
end
end
def receive_damage(damage)
current_health -= damage
explode if current_health == 0
end
def explode
destroy
end
end
class Projectile < MovingObject
def collides_with(other_object)
if other_object.is_a?(Ship)
damage_ship(other_object)
elsif other_object.is_a?(Projectile)
destroy_projectile(other_object)
end
end
def damage_ship(ship)
final_damage = power - ship.armor
ship.receive_damage(final_damage)
end
def destroy_projectile(projectile)
projectile.explode
end
def explode
destroy
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment