summaryrefslogtreecommitdiff
path: root/Game/States/Build/StateBuild.gd
blob: f16ee3d3031a220c63aa16d29ad02824c977ad86 (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
class_name StateBuild
extends State


static var current_builder_element: BuilderElement


func _state_enter():
	%BuildGrid.visible = true


func _state_exit():
	%BuildGrid.visible = false


func _state_input(event: InputEvent):
	if event.is_action_pressed("builder_tower_select"):
		
		if current_builder_element and current_builder_element.can_build():
			var placed_tower = current_builder_element.element.instantiate() as Tower
			placed_tower.attack_range = [
				Client.stage.map.tile_set.tile_size.x * 2,
				Client.stage.map.tile_set.tile_size.x * 3,
				Client.stage.map.tile_set.tile_size.x * 4,
			].pick_random()
			
			Client.place_tower(placed_tower, current_builder_element.global_position)
			
			if not Input.is_action_pressed("builder_tower_place_keep"):
				current_builder_element.queue_free()
				current_builder_element = null
		
	if event.is_action_pressed("build_mode_start"):
		get_viewport().set_input_as_handled()
		
		if current_builder_element:
			current_builder_element.queue_free()
			current_builder_element = null
		
		set_state("StateDefault")
	
	if event.is_action_pressed("builder_cancel") and current_builder_element:
		current_builder_element.queue_free()
		current_builder_element = null


func _state_unhandled_input(_event: InputEvent) -> void:
	pass