Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Builds tilesets
tool
extends TileMap
export(bool) var rebuild_tile_set = false setget rebuild_tile_set_changed # change this to trigger rebuild
export(ImageTexture) var tile_set_texture = null
export(Vector2) var tile_size = Vector2(0, 0) # leave as 0,0 to use TileMap cell size
export(Vector2) var tile_offset = Vector2(0, 0)
export(int) var tile_count = 0 # leave as 0 to use all tiles in texture
export(String) var shapes_node = "" # name of node containing collision bodies, leave null to use self
export(String, FILE, "*.csv") var tile_shapes_filename = "" # CSV file containing collision body indicies for each tile, leave null to use string
export(String) var tile_shapes_string = "" # string of CSV containing collision body indicies for each tile
export(Vector2) var shape_offset = Vector2(0, 0)
func rebuild_tile_set_changed(value):
rebuild_tile_set = false
build_tile_set()
func build_tile_set():
print("Rebuilding tile set...")
if tile_shapes_filename != "":
var file = File.new()
file.open(tile_shapes_filename, File.READ)
tile_shapes_string = file.get_as_text()
tile_shapes_string = tile_shapes_string.replace("\n", "")
var tile_shapes_array
if tile_shapes_string == "":
tile_shapes_array = []
else:
tile_shapes_array = tile_shapes_string.split(",")
var bodies_parent = get_node(shapes_node)
if !bodies_parent:
bodies_parent = self
var shape_arrays = []
shape_arrays.resize(bodies_parent.get_child_count())
for body in range(0, bodies_parent.get_child_count()):
var child = bodies_parent.get_child(body)
shape_arrays[body] = []
shape_arrays[body].resize(child.get_shape_count())
for shape in range(0, child.get_shape_count()):
shape_arrays[body][shape] = child.get_shape(shape)
var tile_size
if self.tile_size != Vector2(0, 0):
tile_size = self.tile_size
else:
tile_size = get_cell_size()
var tiles_wide
var tiles_high
var tile_x
var tile_y
if tile_set_texture:
tiles_wide = tile_set_texture.get_width() / tile_size.width
tiles_high = tile_set_texture.get_height() / tile_size.height
tile_x = 0
tile_y = 0
var length = 0
if tile_count > 0:
length = tile_count
else:
if tile_set_texture:
length = max(length, tiles_wide * tiles_high)
var tile_set = TileSet.new()
for tile in range(0, length):
tile_set.create_tile(tile)
if tile_set_texture:
tile_set.tile_set_texture(tile, tile_set_texture)
tile_set.tile_set_region(tile, Rect2(tile_x * tile_size.width, tile_y * tile_size.height, tile_size.width, tile_size.height))
tile_set.tile_set_texture_offset(tile, tile_offset)
if tile < tile_shapes_array.size():
var body = tile_shapes_array.get(tile).to_int()
if body < shape_arrays.size():
tile_set.tile_set_shapes(tile, shape_arrays[body])
tile_set.tile_set_shape_offset(tile, shape_offset)
if tile_set_texture:
tile_x += 1
if tile_x >= tiles_wide:
tile_y += 1
tile_x = 0
print("...Tile set rebuild complete!")
set_tileset(tile_set)
func _ready():
build_tile_set()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment