summaryrefslogtreecommitdiff
path: root/Stages/eraser.gd
blob: 8595e6ff23e2de25a6523d6b1a468886d23d9e01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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")
	)