-
-
Save omardelarosa/c576329e1481351f40d1901e80ba227f to your computer and use it in GitHub Desktop.
A PyxelEdit Tilemap Importer For GodotScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tool | |
extends EditorScript | |
const TILEMAP_JSON_PATH = "res://Data/Maps/GraveyardTilemap.json" # path to PyxelEdit exported json file | |
const TILESET_PATH = "res://Tiles/GraveyardTileset.tres" # path to Godot Tileset Object | |
func load_json(filepath): | |
var file = File.new() | |
file.open(filepath, file.READ) | |
var file_text = file.get_as_text() | |
var result_json = JSON.parse(file_text) | |
file.close() | |
if result_json.error == OK: | |
return result_json.result | |
else: | |
print("Error reading JSON at " + filepath) | |
return {} | |
func draw_tilemap_from_json(json): | |
var tileset = load(TILESET_PATH) | |
var mapMeta = { | |
'num_layers': len(json.layers), | |
'cell_size': Vector2(json.tilewidth, json.tileheight), | |
'map_size': Vector2(json.tileswide, json.tileshigh) | |
} | |
print("Loading Tilemap: ") | |
print(mapMeta) | |
var maps = [] | |
var tilemapsParent = Node2D.new() | |
tilemapsParent.set_name("PyxelEditTilemaps") | |
get_scene().add_child(tilemapsParent) | |
tilemapsParent.set_owner(get_scene()) | |
for l in json.layers: | |
var tm = TileMap.new() # Instantiate tilemap node | |
tm.tile_set = tileset | |
tm.set_name(l.name) # name node after PyxelEdit Layer | |
tm.cell_size = mapMeta.cell_size | |
for pos in l.tiles: | |
tm.set_cell(pos.x, pos.y, pos.tile) | |
maps.append(tm) | |
while len(maps) > 0: | |
var tm = maps.pop_back() | |
tilemapsParent.add_child(tm) | |
tm.set_owner(get_scene()) | |
# Called when the node enters the scene tree for the first time. | |
func _run(): | |
var json = load_json(TILEMAP_JSON_PATH) | |
draw_tilemap_from_json(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment