88 lines
3.0 KiB
GDScript
88 lines
3.0 KiB
GDScript
# player class
|
|
class_name Player
|
|
extends VBoxContainer
|
|
|
|
|
|
var area_exp := 0.0
|
|
var current_health: int
|
|
var resource_type: String
|
|
var current_resource: int
|
|
|
|
|
|
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)
|
|
_set_player_display()
|
|
$PlayerExpBar.value = _get_level_progress_percent()
|
|
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
|
|
|
|
|
|
func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void:
|
|
var kill_exp = pow(pow(enemyTier + 1, enemyDifficulty), 3)
|
|
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
|
|
# 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_display() -> void:
|
|
var max_health = PlayerState.max_health
|
|
current_health = max_health
|
|
$PlayerHealthBar/PlayerHealthMax.text = str(max_health)
|
|
$PlayerHealthBar/PlayerHealthCurrent.text = str(current_health)
|
|
$PlayerHealthBar.value = current_health
|
|
|
|
|
|
func _set_player_current_health(change: int) -> void:
|
|
var prev_health = current_health
|
|
var new_health = prev_health + change
|
|
current_health = new_health
|
|
if current_health <= 0:
|
|
$PlayerHealthBar/PlayerHealthCurrent.text = str(0)
|
|
$PlayerHealthBar.value = 0
|
|
# TODO: change to scene transition after slowing for a couple seconds
|
|
Engine.time_scale = 0.0001
|
|
else:
|
|
$PlayerHealthBar/PlayerHealthCurrent.text = str(new_health)
|
|
$PlayerHealthBar.value = new_health
|
|
|
|
|
|
func _on_enemy_damaged_player(damage: int) -> void:
|
|
var damage_received = damage
|
|
_set_player_current_health(-damage_received)
|
|
|
|
|
|
func _calculate_player_level() -> float:
|
|
var current_exp = PlayerState.experience
|
|
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:
|
|
PlayerState.experience += area_exp
|
|
PlayerState.level = _calculate_player_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())
|