Last active
May 3, 2016 04:25
-
-
Save neikeq/97ca436919c44bfeb3546980af275461 to your computer and use it in GitHub Desktop.
GDScript version of https://github.com/godotengine/godot/blob/5be9ff7b6715a661e85f99b108f96340de7ef435/tools/editor/plugins/tile_set_editor_plugin.cpp#L37-L142
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
func tileset_from_scene(scene, library, merge): | |
""" | |
@scene Node - The input scene | |
@library TileSet - The output tileset | |
@merge bool - Preserve old tiles | |
""" | |
if merge: | |
library.clear() | |
for child in scene.get_children(): | |
if not child extends Sprite: | |
if child.get_child_count() > 0: | |
child = child.get_child(0) | |
if not child extends Sprite: | |
continue | |
else: | |
continue | |
# Assuming child is Sprite | |
var texture = child.get_texture() | |
var material = child.get_material() | |
if texture == null: | |
continue | |
var id = library.find_tile_by_name() | |
if id < 0: | |
id = library.get_last_unused_tile_id() | |
library.create_tile(id) | |
library.tile_set_name(id, child.get_name()) | |
library.tile_set_texture(id, texture) | |
library.tile_set_material(id, material) | |
var phys_offset = Vector2() | |
if child.is_centered(): | |
var s = Vector2() | |
if child.is_region(): | |
s = child.get_region_rect().size | |
else: | |
s = texture.get_size() | |
phys_offset += -s/2 | |
if child.is_region(): | |
library.tile_set_region(id, child.get_region_rect()) | |
var collisions = [] # Shape2D Array | |
var nav_poly = null | |
var occluder = null | |
for child2 in child.get_children(): | |
if child2 extends NavigationPolygonInstance: | |
nav_poly = child2.get_navigation_polygon() | |
elif child2 extends LightOccluder2D: | |
occluder = child2.get_occluder_polygon() | |
elif child2 extends StaticBody2D: | |
var shape_count = child2.get_shape_count() | |
if shape_count > 0: | |
for shape_index in range(0, shape_count): | |
var collision = child2.get_shape(shape_index) | |
if collision != null: | |
collisions.push_back(collision) | |
if collisions.empty(): | |
library.tile_set_shape_offset(id, Vector2()) | |
else: | |
library.tile_set_shapes(id, collisions) | |
library.tile_set_shape_offset(id, -phys_offset) | |
library.tile_set_texture_offset(id, child.get_offset()) | |
library.tile_set_navigation_polygon(id, nav_poly) | |
library.tile_set_light_occluder(id, occluder) | |
library.tile_set_occluder_offset(id, -phys_offset) | |
library.tile_set_navigation_polygon_offset(id, -phys_offset) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment