Class: Pacman::LevelBuilder
- Inherits:
-
Object
- Object
- Pacman::LevelBuilder
- Defined in:
- lib/pacman/level_builder.rb
Overview
DSL builder
Instance Attribute Summary collapse
-
#level ⇒ Object
readonly
Returns the value of attribute level.
Class Method Summary collapse
Instance Method Summary collapse
- #cage ⇒ Object
- #check_bounds(x, y) ⇒ Object
-
#goto(x, y) ⇒ Object
go to specified position.
-
#initialize(dsl) ⇒ LevelBuilder
constructor
builds a level according to the DSL.
- #pellet ⇒ Object
- #place_object(clazz) ⇒ Object
-
#player(direction) ⇒ Object
set player position.
- #power_pellet ⇒ Object
- #resize(width, height) ⇒ Object
-
#skip(n) ⇒ Object
skip n columns if possible, otherwise skip row.
- #wall ⇒ Object
Constructor Details
#initialize(dsl) ⇒ LevelBuilder
builds a level according to the DSL
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# 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 |
Instance Attribute Details
#level ⇒ Object (readonly)
Returns the value of attribute level.
124 125 126 |
# File 'lib/pacman/level_builder.rb', line 124 def level @level end |
Class Method Details
.build(dsl) ⇒ Object
14 15 16 17 |
# File 'lib/pacman/level_builder.rb', line 14 def build(dsl) builder = new(dsl) builder.level end |
Instance Method Details
#cage ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 |
# 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) ⇒ Object
51 52 53 54 |
# 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) ⇒ Object
go to specified position
67 68 69 70 71 |
# 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 ⇒ Object
88 89 90 91 |
# File 'lib/pacman/level_builder.rb', line 88 def pellet place_object(Pellet) @level.pellet_count += 1 end |
#place_object(clazz) ⇒ Object
73 74 75 76 77 78 79 80 81 82 |
# 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) ⇒ Object
set player position
98 99 100 101 102 103 104 105 106 107 108 |
# 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 ⇒ Object
93 94 95 |
# File 'lib/pacman/level_builder.rb', line 93 def power_pellet place_object(PowerPellet) end |
#resize(width, height) ⇒ Object
47 48 49 |
# File 'lib/pacman/level_builder.rb', line 47 def resize(width, height) @level.resize(width, height) end |
#skip(n) ⇒ Object
skip n columns if possible, otherwise skip row
57 58 59 60 61 62 63 64 |
# 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 ⇒ Object
84 85 86 |
# File 'lib/pacman/level_builder.rb', line 84 def wall place_object(Wall) end |