Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
pusewicz committed May 6, 2024
1 parent 664e33a commit d623b03
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
8 changes: 4 additions & 4 deletions lib/wave_function_collapse/cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ def neighbors(model)
return if model.nil?

@neighbors[model.width * y + x] ||= begin
up = model.grid_cell(@x, @y + 1) if @y < model.height - 1
down = model.grid_cell(@x, @y - 1) if @y.positive?
right = model.grid_cell(@x + 1, @y) if @x < model.width - 1
left = model.grid_cell(@x - 1, @y) if @x.positive?
up = model.cell_at(@x, @y + 1) if @y < model.height - 1
down = model.cell_at(@x, @y - 1) if @y.positive?
right = model.cell_at(@x + 1, @y) if @x < model.width - 1
left = model.cell_at(@x - 1, @y) if @x.positive?

{up: up, down: down, right: right, left: left}
end
Expand Down
10 changes: 3 additions & 7 deletions lib/wave_function_collapse/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ def initialize(tiles, width, height)
@tiles = tiles
@width = width.to_i
@height = height.to_i

# @grid = Array.new(width) { |x| Array.new(height) { |y| Cell.new(x, y, @tiles.shuffle) } }
@grid = []
@height.times { |y| @width.times { |x| @grid << Cell.new(x, y, @tiles.shuffle) } }
@uncollapsed_cells_grid = @grid.reject(&:collapsed)
@max_entropy = @tiles.length
end

def grid_cell(x, y)
def cell_at(x, y)
@grid[@width * y + x]
end

Expand Down Expand Up @@ -59,8 +57,6 @@ def iterate
end

def prepend_empty_row
raise

x = 0
while x < @width
@grid[x].shift
Expand Down Expand Up @@ -93,7 +89,7 @@ def generate_grid
y = 0

while y < @height
rx[y] = grid_cell(x, y).tile
rx[y] = cell_at(x, y).tile
y += 1
end
x += 1
Expand Down Expand Up @@ -141,7 +137,7 @@ def evaluate_neighbor(source_cell, evaluation_direction)
while nci < ncil
tile = neighbor_tiles[nci]
nci += 1
next if new_tile_ids[tile.tileid]
next if new_tile_ids.has_key?(tile.tileid)

tile_edge_hash = tile.send(opposite_direction)
if tile_edge_hash == source_edge_hash
Expand Down
2 changes: 1 addition & 1 deletion lib/wave_function_collapse/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def draw_map
column.reverse.each_with_index do |tile, y|
inverted_y = (y - @model.height + 1).abs

entropy = @model.grid_cell(x, inverted_y).entropy
entropy = @model.cell_at(x, inverted_y).entropy

if entropy > 1
percent_entropy = (entropy.to_f / @model.max_entropy * 255).round
Expand Down

0 comments on commit d623b03

Please sign in to comment.