161 lines
5.1 KiB
C#
161 lines
5.1 KiB
C#
using System.Collections.Generic;
|
|
using Fruitomation.Game;
|
|
using UnityEngine.UI;
|
|
using UnityEngine;
|
|
using System.Linq;
|
|
using System;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Fruitomation.UI
|
|
{
|
|
public class BasicUpgradeButton : MonoBehaviour
|
|
{
|
|
[Serializable] private class LineInfo
|
|
{
|
|
public RectTransform[] LinePoints;
|
|
}
|
|
|
|
private enum UpgradeState
|
|
{
|
|
Unlocked,
|
|
Unlockable,
|
|
Viewable,
|
|
Hidden,
|
|
}
|
|
|
|
[Header("Settings")]
|
|
[SerializeField] private BasicUpgrade Upgrade;
|
|
[SerializeField] private bool DrawDefaultLines;
|
|
[SerializeField] private CurrencyAmount Cost;
|
|
[SerializeField] private bool BigText;
|
|
|
|
[Header("References")]
|
|
[SerializeField] private BasicUpgradeButton[] RequiredUpgrades;
|
|
[SerializeField] private Material LineMaterial;
|
|
|
|
[Header("Lines")]
|
|
[SerializeField] private LineInfo[] Lines;
|
|
|
|
private (LineRenderer, BasicUpgradeButton, LineInfo)[] UpgradeLines;
|
|
|
|
private UpgradeState State = UpgradeState.Hidden;
|
|
private Button AttachedButton;
|
|
private Text AttachedText;
|
|
|
|
private void Awake()
|
|
{
|
|
const int TEXT_SCALAR = 5;
|
|
|
|
AttachedText = gameObject.GetComponentInChildren<Text>();
|
|
AttachedText.transform.localScale = new Vector3(1f / TEXT_SCALAR, 1f / TEXT_SCALAR, 1f);
|
|
AttachedText.fontSize = BigText ? 50 * TEXT_SCALAR : 35 * TEXT_SCALAR;
|
|
|
|
AttachedButton = GetComponent<Button>();
|
|
|
|
AttachedButton.onClick.AddListener(() => { UpgradeManager.Unlock(Upgrade); });
|
|
|
|
/* Stops null reference */
|
|
RequiredUpgrades ??= Array.Empty<BasicUpgradeButton>();
|
|
|
|
List<(LineRenderer, BasicUpgradeButton, LineInfo)> lines = new();
|
|
if (DrawDefaultLines)
|
|
{
|
|
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.widthMultiplier = 0.1f;
|
|
lr.positionCount = 2;
|
|
|
|
lines.Add((lr, required, null));
|
|
}
|
|
}
|
|
|
|
else
|
|
{
|
|
foreach (LineInfo line in Lines)
|
|
{
|
|
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.widthMultiplier = 0.1f;
|
|
|
|
lines.Add((lr, null, line));
|
|
}
|
|
}
|
|
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, LineInfo info) in UpgradeLines)
|
|
{
|
|
if (DrawDefaultLines)
|
|
{
|
|
lr.SetPosition(0, transform.position);
|
|
lr.SetPosition(1, button.transform.position);
|
|
}
|
|
|
|
else
|
|
{
|
|
lr.positionCount = info.LinePoints.Length;
|
|
int index = 0;
|
|
|
|
foreach (RectTransform point in info.LinePoints)
|
|
{
|
|
lr.SetPosition(index++, point.position);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (UpgradeManager.Is(Upgrade))
|
|
{
|
|
State = UpgradeState.Unlocked;
|
|
}
|
|
|
|
else if (IsUnlockable)
|
|
{
|
|
State = UpgradeState.Unlockable;
|
|
}
|
|
|
|
else if (IsViewable)
|
|
{
|
|
State = UpgradeState.Viewable;
|
|
}
|
|
|
|
else
|
|
{
|
|
State = UpgradeState.Hidden;
|
|
}
|
|
|
|
// https://stackoverflow.com/questions/27040325/c-sharp-regex-to-convert-camelcase-to-sentence-case
|
|
string formatted = Regex.Replace(Upgrade.ToString(), @"[A-Z]", " $0")[1..];
|
|
|
|
AttachedText.text = State == UpgradeState.Hidden
|
|
? "???"
|
|
: $"{formatted}\n{Cost.AsString()}";
|
|
}
|
|
|
|
private bool IsUnlocked => State == UpgradeState.Unlocked;
|
|
}
|
|
}
|