Class: SpaceInvaders::Bullet

Inherits:
Base
  • Object
show all
Defined in:
lib/space_invaders/bullets/bullet.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods inherited from Base

#game_status

Constructor Details

#initialize(fireing_vehicle, going_down, bullet_collection, bullet_offset = 10) ⇒ Bullet

Returns a new instance of Bullet.



8
9
10
11
12
13
14
15
16
17
# File 'lib/space_invaders/bullets/bullet.rb', line 8

def initialize fireing_vehicle, going_down, bullet_collection, bullet_offset=10
  super(fireing_vehicle.app)
  @fireing_vehicle = fireing_vehicle
  @image = app.images.bullet
  @bullet_offset = bullet_offset
  @going_down = going_down
  @bullet_collection = bullet_collection
  @bullet_collection << self
  set_position
end

Instance Attribute Details

#bullet_collectionObject (readonly)

Returns the value of attribute bullet_collection.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def bullet_collection
  @bullet_collection
end

#bullet_offsetObject (readonly)

Returns the value of attribute bullet_offset.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def bullet_offset
  @bullet_offset
end

#fireing_vehicleObject (readonly)

Returns the value of attribute fireing_vehicle.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def fireing_vehicle
  @fireing_vehicle
end

#going_downObject (readonly) Also known as: going_down?

Returns the value of attribute going_down.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def going_down
  @going_down
end

#imageObject (readonly)

Returns the value of attribute image.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def image
  @image
end

#x_positionObject (readonly)

Returns the value of attribute x_position.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def x_position
  @x_position
end

#y_positionObject (readonly)

Returns the value of attribute y_position.



4
5
6
# File 'lib/space_invaders/bullets/bullet.rb', line 4

def y_position
  @y_position
end

Instance Method Details

#deleteObject



27
28
29
# File 'lib/space_invaders/bullets/bullet.rb', line 27

def delete
  bullet_collection.delete(self)
end

#drawObject



23
24
25
# File 'lib/space_invaders/bullets/bullet.rb', line 23

def draw
  image.draw x_position, y_position, 1
end

#out_of_screenObject



31
32
33
34
35
36
37
# File 'lib/space_invaders/bullets/bullet.rb', line 31

def out_of_screen
  if going_down
    y_position > app.height
  else
    y_position < 0
  end
end

#set_positionObject



39
40
41
42
43
# File 'lib/space_invaders/bullets/bullet.rb', line 39

def set_position
  @x_position = fireing_vehicle.x_middle - image.width/2
  @y_position = fireing_vehicle.y_position
  @y_position += fireing_vehicle.height if going_down
end

#updateObject



19
20
21
# File 'lib/space_invaders/bullets/bullet.rb', line 19

def update
  @y_position += going_down? ? bullet_offset : -bullet_offset
end