Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AUTOTILE_LOOKUP = [
- 46, 46, 42, 42, 46, 46, 42, 42, 43, 43, 35, 34, 43, 43, 35, 34, 45, 45,
- 37, 37, 45, 45, 36, 36, 33, 33, 23, 22, 33, 33, 21, 20, 46, 46, 42, 42,
- 46, 46, 42, 42, 43, 43, 35, 34, 43, 43, 35, 34, 45, 45, 37, 37, 45, 45,
- 36, 36, 33, 33, 23, 22, 33, 33, 21, 20, 44, 44, 32, 32, 44, 44, 32, 32,
- 41, 41, 19, 17, 41, 41, 19, 17, 39, 39, 27, 27, 39, 39, 26, 26, 31, 31,
- 15, 11, 31, 31, 7, 3, 44, 44, 32, 32, 44, 44, 32, 32, 40, 40, 18, 16,
- 40, 40, 18, 16, 39, 39, 27, 27, 39, 39, 26, 26, 29, 29, 13, 9, 29, 29,
- 5, 1, 46, 46, 42, 42, 46, 46, 42, 42, 43, 43, 35, 34, 43, 43, 35, 34,
- 45, 45, 37, 37, 45, 45, 36, 36, 33, 33, 23, 22, 33, 33, 21, 20, 46, 46,
- 42, 42, 46, 46, 42, 42, 43, 43, 35, 34, 43, 43, 35, 34, 45, 45, 37, 37,
- 45, 45, 36, 36, 33, 33, 23, 22, 33, 33, 21, 20, 44, 44, 32, 32, 44, 44,
- 32, 32, 41, 41, 19, 17, 41, 41, 19, 17, 38, 38, 25, 25, 38, 38, 24, 24,
- 30, 30, 14, 10, 30, 30, 6, 2, 44, 44, 32, 32, 44, 44, 32, 32, 40, 40,
- 18, 16, 40, 40, 18, 16, 38, 38, 25, 25, 38, 38, 24, 24, 28, 28, 12, 8,
- 28, 28, 4, 0
- ]
- def autotile_flag(nb, id)
- nb.inject(0) { |r, nb_id| r << 1 | (nb_id == id ? 1 : 0) }
- end
- def put_autotile(x, y, tile_id)
- @map.data[x, y, 0] = (tile_id * 48 + 2048)
- @map.data[x, y, 1] = 0
- @map.data[x, y, 2] = 0
- update_autotile(x, y, 0)
- neighbors(x, y).each do |x2, y2|
- 3.times { |z| update_autotile(x2, y2, z) }
- end
- end
- def update_autotile(x, y, z)
- id = autotile_type(x, y, z)
- return unless id.between?(0, 47)
- nb = neighbors(x, y).map do |x2, y2|
- autotile_type(x2, y2, z)
- end
- flag = autotile_flag(nb, id)
- @map.data[x, y, z] = (id * 48 + 2048) + AUTOTILE_LOOKUP[flag]
- end
- def neighbors(x, y)
- [*-1..1].product([*-1..1]).map do |iy, ix|
- next nil if iy.zero? && ix.zero?
- x2 = x + ix
- y2 = y + iy
- next [x, y] if !loop_horizontal? && (x2 < 0 || x2 > width)
- next [x, y] if !loop_vertical? && (y2 < 0 || y2 > height)
- [round_x(x2), round_y(y2)]
- end.compact
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement