class MonkeyMusic::Level

Attributes

boost_cooldown[RW]
height[RW]
players[R]
time_limit[RW]
turn_limit[RW]
units[R]
user[R]
width[RW]

Public Class Methods

new(players, user) click to toggle source
# File lib/monkey_music/level.rb, line 8
def initialize(players, user)
  @players = players
  @user = user
  @width = 0
  @height = 0
  @boost_cooldown = 0
  @units = []
end

Public Instance Methods

accessible?(x, y) click to toggle source
# File lib/monkey_music/level.rb, line 48
def accessible?(x, y)
  not out_of_bounds?(x, y) && empty?(x, y)
end
add(unit, x, y) click to toggle source
# File lib/monkey_music/level.rb, line 17
def add(unit, x, y)
  unit.place!(self, x, y)
  unit.assign_id
  @units << unit
end
at(x, y) click to toggle source
# File lib/monkey_music/level.rb, line 23
def at(x, y)
  @units.detect { |u| u.at?(x, y) }
end
complete?() click to toggle source
# File lib/monkey_music/level.rb, line 31
def complete?
  (@units.detect { |u| u.kind_of? Track }).nil? &&
    (@players.detect { |p| p.monkey.carrying.count > 0 }).nil?
end
empty?(x, y) click to toggle source
# File lib/monkey_music/level.rb, line 27
def empty?(x, y)
  at(x, y).nil?
end
load_from_file(file) click to toggle source
# File lib/monkey_music/level.rb, line 52
def load_from_file(file)
  LevelLoader.new(self).instance_eval(IO.read(file))
  self
end
out_of_bounds?(x, y) click to toggle source
# File lib/monkey_music/level.rb, line 44
def out_of_bounds?(x, y)
  x < 0 || y < 0 || x > @width-1 || y > @height-1
end
remove(unit) click to toggle source
# File lib/monkey_music/level.rb, line 40
def remove(unit)
  @units.reject! { |u| u == unit }
end
serialize() click to toggle source
# File lib/monkey_music/level.rb, line 73
def serialize
  rows = []
  @height.times do |y|
    row = []
    @width.times do |x|
      unit = at(x, y)
      row << if unit then unit.serialize else '_' end
    end
    rows << row.join(",")
  end
  rows.join("\n")
end
to_s() click to toggle source
# File lib/monkey_music/level.rb, line 57
def to_s
  rows = []
  rows << " " + ("=" * @width)
  @height.times do |y|
    row = ["|"]
    @width.times do |x|
      unit = at(x, y)
      row << if unit then unit.to_s else ' ' end
    end
    row << "|"
    rows << row.join
  end
  rows << " " + ("=" * @width)
  rows.join("\n")
end
tracks() click to toggle source
# File lib/monkey_music/level.rb, line 36
def tracks
  @units.select {|u| u.kind_of? Track }
end