aboutsummaryrefslogtreecommitdiff
path: root/scripts/wave.gd
blob: 691b7033054cba3d9b3bd6b826ad81c78c518c30 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
extends Node

var _timer : Resource = load("res://scenes/wave/timer_batch.tscn")

var wave_num : int = 0
var waves = []

var ongoing = false

var load_shapes : Node
var gui : Node

var rng = RandomNumberGenerator.new()

func fetch ():
	var root = get_tree().root.get_node("world")
	var saveload = root.get_node("saveload")
	load_shapes = saveload.get_node("load_shapes")
	gui = root.get_node("gui")

func make_queue (spawn):
	var queue = []
	for order in spawn.enemies:
		for n in order.amount:
			var e = {
				"cooldown": order.cooldown,
				"enemy": order.enemy,
				"hp": order.hp
			}
			queue += [e]
	if spawn.random: queue.shuffle()
	return queue

func add_spawner (spawn):
	var timer = _timer.instance()
	timer.queue = make_queue(spawn)
	add_child(timer)
	timer.fetch()
	timer.start(spawn.time)
	
func start_wave (wave):
	for spawn in wave:
		add_spawner(spawn)
		

func get_hp_budget (x : int):
	var hp = pow((x + 10), 2.7) * 1.8 - 850
	return hp
	
func get_affordable_enemies (hp_budget : int):
	var aff = []
	for enemy in load_shapes.info:
		if load_shapes.info[enemy].cost <= hp_budget:
			aff += [load_shapes.info[enemy]]
	return aff

func sort_aff_comp(a, b): return a.cost < b.cost
		
func pick_enemy (hp_budget : int, density : float, height: float):
	var amt = 1
	var aff = get_affordable_enemies(hp_budget)
	aff.sort_custom(self, "sort_aff_comp")
	
	#var rn = rng.randf()
	#var pick : float = pow(rn, 2*height*(1-rn) + rn)
	var enemy = aff[round(height * (aff.size()-1))]
	
	var cooldown : float = rng.randf() * pow(4, density - 0.5)
	
	var hp_adjusted = max(1, enemy.lives * height)
	var cost = enemy.cost - enemy.lives + hp_adjusted
	
	print(cost)
	
	var order = {
		"amount" : amt,
		"enemy": enemy.name,
		"hp": hp_adjusted,
		"cooldown": cooldown
	}
	return { "enemy": order, "cost": cost }

func gen_wave (wave_num : int, density : float, height: float):
	var hp_budget = get_hp_budget(wave_num)
	
	var enemies = []
	for i in range(0, 1000):
		if hp_budget < 10: break
		var enemy_cost = pick_enemy(hp_budget, density, height)
		enemies += [ enemy_cost.enemy ]
		hp_budget -= enemy_cost.cost
	
	var w = [
		{
			"time": 0, "random": true,
			"enemies": enemies
		}
	]
	return w

func start():
	fetch()
	rng.randomize()
	ongoing = true
	var w = null
	if wave_num <= waves.size()-1: w = waves[wave_num]
	else: w = gen_wave(wave_num, 0.5, 0.2)
	start_wave(w)
	
func end():
	ongoing = false
	wave_num += 1