Class: Asteroids::Ship

Inherits:
GameObject show all
Defined in:
lib/asteroids/ship/ship.rb

Constant Summary collapse

SHOOT_DELAY =
600

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from GameObject

#components, #draw, #mark_for_removal, #removable?, #update

Constructor Details

#initialize(object_pool) ⇒ Ship

Returns a new instance of Ship.



9
10
11
12
13
14
15
16
17
18
# File 'lib/asteroids/ship/ship.rb', line 9

def initialize(object_pool)
  super(object_pool)
  @physics = ShipPhysics.new(self, object_pool)
  @graphics = ShipGraphics.new(self)
  @vel_x = @vel_y = @angle = 0.0
  @radius = 35
  @lives = 3
  @score = 0
  @object_pool = object_pool
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def angle
  @angle
end

#livesObject

Returns the value of attribute lives.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def lives
  @lives
end

#radiusObject

Returns the value of attribute radius.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def radius
  @radius
end

#scoreObject

Returns the value of attribute score.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def score
  @score
end

#thrustObject

Returns the value of attribute thrust.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def thrust
  @thrust
end

#vel_xObject

Returns the value of attribute vel_x.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def vel_x
  @vel_x
end

#vel_yObject

Returns the value of attribute vel_y.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def vel_y
  @vel_y
end

#xObject

Returns the value of attribute x.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def x
  @x
end

#yObject

Returns the value of attribute y.



6
7
8
# File 'lib/asteroids/ship/ship.rb', line 6

def y
  @y
end

Instance Method Details

#add_score(points) ⇒ Object



46
47
48
# File 'lib/asteroids/ship/ship.rb', line 46

def add_score(points)
  @score += points
end

#can_shoot?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/asteroids/ship/ship.rb', line 27

def can_shoot?
  Gosu.milliseconds - (@last_shot || 0) > SHOOT_DELAY
end

#explodeObject



31
32
33
34
35
# File 'lib/asteroids/ship/ship.rb', line 31

def explode
  Explosion.new(object_pool, @x, @y)
  @lives -= 1
  spawn
end

#is_alive?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/asteroids/ship/ship.rb', line 37

def is_alive?
  return true if @lives != 0 or @lives > 0
end

#shootObject



20
21
22
23
24
25
# File 'lib/asteroids/ship/ship.rb', line 20

def shoot
  if can_shoot?
    @last_shot = Gosu.milliseconds
    Missile.new(self, object_pool, @x, @y, @vel_x, @vel_y, @angle)
  end
end

#spawnObject



41
42
43
44
# File 'lib/asteroids/ship/ship.rb', line 41

def spawn
  @x = object_pool.find_empty_space[:x]
  @y = object_pool.find_empty_space[:y]
end