Add XP bar #9
| @@ -84,10 +84,13 @@ offset_bottom = 100.0 | |||||||
| [node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/PlayerUI/CenterContainer"] | [node name="VBoxContainer" type="VBoxContainer" parent="CanvasLayer/PlayerUI/CenterContainer"] | ||||||
| layout_mode = 2 | 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) | custom_minimum_size = Vector2(400, 0) | ||||||
| layout_mode = 2 | 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"] | [node name="ElapsedLabel" type="Label" parent="CanvasLayer/PlayerUI/CenterContainer/VBoxContainer"] | ||||||
| layout_mode = 2 | layout_mode = 2 | ||||||
| text = "10:00:00" | text = "10:00:00" | ||||||
|   | |||||||
| @@ -1,7 +1,9 @@ | |||||||
| class_name PlayerUI | class_name PlayerUI | ||||||
| extends Control | 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 | @onready var elapsed_label: Label = $CenterContainer/VBoxContainer/ElapsedLabel | ||||||
|  |  | ||||||
| const ANIM_SPEED = 4.0 | const ANIM_SPEED = 4.0 | ||||||
| @@ -9,7 +11,8 @@ const TRESHOLD = 1 | |||||||
|  |  | ||||||
| var wanted_hp_value: float | var wanted_hp_value: float | ||||||
| var wanted_hp_max: float | var wanted_hp_max: float | ||||||
|  | var wanted_xp_value: float | ||||||
|  | var wanted_xp_max: float | ||||||
| var player: Player | var player: Player | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -18,6 +21,11 @@ func _ready() -> void: | |||||||
| 	if player: | 	if player: | ||||||
| 		hp_bar.value = player.player_stats.current_health | 		hp_bar.value = player.player_stats.current_health | ||||||
| 		hp_bar.max_value = player.player_stats.max_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(): | func update_hp(): | ||||||
| @@ -29,19 +37,43 @@ func update_hp(): | |||||||
| 	wanted_hp_max = max_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: | 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: | 	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) | 		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: | 		if abs(hp_bar.max_value - wanted_hp_max) < TRESHOLD: | ||||||
| 			hp_bar.max_value = wanted_hp_max | 			hp_bar.max_value = wanted_hp_max | ||||||
| 	if wanted_hp_value != hp_bar.value: | 	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) | 		hp_bar.value = lerpf(hp_bar.value, wanted_hp_value, ANIM_SPEED * delta) | ||||||
| 		if abs(hp_bar.value - wanted_hp_value) < TRESHOLD: | 		if abs(hp_bar.value - wanted_hp_value) < TRESHOLD: | ||||||
| 			hp_bar.value = wanted_hp_value | 			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): | func set_elapsed_time(value: float): | ||||||
| 	elapsed_label.text = format_time(value) | 	elapsed_label.text = format_time(value) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -96,11 +96,12 @@ func toggle_god_mode(value: bool): | |||||||
|  |  | ||||||
| func add_xp(amount: float) -> void: | func add_xp(amount: float) -> void: | ||||||
| 	player_stats.current_xp += amount | 	player_stats.current_xp += amount | ||||||
|  | 	main_ui.player_ui.update_xp() | ||||||
| 	_check_level_up() | 	_check_level_up() | ||||||
|  |  | ||||||
|  |  | ||||||
| func _check_level_up() -> void: | func _check_level_up() -> void: | ||||||
| 	var required_for_level = 25.0 | 	var required_for_level = player_stats.xp_required_for_level() | ||||||
| 	if player_stats.current_xp >= required_for_level: | 	if player_stats.current_xp >= required_for_level: | ||||||
| 		player_stats.current_level += 1 | 		player_stats.current_level += 1 | ||||||
| 		player_stats.current_xp -= required_for_level | 		player_stats.current_xp -= required_for_level | ||||||
|   | |||||||
| @@ -28,4 +28,4 @@ func get_final(stat: String, modifiers: Array[PlayerStatsModifier]) -> Variant: | |||||||
|  |  | ||||||
|  |  | ||||||
| func xp_required_for_level() -> float: | func xp_required_for_level() -> float: | ||||||
| 	return 100 * (100 * (1.5 ** (current_level - 1))) | 	return floorf(100 * (1.5 ** (current_level - 1))) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user