Created
November 8, 2012 18:35
-
-
Save Sephi-Chan/4040637 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MovingObject < ActiveRecord::Base | |
def move | |
# Move the object. | |
end | |
def collisions | |
# Find things that collides with me. | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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