Refactor UI components to inherit from Control and update node paths for consistency
This commit is contained in:
13
scripts/Resources/LevelResource.cs
Normal file
13
scripts/Resources/LevelResource.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class LevelResource : Resource
|
||||
{
|
||||
[Export]
|
||||
public string LevelName { get; set; } = string.Empty;
|
||||
|
||||
[Export]
|
||||
public string ScenePath { get; set; } = string.Empty;
|
||||
}
|
@@ -3,7 +3,7 @@ using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class AudioSettings : Node
|
||||
public partial class AudioSettings : Control
|
||||
{
|
||||
[Export] public Slider MasterVolumeSlider { get; set; }
|
||||
[Export] public Slider MusicVolumeSlider { get; set; }
|
||||
|
@@ -4,7 +4,7 @@ using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class ChargeProgressBar : Node
|
||||
public partial class ChargeProgressBar : ProgressBar
|
||||
{
|
||||
[Export] public ProgressBar ProgressBar { get; set; }
|
||||
[Export] public BrickThrowComponent ThrowComponent { get; set; }
|
||||
@@ -27,6 +27,11 @@ public partial class ChargeProgressBar : Node
|
||||
|
||||
private void SetupDependencies()
|
||||
{
|
||||
if (ThrowComponent == null || ProgressBar == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (ThrowComponent.ThrowInputBehavior is ChargeThrowInputResource throwInput)
|
||||
{
|
||||
_throwInput = throwInput;
|
||||
|
72
scripts/UI/DeathScreen.cs
Normal file
72
scripts/UI/DeathScreen.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
[GlobalClass]
|
||||
public partial class DeathScreen : Control
|
||||
{
|
||||
[Export] public LevelResource CurrentLevel { get; set; }
|
||||
[Export] public Label CurrentLevelLabel { get; set; }
|
||||
[Export] public Label LivesLeftLabel { get; set; }
|
||||
[Export] public float TimeoutTime { get; set; } = 2.0f;
|
||||
[Export] public Godot.Collections.Array<Node> NodesToDisable { get; set; } = new();
|
||||
|
||||
private GameManager _gameManager;
|
||||
private Timer _timer;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
SetLabels();
|
||||
}
|
||||
|
||||
private void SetLabels()
|
||||
{
|
||||
if (_gameManager == null) return;
|
||||
|
||||
if (CurrentLevel != null)
|
||||
{
|
||||
CurrentLevelLabel.Text = CurrentLevel.LevelName;
|
||||
}
|
||||
LivesLeftLabel.Text = $" x {_gameManager.GetLives()}";
|
||||
}
|
||||
|
||||
private void SetupTimer()
|
||||
{
|
||||
_timer = new Timer();
|
||||
_timer.WaitTime = TimeoutTime;
|
||||
_timer.OneShot = true;
|
||||
_timer.Timeout += OnTimeout;
|
||||
AddChild(_timer);
|
||||
_timer.Start();
|
||||
}
|
||||
|
||||
private void ToggleNodes()
|
||||
{
|
||||
foreach (var node in NodesToDisable)
|
||||
{
|
||||
node.ProcessMode = node.ProcessMode == ProcessModeEnum.Disabled
|
||||
? ProcessModeEnum.Inherit
|
||||
: ProcessModeEnum.Disabled;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPlayerDeath()
|
||||
{
|
||||
if (_gameManager == null) return;
|
||||
|
||||
ToggleNodes();
|
||||
SetLabels();
|
||||
Show();
|
||||
SetupTimer();
|
||||
}
|
||||
|
||||
private void OnTimeout()
|
||||
{
|
||||
if (_gameManager == null || _gameManager.GetLives() == 0) return;
|
||||
|
||||
GetTree().ReloadCurrentScene();
|
||||
}
|
||||
}
|
@@ -3,7 +3,7 @@ using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class GameOverScreen : Node
|
||||
public partial class GameOverScreen : Control
|
||||
{
|
||||
[Export] public Control GameOverPanel { get; set; }
|
||||
[Export] public Button RestartButton { get; set; }
|
||||
|
@@ -4,7 +4,7 @@ using Mr.BrickAdventures.scripts.components;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class Hud : Node
|
||||
public partial class Hud : Control
|
||||
{
|
||||
[Export] public HealthComponent Health { get; set; }
|
||||
[Export] public Label CoinsLabel { get; set; }
|
||||
|
@@ -3,7 +3,7 @@ using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class MainMenu : Node
|
||||
public partial class MainMenu : Control
|
||||
{
|
||||
[Export] public Control MainMenuControl { get; set; }
|
||||
[Export] public Button NewGameButton { get; set; }
|
||||
|
@@ -7,7 +7,7 @@ using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class Marketplace : Node
|
||||
public partial class Marketplace : Control
|
||||
{
|
||||
[Export] public Array<SkillData> Skills { get; set; } = [];
|
||||
[Export] public GridContainer ToUnlockGrid { get; set; }
|
||||
@@ -24,6 +24,8 @@ public partial class Marketplace : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
|
||||
var skillsToUnlock = new List<SkillData>();
|
||||
|
||||
foreach (var skill in Skills) skillsToUnlock.Add(skill);
|
||||
|
@@ -3,7 +3,7 @@ using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class PauseMenu : Node
|
||||
public partial class PauseMenu : Control
|
||||
{
|
||||
[Export] public Control PauseMenuControl { get; set; }
|
||||
[Export] public Control SettingsControl { get; set; }
|
||||
|
@@ -3,7 +3,7 @@ using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class SettingsMenu : Node
|
||||
public partial class SettingsMenu : Control
|
||||
{
|
||||
[Export] public Control InputSettingsControl { get; set; }
|
||||
[Export] public Control AudioSettingsControl { get; set; }
|
||||
|
@@ -16,16 +16,13 @@ public partial class DamageComponent : Node
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
if (Area == null)
|
||||
if (Area != null)
|
||||
{
|
||||
GD.PushError($"DamageComponent: Area2D node is not set.");
|
||||
return;
|
||||
Area.BodyEntered += OnAreaBodyEntered;
|
||||
Area.BodyExited += OnAreaBodyExited;
|
||||
Area.AreaEntered += OnAreaAreaEntered;
|
||||
}
|
||||
|
||||
Area.BodyEntered += OnAreaBodyEntered;
|
||||
Area.BodyExited += OnAreaBodyExited;
|
||||
Area.AreaEntered += OnAreaAreaEntered;
|
||||
|
||||
if (DamageTimer != null)
|
||||
{
|
||||
DamageTimer.Timeout += OnDamageTimerTimeout;
|
||||
|
@@ -18,6 +18,8 @@ public partial class ExitDoorComponent : Node, IUnlockable
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
|
||||
if (ExitArea == null)
|
||||
{
|
||||
GD.PushError("ExitDoorComponent: ExitArea is not set.");
|
||||
@@ -26,12 +28,15 @@ public partial class ExitDoorComponent : Node, IUnlockable
|
||||
|
||||
ExitArea.BodyEntered += OnExitAreaBodyEntered;
|
||||
|
||||
_gameManager = GetNode<GameManager>("/root/gameManager");
|
||||
}
|
||||
|
||||
private void OnExitAreaBodyEntered(Node2D body)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
if (Locked) return;
|
||||
|
||||
EmitSignalExitTriggered();
|
||||
_gameManager.UnlockLevel((int)_gameManager.PlayerState["CurrentLevel"] + 1);
|
||||
CallDeferred(nameof(GoToNextLevel));
|
||||
}
|
||||
|
||||
public void Unlock()
|
||||
|
@@ -4,7 +4,7 @@ using Mr.BrickAdventures.scripts.interfaces;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class PlayerController : Node2D
|
||||
public partial class PlayerController : CharacterBody2D
|
||||
{
|
||||
[Export]
|
||||
public string DefaultMovementType { get; set; } = "platform";
|
||||
|
@@ -14,7 +14,7 @@ public partial class PlayerDeathComponent : Node2D
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/gameManager");
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
HealthComponent.Death += OnDeath;
|
||||
}
|
||||
|
||||
|
@@ -20,10 +20,12 @@ public partial class ScoreComponent : Node
|
||||
return;
|
||||
}
|
||||
|
||||
var coins = GetTree().GetNodesInGroup("Coins");
|
||||
var coins = GetTree().GetNodesInGroup("coins");
|
||||
GD.Print($"Found {coins.Count} coins in the scene.");
|
||||
foreach (var coin in coins)
|
||||
{
|
||||
var c = coin.GetNodeOrNull<CollectableComponent>("CollectableComponent");
|
||||
GD.Print(c == null ? "CollectableComponent not found on coin." : "CollectableComponent found on coin.");
|
||||
if (c != null)
|
||||
{
|
||||
c.Collected += OnCollected;
|
||||
@@ -31,6 +33,17 @@ public partial class ScoreComponent : Node
|
||||
}
|
||||
}
|
||||
|
||||
public override void _Process(double delta)
|
||||
{
|
||||
GetCoinsInScene();
|
||||
}
|
||||
|
||||
private void GetCoinsInScene()
|
||||
{
|
||||
var coins = GetTree().GetNodesInGroup("Coins");
|
||||
GD.Print($"Found {coins.Count} coins in the scene.");
|
||||
}
|
||||
|
||||
private void OnCollected(float amount, CollectableType type, Node2D body)
|
||||
{
|
||||
if (type != CollectableType.Coin) return;
|
||||
|
@@ -2,9 +2,8 @@ using Godot;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.components;
|
||||
|
||||
public partial class TooltipComponent : Node
|
||||
public partial class TooltipComponent : Area2D
|
||||
{
|
||||
[Export] public Area2D Area { get; set; }
|
||||
[Export] public Control UiRoot { get; set; }
|
||||
[Export] public string Text { get; set; } = string.Empty;
|
||||
[Export] public Label TooltipLabel { get; set; }
|
||||
@@ -13,8 +12,8 @@ public partial class TooltipComponent : Node
|
||||
{
|
||||
TooltipLabel.Text = Text;
|
||||
UiRoot.Visible = false;
|
||||
Area.BodyEntered += OnBodyEntered;
|
||||
Area.BodyExited += OnBodyExited;
|
||||
BodyEntered += OnBodyEntered;
|
||||
BodyExited += OnBodyExited;
|
||||
}
|
||||
|
||||
private void OnBodyEntered(Node2D body)
|
||||
|
@@ -1,5 +0,0 @@
|
||||
class_name LevelResource
|
||||
extends Resource
|
||||
|
||||
@export var level_name: String
|
||||
@export var scene_path: String
|
@@ -1 +0,0 @@
|
||||
uid://cp68km8bykymb
|
@@ -48,4 +48,4 @@ func set_health_progressbar() -> void:
|
||||
return
|
||||
|
||||
health_progressbar.value = player_health.health
|
||||
health_progressbar.max_value = player_health.max_health
|
||||
health_progressbar.max_value = player_health.max_health
|
||||
|
Reference in New Issue
Block a user