Add MainMenu and Marketplace components; implement game management and skill unlocking features
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.Autoloads;
|
||||
@@ -7,6 +10,10 @@ namespace Mr.BrickAdventures.Autoloads;
|
||||
public partial class GameManager : Node
|
||||
{
|
||||
[Export] public Array<PackedScene> LevelScenes { get; set; } = new();
|
||||
|
||||
public PlayerController Player { get; set; }
|
||||
|
||||
private List<Node> _sceneNodes = new();
|
||||
|
||||
public Dictionary PlayerState { get; set; } = new()
|
||||
{
|
||||
@@ -24,6 +31,29 @@ public partial class GameManager : Node
|
||||
{ "skills_unlocked", new Array<SkillData>() }
|
||||
};
|
||||
|
||||
public override void _EnterTree()
|
||||
{
|
||||
GetTree().NodeAdded += OnNodeAdded;
|
||||
GetTree().NodeRemoved += OnNodeRemoved;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
GetTree().NodeAdded -= OnNodeAdded;
|
||||
GetTree().NodeRemoved -= OnNodeRemoved;
|
||||
_sceneNodes.Clear();
|
||||
}
|
||||
|
||||
private void OnNodeAdded(Node node)
|
||||
{
|
||||
_sceneNodes.Add(node);
|
||||
}
|
||||
|
||||
private void OnNodeRemoved(Node node)
|
||||
{
|
||||
_sceneNodes.Remove(node);
|
||||
}
|
||||
|
||||
public void AddCoins(int amount)
|
||||
{
|
||||
PlayerState["coins"] = Mathf.Max(0, (int)PlayerState["coins"] + amount);
|
||||
@@ -192,4 +222,20 @@ public partial class GameManager : Node
|
||||
joined.AddRange((Array)session ?? new Array());
|
||||
return joined;
|
||||
}
|
||||
|
||||
public PlayerController GetPlayer()
|
||||
{
|
||||
if (Player != null) return Player;
|
||||
|
||||
foreach (var node in _sceneNodes)
|
||||
{
|
||||
if (node is not PlayerController player) continue;
|
||||
|
||||
Player = player;
|
||||
return Player;
|
||||
}
|
||||
|
||||
GD.PrintErr("PlayerController not found in the scene tree.");
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -16,7 +16,7 @@ public partial class GameOverScreen : Node
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
RestartButton.Pressed += OnRestartClicked;
|
||||
MainMenuButton.Pressed += OnMainMenuClicked;
|
||||
MainMenuButton.Pressed += OnMainMenuClicked;
|
||||
}
|
||||
|
||||
private void OnMainMenuClicked()
|
||||
|
1
scripts/UI/Hud.cs.uid
Normal file
1
scripts/UI/Hud.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://wfj674u4486f
|
67
scripts/UI/MainMenu.cs
Normal file
67
scripts/UI/MainMenu.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class MainMenu : Node
|
||||
{
|
||||
[Export] public Control MainMenuControl { get; set; }
|
||||
[Export] public Button NewGameButton { get; set; }
|
||||
[Export] public Button ContinueButton { get; set; }
|
||||
[Export] public Button SettingsButton { get; set; }
|
||||
[Export] public Button CreditsButton { get; set; }
|
||||
[Export] public Button ExitButton { get; set; }
|
||||
[Export] public Label VersionLabel { get; set; }
|
||||
[Export] public Control SettingsControl { get; set; }
|
||||
[Export] public Control CreditsControl { get; set; }
|
||||
|
||||
private SaveSystem _saveSystem;
|
||||
private GameManager _gameManager;
|
||||
private UIManager _uiManager;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_saveSystem = GetNode<SaveSystem>("/root/SaveSystem");
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
_uiManager = GetNode<UIManager>("/root/UIManager");
|
||||
|
||||
NewGameButton.Pressed += OnNewGamePressed;
|
||||
ContinueButton.Pressed += OnContinuePressed;
|
||||
SettingsButton.Pressed += OnSettingsPressed;
|
||||
CreditsButton.Pressed += OnCreditsPressed;
|
||||
ExitButton.Pressed += OnExitPressed;
|
||||
|
||||
VersionLabel.Text = $"v. {ProjectSettings.GetSetting("application/config/version")}";
|
||||
ContinueButton.Disabled = !_saveSystem.CheckSaveExists();
|
||||
|
||||
if (_saveSystem.CheckSaveExists())
|
||||
ContinueButton.GrabFocus();
|
||||
else
|
||||
NewGameButton.GrabFocus();
|
||||
}
|
||||
|
||||
private void OnExitPressed()
|
||||
{
|
||||
_gameManager.QuitGame();
|
||||
}
|
||||
|
||||
private void OnCreditsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(CreditsControl);
|
||||
}
|
||||
|
||||
private void OnSettingsPressed()
|
||||
{
|
||||
_uiManager.PushScreen(SettingsControl);
|
||||
}
|
||||
|
||||
private void OnContinuePressed()
|
||||
{
|
||||
_gameManager.ContinueGame();
|
||||
}
|
||||
|
||||
private void OnNewGamePressed()
|
||||
{
|
||||
_gameManager.StartNewGame();
|
||||
}
|
||||
}
|
1
scripts/UI/MainMenu.cs.uid
Normal file
1
scripts/UI/MainMenu.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bna3ggr6n7ycr
|
113
scripts/UI/Marketplace.cs
Normal file
113
scripts/UI/Marketplace.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class Marketplace : Node
|
||||
{
|
||||
[Export] public Array<SkillData> Skills { get; set; } = [];
|
||||
[Export] public GridContainer ToUnlockGrid { get; set; }
|
||||
[Export] public GridContainer UnlockedGrid { get; set; }
|
||||
[Export] public Font Font { get; set; }
|
||||
[Export] public SkillUnlockedComponent SkillUnlockedComponent { get; set; }
|
||||
[Export] public Array<Node> ComponentsToDisable { get; set; } = [];
|
||||
[Export] public PackedScene MarketplaceButtonScene { get; set; }
|
||||
[Export] public PackedScene SkillButtonScene { get; set; }
|
||||
|
||||
private GameManager _gameManager;
|
||||
private List<Button> _unlockButtons = [];
|
||||
private List<Button> _skillButtons = [];
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
var skillsToUnlock = new List<SkillData>();
|
||||
|
||||
foreach (var skill in Skills) skillsToUnlock.Add(skill);
|
||||
|
||||
foreach (var skill in skillsToUnlock) CreateUpgradeButton(skill);
|
||||
|
||||
var unlockedSkills = _gameManager.GetUnlockedSkills();
|
||||
foreach (var skill in unlockedSkills) CreateSkillButton(skill);
|
||||
|
||||
SkillUnlockedComponent.SkillUnlocked += OnSkillUnlocked;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
SkillUnlockedComponent.SkillUnlocked -= OnSkillUnlocked;
|
||||
}
|
||||
|
||||
public override void _Input(InputEvent @event)
|
||||
{
|
||||
var root = Owner as Control;
|
||||
|
||||
if (!@event.IsActionPressed("show_marketplace")) return;
|
||||
|
||||
if (root != null && root.IsVisible())
|
||||
{
|
||||
root.Hide();
|
||||
foreach (var c in ComponentsToDisable) c.ProcessMode = ProcessModeEnum.Inherit;
|
||||
}
|
||||
else
|
||||
{
|
||||
root?.Show();
|
||||
foreach (var c in ComponentsToDisable) c.ProcessMode = ProcessModeEnum.Disabled;
|
||||
}
|
||||
}
|
||||
|
||||
private string GetButtonText(SkillData skill)
|
||||
{
|
||||
return $"{Tr(skill.Name)} {skill.Cost}";
|
||||
}
|
||||
|
||||
private void OnSkillUnlocked(SkillData skill)
|
||||
{
|
||||
if (_skillButtons.Count == 0) CreateSkillButton(skill);
|
||||
|
||||
foreach (var btn in _skillButtons)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void CreateSkillButton(Variant skill)
|
||||
{
|
||||
throw new System.NotImplementedException();
|
||||
}
|
||||
|
||||
private void CreateUpgradeButton(SkillData skill)
|
||||
{
|
||||
var button = MarketplaceButtonScene.Instantiate<MarketplaceButton>();
|
||||
button.Text = GetButtonText(skill);
|
||||
button.Data = skill;
|
||||
button.Icon = skill.Icon;
|
||||
button.Pressed += () => OnUpgradeButtonPressed(skill);
|
||||
|
||||
_skillButtons.Add(button);
|
||||
UnlockedGrid.AddChild(button);
|
||||
UnlockedGrid.QueueSort();
|
||||
}
|
||||
|
||||
private void OnUpgradeButtonPressed(SkillData skill) {}
|
||||
|
||||
private void RemoveButton(SkillData skill)
|
||||
{
|
||||
foreach (var node in ToUnlockGrid.GetChildren())
|
||||
{
|
||||
var child = (Button)node;
|
||||
if (child.Text != GetButtonText(skill)) continue;
|
||||
|
||||
child.QueueFree();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSkillButtonPressed(SkillData skill)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
1
scripts/UI/Marketplace.cs.uid
Normal file
1
scripts/UI/Marketplace.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://bnc16gndpl87i
|
62
scripts/UI/MarketplaceButton.cs
Normal file
62
scripts/UI/MarketplaceButton.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using Godot;
|
||||
using Mr.BrickAdventures.Autoloads;
|
||||
using Mr.BrickAdventures.scripts.components;
|
||||
using Mr.BrickAdventures.scripts.Resources;
|
||||
|
||||
namespace Mr.BrickAdventures.scripts.UI;
|
||||
|
||||
public partial class MarketplaceButton : Button
|
||||
{
|
||||
[Export] public SkillData Data { get; set; }
|
||||
[Export] public Texture2D UnlockedSkillIcon { get; set; }
|
||||
[Export] public Texture2D LockedSkillIcon { get; set; }
|
||||
[Export] public Container SkillLevelContainer { get; set; }
|
||||
|
||||
private GameManager _gameManager;
|
||||
private SkillUnlockedComponent _skillUnlockedComponent;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
_gameManager = GetNode<GameManager>("/root/GameManager");
|
||||
|
||||
Setup();
|
||||
var player = _gameManager.Player;
|
||||
|
||||
var skillUnlockerComponent = player?.GetNodeOrNull<SkillUnlockedComponent>("SkillUnlockerComponent");
|
||||
if (skillUnlockerComponent == null) return;
|
||||
|
||||
skillUnlockerComponent.SkillUnlocked += OnSkillUnlock;
|
||||
}
|
||||
|
||||
public override void _ExitTree()
|
||||
{
|
||||
_skillUnlockedComponent.SkillUnlocked -= OnSkillUnlock;
|
||||
}
|
||||
|
||||
private void Setup()
|
||||
{
|
||||
if (Data == null) return;
|
||||
|
||||
for (var i = 0; i < Data.MaxLevel; i++)
|
||||
{
|
||||
var icon = new TextureRect()
|
||||
{
|
||||
Texture = i < Data.Level ? UnlockedSkillIcon : LockedSkillIcon,
|
||||
};
|
||||
SkillLevelContainer.AddChild(icon);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnSkillUnlock(SkillData skill)
|
||||
{
|
||||
if (skill.Name != Data.Name) return;
|
||||
|
||||
for (var i = 0; i < Data.MaxLevel; i++)
|
||||
{
|
||||
var icon = SkillLevelContainer.GetChildOrNull<TextureRect>(i);
|
||||
if (icon == null) continue;
|
||||
icon.Texture = i < Data.Level ? UnlockedSkillIcon : LockedSkillIcon;
|
||||
Disabled = i >= Data.Level;
|
||||
}
|
||||
}
|
||||
}
|
1
scripts/UI/MarketplaceButton.cs.uid
Normal file
1
scripts/UI/MarketplaceButton.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://vokgv56bjpf1
|
Reference in New Issue
Block a user