aboutsummaryrefslogtreecommitdiff
path: root/scripts/enemies.gd
blob: 08cc389c79e9e3721551c2c2ffd2f2a3cb44fab3 (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
113
114
115
116
117
extends Node

var _path
var _enemy_blue
var _dissolve_mat : ShaderMaterial

var _fx_holder
var _fx_enemy_damage

var _shapes = {}

var serial_enemy = 0
var enemies = {}


func _ready():
	_fx_holder = get_tree().root.get_child(0).find_node("fx")
	_path = get_tree().root.get_child(0).find_node("path")
	_dissolve_mat = load("res://shaders/dissolve_mat.tres")
	_enemy_blue = load("res://scenes/enemy.tscn")
	_fx_enemy_damage = load("res://scenes/fx/enemy_damage.tscn")
	
	load_shapes()
	
func load_shapes():
	_shapes = {}
	var dir = Directory.new()
	dir.open("res://models/shapes")
	dir.list_dir_begin(true)
	var shape = dir.get_next()
	while shape != "":
		if (shape.ends_with(".glb")):
			var model = load("res://models/shapes/" + shape)
			var sname = shape.substr(0, shape.length()-4)
			_shapes[sname] = model
		shape = dir.get_next()

func spawn():
	var instance = _enemy_blue.instance()
	add_child(instance)
	instance.transform.origin = _path.nodes[0].transform.origin;
	instance.name = str(serial_enemy)
	var instance_model = _shapes[_shapes.keys()[randi() % _shapes.size()]].instance()
	instance.add_child(instance_model)
	var axis : Vector3 = Quat(Vector3(0, randf()*TAU, 0)) * Vector3.RIGHT 
	enemies[serial_enemy] = { 
		"cur": 0, "hp": 10, "rel": 0, "ops": instance_model.name,
		"axis": [axis.x, axis.y, axis.z] }
	serial_enemy += 1
	
func node_from_id (id):
	return get_node(str(id))

func _physics_process(delta):
	var delist = []
	for child in get_children():
		var id = str(child.name).to_int()
		var enemy = enemies[id]
		
		if enemy.hp <= 0:
			if enemy.ops == "T":
				delist.append(id)
				enemy.rel = 0
				continue
			else:
				child.get_node(enemy.ops).queue_free()
				enemy.ops = enemy.ops.substr(1, enemy.ops.length()-1)
				enemy.hp = 10
				var instance_model = _shapes[enemy.ops].instance()
				child.add_child(instance_model)
			
		var speed = 1
		enemy.rel += speed * delta
		while enemy.rel > 1: 
			enemy.rel -= 1
			enemy.cur += 1
		
		var destination = enemy.cur+1
		if destination >= _path.nodes.size():
			delist.append(id)
			enemy.rel = 0
			continue
			
		var from = _path.nodes[enemy.cur].transform.origin
		var to = _path.nodes[destination].transform.origin
		child.transform.origin = lerp(from, to, enemy.rel)
		
		var axis = Vector3(enemy.axis[0], enemy.axis[1], enemy.axis[2])
		child.transform.basis = child.transform.basis.rotated(axis, delta)
	
	for id in delist:
		get_node(str(id)).queue_free()
		enemies.erase(id)


func damage(name):
	var id = int(name)
	var enemy = enemies[id]
	enemies[id].hp -= 1
	
	fx_damage(name)
	
func fx_damage(name):
	var id = int(name)
	var enemy = enemies[id]
	
	var instance = _fx_enemy_damage.instance()
	_fx_holder.add_child(instance)
	
	var node = node_from_id(id)
	instance.transform = node.transform;
	instance.refresh_basis()
	
	var instance_model = _shapes[enemy["ops"]].instance()
	instance.add_child(instance_model)
	
	instance.refresh_shader(_dissolve_mat.duplicate())