Class: GamesAndRpgParadise::GUI::Sokoban::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles, player) ⇒ Level

#

initialize

#


22
23
24
25
26
27
28
29
30
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 22

def initialize(tiles, player)
  @tiles, @player = tiles, player
  @height = tiles.size * 50
  width = tiles.map { |line| line.size }.max
  @tiles.map! { |line|
    line + Array.new(width - line.size) { ' ' }
  }
  @width = width * 50
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



17
18
19
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 17

def height
  @height
end

#tilesObject (readonly)

Returns the value of attribute tiles.



15
16
17
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 15

def tiles
  @tiles
end

#widthObject (readonly)

Returns the value of attribute width.



16
17
18
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 16

def width
  @width
end

Instance Method Details

#boulder?(x, y) ⇒ Boolean

#

boulder?

Query whether our object is a boulder.

#

Returns:

  • (Boolean)


37
38
39
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 37

def boulder?(x, y)
  'o*'.index(@tiles[y][x])
end

#finished?Boolean

#

finished?

#

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 57

def finished?
  @tiles.all? { |line|
    not line.any? { |tile|
      "o.".index(tile)
    }
  }
end

#free?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 50

def free?(x, y)
  not solid?(x, y)
end

#free_boulder(bx, by) ⇒ Object

#

free_boulder

#


82
83
84
85
86
87
88
89
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 82

def free_boulder(bx, by)
  @tiles[by][bx] = case @tiles[by][bx]
    when 'o'
      ' '
    when '*'
      '.'
  end
end

#go_downObject

#

go_down

#


153
154
155
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 153

def go_down
  move_player(0, +1)
end

#go_leftObject

#

go_left

#


132
133
134
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 132

def go_left
  move_player(-1, 0)
end

#go_rightObject

#

go_right

#


139
140
141
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 139

def go_right
  move_player(+1, 0)
end

#go_upObject

#

go_up

#


146
147
148
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 146

def go_up
  move_player(0, -1)
end

#move_boulder(bx, by, vx, vy) ⇒ Object

#

move_boulder

#


104
105
106
107
108
109
110
111
112
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 104

def move_boulder(bx, by, vx, vy)
  nx, ny = bx + vx, by + vy
  if free?(nx, ny)
    free_boulder(bx, by)
    put_boulder(nx, ny)
    return true
  end
  return false
end

#move_player(vx, vy) ⇒ Object

#

move_player

#


117
118
119
120
121
122
123
124
125
126
127
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 117

def move_player(vx, vy)
  nx, ny = px + vx, py + vy
  lambda { |l,y| p [l,y]; y }
  success = (free?(nx, ny) or
    (boulder?(nx, ny) and move_boulder(nx, ny, vx, vy)))

  if success
    @player = [nx, ny]
    return true
  end
end

#put_boulder(bx, by) ⇒ Object

#

put_boulder

#


94
95
96
97
98
99
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 94

def put_boulder(bx, by)
  @tiles[by][bx] = case @tiles[by][bx]
    when " " then 'o'
    when "." then '*'
  end
end

#pxObject

#

px

#


68
69
70
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 68

def px
  @player[0]
end

#pyObject

#

py

#


75
76
77
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 75

def py
  @player[1]
end

#solid?(x, y) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 47

def solid?(x, y)
  wall?(x, y) or boulder?(x, y)
end

#wall?(x, y) ⇒ Boolean

#

wall?

#

Returns:

  • (Boolean)


44
45
46
# File 'lib/games_and_rpg_paradise/gui/gosu/sokoban/level.rb', line 44

def wall?(x, y)
  "#".index(@tiles[y][x])
end