summaryrefslogtreecommitdiff
path: root/UI/hp_bar.gd
blob: bb18bf21b9e63a598b73c99b9a2eb3fcd79f5663 (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
extends Control


var value_difference = 0


func init(health):
	$HealthBar.max_value = health
	$HealthBar.value = health
	
	$ImmediateDamageBar.max_value = health
	$ImmediateDamageBar.value = health
	
	$TotalDamageBar.max_value = health
	$TotalDamageBar.value = health


func set_value(value):
	value_difference = $HealthBar.value - value
	$HealthBar.value = value
	
	if $HealthBar.max_value == $HealthBar.value:
		visible = false
	else:
		visible = true


func show_numbers(value: float):
	# TODO: factor out to scene
	
	var label = Label.new()
	label.text = str(value)
	label.position.x = randf_range(0.0, size.x)
	label.position.y = -size.y*5
	label.add_theme_font_size_override("font_size", 8)
	add_child(label)
	
	var tween = create_tween()
	tween.tween_property(label, "position", Vector2(label.position.x, label.position.y - size.y), 0.25)
	tween.tween_property(label, "self_modulate", Color(1, 1, 1, 0), 0.25)
	await tween.finished
	label.queue_free()


func _on_health_bar_value_changed(value: float) -> void:
	if not $ImmediateDamageTimer.is_stopped():
		$ImmediateDamageBar.value = $HealthBar.value + value_difference
	
	$ImmediateDamageTimer.start()
	
	show_numbers(value_difference)


func _on_immediate_damage_timer_timeout() -> void:
	var tween = get_tree().create_tween()
	tween.tween_property($ImmediateDamageBar, "value", $HealthBar.value, 0.2)
	$TotalDamageTimer.start()


func _on_total_damage_timer_timeout() -> void:
	var tween = get_tree().create_tween()
	tween.tween_property(
		$TotalDamageBar,
		"value",
		$HealthBar.value,
		0.3
	)