From 4cbf424c69755e141518a3f6eea877d5ffa113c4 Mon Sep 17 00:00:00 2001 From: tonydero Date: Thu, 24 Oct 2024 08:46:36 -0600 Subject: [PATCH] refactor player health changes and initialization, rename PlayerState variables --- scripts/NPCEnemy.gd | 8 +++++-- scripts/Player.gd | 47 ++++++++++++++++++++++++++++-------------- scripts/PlayerState.gd | 16 +++++++------- scripts/SignalBus.gd | 2 +- 4 files changed, 47 insertions(+), 26 deletions(-) diff --git a/scripts/NPCEnemy.gd b/scripts/NPCEnemy.gd index a8646c7..e693c10 100644 --- a/scripts/NPCEnemy.gd +++ b/scripts/NPCEnemy.gd @@ -4,6 +4,8 @@ extends NPC var target +var attack_base := 1 +var attack_damage := 0 var speed := 200 var prevCollisions := 0 @@ -32,6 +34,9 @@ func _ready() -> void: %Healthbar.max_value = maxHealth %Healthbar.value = maxHealth + # set damage + attack_damage = attack_base + func _process(_delta: float) -> void: %Healthbar.value = maxHealth - damageTaken @@ -59,8 +64,7 @@ func _physics_process(_delta): func _deal_damage() -> void: - PlayerState.player_currenthealth -= 1 - SignalBus.enemy_damaged_player.emit() + SignalBus.enemy_damaged_player.emit(attack_damage) func _on_attack_timer_timeout() -> void: _deal_damage() diff --git a/scripts/Player.gd b/scripts/Player.gd index 9d5cf62..3f3d359 100644 --- a/scripts/Player.gd +++ b/scripts/Player.gd @@ -4,9 +4,9 @@ extends VBoxContainer var area_exp := 0.0 -var current_health : int -var resource_type : String -var current_resource : int +var current_health: int +var resource_type: String +var current_resource: int func _ready() -> void: @@ -15,7 +15,7 @@ func _ready() -> void: SignalBus.enemy_damaged_player.connect(_on_enemy_damaged_player) _set_player_display() $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: @@ -29,7 +29,7 @@ func _on_enemy_died(enemyDifficulty, enemyTier, _enemyFaction) -> void: func _get_player_current_health() -> void: - var playerCurrHeath = PlayerState.player_currenthealth + var playerCurrHeath = PlayerState.current_health $PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath) $PlayerHealthBar.value = playerCurrHeath if playerCurrHeath <= 0: @@ -38,19 +38,34 @@ func _get_player_current_health() -> void: func _set_player_display() -> void: - var playerMaxHeath = PlayerState.player_maxhealth - var playerCurrHeath = PlayerState.player_currenthealth - $PlayerHealthBar/PlayerHealthMax.text = str(playerMaxHeath) - $PlayerHealthBar/PlayerHealthCurrent.text = str(playerCurrHeath) - $PlayerHealthBar.value = playerCurrHeath + 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() -> void: - _get_player_current_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.player_exp + var current_exp = PlayerState.experience 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: - PlayerState.player_exp += area_exp - PlayerState.player_level = _calculate_player_level() + PlayerState.experience += area_exp + PlayerState.level = _calculate_player_level() $PlayerExpBar.value = _get_level_progress_percent() - $PlayerLabel/PlayerLevel.text = str(floor(PlayerState.player_level)) + $PlayerLabel/PlayerLevel.text = str(floor(PlayerState.level)) # if Globals.debug: # print("FA: ", area_exp) # print("L: ", PlayerState.player_level) diff --git a/scripts/PlayerState.gd b/scripts/PlayerState.gd index 939f4a5..237592d 100644 --- a/scripts/PlayerState.gd +++ b/scripts/PlayerState.gd @@ -1,12 +1,14 @@ # singleton to store player state/values +# everything contained here needs to be utilized in many places or saved +# between play sessions extends Node -@export var player_constitution := 1 -@export var player_exp := 0.0 -@export var player_level := 1.0 -@export var player_maxhealth := player_constitution * player_level * 100 +var constitution := 1 +var experience := 0.0 +var level := 1.0 +var max_health := constitution * 100 # TEST: change current health to var on player and use signals to take damage? -@export var player_currenthealth := player_maxhealth -var player_maxresource := 100 -var player_weapon_type : Globals.Weapon.WeaponType +var current_health := max_health +var max_resource := 100 +var weapon_type: Globals.Weapon.WeaponType diff --git a/scripts/SignalBus.gd b/scripts/SignalBus.gd index 7034e94..2f23e26 100644 --- a/scripts/SignalBus.gd +++ b/scripts/SignalBus.gd @@ -7,7 +7,7 @@ signal enemy_died( npcTier: NPC.NPCTier, faction: Globals.World.Faction, ) -signal enemy_damaged_player +signal enemy_damaged_player(damage : int) signal quest_generated(quest: Quest) signal quest_completed signal area_continue_pressed()