Files
source/Assets/Scripts/PlayMenu.cs

196 lines
8.6 KiB
C#

using System;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class PlayMenu : MonoBehaviour
{
[SerializeField] private GameObject selectionMenu;
[SerializeField] private GameObject customMenu;
[SerializeField] private Button customButton;
[SerializeField] private Button customBackButton;
[SerializeField] private Button customPlayButton;
[SerializeField] private Button customNormalizeButton;
[SerializeField] private TMP_Text validateTotalText;
private const float defaultNormalBerryChance = 47.5f;
private const float defaultPoisonBerryChance = 12.5f;
private const float defaultSlowBerryChance = 10f;
private const float defaultUltraBerryChance = 10f;
private const float defaultSpeedyBerryChance = 10f;
private const float defaultRandomBerryChance = 5f;
private const float defaultAntiBerryChance = 5f;
private const float defaultNothingBerryChance = 0f;
private float normalBerryChance = defaultNormalBerryChance;
private float poisonBerryChance = defaultPoisonBerryChance;
private float slowBerryChance = defaultSlowBerryChance;
private float ultraBerryChance = defaultUltraBerryChance;
private float speedyBerryChance = defaultSpeedyBerryChance;
private float randomBerryChance = defaultRandomBerryChance;
private float antiBerryChance = defaultAntiBerryChance;
private float nothingBerryChance = defaultNothingBerryChance;
[SerializeField] private TMP_Text modifyTip;
void Awake()
{
modifyTip.text = Application.isMobilePlatform ? "Tap a berry to modify it's spawn chances!" : "Click a berry to modify it's spawn chances!";
customButton.onClick.AddListener(() =>
{
selectionMenu.SetActive(false);
customMenu.SetActive(true);
});
customBackButton.onClick.AddListener(() =>
{
customMenu.SetActive(false);
selectionMenu.SetActive(true);
normalBerryChance = defaultNormalBerryChance;
poisonBerryChance = defaultPoisonBerryChance;
slowBerryChance = defaultSlowBerryChance;
ultraBerryChance = defaultUltraBerryChance;
speedyBerryChance = defaultSpeedyBerryChance;
randomBerryChance = defaultRandomBerryChance;
antiBerryChance = defaultAntiBerryChance;
nothingBerryChance = defaultNothingBerryChance;
ValidateTotal();
});
customNormalizeButton.onClick.AddListener(() =>
{
int divideBy = 0;
divideBy += normalBerryChance > 0 ? 1 : 0;
divideBy += poisonBerryChance > 0 ? 1 : 0;
divideBy += slowBerryChance > 0 ? 1 : 0;
divideBy += ultraBerryChance > 0 ? 1 : 0;
divideBy += speedyBerryChance > 0 ? 1 : 0;
divideBy += randomBerryChance > 0 ? 1 : 0;
divideBy += antiBerryChance > 0 ? 1 : 0;
divideBy += nothingBerryChance > 0 ? 1 : 0;
float addedChances = normalBerryChance + poisonBerryChance + slowBerryChance + ultraBerryChance + speedyBerryChance + randomBerryChance + antiBerryChance + nothingBerryChance;
float difference = addedChances - 100f;
if (normalBerryChance > 0) normalBerryChance -= difference / divideBy;
if (poisonBerryChance > 0) poisonBerryChance -= difference / divideBy;
if (slowBerryChance > 0) slowBerryChance -= difference / divideBy;
if (ultraBerryChance > 0) ultraBerryChance -= difference / divideBy;
if (speedyBerryChance > 0) speedyBerryChance -= difference / divideBy;
if (randomBerryChance > 0) randomBerryChance -= difference / divideBy;
if (antiBerryChance > 0) antiBerryChance -= difference / divideBy;
if (nothingBerryChance > 0) nothingBerryChance -= difference / divideBy;
if (normalBerryChance > 0) normalBerryChance = (float)Math.Floor(normalBerryChance);
if (poisonBerryChance > 0) poisonBerryChance = (float)Math.Floor(poisonBerryChance);
if (slowBerryChance > 0) slowBerryChance = (float)Math.Floor(slowBerryChance);
if (ultraBerryChance > 0) ultraBerryChance = (float)Math.Floor(ultraBerryChance);
if (speedyBerryChance > 0) speedyBerryChance = (float)Math.Floor(speedyBerryChance);
if (randomBerryChance > 0) randomBerryChance = (float)Math.Floor(randomBerryChance);
if (antiBerryChance > 0) antiBerryChance = (float)Math.Floor(antiBerryChance);
if (nothingBerryChance > 0) nothingBerryChance = (float)Math.Floor(nothingBerryChance);
float addedChances2 = normalBerryChance + poisonBerryChance + slowBerryChance + ultraBerryChance + speedyBerryChance + randomBerryChance + antiBerryChance + nothingBerryChance;
float difference2 = addedChances2 - 100f;
bool fixedValues = false;
if (normalBerryChance > 0)
{
normalBerryChance = !fixedValues ? (normalBerryChance - difference2) : normalBerryChance;
fixedValues = true;
}
if (poisonBerryChance > 0)
{
poisonBerryChance = !fixedValues ? (poisonBerryChance - difference2) : poisonBerryChance;
fixedValues = true;
}
if (slowBerryChance > 0)
{
slowBerryChance = !fixedValues ? (slowBerryChance - difference2) : slowBerryChance;
fixedValues = true;
}
if (ultraBerryChance > 0)
{
ultraBerryChance = !fixedValues ? (ultraBerryChance - difference2) : ultraBerryChance;
fixedValues = true;
}
if (speedyBerryChance > 0)
{
speedyBerryChance = !fixedValues ? (speedyBerryChance - difference2) : speedyBerryChance;
fixedValues = true;
}
if (randomBerryChance > 0)
{
randomBerryChance = !fixedValues ? (randomBerryChance - difference2) : randomBerryChance;
fixedValues = true;
}
if (antiBerryChance > 0)
{
antiBerryChance = !fixedValues ? (antiBerryChance - difference2) : antiBerryChance;
fixedValues = true;
}
if (nothingBerryChance > 0)
{
nothingBerryChance = !fixedValues ? (nothingBerryChance - difference2) : nothingBerryChance;
fixedValues = true;
}
ValidateTotal();
});
customPlayButton.onClick.AddListener(async () =>
{
GameObject obj = new("CustomGameTempData");
obj.AddComponent<CustomGameTempData>();
CustomGameTempData customGameTempData = obj.GetComponent<CustomGameTempData>();
customGameTempData.normalBerryChance = normalBerryChance;
customGameTempData.poisonBerryChance = poisonBerryChance;
customGameTempData.slowBerryChance = slowBerryChance;
customGameTempData.ultraBerryChance = ultraBerryChance;
customGameTempData.speedyBerryChance = speedyBerryChance;
customGameTempData.randomBerryChance = randomBerryChance;
customGameTempData.antiBerryChance = antiBerryChance;
customGameTempData.nothingBerryChance = nothingBerryChance;
await SceneManager.LoadSceneAsync("CustomGamePlayer");
});
}
void ValidateTotal()
{
customBackButton.interactable = false;
customPlayButton.interactable = false;
customNormalizeButton.interactable = false;
float total = 0f;
total += normalBerryChance;
total += poisonBerryChance;
total += slowBerryChance;
total += ultraBerryChance;
total += speedyBerryChance;
total += randomBerryChance;
total += antiBerryChance;
total += nothingBerryChance;
if (total == 100f)
{
customBackButton.interactable = true;
customPlayButton.interactable = true;
validateTotalText.gameObject.SetActive(false);
}
else
{
customNormalizeButton.interactable = true;
validateTotalText.text = "Total must add up to 100%!";
validateTotalText.gameObject.SetActive(true);
}
}
async void Update()
{
if (Keyboard.current.escapeKey.wasPressedThisFrame)
{
if (customMenu.activeSelf) customBackButton.onClick.Invoke();
else await SceneManager.LoadSceneAsync("MainMenu");
}
}
}