From c467182087ae1d157c2b42461061169a3a056b45 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 27 Jan 2024 23:30:23 +0100 Subject: [PATCH] Added energy and replenishment --- entities/Player.gd | 32 +++++++++++++++++++++++++------- scenes/Game.tscn | 12 +++++++++++- scenes/GameUI.gd | 4 ++++ scenes/World.gd | 3 ++- scenes/World.tscn | 5 +---- 5 files changed, 43 insertions(+), 13 deletions(-) diff --git a/entities/Player.gd b/entities/Player.gd index 90932d1..ea43853 100644 --- a/entities/Player.gd +++ b/entities/Player.gd @@ -24,7 +24,7 @@ enum PlayerState { } var score : int = 0 -var energy : float = 1.0 +var energy : float = 100.0 var state : PlayerState = PlayerState.Alive var state_last : PlayerState = PlayerState.Dead @@ -39,7 +39,10 @@ var coloring_bomb_sprite : Sprite2D const SPEED = 5.0 const DASH_SPEED: float = 20 const DASH_DURATION: float = 0.2 +const DASH_ENERGY: float = 40 const BOMB_DURATION: float = 0.1 +const BOMB_ENERGY: float = 60 +const ENERGY_REPLENISH_RATE: float = 0.5; # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") @@ -53,6 +56,9 @@ func _ready(): coloring_bomb_sprite.visible = false func _physics_process(delta): + if energy < 100: + energy = min (100, energy + delta + ENERGY_REPLENISH_RATE) + if state_last != state: if state == PlayerState.Alive: on_player_spawn() @@ -85,9 +91,10 @@ func _physics_process(delta): coloring_bomb_sprite.visible = false bomb_time = 0 else: - if Input.is_action_just_pressed(bomb_action) and state == PlayerState.Alive: - coloring_bomb_sprite.visible = true; - bomb_time = BOMB_DURATION + if Input.is_action_just_pressed(bomb_action) \ + and state == PlayerState.Alive \ + and energy > BOMB_ENERGY: + on_drop_bomb() # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. @@ -103,9 +110,10 @@ func _physics_process(delta): velocity.z = move_toward(velocity.z, 0, SPEED) geometry.global_basis = Basis.from_euler(Vector3(0, -angle, 0)) - if state == PlayerState.Alive and Input.is_action_just_pressed(dash_action): - is_dashing = true; - dash_time = DASH_DURATION + if Input.is_action_just_pressed(dash_action) \ + and state == PlayerState.Alive \ + and energy > DASH_ENERGY: + on_dash() if is_dashing: velocity.x = sin(angle) * DASH_SPEED @@ -134,3 +142,13 @@ func on_player_falling(): func on_player_dead(): print ("Player " + str(self) + ": death") + +func on_dash(): + is_dashing = true; + dash_time = DASH_DURATION + energy = max (0, energy - DASH_ENERGY) + +func on_drop_bomb(): + coloring_bomb_sprite.visible = true; + bomb_time = BOMB_DURATION + energy = max (0, energy - BOMB_ENERGY) diff --git a/scenes/Game.tscn b/scenes/Game.tscn index 044ced1..04dfc65 100644 --- a/scenes/Game.tscn +++ b/scenes/Game.tscn @@ -114,7 +114,6 @@ text = "Game Over!" horizontal_alignment = 1 [node name="GameRunningWidgets" type="Panel" parent="GameUI"] -visible = false custom_minimum_size = Vector2(0, 80) layout_mode = 1 anchors_preset = 10 @@ -137,6 +136,11 @@ layout_mode = 2 text = "0 " +[node name="EnergyProgressBar" type="ProgressBar" parent="GameUI/GameRunningWidgets/Player1Widgets"] +custom_minimum_size = Vector2(400, 0) +layout_mode = 2 +value = 33.0 + [node name="Player2Widgets" type="VBoxContainer" parent="GameUI/GameRunningWidgets"] layout_direction = 3 layout_mode = 1 @@ -154,6 +158,12 @@ layout_mode = 2 text = "0 " +[node name="EnergyProgressBar" type="ProgressBar" parent="GameUI/GameRunningWidgets/Player2Widgets"] +custom_minimum_size = Vector2(400, 0) +layout_direction = 2 +layout_mode = 2 +value = 75.0 + [node name="TimeWidgets" type="VBoxContainer" parent="GameUI/GameRunningWidgets"] layout_mode = 1 anchors_preset = 5 diff --git a/scenes/GameUI.gd b/scenes/GameUI.gd index ee30712..e734b4d 100644 --- a/scenes/GameUI.gd +++ b/scenes/GameUI.gd @@ -7,7 +7,9 @@ extends Control @onready var game_finished_widgets : Control = $'GameFinishedWidgets' @onready var player1_score_label : Label = $'GameRunningWidgets/Player1Widgets/ScoreLabel' +@onready var player1_energy_progressbar : ProgressBar = $'GameRunningWidgets/Player1Widgets/EnergyProgressBar' @onready var player2_score_label : Label = $'GameRunningWidgets/Player2Widgets/ScoreLabel' +@onready var player2_energy_progressbar : ProgressBar = $'GameRunningWidgets/Player2Widgets/EnergyProgressBar' @onready var player2_widgets : Control = $'GameRunningWidgets/Player2Widgets' @onready var time_left_label : Label = $'GameRunningWidgets/TimeWidgets/TimeLeftLabel' @@ -42,9 +44,11 @@ func _process(_delta): game_finished_widgets.visible = true player1_score_label.text = str(world.players[0].score) + player1_energy_progressbar.value = world.players[0].energy if player2_widgets.visible: player2_score_label.text = str(world.players[1].score) + player2_energy_progressbar.value = world.players[1].energy func _input(event): if event.is_action_pressed("ui_accept"): diff --git a/scenes/World.gd b/scenes/World.gd index 857778e..798c69d 100644 --- a/scenes/World.gd +++ b/scenes/World.gd @@ -181,10 +181,11 @@ func stop_gameplay(): player.set_physics_process(false) func start_gameplay(): - print("Starting gameplay") + print("Starting ggameplay") time_left = GAME_DURATION_SECONDS for player: Player in players: + player.energy = 100; player.coloring_sprite.visible = true player.coloring_bomb_sprite.visible = false player.set_process(true) diff --git a/scenes/World.tscn b/scenes/World.tscn index 4085a1d..20db0ef 100644 --- a/scenes/World.tscn +++ b/scenes/World.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=14 format=3 uid="uid://b1nm5h3yccr16"] +[gd_scene load_steps=13 format=3 uid="uid://b1nm5h3yccr16"] [ext_resource type="Script" path="res://scenes/World.gd" id="1_gtcjp"] [ext_resource type="PackedScene" uid="uid://bfyjtfdko3l7o" path="res://entities/Player.tscn" id="2_a343a"] @@ -8,7 +8,6 @@ [ext_resource type="PackedScene" uid="uid://7a2ma10hq0qa" path="res://scenes/PlatformA.tscn" id="6_83plb"] [ext_resource type="PackedScene" uid="uid://bnmtyc1kkt6ku" path="res://scenes/PlatformB.tscn" id="7_cld3m"] [ext_resource type="PackedScene" uid="uid://cc521i07ajufm" path="res://entities/SpawnPoint.tscn" id="8_ykvyf"] -[ext_resource type="PackedScene" uid="uid://0lrpepyejfxg" path="res://scenes/TestObstacles.tscn" id="9_eml1e"] [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_5gauv"] sky_energy_multiplier = 5.0 @@ -137,5 +136,3 @@ player_index = 1 [node name="SpawnPointPlayer3" parent="Level" instance=ExtResource("8_ykvyf")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 9.55561, 0, 3.26255) - -[node name="Obstacles" parent="Level" instance=ExtResource("9_eml1e")]