summaryrefslogtreecommitdiff
path: root/area_2d.gd
blob: b9ed0d47a974e9e58883644e058e368861e98273 (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
extends Area2D

var overlapping_nodes: Dictionary = {}

func _ready():
	connect("body_shape_entered", Callable(self, "_on_body_shape_entered"))
	connect("body_shape_exited", Callable(self, "_on_body_shape_exited"))


func get_overlapping_nodes():
	for key in overlapping_nodes:
		var body = overlapping_nodes[key].body
		var body_shape: int = overlapping_nodes[key].body_shape
		var area_shape: int = overlapping_nodes[key].area_shape
		
		var body_shape2d: Shape2D = body.shape_owner_get_shape(body_shape, 0)
		var area_shape2d: Shape2D = shape_owner_get_shape(area_shape, 0)
		
		var body_shape2d_transform = body.shape_owner_get_owner(body_shape).get_global_transform()
		var area_shape2d_transform = shape_owner_get_owner(area_shape).get_global_transform()
		
		var collision_points = area_shape2d.collide_and_get_contacts(area_shape2d_transform, body_shape2d, body_shape2d_transform)
		overlapping_nodes[key].collision_points = collision_points
		
		if collision_points.size() == 2:
			overlapping_nodes[key].collision_points.append(
				overlapping_nodes[key].collision_points[0] + Vector2(0, 16)
			)
			overlapping_nodes[key].collision_points.append(
				overlapping_nodes[key].collision_points[1] + Vector2(0, 16)
			)
			
	
	return overlapping_nodes


func _on_body_shape_entered(id, body, body_shape, area_shape):
	if body != null:
		#print("Id: ", id, " [", body, ", ", body_shape, ", ", area_shape, "] has entered. ")
		var key := str(id) + ":" + str(body_shape)
		overlapping_nodes[key] = {
			"body": body,
			"body_shape": body_shape,
			"area_shape": area_shape,
			"collision_points": [],
		}


func _on_body_shape_exited(id, body, body_shape, area_shape):
	if body != null:
		#print("Id: ", id, " [", body, ", ", body_shape, ", ", area_shape, "] has exited. ")
		var key := str(id) + ":" + str(body_shape)
		overlapping_nodes.erase(key)