semi-idle-arpg/scripts/Player.gd
2024-11-05 09:29:20 -07:00

139 lines
5.1 KiB
GDScript

# player class
class_name Player
extends VBoxContainer
@export var health_regen := 1
@export var resource_regen := 10
var area_exp := 0.0
var max_health
var max_resource
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)
SignalBus.player_used_ability.connect(_on_player_used_ability)
max_health = _set_player_health()
max_resource = _set_player_resource()
_init_player_health_display()
_init_player_resource_display()
$PlayerExpBar.value = _get_level_progress_percent()
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
func _set_player_health() -> int:
# modify player health based on stats or temporary buffs
return PlayerState.base_health * PlayerState.constitution
func _set_player_resource() -> int:
# modify player resources based on weapon and stats or temporary buffs
match PlayerState.weapon_type:
Globals.Weapon.WeaponType.BOW:
return PlayerState.base_resource
_:
return PlayerState.base_resource
func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void:
# var kill_exp = pow(pow(enemyTier + 1, enemyDifficulty), 3)
var kill_exp = (enemyTier + 1) * enemyDifficulty
area_exp += kill_exp
# Globals.debug_print("T: " + str(enemyTier) + " D: " + str(enemyDifficulty))
# Globals.debug_print("K: " + str(kill_exp))
# Globals.debug_print("A: " + str(area_exp))
# TODO: maybe show earned exp on bar, separate from current
# overlay second bar under first, but still above foreground?
# 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 _init_player_health_display() -> void:
$PlayerHealthBar/PlayerHealthMax.text = str(max_health)
$PlayerHealthBar/PlayerHealthCurrent.text = str(max_health)
$PlayerHealthBar.value = max_health
func _update_player_health(change: int) -> void:
$PlayerHealthBar.value += change
var current_health = $PlayerHealthBar.value
$PlayerHealthBar/PlayerHealthCurrent.text = str(current_health)
if current_health <= 0:
print("%s - You died." % Globals.elapsed_time_format(Globals.elapsed_time(PlayerState.zone_duration)))
# TODO: change to scene transition after slowing for a couple seconds
# Engine.time_scale = 0.0001
# get_tree().paused = true
get_tree().change_scene_to_file("res://scenes/main_menu.tscn")
func _init_player_resource_display() -> void:
$PlayerResourceBar/PlayerResourceMax.text = str(max_resource)
$PlayerResourceBar/PlayerResourceCurrent.text = str(max_resource)
$PlayerResourceBar.value = max_resource
func _update_player_resource(change: int) -> void:
$PlayerResourceBar.value += change
var current_resource = $PlayerResourceBar.value
PlayerState.current_resource = current_resource
$PlayerResourceBar/PlayerResourceCurrent.text = str(current_resource)
func _on_enemy_damaged_player(damage: int) -> void:
var damage_received = damage
_update_player_health(-damage_received)
func _on_player_used_ability(ability_cost: int) -> void:
_update_player_resource(-ability_cost)
func _calculate_player_level() -> float:
const EXP_COEFFICIENT := 0.04
var current_exp = PlayerState.experience
return EXP_COEFFICIENT * pow(current_exp, 0.5) + 1
# return pow(current_exp / EXP_COEFFICIENT, 1 / EXP_EXPONENT) + 1
# return log((0.2 * current_exp + 5) * 0.2) + 1
func _get_level_progress_percent() -> float:
var current_level = _calculate_player_level()
return fmod(current_level, 1) * 100.0
func _on_quest_completed() -> void:
const QUEST_EXP_BASE := 10
var previous_whole_level = floor(PlayerState.level)
var quest_exp = QUEST_EXP_BASE ** (PlayerState.world_tier + 1)
area_exp += quest_exp
PlayerState.experience += area_exp
PlayerState.level = _calculate_player_level()
var current_whole_level = floor(PlayerState.level)
if current_whole_level > previous_whole_level:
SignalBus.player_leveled_up.emit(current_whole_level)
print("%s - You leveled up to %s!" % [Globals.elapsed_time_format(Globals.elapsed_time(PlayerState.zone_duration)), current_whole_level])
$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(health_regen)
_update_player_resource(resource_regen)