Class: SpaceInvaders::RedInvader

Inherits:
Invader show all
Defined in:
lib/space_invaders/invaders/red_invader.rb

Instance Attribute Summary collapse

Attributes inherited from Invader

#original_x_position

Attributes included from Mesurable

#x_position, #y_position

Attributes inherited from Base

#app

Instance Method Summary collapse

Methods included from Collideable

#collides_with?, #got_hit_by?

Methods included from Mesurable

#height, #width, #x_middle

Methods inherited from Base

#game_status

Constructor Details

#initialize(app, x_position = 0, y_position = 50) ⇒ RedInvader

Returns a new instance of RedInvader.



9
10
11
12
13
14
15
16
# File 'lib/space_invaders/invaders/red_invader.rb', line 9

def initialize app, x_position=0, y_position=50
  @first_image = app.images.red_invader
  @second_image = app.images.red_invader
  @can_move = Time.now
  @direction = :right
  @dead = false
  super
end

Instance Attribute Details

#deadObject Also known as: dead?

Returns the value of attribute dead.



5
6
7
# File 'lib/space_invaders/invaders/red_invader.rb', line 5

def dead
  @dead
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/space_invaders/invaders/red_invader.rb', line 22

def alive?
  not dead?
end

#can_move?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/space_invaders/invaders/red_invader.rb', line 61

def can_move?
  Time.now - @can_move > 0.1
end

#drawObject



38
39
40
41
# File 'lib/space_invaders/invaders/red_invader.rb', line 38

def draw
  return if dead?
  @image.draw @x_position, @y_position, 1
end

#handle_deathObject



43
44
45
46
47
# File 'lib/space_invaders/invaders/red_invader.rb', line 43

def handle_death
  @dead = true
  app.score_tracker.increase_by(points)
  app.sounds.play_invader_hit!
end

#pointsObject



18
19
20
# File 'lib/space_invaders/invaders/red_invader.rb', line 18

def points
  100
end

#rival_bulletsObject



57
58
59
# File 'lib/space_invaders/invaders/red_invader.rb', line 57

def rival_bullets
  app.ship.bullets
end

#set_directionObject



49
50
51
52
53
54
55
# File 'lib/space_invaders/invaders/red_invader.rb', line 49

def set_direction
  if @x_position >= app.width - 80
    @direction = :left
  elsif @x_position <= 20
    @direction = :right
  end
end

#updateObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/space_invaders/invaders/red_invader.rb', line 26

def update
  return if dead?

  if collides_with? rival_bullets
    handle_death
  elsif can_move?
    set_direction
    @x_position += @direction == :right ? 10 : -10
    @can_move = Time.now
  end
end