class Pacman::LevelBuilder

DSL builder

Attributes

level[R]

Public Class Methods

build(dsl) click to toggle source
# File lib/pacman/level_builder.rb, line 14
def build(dsl)
  builder = new(dsl)
  builder.level
end
new(dsl) click to toggle source

builds a level according to the DSL

# File lib/pacman/level_builder.rb, line 23
def initialize(dsl)
  @level = Level.new
  @x = 0
  @y = 0

  # exec DSL
  instance_eval(dsl)

  # place ghosts to the cage
  if @level.cage_obj
    cage_obj = @level.cage_obj
    @level.ghosts = { blinky: BlinkyGhost.new(cage_obj.x, cage_obj.y, :left),
                      pinky: PinkyGhost.new(cage_obj.x + 1, cage_obj.y,
                                            :left),
                      inky: InkyGhost.new(cage_obj.x + 2, cage_obj.y, :right),
                      clyde: ClydeGhost.new(cage_obj.x + 3, cage_obj.y,
                                            :right) }
  end

  @level.pellets_left = @level.pellet_count

  @level
end

Public Instance Methods

cage() click to toggle source
# File lib/pacman/level_builder.rb, line 110
def cage
  check_bounds(@x, @y)
  cage_obj = Cage.new(@x, @y)
  @level[@y][@x] = cage_obj
  begin
    skip(1)
  rescue
    raise OutOfBoundsError.new unless @x == @level.width - 1 &&
        @y == @level.height - 1
  end

  @level.cage_obj = cage_obj
end
check_bounds(x, y) click to toggle source
# File lib/pacman/level_builder.rb, line 51
def check_bounds(x, y)
  fail OutOfBoundsError.new if x < 0 || y < 0 || @level.width < x ||
      @level.height < y
end
goto(x, y) click to toggle source

go to specified position

# File lib/pacman/level_builder.rb, line 67
def goto(x, y)
  fail OutOfBoundsError.new if y < 0 || x < 0 || y >= @level.height ||
      x >= @level.width
  @x, @y = x, y
end
pellet() click to toggle source
# File lib/pacman/level_builder.rb, line 88
def pellet
  place_object(Pellet)
  @level.pellet_count += 1
end
place_object(clazz) click to toggle source
# File lib/pacman/level_builder.rb, line 73
def place_object(clazz)
  check_bounds(@x, @y)
  @level[@y][@x] = clazz.new
  begin
    skip(1)
  rescue
    raise OutOfBoundsError.new unless @x == @level.width - 1 &&
          @y == @level.height - 1
  end
end
player(direction) click to toggle source

set player position

# File lib/pacman/level_builder.rb, line 98
def player(direction)
  check_bounds(@x, @y)
  player = Player.new(direction, @x, @y)
  level.player = player
  begin
    skip(1)
  rescue
    raise OutOfBoundsError.new unless @x == @level.width - 1 &&
      @y == @level.height - 1
  end
end
power_pellet() click to toggle source
# File lib/pacman/level_builder.rb, line 93
def power_pellet
  place_object(PowerPellet)
end
resize(width, height) click to toggle source
# File lib/pacman/level_builder.rb, line 47
def resize(width, height)
  @level.resize(width, height)
end
skip(n) click to toggle source

skip n columns if possible, otherwise skip row

# File lib/pacman/level_builder.rb, line 57
def skip(n)
  x = (@x + n) % @level.width
  y = (@y * @level.width + @x + n) / @level.width
  fail OutOfBoundsError.new if y >= @level.height || x >= @level.width

  @y = y
  @x = x
end
wall() click to toggle source
# File lib/pacman/level_builder.rb, line 84
def wall
  place_object(Wall)
end