Skip to content

Instantly share code, notes, and snippets.

@jules-goose
Created June 28, 2021 15:11
Show Gist options
  • Save jules-goose/c9866ee98658f56534141618f3a23512 to your computer and use it in GitHub Desktop.
Save jules-goose/c9866ee98658f56534141618f3a23512 to your computer and use it in GitHub Desktop.
a proof of concept (and limited in functionality) gridmap autotiler for godot
tool
extends GridMap
#based on https://gamedevelopment.tutsplus.com/tutorials/how-to-use-tile-bitmasking-to-auto-tile-your-level-layouts--cms-25673 tutorial
# and using a 47 tile meshlibrary like this one : https://imgur.com/yuprM0n
var tilebitmask = { "254": 45, "90": 22, "22": 7, "0": 47, "10": 3, "2": 1, "94": 24, "120": 29, "11": 4, "208": 34, "107": 28, "82": 19, "216": 37, "86": 20, "223": 41, "214": 36, "104": 26, "222": 40, "74": 16, "18": 6, "8": 2, "248": 42, "255": 46, "127": 33, "123": 31, "66": 14, "16": 5, "219": 39, "75": 17, "80": 18, "122": 30, "30": 11, "126": 32, "31": 12, "250": 43, "88": 21, "64": 13, "95": 25, "251": 44, "91": 23, "24": 8, "27": 10, "218": 38, "72": 15, "106": 27, "26": 9, "210": 35 }
var bitmask = [
[1,2,4],
[8,0,16],
[32,64,128]
]
export(int) var map_w = 20
export(int) var map_h = 20
export(bool) var retile setget retile
func retile(value = null):
# only do this if we are working in the editor
if !Engine.is_editor_hint(): return
autotile(map_w,map_h)
func autotile(a:int,b:int):
for x in range(a):
for y in range(b):
if get_cell_item(x,0,y) != -1:
directional_check(Vector2(x,y))
func directional_check(coord:Vector2):
var north_tile
var south_tile
var west_tile
var east_tile
var north_east_tile
var north_west_tile
var south_east_tile
var south_west_tile
var tile_score = 0
north_tile = place_meeting(coord.x,coord.y-1)
south_tile = place_meeting(coord.x,coord.y+1)
west_tile = place_meeting(coord.x-1,coord.y)
east_tile = place_meeting(coord.x+1,coord.y)
north_east_tile = place_meeting(coord.x+1,coord.y-1) && north_tile && east_tile
north_west_tile = place_meeting(coord.x-1,coord.y-1) && west_tile && north_tile
south_east_tile = place_meeting(coord.x+1,coord.y+1) && south_tile && east_tile
south_west_tile = place_meeting(coord.x-1,coord.y+1) && south_tile && west_tile
tile_score = int(north_west_tile) + 2*int(north_tile) + 4*int(north_east_tile) + 8*int(west_tile) + 16*int(east_tile) + 32*int(south_west_tile) + 64*int(south_tile) + 128*int(south_east_tile)
set_cell_item(coord.x,0,coord.y,tilebitmask[String(tile_score)]-1)
func place_meeting(x,y):
if get_cell_item(x,0,y) != -1:
return true
else:
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment