Class: SpaceInvaders::Block

Inherits:
Base
  • Object
show all
Includes:
Collideable
Defined in:
lib/space_invaders/blocks/block.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods included from Collideable

#collides_with?, #got_hit_by?

Methods inherited from Base

#game_status

Constructor Details

#initialize(u_block, x_position, y_position) ⇒ Block

Returns a new instance of Block.



10
11
12
13
14
15
16
17
18
# File 'lib/space_invaders/blocks/block.rb', line 10

def initialize u_block, x_position, y_position
  super(u_block.app)

  @u_block = u_block
  @x_position = x_position
  @y_position = y_position
  @health = 3
  set_image
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



8
9
10
# File 'lib/space_invaders/blocks/block.rb', line 8

def health
  @health
end

#u_blockObject

Returns the value of attribute u_block.



8
9
10
# File 'lib/space_invaders/blocks/block.rb', line 8

def u_block
  @u_block
end

#x_positionObject

Returns the value of attribute x_position.



8
9
10
# File 'lib/space_invaders/blocks/block.rb', line 8

def x_position
  @x_position
end

#y_positionObject

Returns the value of attribute y_position.



8
9
10
# File 'lib/space_invaders/blocks/block.rb', line 8

def y_position
  @y_position
end

Instance Method Details

#dead?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/space_invaders/blocks/block.rb', line 40

def dead?
  health.zero?
end

#downgrade_image_or_dieObject



44
45
46
47
48
49
50
51
# File 'lib/space_invaders/blocks/block.rb', line 44

def downgrade_image_or_die
  self.health -= 1
  if health > 0
    set_image 
  else
    u_block.delete(self)
  end
end

#drawObject



24
25
26
# File 'lib/space_invaders/blocks/block.rb', line 24

def draw
  @image.draw x_position, y_position, 1
end

#heightObject



36
37
38
# File 'lib/space_invaders/blocks/block.rb', line 36

def height
  @image.height
end

#rival_bulletsObject



28
29
30
# File 'lib/space_invaders/blocks/block.rb', line 28

def rival_bullets
  app.ship.bullets.bullets + app.invaders_container.bullets.bullets
end

#set_imageObject



53
54
55
56
57
58
59
# File 'lib/space_invaders/blocks/block.rb', line 53

def set_image
  @image = case health
           when 3 then app.images.full_block
           when 2 then app.images.ok_block
           when 1 then app.images.weak_block
           end
end

#updateObject



20
21
22
# File 'lib/space_invaders/blocks/block.rb', line 20

def update
  downgrade_image_or_die if collides_with? rival_bullets
end

#widthObject



32
33
34
# File 'lib/space_invaders/blocks/block.rb', line 32

def width
  @image.width
end