Stamina counter

This commit is contained in:
Dustin 2025-04-10 20:12:47 -07:00
parent 2f8d233559
commit 41b740be4f
8 changed files with 87 additions and 11 deletions

View File

@ -0,0 +1,26 @@
class_name StaminaComponent
extends Node
export var max_stamina: int = 0
onready var stamina :int = max_stamina
signal stamina_depleted()
func reduce_stamina(_amount :int):
if _amount < 0:
push_error("ERROR: Only positive numbers in stamina component functions.")
stamina -= _amount
if stamina <= 0:
emit_signal("stamina_depleted")
stamina = 0
func recover(recover_amount :int):
if recover_amount < 0:
push_error("ERROR: Only positive numbers in stamina component functions.")
return
stamina += recover_amount
if stamina > max_stamina:
stamina = max_stamina

View File

@ -0,0 +1,6 @@
[gd_scene load_steps=2 format=2]
[ext_resource path="res://lib/components/Stamina_Component.gd" type="Script" id=1]
[node name="Stamina_Component" type="Node"]
script = ExtResource( 1 )

View File

@ -104,6 +104,11 @@ _global_script_classes=[ {
"language": "GDScript", "language": "GDScript",
"path": "res://lib/classes/sound_effect_state_frame.gd" "path": "res://lib/classes/sound_effect_state_frame.gd"
}, { }, {
"base": "Node",
"class": "StaminaComponent",
"language": "GDScript",
"path": "res://lib/components/Stamina_Component.gd"
}, {
"base": "Resource", "base": "Resource",
"class": "State", "class": "State",
"language": "GDScript", "language": "GDScript",
@ -159,6 +164,7 @@ _global_script_class_icons={
"Movement_StateReceiver": "", "Movement_StateReceiver": "",
"Projectile": "", "Projectile": "",
"SE_StateFrame": "", "SE_StateFrame": "",
"StaminaComponent": "",
"State": "", "State": "",
"StateAnimatedActor": "", "StateAnimatedActor": "",
"StateMachine": "", "StateMachine": "",

View File

@ -5,6 +5,9 @@ var parent_request_state_change :FuncRef
var player_number: int = 1 var player_number: int = 1
onready var pew_machine = $"%PewMachine" onready var pew_machine = $"%PewMachine"
onready var stamina_component = $"%Stamina_Component"
var state_stamina_cost :Dictionary
func _ready(): func _ready():
var state_ref :State var state_ref :State
@ -30,7 +33,7 @@ func get_movement_direction() -> float:
var _wants_jump :bool var _wants_jump :bool
func wants_jump() -> bool: func wants_jump() -> bool:
if Input.is_action_just_pressed("jump_" + str(player_number)): if Input.is_action_just_pressed("jump_" + str(player_number)) and stamina_component.stamina >= state_stamina_cost["jump"]:
desired_movement_vector.y = UP desired_movement_vector.y = UP
_wants_jump = true _wants_jump = true
return true return true

View File

@ -7,25 +7,44 @@ export var player_number: int = 1
# var b = "text" # var b = "text"
onready var player_inventory :InventoryManager = InventoryManager.new() onready var player_inventory :InventoryManager = InventoryManager.new()
onready var stamina_component = $"%Stamina_Component"
onready var health_component = $"%Health_Component"
var punch_item :Item = preload("res://assets/items/punch.tres") var punch_item :Item = preload("res://assets/items/punch.tres")
const state_stamina_cost :Dictionary = {
"jump": 10,
"attack_sword":20
}
# Called when the node enters the scene tree for the first time. # Called when the node enters the scene tree for the first time.
func _ready(): func _ready():
PlayerInfo.player_health = $Health_Component.health PlayerInfo.player_health = health_component.health
PlayerInfo.player_inventory = player_inventory PlayerInfo.player_inventory = player_inventory
PlayerInfo.player_stamina = stamina_component.stamina
# Set initial player position in world. # Set initial player position in world.
global_position = LevelInfo.player_start_position global_position = LevelInfo.player_start_position
player_inventory.add_to_inventory(punch_item) player_inventory.add_to_inventory(punch_item)
player_inventory.select_primary(punch_item) player_inventory.select_primary(punch_item)
movement_component.state_stamina_cost = state_stamina_cost
var stamina_recovery :float = 0.0
# Called every frame. 'delta' is the elapsed time since the previous frame. # Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta): func _process(delta):
PlayerInfo.player_position = global_position.round() PlayerInfo.player_position = global_position.round()
stamina_recovery += delta
if stamina_recovery > 1.0:
stamina_component.recover(1)
stamina_recovery = 0.0
#print("recover ", stamina_component.stamina)
PlayerInfo.player_stamina = stamina_component.stamina
match movement_state_machine.current_state.name: match movement_state_machine.current_state.name:
"idle": "idle":
if Input.is_action_just_released("switch_primary_item_" + str(player_number)): if Input.is_action_just_released("switch_primary_item_" + str(player_number)):
@ -39,10 +58,10 @@ func _process(delta):
func hit_Receiver(damage): func hit_Receiver(damage):
$Hurtbox_Component.set_hurtbox(false) $Hurtbox_Component.set_hurtbox(false)
$Health_Component.take_damage(damage) health_component.take_damage(damage)
if $Health_Component.health > 0: if $Health_Component.health > 0:
movement_state_machine.change_to_known_state('hurt') movement_state_machine.change_to_known_state('hurt')
PlayerInfo.player_health = $Health_Component.health PlayerInfo.player_health = health_component.health
if PlayerInfo.player_health <= 0: if PlayerInfo.player_health <= 0:
return return
yield( get_tree().create_timer(2 + movement_state_machine.get_state_reference('hurt').timeout_seconds), "timeout") yield( get_tree().create_timer(2 + movement_state_machine.get_state_reference('hurt').timeout_seconds), "timeout")
@ -111,3 +130,8 @@ func secondary_item() -> String:
# _: # None # _: # None
# return true # return true
# return true # return true
func _on_Movement_StateMachine_state_changed(old_state_name, new_state):
if new_state.name == "jump":
stamina_component.reduce_stamina(state_stamina_cost["jump"])

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=35 format=2] [gd_scene load_steps=36 format=2]
[ext_resource path="res://lib/parent_scenes/actor.tscn" type="PackedScene" id=1] [ext_resource path="res://lib/parent_scenes/actor.tscn" type="PackedScene" id=1]
[ext_resource path="res://assets/actors/players/playerE/PlayerE_SpriteFrames.tres" type="SpriteFrames" id=2] [ext_resource path="res://assets/actors/players/playerE/PlayerE_SpriteFrames.tres" type="SpriteFrames" id=2]
@ -28,6 +28,7 @@
[ext_resource path="res://assets_tmp/SE/land.wav" type="AudioStream" id=26] [ext_resource path="res://assets_tmp/SE/land.wav" type="AudioStream" id=26]
[ext_resource path="res://src/actors/players/playerE/states/attack_punch.tres" type="Resource" id=27] [ext_resource path="res://src/actors/players/playerE/states/attack_punch.tres" type="Resource" id=27]
[ext_resource path="res://src/components/PewMachine.tscn" type="PackedScene" id=28] [ext_resource path="res://src/components/PewMachine.tscn" type="PackedScene" id=28]
[ext_resource path="res://lib/components/Stamina_Component.tscn" type="PackedScene" id=29]
[sub_resource type="Resource" id=3] [sub_resource type="Resource" id=3]
resource_local_to_scene = true resource_local_to_scene = true
@ -131,9 +132,14 @@ position = Vector2( 0, 2 )
shape = SubResource( 1 ) shape = SubResource( 1 )
[node name="Health_Component" parent="." index="8" instance=ExtResource( 17 )] [node name="Health_Component" parent="." index="8" instance=ExtResource( 17 )]
unique_name_in_owner = true
max_health = 100 max_health = 100
[node name="Hurtbox_Component" parent="." index="9" instance=ExtResource( 16 )] [node name="Stamina_Component" parent="." index="9" instance=ExtResource( 29 )]
unique_name_in_owner = true
max_stamina = 50
[node name="Hurtbox_Component" parent="." index="10" instance=ExtResource( 16 )]
collision_layer = 16 collision_layer = 16
collision_mask = 128 collision_mask = 128
hurtbox_entered_function = "hit_Receiver" hurtbox_entered_function = "hit_Receiver"
@ -143,8 +149,10 @@ modulate = Color( 0, 0, 1, 1 )
position = Vector2( -1, 2 ) position = Vector2( -1, 2 )
shape = SubResource( 2 ) shape = SubResource( 2 )
[node name="Interactable_Receiver" type="Node" parent="." index="10"] [node name="Interactable_Receiver" type="Node" parent="." index="11"]
script = ExtResource( 21 ) script = ExtResource( 21 )
interactable_parent_callback = "touch_the_thing" interactable_parent_callback = "touch_the_thing"
[node name="PewMachine" parent="." index="11" instance=ExtResource( 28 )] [node name="PewMachine" parent="." index="12" instance=ExtResource( 28 )]
[connection signal="state_changed" from="Movement_StateMachine" to="." method="_on_Movement_StateMachine_state_changed"]

View File

@ -16,6 +16,8 @@ onready var DialogIndicator = $PanelContainer/Polygon2D
onready var health_bar = $HealthBar onready var health_bar = $HealthBar
onready var stamina_bar = $StaminaBar
## Cool I can bring this in with a ctrl drag ## Cool I can bring this in with a ctrl drag
onready var selected_inventory_items = $"%SelectedInventoryItems" onready var selected_inventory_items = $"%SelectedInventoryItems"

View File

@ -1,4 +1,4 @@
[gd_scene load_steps=9 format=2] [gd_scene load_steps=10 format=2]
[ext_resource path="res://icon.png" type="Texture" id=1] [ext_resource path="res://icon.png" type="Texture" id=1]
[ext_resource path="res://assets/Fonts/QuinqueFive.tres" type="DynamicFont" id=2] [ext_resource path="res://assets/Fonts/QuinqueFive.tres" type="DynamicFont" id=2]
@ -8,6 +8,7 @@
[ext_resource path="res://assets/UI/Healthbar-Indicator.png" type="Texture" id=6] [ext_resource path="res://assets/UI/Healthbar-Indicator.png" type="Texture" id=6]
[ext_resource path="res://assets/UI/Staminabar-Indicator.png" type="Texture" id=7] [ext_resource path="res://assets/UI/Staminabar-Indicator.png" type="Texture" id=7]
[ext_resource path="res://src/ui/SelectedInventoryItems.gd" type="Script" id=8] [ext_resource path="res://src/ui/SelectedInventoryItems.gd" type="Script" id=8]
[ext_resource path="res://src/ui/StaminaBar.gd" type="Script" id=9]
[node name="HUD" type="CanvasLayer"] [node name="HUD" type="CanvasLayer"]
pause_mode = 2 pause_mode = 2
@ -71,7 +72,7 @@ value = 50.0
texture_under = ExtResource( 4 ) texture_under = ExtResource( 4 )
texture_progress = ExtResource( 7 ) texture_progress = ExtResource( 7 )
texture_progress_offset = Vector2( 1, 1 ) texture_progress_offset = Vector2( 1, 1 )
script = ExtResource( 5 ) script = ExtResource( 9 )
[node name="Debug" type="Label" parent="."] [node name="Debug" type="Label" parent="."]
anchor_left = 1.0 anchor_left = 1.0