Advertisement
Guest User

Untitled

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