game: add xp bar
This commit is contained in:
		| @@ -84,10 +84,13 @@ offset_bottom = 100.0 | ||||
| [node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/PlayerUI/CenterContainer"] | ||||
| layout_mode = 2 | ||||
|  | ||||
| [node name="ProgressBar" type="ProgressBar" parent="CanvasLayer/PlayerUI/CenterContainer/VBoxContainer"] | ||||
| [node name="HPBar" type="ProgressBar" parent="CanvasLayer/PlayerUI/CenterContainer/VBoxContainer"] | ||||
| custom_minimum_size = Vector2(400, 0) | ||||
| layout_mode = 2 | ||||
|  | ||||
| [node name="XPBar" type="ProgressBar" parent="CanvasLayer/PlayerUI/CenterContainer/VBoxContainer"] | ||||
| layout_mode = 2 | ||||
|  | ||||
| [node name="ElapsedLabel" type="Label" parent="CanvasLayer/PlayerUI/CenterContainer/VBoxContainer"] | ||||
| layout_mode = 2 | ||||
| text = "10:00:00" | ||||
|   | ||||
| @@ -1,7 +1,9 @@ | ||||
| class_name PlayerUI | ||||
| extends Control | ||||
|  | ||||
| @onready var hp_bar: ProgressBar = $CenterContainer/VBoxContainer/ProgressBar | ||||
| @onready var hp_bar: ProgressBar = $CenterContainer/VBoxContainer/HPBar | ||||
| @onready var xp_bar: ProgressBar = $CenterContainer/VBoxContainer/XPBar | ||||
|  | ||||
| @onready var elapsed_label: Label = $CenterContainer/VBoxContainer/ElapsedLabel | ||||
|  | ||||
| const ANIM_SPEED = 4.0 | ||||
| @@ -9,7 +11,8 @@ const TRESHOLD = 1 | ||||
|  | ||||
| var wanted_hp_value: float | ||||
| var wanted_hp_max: float | ||||
|  | ||||
| var wanted_xp_value: float | ||||
| var wanted_xp_max: float | ||||
| var player: Player | ||||
|  | ||||
|  | ||||
| @@ -18,6 +21,11 @@ func _ready() -> void: | ||||
| 	if player: | ||||
| 		hp_bar.value = player.player_stats.current_health | ||||
| 		hp_bar.max_value = player.player_stats.max_health | ||||
| 		xp_bar.value = player.player_stats.current_xp | ||||
| 		xp_bar.max_value = player.player_stats.xp_required_for_level() | ||||
| 	await get_tree().create_timer(0.5).timeout | ||||
| 	update_hp() | ||||
| 	update_xp() | ||||
|  | ||||
|  | ||||
| func update_hp(): | ||||
| @@ -29,19 +37,43 @@ func update_hp(): | ||||
| 	wanted_hp_max = max_hp | ||||
|  | ||||
|  | ||||
| func update_xp(): | ||||
| 	if not player: | ||||
| 		push_error("cant update xp, no player found") | ||||
| 	var current_xp = player.player_stats.get_final("current_xp", player.modifiers) | ||||
| 	var max_xp = player.player_stats.xp_required_for_level() | ||||
| 	print_debug("updating xp bar: %s-%s" % [current_xp, max_xp]) | ||||
| 	wanted_xp_value = current_xp | ||||
| 	wanted_xp_max = max_xp | ||||
|  | ||||
|  | ||||
| func _process(delta: float) -> void: | ||||
| 	update_hp_bar(delta) | ||||
| 	update_xp_bar(delta) | ||||
|  | ||||
|  | ||||
| func update_hp_bar(delta: float): | ||||
| 	if wanted_hp_max != hp_bar.max_value: | ||||
| 		print_debug("wanted: %f-%f" % [wanted_hp_max, hp_bar.max_value]) | ||||
| 		hp_bar.max_value = lerpf(hp_bar.max_value, wanted_hp_max, ANIM_SPEED * delta) | ||||
| 		if abs(hp_bar.max_value - wanted_hp_max) < TRESHOLD: | ||||
| 			hp_bar.max_value = wanted_hp_max | ||||
| 	if wanted_hp_value != hp_bar.value: | ||||
| 		print_debug("current: %f-%f" % [wanted_hp_value, hp_bar.value]) | ||||
| 		hp_bar.value = lerpf(hp_bar.value, wanted_hp_value, ANIM_SPEED * delta) | ||||
| 		if abs(hp_bar.value - wanted_hp_value) < TRESHOLD: | ||||
| 			hp_bar.value = wanted_hp_value | ||||
|  | ||||
|  | ||||
| func update_xp_bar(delta: float): | ||||
| 	if wanted_xp_max != xp_bar.max_value: | ||||
| 		xp_bar.max_value = lerpf(xp_bar.max_value, wanted_xp_max, ANIM_SPEED * delta) | ||||
| 		if abs(xp_bar.max_value - wanted_xp_max) < TRESHOLD: | ||||
| 			xp_bar.max_value = wanted_xp_max | ||||
| 	if wanted_xp_value != xp_bar.value: | ||||
| 		xp_bar.value = lerpf(xp_bar.value, wanted_xp_value, ANIM_SPEED * delta) | ||||
| 		if abs(xp_bar.value - wanted_xp_value) < TRESHOLD: | ||||
| 			xp_bar.value = wanted_xp_value | ||||
|  | ||||
|  | ||||
| func set_elapsed_time(value: float): | ||||
| 	elapsed_label.text = format_time(value) | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user