robc / gosu-asteroids

A simple clone of Asteroids written in Ruby using the Gosu game development framework

gosu-asteroids / asteroid.rb
100644 32 lines (25 sloc) 0.865 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'game_constants'
require 'game_object'
require 'conversions'
 
class Asteroid < GameObject
  include GameConstants
  
  attr_accessor :asteroid_size, :object_image
  
  def initialize(asteroid_size, asteroid_image, bounding_sphere_radius)
    super(asteroid_image, bounding_sphere_radius)
    @asteroid_size = asteroid_size
    @angle_rotation = (rand(AsteroidRotationSpeed * 2) - AsteroidRotationSpeed)
  end
  
  def set_forward_velocity(velocity)
    angle_in_rad = Conversions.transform_degrees_to_radians(@angle - 90)
    
    @velocity_x = Math.cos(angle_in_rad)
    @velocity_y = Math.sin(angle_in_rad)
  end
 
  def set_asteroid_size_and_image(asteroid_size, asteroid_image)
    @asteroid_size = asteroid_size
    @object_image = asteroid_image
  end
  
  def update
    super
    @angle = Conversions.limit_angle((@angle + @angle_rotation))
  end
end