Improve PlayMenu.cs

This commit is contained in:
2025-10-06 18:19:00 -07:00
parent d38e98c4b3
commit e0568cbdde

View File

@@ -1,4 +1,3 @@
using System;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using UnityEngine.SceneManagement; using UnityEngine.SceneManagement;
@@ -6,22 +5,31 @@ using UnityEngine.UI;
public class PlayMenu : MonoBehaviour public class PlayMenu : MonoBehaviour
{ {
public GameObject selectionMenu; [SerializeField] private GameObject selectionMenu;
public GameObject customMenu; [SerializeField] private GameObject customMenu;
public Button customButton; [SerializeField] private Button customButton;
public Button customBackButton; [SerializeField] private Button customBackButton;
public Button customPlayButton; [SerializeField] private Button customPlayButton;
public TMP_InputField normalBerryChance; [SerializeField] private TMP_InputField normalBerryChance;
public TMP_InputField poisonBerryChance; [SerializeField] private TMP_InputField poisonBerryChance;
public TMP_InputField slowBerryChance; [SerializeField] private TMP_InputField slowBerryChance;
public TMP_InputField ultraBerryChance; [SerializeField] private TMP_InputField ultraBerryChance;
public TMP_InputField speedyBerryChance; [SerializeField] private TMP_InputField speedyBerryChance;
public TMP_InputField randomBerryChance; [SerializeField] private TMP_InputField randomBerryChance;
public TMP_InputField antiBerryChance; [SerializeField] private TMP_InputField antiBerryChance;
public TMP_InputField nothingBerryChance; [SerializeField] private TMP_InputField nothingBerryChance;
public TMP_Text validateTotalText; [SerializeField] private TMP_Text validateTotalText;
private readonly float defaultNormalBerryChance = 47.5f;
private readonly float defaultPoisonBerryChance = 12.5f;
private readonly float defaultSlowBerryChance = 10f;
private readonly float defaultUltraBerryChance = 10f;
private readonly float defaultSpeedyBerryChance = 10f;
private readonly float defaultRandomBerryChance = 5f;
private readonly float defaultAntiBerryChance = 5f;
private readonly float defaultNothingBerryChance = 0f;
void Awake() void Awake()
{ {
@@ -35,14 +43,14 @@ public class PlayMenu : MonoBehaviour
customMenu.SetActive(false); customMenu.SetActive(false);
selectionMenu.SetActive(true); selectionMenu.SetActive(true);
normalBerryChance.text = "47.5%"; normalBerryChance.text = defaultNormalBerryChance.ToString();
poisonBerryChance.text = "12.5%"; poisonBerryChance.text = defaultPoisonBerryChance.ToString();
slowBerryChance.text = "10%"; slowBerryChance.text = defaultSlowBerryChance.ToString();
ultraBerryChance.text = "10%"; ultraBerryChance.text = defaultUltraBerryChance.ToString();
speedyBerryChance.text = "10%"; speedyBerryChance.text = defaultSpeedyBerryChance.ToString();
randomBerryChance.text = "5%"; randomBerryChance.text = defaultRandomBerryChance.ToString();
antiBerryChance.text = "5%"; antiBerryChance.text = defaultAntiBerryChance.ToString();
nothingBerryChance.text = "0%"; nothingBerryChance.text = defaultNothingBerryChance.ToString();
ValidateTotal(); ValidateTotal();
}); });
customPlayButton.onClick.AddListener(async () => customPlayButton.onClick.AddListener(async () =>
@@ -50,183 +58,47 @@ public class PlayMenu : MonoBehaviour
GameObject obj = new("CustomGameTempData"); GameObject obj = new("CustomGameTempData");
obj.AddComponent<CustomGameTempData>(); obj.AddComponent<CustomGameTempData>();
CustomGameTempData customGameTempData = obj.GetComponent<CustomGameTempData>(); CustomGameTempData customGameTempData = obj.GetComponent<CustomGameTempData>();
customGameTempData.normalBerryChance = float.Parse(normalBerryChance.text.Replace("%", "").Trim()); customGameTempData.normalBerryChance = GetValueFrom(normalBerryChance);
customGameTempData.poisonBerryChance = float.Parse(poisonBerryChance.text.Replace("%", "").Trim()); customGameTempData.poisonBerryChance = GetValueFrom(poisonBerryChance);
customGameTempData.slowBerryChance = float.Parse(slowBerryChance.text.Replace("%", "").Trim()); customGameTempData.slowBerryChance = GetValueFrom(slowBerryChance);
customGameTempData.ultraBerryChance = float.Parse(ultraBerryChance.text.Replace("%", "").Trim()); customGameTempData.ultraBerryChance = GetValueFrom(ultraBerryChance);
customGameTempData.speedyBerryChance = float.Parse(speedyBerryChance.text.Replace("%", "").Trim()); customGameTempData.speedyBerryChance = GetValueFrom(speedyBerryChance);
customGameTempData.randomBerryChance = float.Parse(randomBerryChance.text.Replace("%", "").Trim()); customGameTempData.randomBerryChance = GetValueFrom(randomBerryChance);
customGameTempData.antiBerryChance = float.Parse(antiBerryChance.text.Replace("%", "").Trim()); customGameTempData.antiBerryChance = GetValueFrom(antiBerryChance);
customGameTempData.nothingBerryChance = float.Parse(nothingBerryChance.text.Replace("%", "").Trim()); customGameTempData.nothingBerryChance = GetValueFrom(nothingBerryChance);
await SceneManager.LoadSceneAsync("CustomGamePlayer"); await SceneManager.LoadSceneAsync("CustomGamePlayer");
}); });
normalBerryChance.onSelect.AddListener((value) => normalBerryChance.onSelect.AddListener((value) => OnSelect(value, normalBerryChance));
{ normalBerryChance.onDeselect.AddListener((value) => OnDeselect(value, normalBerryChance));
validateTotalText.gameObject.SetActive(false); poisonBerryChance.onSelect.AddListener((value) => OnSelect(value, poisonBerryChance));
customBackButton.interactable = false; poisonBerryChance.onDeselect.AddListener((value) => OnDeselect(value, poisonBerryChance));
customPlayButton.interactable = false; slowBerryChance.onSelect.AddListener((value) => OnSelect(value, slowBerryChance));
normalBerryChance.text = value.Replace("%", ""); slowBerryChance.onDeselect.AddListener((value) => OnDeselect(value, slowBerryChance));
}); ultraBerryChance.onSelect.AddListener((value) => OnSelect(value, ultraBerryChance));
normalBerryChance.onDeselect.AddListener((value) => ultraBerryChance.onDeselect.AddListener((value) => OnDeselect(value, ultraBerryChance));
{ speedyBerryChance.onSelect.AddListener((value) => OnSelect(value, speedyBerryChance));
if (float.TryParse(value, out var value2) && value2 < 0f) speedyBerryChance.onDeselect.AddListener((value) => OnDeselect(value, speedyBerryChance));
{ randomBerryChance.onSelect.AddListener((value) => OnSelect(value, randomBerryChance));
value = "0"; randomBerryChance.onDeselect.AddListener((value) => OnDeselect(value, randomBerryChance));
} antiBerryChance.onSelect.AddListener((value) => OnSelect(value, antiBerryChance));
normalBerryChance.text = value + "%"; antiBerryChance.onDeselect.AddListener((value) => OnDeselect(value, antiBerryChance));
customBackButton.interactable = true; nothingBerryChance.onSelect.AddListener((value) => OnSelect(value, nothingBerryChance));
ValidateTotal(); nothingBerryChance.onDeselect.AddListener((value) => OnDeselect(value, nothingBerryChance));
});
poisonBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
poisonBerryChance.text = value.Replace("%", "");
poisonBerryChance.stringPosition = poisonBerryChance.text.Length;
});
poisonBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
poisonBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
slowBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
slowBerryChance.text = value.Replace("%", "");
slowBerryChance.stringPosition = slowBerryChance.text.Length;
});
slowBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
slowBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
ultraBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
ultraBerryChance.text = value.Replace("%", "");
ultraBerryChance.stringPosition = ultraBerryChance.text.Length;
});
ultraBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
ultraBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
speedyBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
speedyBerryChance.text = value.Replace("%", "");
speedyBerryChance.stringPosition = speedyBerryChance.text.Length;
});
speedyBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
speedyBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
randomBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
randomBerryChance.text = value.Replace("%", "");
randomBerryChance.stringPosition = randomBerryChance.text.Length;
});
randomBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
randomBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
antiBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
antiBerryChance.text = value.Replace("%", "");
antiBerryChance.stringPosition = antiBerryChance.text.Length;
});
antiBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
antiBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
nothingBerryChance.onSelect.AddListener((value) =>
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
nothingBerryChance.text = value.Replace("%", "");
nothingBerryChance.stringPosition = nothingBerryChance.text.Length;
});
nothingBerryChance.onDeselect.AddListener((value) =>
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
nothingBerryChance.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
});
} }
void ValidateTotal() void ValidateTotal()
{ {
customPlayButton.interactable = false; customPlayButton.interactable = false;
float total = 0f; float total = 0f;
try total += GetValueFrom(normalBerryChance);
{ total += GetValueFrom(poisonBerryChance);
total += float.Parse(normalBerryChance.text.Replace("%", "")); total += GetValueFrom(slowBerryChance);
total += float.Parse(poisonBerryChance.text.Replace("%", "")); total += GetValueFrom(ultraBerryChance);
total += float.Parse(slowBerryChance.text.Replace("%", "")); total += GetValueFrom(speedyBerryChance);
total += float.Parse(ultraBerryChance.text.Replace("%", "")); total += GetValueFrom(randomBerryChance);
total += float.Parse(speedyBerryChance.text.Replace("%", "")); total += GetValueFrom(antiBerryChance);
total += float.Parse(randomBerryChance.text.Replace("%", "")); total += GetValueFrom(nothingBerryChance);
total += float.Parse(antiBerryChance.text.Replace("%", ""));
total += float.Parse(nothingBerryChance.text.Replace("%", ""));
}
catch (Exception)
{
validateTotalText.text = "Failed to parse total";
validateTotalText.gameObject.SetActive(true);
return;
}
if (total == 100f) if (total == 100f)
{ {
customPlayButton.interactable = true; customPlayButton.interactable = true;
@@ -238,4 +110,41 @@ public class PlayMenu : MonoBehaviour
validateTotalText.gameObject.SetActive(true); validateTotalText.gameObject.SetActive(true);
} }
} }
private float GetValueFrom(TMP_InputField inputField)
{
return GetValueFroText(inputField.text);
}
private float GetValueFroText(string text)
{
try
{
return float.Parse(text.Replace("%", "").Trim());
}
catch
{
return 0f;
}
}
void OnSelect(string value, TMP_InputField inputField)
{
validateTotalText.gameObject.SetActive(false);
customBackButton.interactable = false;
customPlayButton.interactable = false;
inputField.text = value.Replace("%", "");
inputField.stringPosition = inputField.text.Length;
}
void OnDeselect(string value, TMP_InputField inputField)
{
if (float.TryParse(value, out var value2) && value2 < 0f)
{
value = "0";
}
inputField.text = value + "%";
customBackButton.interactable = true;
ValidateTotal();
}
} }