Files
Fruitomation/Assets/Scripts/UI/BasicUpgradeButton.cs
2026-04-17 11:49:55 +01:00

110 lines
3.2 KiB
C#

using System.Collections.Generic;
using Fruitomation.Game;
using UnityEngine.UI;
using UnityEngine;
using System.Linq;
using System;
namespace Fruitomation.UI
{
public class BasicUpgradeButton : MonoBehaviour
{
private enum UpgradeState
{
Unlocked,
Unlockable,
Viewable,
Hidden,
}
[Header("Settings")]
[SerializeField] private BasicUpgrade Upgrade;
[SerializeField] private int UpgradeCost;
[Header("References")]
[SerializeField] private BasicUpgradeButton[] RequiredUpgrades;
[SerializeField] private Material LineMaterial;
private (LineRenderer, BasicUpgradeButton)[] UpgradeLines;
private UpgradeState State = UpgradeState.Hidden;
private Button AttachedButton;
private Text AttachedText;
private void Awake()
{
AttachedText = gameObject.GetComponentInChildren<Text>();
AttachedButton = GetComponent<Button>();
AttachedButton.onClick.AddListener(() =>
{
UpgradeManager.Unlock(Upgrade);
});
/* Stops null reference */
RequiredUpgrades ??= Array.Empty<BasicUpgradeButton>();
List<(LineRenderer, BasicUpgradeButton)> lines = new();
foreach (BasicUpgradeButton required in RequiredUpgrades)
{
GameObject go = new("LineRenderer(Script Spawned)");
go.transform.SetParent(transform);
RectTransform rt = go.AddComponent<RectTransform>();
rt.anchoredPosition = new Vector2();
LineRenderer lr = go.AddComponent<LineRenderer>();
lr.material = LineMaterial;
lr.positionCount = 2;
lines.Add((lr, required));
}
UpgradeLines = lines.ToArray();
}
private bool IsUnlockable =>
RequiredUpgrades.Length == 0 ||
RequiredUpgrades.All(required => required.IsUnlocked);
private bool IsViewable =>
RequiredUpgrades.Length == 0 ||
RequiredUpgrades.Any(required => required.IsUnlocked);
private void Update()
{
foreach ((LineRenderer lr, BasicUpgradeButton button) in UpgradeLines)
{
lr.SetPosition(0, transform.position);
lr.SetPosition(1, button.transform.position);
}
if (UpgradeManager.Is(Upgrade))
{
State = UpgradeState.Unlocked;
}
else if (IsUnlockable)
{
State = UpgradeState.Unlockable;
}
else if (IsViewable)
{
State = UpgradeState.Viewable;
}
else
{
State = UpgradeState.Hidden;
}
//
AttachedText.text = State == UpgradeState.Hidden
? "???"
: $"{Upgrade.ToString()} [{UpgradeCost}]";
}
private bool IsUnlocked => State == UpgradeState.Unlocked;
}
}