From d4c43a6101be6eea02f68607d94282a0908be033 Mon Sep 17 00:00:00 2001 From: Robin Ebert Date: Mon, 15 Apr 2024 22:26:58 +0200 Subject: [PATCH] Add healing on room change --- src/scenes/player_ui.tscn | 1 + src/scripts/door.gd | 2 ++ src/scripts/hp-bar.gd | 4 +++- src/scripts/player.gd | 34 +++++++++++++++++++++++----------- src/scripts/player_ui.gd | 4 +++- 5 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/scenes/player_ui.tscn b/src/scenes/player_ui.tscn index b63df0e..0c0d006 100644 --- a/src/scenes/player_ui.tscn +++ b/src/scenes/player_ui.tscn @@ -132,6 +132,7 @@ label_settings = SubResource("LabelSettings_kmvbg") vertical_alignment = 1 [node name="ScoreText" type="Label" parent="MarginContainer/VBoxContainer/MarginContainer/VBoxContainer/HPBar"] +layout_mode = 0 offset_left = 210.0 offset_top = 16.0 offset_right = 269.0 diff --git a/src/scripts/door.gd b/src/scripts/door.gd index 74a1392..fac9b53 100644 --- a/src/scripts/door.gd +++ b/src/scripts/door.gd @@ -9,6 +9,8 @@ func enable(): modulate *= 2 func _on_body_entered(_body): + var player = _body as Player + player.heal(player.HEAL_ON_ROOM_CHANGE) var score = get_parent().score var world = get_tree().get_first_node_in_group("world") world.add_score(score) diff --git a/src/scripts/hp-bar.gd b/src/scripts/hp-bar.gd index 2b7d77d..08e5cdd 100644 --- a/src/scripts/hp-bar.gd +++ b/src/scripts/hp-bar.gd @@ -12,4 +12,6 @@ func set_value(val: int) -> void: label.text = " " + str(val) func _ready() -> void: - set_value(max_value) + var player = get_tree().get_first_node_in_group("player") + player.life_changed.connect(set_value) + set_value(player.life) diff --git a/src/scripts/player.gd b/src/scripts/player.gd index 81bfbe4..6a3d1c4 100644 --- a/src/scripts/player.gd +++ b/src/scripts/player.gd @@ -2,19 +2,31 @@ class_name Player extends CharacterBody2D +const SPIKE_DAMAGE := 10 +const SPIKE_DAMAGE_COOLDOWN := 0.7 +const ATTACK_ANIMATION_LENGTH := 1.0 +const KNOCKBACK_ENVELOPE: float = 0.86 +const LOOK_AHEAD_MAX := 20.0 +const LOOK_AHEAD_SPEED := 60.0 +const MAX_LIFE: float = 100 +const HEAL_ON_ROOM_CHANGE: float = MAX_LIFE / 2 + +# Ghosts +const Ghost = preload("res://scenes/ghost.tscn") + +enum MovementPhase { STANDING = 0, ACCELERATING = 1, DECELERATING = 2, TURNING = 3 } + @onready var sprite := $Sprite @onready var ui := $Camera2D/PlayerUI @onready var hit_indicator := $HitIndicationAnimationPlayer @onready var ghost_inventory := $GhostInventory @onready var cam := $Camera2D + var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") -var life = 100 -const SPIKE_DAMAGE := 10 -const SPIKE_DAMAGE_COOLDOWN := 0.7 +var life := MAX_LIFE var spike_damage_timer := 0.0 var colliding_spike := false var shield_multiplier := 1.0 -const ATTACK_ANIMATION_LENGTH := 1.0 var attack_animation_t := 0.0 # Movement @@ -27,10 +39,6 @@ var DECELERATION_SPEED := 550.0 var TURNING_SPEED := 1500.0 var MAX_SPEED := 100.0 var EPSILON := 0.001 -const KNOCKBACK_ENVELOPE: float = 0.86 -const LOOK_AHEAD_MAX := 20.0 -const LOOK_AHEAD_SPEED := 60.0 -enum MovementPhase { STANDING = 0, ACCELERATING = 1, DECELERATING = 2, TURNING = 3 } var movement_phase := MovementPhase.STANDING var knockback := Vector2(0, 0) var direction := 0.0 @@ -47,8 +55,7 @@ var jump_buffer := 0 var spell_inventory: Array[SpellBook.Spells] = [] var active_spells: Array[bool] = [] -# Ghosts -const Ghost = preload("res://scenes/ghost.tscn") +signal life_changed(amount: int) func _ready(): hit_indicator.play("RESET") @@ -184,9 +191,14 @@ func game_over(): func take_damage(dmg: int): SfxAudio.play_sfx(SfxAudio.Sound.HIT) life -= dmg * shield_multiplier - get_tree().get_first_node_in_group('hp-bar').set_value(life) + life_changed.emit(life) hit_indicator.play('hit') +func heal(amount: int): + life += amount + life = min(life, MAX_LIFE) + life_changed.emit(life) + func enable_shield(strength: float): shield_multiplier = strength $Shield.visible = true diff --git a/src/scripts/player_ui.gd b/src/scripts/player_ui.gd index 4c67227..f856007 100644 --- a/src/scripts/player_ui.gd +++ b/src/scripts/player_ui.gd @@ -17,7 +17,9 @@ func _ready() -> void: ovrride_box.border_color = Color(1.0, 0.4, 0.6) for box in [dflt_box, ovrride_box]: box.set_border_width_all(4.0) - get_tree().get_first_node_in_group("world").score_changed.connect(_update_score) + + var world = get_tree().get_first_node_in_group("world") + world.score_changed.connect(_update_score) func add_spell_item_panel(spell: SpellBook.Spells): var panel: MarginContainer = inPanel.duplicate()