-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcell.rb
More file actions
52 lines (44 loc) · 1.17 KB
/
Copy pathcell.rb
File metadata and controls
52 lines (44 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module WaveFunctionCollapse
class Cell < BasicObject
@@cellid = 0
attr_reader :tiles, :cellid
attr_accessor :collapsed, :entropy, :x, :y
alias_method :collapsed?, :collapsed
def initialize(x, y, tiles)
@cellid = @@cellid
@collapsed = tiles.size == 1
@entropy = tiles.size
@tiles = tiles
@neighbors = {}
@x = x
@y = y
@@cellid = @@cellid.succ
end
def ==(other)
@cellid == other.cellid
end
def tiles=(new_tiles)
@tiles = new_tiles
update
end
def update
@entropy = @tiles.size
@collapsed = @entropy == 1
end
def tile
@tiles[0] if @collapsed
end
def collapse
self.tiles = [@tiles.max_by { |t| ::Kernel.rand**(1.0 / t.probability) }]
end
def neighbors(model)
@neighbors[model.width * y + x] ||= begin
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
end
end
end