Class: Swarm::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/swarm/level.rb

Direct Known Subclasses

Five, Four, Intro, One, Three, Two

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLevel

Returns a new instance of Level.



51
52
53
54
55
56
# File 'lib/swarm/level.rb', line 51

def initialize
  @map = Map.new(Console.width, Console.height, tile_width: 2)

  @map.each &:empty!
  @map.update
end

Class Method Details

.each(&block) ⇒ Object



5
6
7
# File 'lib/swarm/level.rb', line 5

def Level.each(&block)
  [Intro, One, Two, Three, Four, Five].each { |level| block.call level.new }
end

Instance Method Details

#find_playerObject



18
19
20
# File 'lib/swarm/level.rb', line 18

def find_player
  @player = (@player && @player.player?) ? @player : @map.find(&:player?)
end

#move(tile, direction) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/swarm/level.rb', line 30

def move(tile, direction)
  case direction
  when :north, :south, :west, :east
    @map.move(tile, direction)
  else
    available  = @map.available_moves(tile)
    aggressive = @map.aggressive_moves(tile, direction)
    aggressive &= available

    if aggressive.any?
      @map.move tile, aggressive.sample
    elsif available.any?
      @map.move tile, available.sample
    end
  end
end

#move_player(direction) ⇒ Object



26
27
28
# File 'lib/swarm/level.rb', line 26

def move_player(direction)
  move find_player, direction
end

#over?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/swarm/level.rb', line 47

def over?
  !Catalog.select(*%i[worker soldier queen egg]).any?
end

#spawn_playerObject



22
23
24
# File 'lib/swarm/level.rb', line 22

def spawn_player
  @map.center.player!
end

#updateObject



9
10
11
# File 'lib/swarm/level.rb', line 9

def update
  @map.update
end

#update!Object



13
14
15
16
# File 'lib/swarm/level.rb', line 13

def update!
  @map.each(&:change!)
  @map.update
end