refactor player health changes and initialization, rename PlayerState variables

This commit is contained in:
tonydero 2024-10-24 08:46:36 -06:00
parent e752b512ec
commit 4cbf424c69
4 changed files with 47 additions and 26 deletions

View File

@ -4,6 +4,8 @@ extends NPC
var target var target
var attack_base := 1
var attack_damage := 0
var speed := 200 var speed := 200
var prevCollisions := 0 var prevCollisions := 0
@ -32,6 +34,9 @@ func _ready() -> void:
%Healthbar.max_value = maxHealth %Healthbar.max_value = maxHealth
%Healthbar.value = maxHealth %Healthbar.value = maxHealth
# set damage
attack_damage = attack_base
func _process(_delta: float) -> void: func _process(_delta: float) -> void:
%Healthbar.value = maxHealth - damageTaken %Healthbar.value = maxHealth - damageTaken
@ -59,8 +64,7 @@ func _physics_process(_delta):
func _deal_damage() -> void: func _deal_damage() -> void:
PlayerState.player_currenthealth -= 1 SignalBus.enemy_damaged_player.emit(attack_damage)
SignalBus.enemy_damaged_player.emit()
func _on_attack_timer_timeout() -> void: func _on_attack_timer_timeout() -> void:
_deal_damage() _deal_damage()

View File

@ -4,9 +4,9 @@ extends VBoxContainer
var area_exp := 0.0 var area_exp := 0.0
var current_health : int var current_health: int
var resource_type : String var resource_type: String
var current_resource : int var current_resource: int
func _ready() -> void: func _ready() -> void:
@ -15,7 +15,7 @@ func _ready() -> void:
SignalBus.enemy_damaged_player.connect(_on_enemy_damaged_player) SignalBus.enemy_damaged_player.connect(_on_enemy_damaged_player)
_set_player_display() _set_player_display()
$PlayerExpBar.value = _get_level_progress_percent() $PlayerExpBar.value = _get_level_progress_percent()
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.player_level)) $PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void: func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void:
@ -29,7 +29,7 @@ func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void:
func _get_player_current_health() -> void: func _get_player_current_health() -> void:
var playerCurrHeath = PlayerState.player_currenthealth var playerCurrHeath = PlayerState.current_health
$PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath) $PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath)
$PlayerHealthBar.value = playerCurrHeath $PlayerHealthBar.value = playerCurrHeath
if playerCurrHeath <= 0: if playerCurrHeath <= 0:
@ -38,19 +38,34 @@ func _get_player_current_health() -> void:
func _set_player_display() -> void: func _set_player_display() -> void:
var playerMaxHeath = PlayerState.player_maxhealth var max_health = PlayerState.max_health
var playerCurrHeath = PlayerState.player_currenthealth current_health = max_health
$PlayerHealthBar/PlayerHealthMax.text = str(playerMaxHeath) $PlayerHealthBar/PlayerHealthMax.text = str(max_health)
$PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath) $PlayerHealthBar/PlayerHealthCurrent.text = str(current_health)
$PlayerHealthBar.value = playerCurrHeath $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() -> void: func _on_enemy_damaged_player(damage: int) -> void:
_get_player_current_health() var damage_received = damage
_set_player_current_health(-damage_received)
func _calculate_player_level() -> float: func _calculate_player_level() -> float:
var current_exp = PlayerState.player_exp var current_exp = PlayerState.experience
return log((0.2 * current_exp + 5) / 5.0) + 1 return log((0.2 * current_exp + 5) / 5.0) + 1
@ -60,10 +75,10 @@ func _get_level_progress_percent() -> float:
func _on_quest_completed() -> void: func _on_quest_completed() -> void:
PlayerState.player_exp += area_exp PlayerState.experience += area_exp
PlayerState.player_level = _calculate_player_level() PlayerState.level = _calculate_player_level()
$PlayerExpBar.value = _get_level_progress_percent() $PlayerExpBar.value = _get_level_progress_percent()
$PlayerLabel/PlayerLevel.text = str(floor(PlayerState.player_level)) $PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level))
# if Globals.debug: # if Globals.debug:
# print("FA: ", area_exp) # print("FA: ", area_exp)
# print("L: ", PlayerState.player_level) # print("L: ", PlayerState.player_level)

View File

@ -1,12 +1,14 @@
# singleton to store player state/values # singleton to store player state/values
# everything contained here needs to be utilized in many places or saved
# between play sessions
extends Node extends Node
@export var player_constitution := 1 var constitution := 1
@export var player_exp := 0.0 var experience := 0.0
@export var player_level := 1.0 var level := 1.0
@export var player_maxhealth := player_constitution * player_level * 100 var max_health := constitution * 100
# TEST: change current health to var on player and use signals to take damage? # TEST: change current health to var on player and use signals to take damage?
@export var player_currenthealth := player_maxhealth var current_health := max_health
var player_maxresource := 100 var max_resource := 100
var player_weapon_type : Globals.Weapon.WeaponType var weapon_type: Globals.Weapon.WeaponType

View File

@ -7,7 +7,7 @@ signal enemy_died(
npcTier: NPC.NPCTier, npcTier: NPC.NPCTier,
faction: Globals.World.Faction, faction: Globals.World.Faction,
) )
signal enemy_damaged_player signal enemy_damaged_player(damage : int)
signal quest_generated(quest: Quest) signal quest_generated(quest: Quest)
signal quest_completed signal quest_completed
signal area_continue_pressed() signal area_continue_pressed()