-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasteroid.rb
51 lines (41 loc) · 1.1 KB
/
asteroid.rb
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Asteroid
attr_reader :x, :y, :size, :angle, :speed, :radious
def initialize(img, father = nil, angle = nil)
@img = img
if father.nil?
@x = rand * 640
@y = rand * 480
@size = 1 # Size could be 1, 2 or 4
@view_angle = @angle = rand(359)
@speed = rand(4) + 1
else
@x = father.x
@y = father.y
@size = father.size * 2
@view_angle = @angle = angle
@speed = father.speed + rand(2)
end
@radious = img.width / 2.0 / @size
end
def main_asteroid
@size == 1
end
def final_asteroid?
@size == 4
end
def update
@x += Gosu.offset_x(@angle, @speed)
@y += Gosu.offset_y(@angle, @speed)
@view_angle += @speed
return if in_screen?
@x %= SampleWindow::SCREEN_WIDTH + @radious
@y %= SampleWindow::SCREEN_HEIGHT + @radious
end
def draw
@img.draw_rot(@x, @y, ZOrder::STARS, @view_angle, 0.5, 0.5, 1.0 / @size, 1.0 / @size)
end
private
def in_screen?
(- @radious < @x && @x < SampleWindow::SCREEN_WIDTH + @radious) && (- @radious < @y && @y < SampleWindow::SCREEN_HEIGHT + @radious)
end
end