summaryrefslogtreecommitdiff
path: root/world.gd
blob: ef467d107537bf414ac2d366b3b3a9d30707b1ae (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
extends Node2D


var current_builder_element: BuilderElement


func _ready():
	Game.initialize_stage($Layer0)
	
	for tower in $Towers.get_children():
		tower.selected.connect(func():
			%Tower.text = "Range: %s - Power: %s - Speed: %s" % [
				tower.attack_range, tower.attack_power, tower.attack_speed
			]
		)
	
	while true:
		var scene = preload("res://Mob.tscn")
		var mob = scene.instantiate()
		add_child(Game.spawn_mob(mob, $Spawn))
		await get_tree().create_timer(randi_range(1, 2)).timeout
		await get_tree().create_timer(0.01).timeout
		#break


func _input(event: InputEvent):
	if event.is_action_pressed("ui_accept"):
		for _i in range(0, 1):
			var scene = preload("res://Mob.tscn")
			var mob = scene.instantiate()
			add_child(Game.spawn_mob(mob, $Spawn))
	
	if event.is_action_pressed("spawn_box_toggle"):
		%SpawnBox.visible = not %SpawnBox.visible
	
	if Game.mode == Game.Mode.DEFAULT:
		if event.is_action_pressed("build_mode_start"):
			Game.start_build_mode()
		
		if event.is_action_pressed("builder_tower_select"):
			if Tower.selected_tower:
				Tower.selected_tower.is_selected = false
		if event.is_action_pressed("select"):
			if Mob.selected_unit:
				Mob.selected_unit.is_selected = false
	
	elif Game.mode == Game.Mode.BUILD:
		if event.is_action_pressed("builder_tower_select"):
			get_viewport().set_input_as_handled()
			
			if Game.current_builder_element.can_build():
				var placed_tower = Game.current_builder_element.element.duplicate() as Tower
				Game.place_tower(placed_tower, Game.current_builder_element.global_position)
				
				placed_tower.selected.connect(func():
					%Tower.text = "Range: %s - Power: %s - Speed: %s" % [
						placed_tower.attack_range, placed_tower.attack_power, placed_tower.attack_speed
					]
				)
				
				if not Input.is_action_pressed("builder_tower_place_keep"):
					Game.stop_build_mode()
			
		if event.is_action_pressed("builder_cancel") or event.is_action_pressed("build_mode_start"):
			get_viewport().set_input_as_handled()
			Game.stop_build_mode()


func _on_build_mode_button_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("builder_tower_select"):
		Game.mode = Game.Mode.BUILD
		Game.start_build_mode()


func _on_spawner_box_button_gui_input(event: InputEvent) -> void:
	if event.is_action_pressed("select"):
		%SpawnBox.visible = not %SpawnBox.visible