summaryrefslogtreecommitdiff
path: root/Game/States/Build/BuilderElement.gd
blob: cc9c051f10cdad83bde2c98f0fc846aeebeee1ab (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class_name BuilderElement
extends Node2D


@export var element: Resource
var instance: Tower

var collision_shape: Shape2D

var previous_position: Vector2

var collision_areas: Array[Area2D] = []


func _ready():
	instance = element.instantiate()
	
	var element_sprite: AnimatedSprite2D = instance.get_node("AnimatedSprite2D").duplicate()
	element_sprite.scale *= instance.scale
	element_sprite.position *= instance.scale
	add_child(element_sprite)
	element_sprite.play()
	#$Sprite2D.texture = element_sprite.sprite_frames.get_frame_texture("default", 0)
	#$Sprite2D.region_enabled = element_sprite.region_enabled
	#$Sprite2D.region_rect = element_sprite.region_rect
	#$Sprite2D.scale = element_sprite.scale
	#$Sprite2D.position = element_sprite.position
	#$Sprite2D.scale = instance.scale
	
	var element_collision_shape = instance.get_node("CollisionShape2D").duplicate() as CollisionShape2D
	collision_shape = element_collision_shape.shape
	
	var tile_size = Client.current_stage.map.tile_set.tile_size
	var snapped_size = (collision_shape.size * instance.scale).snapped(tile_size)
	for x in (snapped_size.x / tile_size.x):
		for y in (snapped_size.y / tile_size.y):
			var collision_visibility_area_scene = preload("res://UI/CollisionVisibilityArea.tscn")
			var collision_visibility_area = collision_visibility_area_scene.instantiate()
			collision_visibility_area.position = Vector2(
				x * tile_size.x,
				y * tile_size.y
			)
			collision_visibility_area.set_collision_layer_value(3, true)
			collision_visibility_area.set_collision_mask_value(1, true)
			collision_visibility_area.set_collision_mask_value(2, true)
			collision_visibility_area.set_collision_mask_value(3, true)
			collision_visibility_area.set_collision_mask_value(4, true)
			collision_visibility_area.set_collision_mask_value(8, true)
			collision_visibility_area.colliding_color = Color(1, 0, 0, 0.5)
			collision_visibility_area.not_colliding_color = Color(0, 1, 0, 0.25)
			
			if Client.current_stage.has_method("get_builder_collision_masks"):
				for idx in Client.current_stage.get_builder_collision_masks():
					collision_visibility_area.set_collision_mask_value(idx, true)
			
			var shape = RectangleShape2D.new()
			shape.size = Vector2(tile_size - Vector2i(1, 1))
			var cshape = CollisionShape2D.new()
			cshape.shape = shape
			cshape.position = tile_size / 2
			
			collision_visibility_area.add_child(cshape)
			add_child(collision_visibility_area)
			
			collision_areas.append(collision_visibility_area)


func _process(_delta):
	previous_position = global_position
	global_position = (
		get_global_mouse_position().snapped(Client.current_stage.map.tile_set.tile_size)
	 	- Vector2(Client.current_stage.map.tile_set.tile_size)
	)
	
	# TODO: rpc to other peers
	# TODO: only as preview not as blocking to build


func can_build():
	for area in collision_areas:
		if area.get_overlapping_areas().size() > 0:
			return false
		
		if area.get_overlapping_bodies().size() > 0:
			return false
	
	#if Client.current_stage.has_method("can_build"):
		#return Client.current_stage.can_build()
	
	return true