From c8bd14d6feeca72fc96b895c9382ebc16a1caf55 Mon Sep 17 00:00:00 2001 From: Daniel Weipert Date: Wed, 2 Oct 2024 19:23:19 +0200 Subject: initial commit --- Stages/eraser.gd | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Stages/eraser.gd (limited to 'Stages/eraser.gd') diff --git a/Stages/eraser.gd b/Stages/eraser.gd new file mode 100644 index 0000000..8595e6f --- /dev/null +++ b/Stages/eraser.gd @@ -0,0 +1,68 @@ +extends Node2D + + +var is_erasing = false +@onready var ground = get_parent().get_node("GroundCollision") as StaticBody2D +@onready var map = get_parent().get_node("Map") as Node2D +@onready var tilemap = map.get_child(0).get_child(0) + +var size = Vector2(12, 12) +var carve_area := PackedVector2Array([ + (size / 2) * Vector2(-1, -1), + (size / 2) * Vector2(1, -1), + (size / 2) * Vector2(1, 1), + (size / 2) * Vector2(-1, 1), +]) + + +func _ready(): + map.get_child(0).polygon = ground.get_child(0).polygon + + +func _physics_process(_delta: float) -> void: + if Input.is_action_just_pressed("select"): + is_erasing = true + if Input.is_action_just_released("select"): + is_erasing = false + + global_position = get_global_mouse_position() + queue_redraw() + + if Input.is_action_just_pressed("select"): #if is_erasing: + var points = PackedVector2Array() + for point in carve_area: + points.append(global_position + point) + + var collision_polygons = ground.get_children() + var visibility_polygons = map.get_children() + for idx in range(collision_polygons.size()): + var collision_node: CollisionPolygon2D = collision_polygons[idx] + var visilibty_node: Polygon2D = visibility_polygons[idx] + + var clipped = Geometry2D.clip_polygons(collision_node.polygon, points) + #print(clipped.size(), clipped) + if clipped.size() > 0 and not Geometry2D.is_polygon_clockwise(clipped[0]): + collision_node.polygon = clipped[0] + visilibty_node.polygon = clipped[0] + + if clipped.size() > 1 and not Geometry2D.is_polygon_clockwise(clipped[1]): + var p = CollisionPolygon2D.new() + p.polygon = clipped[1] + ground.add_child(p) + var po = Polygon2D.new() + po.polygon = clipped[1] + po.clip_children = CanvasItem.CLIP_CHILDREN_ONLY + po.add_child(tilemap.duplicate()) + map.add_child(po) + + +func _draw() -> void: + draw_rect( + Rect2((size / 2) * -1, size), + Color("#fff"), + is_erasing + ) + draw_colored_polygon( # TODO: use carve_area to draw + carve_area, + Color("#fff") + ) -- cgit v1.2.3