semi-idle-arpg/scripts/Player.gd

135 lines
4.9 KiB
GDScript3
Raw Normal View History

2024-10-20 16:44:08 -06:00
# player class
class_name Player
extends VBoxContainer
2024-10-27 17:20:33 -06:00
const EXP_COEFFICIENT := 10
const EXP_EXPONENT := 2.4
2024-10-20 16:44:08 -06:00
var area_exp := 0.0
var current_health: int
var resource_type: String
var current_resource: int
var max_health
var max_resource
2024-10-20 16:44:08 -06:00
func _ready() -> void:
SignalBus.enemy_died.connect(_on_enemy_died)
SignalBus.quest_completed.connect(_on_quest_completed)
SignalBus.enemy_damaged_player.connect(_on_enemy_damaged_player)
max_health = PlayerState.max_health
max_resource = PlayerState.max_resource
_set_player_health_display()
_set_player_resource_display()
2024-10-20 16:44:08 -06:00
$PlayerExpBar.value = _get_level_progress_percent()
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
2024-10-20 16:44:08 -06:00
func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void:
2024-10-27 17:20:33 -06:00
# var kill_exp = pow(pow(enemyTier + 1, enemyDifficulty), 3)
var kill_exp = (enemyTier + 1) * enemyDifficulty
2024-10-20 16:44:08 -06:00
area_exp += kill_exp
2024-10-21 13:09:51 -06:00
# Globals.debug_print("T: " + str(enemyTier) + " D: " + str(enemyDifficulty))
# Globals.debug_print("K: " + str(kill_exp))
# Globals.debug_print("A: " + str(area_exp))
2024-10-20 16:44:08 -06:00
# TODO: maybe show earned exp on bar, separate from current
2024-10-25 22:22:13 -06:00
# overlay second bar under first, but still above foreground?
2024-10-20 16:44:08 -06:00
# use xp to next level and current level, subtract, figure out percentage per point exp, add to display?
# func _get_player_current_health() -> void:
# var playerCurrHeath = PlayerState.current_health
# $PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath)
# $PlayerHealthBar.value = playerCurrHeath
# if playerCurrHeath <= 0:
# %PlayerInfo/PlayerHealthBar.value = 0
# Engine.time_scale = 0.0001
func _set_player_health_display() -> void:
current_health = max_health
$PlayerHealthBar/PlayerHealthMax.text = str(max_health)
$PlayerHealthBar/PlayerHealthCurrent.text = str(current_health)
$PlayerHealthBar.value = current_health
func _update_player_health(change: int) -> void:
var prev_health = current_health
var new_health = prev_health + change
current_health = new_health
if current_health <= 0:
2024-10-24 14:20:00 -06:00
$PlayerHealthBar/PlayerHealthCurrent.text = str(0)
$PlayerHealthBar.value = 0
# TODO: change to scene transition after slowing for a couple seconds
# Engine.time_scale = 0.0001
2024-10-27 17:20:33 -06:00
print("You died.")
get_tree().paused = true
elif current_health >= max_health:
$PlayerHealthBar/PlayerHealthCurrent.text = str(max_health)
$PlayerHealthBar.value = max_health
else:
$PlayerHealthBar/PlayerHealthCurrent.text = str(new_health)
$PlayerHealthBar.value = new_health
func _set_player_resource_display() -> void:
current_resource = max_resource
$PlayerResourceBar/PlayerResourceMax.text = str(max_resource)
$PlayerResourceBar/PlayerResourceCurrent.text = str(current_resource)
$PlayerResourceBar.value = current_resource
func _update_player_resource(change: int) -> void:
var prev_resource = current_resource
var new_resource = prev_resource + change
current_resource = new_resource
PlayerState.current_resource = current_resource
if current_resource <= 0:
$PlayerResourceBar/PlayerResourceCurrent.text = str(0)
$PlayerResourceBar.value = 0
elif current_resource >= max_resource:
$PlayerResourceBar/PlayerResourceCurrent.text = str(max_resource)
$PlayerResourceBar.value = max_resource
else:
$PlayerResourceBar/PlayerResourceCurrent.text = str(new_resource)
$PlayerResourceBar.value = new_resource
func _on_enemy_damaged_player(damage: int) -> void:
var damage_received = damage
_update_player_health(-damage_received)
2024-10-20 16:44:08 -06:00
func _calculate_player_level() -> float:
var current_exp = PlayerState.experience
2024-10-27 17:20:33 -06:00
return pow(current_exp / EXP_COEFFICIENT, 1 / EXP_EXPONENT) + 1
# return log((0.2 * current_exp + 5) * 0.2) + 1
2024-10-20 16:44:08 -06:00
func _get_level_progress_percent() -> float:
var current_level = _calculate_player_level()
return fmod(current_level, 1) * 100.0
func _on_quest_completed() -> void:
2024-10-27 17:20:33 -06:00
var previous_whole_level = floor(PlayerState.level)
PlayerState.experience += area_exp
PlayerState.level = _calculate_player_level()
2024-10-27 17:20:33 -06:00
var current_whole_level = floor(PlayerState.level)
if current_whole_level > previous_whole_level:
SignalBus.player_leveled_up.emit()
print("You leveled up to %s!" % current_whole_level)
2024-10-20 16:44:08 -06:00
$PlayerExpBar.value = _get_level_progress_percent()
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
# if Globals.debug:
# print("FA: ", area_exp)
# print("L: ", PlayerState.player_level)
# print("XP: ", PlayerState.player_exp)
# print("CL: ", _calculate_player_level())
# print("LP: ", _get_level_progress_percent())
func _on_regen_timer_timeout() -> void:
_update_player_health(1)
_update_player_resource(1)