class SpaceInvaders::InvadersContainer

Attributes

invader_rows[R]

Public Class Methods

new(app) click to toggle source
Calls superclass method SpaceInvaders::Base::new
# File lib/space_invaders/invaders/invaders_container.rb, line 10
def initialize app
  super
  create_invader_rows
  @change_time = Time.now
  @can_fire = Time.now
  @direction = :right
  @y_offset = 0
end

Public Instance Methods

any_invaders?() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 49
def any_invaders?
  not count.zero?
end
count() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 45
def count
  invader_rows.map {|invader_row| invader_row.count}.inject(:+) || 0
end
draw() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 40
def draw
  invader_rows.each {|invader_row| invader_row.draw }
  bullets.draw
end
no_invaders?() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 53
def no_invaders?
  count.zero?
end
reinitialize!() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 57
def reinitialize!
  create_invader_rows
  bullets.clear
  rival_bullets.clear
  @change_time = Time.now
  @can_fire = Time.now
end
update() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 19
def update
  check_collision

  if no_invaders?
    app.next_level_screen.timer_start!
    return game_status.next_level!
  end

  if can_change?
    change_direction
    @change_time = Time.now
  end

  if can_fire?
    fire!
    @can_fire = Time.now
  end

  bullets.update
end

Private Instance Methods

can_change?() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 108
def can_change?
  Time.now > @change_time + 0.25
end
can_fire?() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 104
def can_fire?
  Time.now > @can_fire + 2
end
change_direction() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 79
def change_direction
  if farmost_right_position >= app.width - 80
    @direction = :left
    @y_offset = 10
  elsif farmost_left_position <= 20
    @direction = :right
    @y_offset = 10
  else
    @y_offset = 0
  end
  update_direction(@direction, @y_offset)
end
check_collision() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 67
def check_collision
  invader_rows.each { |invader_row| invader_row.check_collision(rival_bullets) }
  invader_rows.delete_if {|invader_row| invader_row.empty?}
  hit_rock_bottom? if any_invaders?
end
create_invader_rows() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 120
def create_invader_rows
  @invader_rows = [
    InvaderRow.new(app, 100, InvaderA),
    InvaderRow.new(app, 150, InvaderB),
    InvaderRow.new(app, 200, InvaderB),
    InvaderRow.new(app, 250, InvaderC),
    InvaderRow.new(app, 300, InvaderC),
  ]
end
farmost_left_position() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 140
def farmost_left_position
  invader_rows.min_by do |invader_row|
    invader_row.farmost_left_position
  end.farmost_left_position
end
farmost_right_position() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 134
def farmost_right_position
  invader_rows.max_by do |invader_row|
    invader_row.farmost_right_position
  end.farmost_right_position
end
fireable_invaders() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 112
def fireable_invaders
  InvaderRow::X_POSITIONS.map do |x_position|
    invader_rows.reverse.map do |invader_row|
      invader_row.find {|invader| invader.original_x_position == x_position }
    end.compact.first
  end.compact
end
hit_rock_bottom?() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 73
def hit_rock_bottom?
  if any_invaders? and fireable_invaders.first.y_position == app.height
    app.game_status.finished!
  end
end
rival_bullets() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 130
def rival_bullets
  app.ship.bullets
end
shooter() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 96
def shooter
  fireable_invaders.sample
end
sound() click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 100
def sound
  app.sounds.invader_bullet_sound
end
update_direction(direction, y_offset) click to toggle source
# File lib/space_invaders/invaders/invaders_container.rb, line 92
def update_direction direction, y_offset
  invader_rows.each {|invader_row| invader_row.update direction, y_offset }
end