Pardok / TileMapsChef

Godot plugin to add some functionality to TileMaps
GNU General Public License v3.0
8 stars 1 forks source link

Enhancing TileMapsChef to consider Scenes with Collision Shapes #1

Open Templum opened 10 months ago

Templum commented 10 months ago

I found this plugin very helpful, but was wondering if you would consider extending it to also accommodate TileScenes. Locally I was able to hack together a proof of concept:

                    var scene_id: int = selected_tilemap.get_cell_alternative_tile(layer, cell)
                    var tileset_source_id: int = selected_tilemap.get_cell_source_id(layer, cell)
                    var tileset_source: TileSetSource = selected_tilemap.tile_set.get_source(tileset_source_id)
                    if tileset_source is TileSetScenesCollectionSource and scene_id > 0:
                        var scene: PackedScene = (tileset_source as TileSetScenesCollectionSource).get_scene_tile_scene(scene_id)
                        if scene != null:
                            var instance: Node2D = scene.instantiate()
                            for node in instance.get_children():
                                if node is CollisionShape2D and node.shape != null:
                                    print_debug("Found a scene that contains a CollisionShape2D")

                                    var rect: Rect2 = node.shape.get_rect()
                                    var lt = rect.position
                                    var rt = rect.position + Vector2(rect.size.x, 0)
                                    var ld = rect.position + Vector2(0, rect.size.y)
                                    var rd = rect.end
                                    var polygon = PackedVector2Array([lt, rt, rd, ld])
                                    print_debug("Collison Shape Rect", rect, polygon)

                                    polygon = GeometryUtils.polygon_to_local(polygon, cell_pos)
                                    print_debug("After Transform", polygon)
                                    nav_data.add_obstruction_outline(polygon)

                            instance.queue_free()

It is meant to run for situations where cell_data is null. It's a bit hacky but, it should be a nice stepping stone towards respecting scenes.

What do you think about it ?

Templum commented 10 months ago

image

Pardok commented 10 months ago

Hey @Templum ! What do you mean by "TileScenes", this type: TileSetScenesCollectionSource? I haven't worked with it (and didn't know about it, to be honest), will look into it. Thanks for your interest and contribution!

Templum commented 10 months ago

@Pardok thats correct, I was speaking about them. They come in handy in case you want to have something like an animated sprite tile or other things.