Aimbot-ParallelEnv/Assets/TargetLevelProbabilityPanel.cs

223 lines
9.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
public class TargetLevelProbabilityPanel : MonoBehaviour
{
public GameObject singleLevelProbabilityPanel;
public GameObject targetTitleText;
private GameObject titleText;
private List<GameObject> singleLevelPanelsObjs = new List<GameObject>();
private List<SingleLevelProbabilityPanel> singleLevelPanels = new List<SingleLevelProbabilityPanel>();
private int panelNum = 0;
public void IntializePanels(int levelNum, string titleName)
{
// initialize target level probability panel size
float defaultWidth = singleLevelProbabilityPanel.GetComponent<RectTransform>().sizeDelta.x;
float defaultLevelHeight = singleLevelProbabilityPanel.GetComponent<RectTransform>().sizeDelta.y;
float titleHeight = targetTitleText.GetComponent<RectTransform>().sizeDelta.y;
float averageProbability = 1f / levelNum;
float lastLevelProbability = 1f - averageProbability * (levelNum - 1);
Debug.Log("averageProbability: " + averageProbability);
transform.GetComponent<RectTransform>().sizeDelta = new Vector2(defaultWidth, (defaultLevelHeight * levelNum) + titleHeight);
// create title text
titleText = Instantiate(targetTitleText, transform);
titleText.GetComponent<TextMeshProUGUI>().text = titleName;
// create and initialize single level probability panels
for (int i = 0; i < levelNum; i++)
{
int tempIndex = i;
singleLevelPanelsObjs.Add(Instantiate(singleLevelProbabilityPanel, transform));
singleLevelPanels.Add(singleLevelPanelsObjs[i].GetComponent<SingleLevelProbabilityPanel>());
singleLevelPanels[i].InitializeLevelProbabilityPanel(i, i == levelNum - 1 ? lastLevelProbability : averageProbability);
//add onValueChanged event to slider and input field
singleLevelPanels[i].probabilitySlider.onValueChanged.AddListener((value) => OnSliderValueChange(value, tempIndex));
}
panelNum = levelNum;
}
/// <summary>
/// Adds an event trigger entry to an event trigger.
/// </summary>
/// <param name="trigger">The event trigger object.</param>
/// <param name="type">The event trigger type.</param>
/// <param name="action">The event handler method to execute.</param>
private void AddEventTrigger(GameObject gameObject, EventTriggerType triggerType, System.Action<BaseEventData> action)
{
EventTrigger eventTrigger = gameObject.GetComponent<EventTrigger>();
if (eventTrigger == null)
{
eventTrigger = gameObject.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = triggerType;
entry.callback.AddListener(new UnityEngine.Events.UnityAction<BaseEventData>(action));
eventTrigger.triggers.Add(entry);
}
/// <summary>
/// On Probability Slider Value Change.Adjust other sliders' value to make sure the total value is 1.
/// </summary>
/// <param name="value">change to this value</param>
/// <param name="exceptedIndex">changed panel index</param>
private void OnSliderValueChange(float value, int exceptedIndex)
{
float newTotalValue = 0;
int unlockedPanelNum = 0;
int remainCorrectionNum = panelNum;
float extraValue = 0;
float[] correctionValues = new float[panelNum];
// calculate total probability value
for (int i = 0; i < panelNum; i++)
{
// disable slider listener
singleLevelPanels[i].probabilitySlider.onValueChanged.RemoveAllListeners();
newTotalValue += (i == exceptedIndex ? value : singleLevelPanels[i].ProbabilityValue);
if (singleLevelPanels[i].UnLocked)
{
unlockedPanelNum++;
}
}
// only have one panel
if (panelNum == 1)
{
singleLevelPanels[exceptedIndex].SetProbability(1);
enableSliderListener();
return;
}
//only one panel is unlocked
if (unlockedPanelNum == 1)
{
// limit this panel value under 1 - (newTotalValue - value)
singleLevelPanels[exceptedIndex].SetProbability(newTotalValue - 1 > 0 ? 1 - newTotalValue + value : value);
enableSliderListener();
return;
}
// Calculate the average correction value
extraValue = newTotalValue - 1;
// initialize correction value to each panel
correctionValues = Enumerable.Repeat(extraValue / (unlockedPanelNum - 1), panelNum).ToArray();
// make sure all probability value is not less than 0 and equal to 1
int iterationCount = 0;
while (remainCorrectionNum > 0)
{
iterationCount++;
(correctionValues, remainCorrectionNum) = reCalculateCorrectionValues(correctionValues, exceptedIndex, value, extraValue);
// protect the infinite loop
if (iterationCount >= 100)
{
Debug.LogError("Infinite loop detected!");
break;
}
}
// applicate the value to all unlocked panels excepted the changed one
applyCorrectionValue(correctionValues);
enableSliderListener();
}
/// <summary>
/// enable all sliders' onValueChanged action listener
/// </summary>
private void enableSliderListener()
{
for (int i = 0; i < panelNum; i++)
{
int tempIndex = i;
singleLevelPanels[i].probabilitySlider.onValueChanged.AddListener((value) => OnSliderValueChange(value, tempIndex));
}
}
/// <summary>
/// calculate the correction value to each panel,while the total value is not equal to 1
/// </summary>
private (float[], int) reCalculateCorrectionValues(float[] correctionValues, int exceptedIndex, float value, float extraValue)
{
int remainCorrectionNum = 0;
List<int> reCorrectionIndex = new List<int>();
int lastReCorrectionIndex = 0;
float underZeroExtraValueAfterCorrected = 0;
for (int i = 0; i < panelNum; ++i)
{
// if the panel is the changed one
if (i == exceptedIndex)
{
correctionValues[i] = singleLevelPanels[i].ProbabilityValue - value;
continue;
}
// if the panel is locked
if (singleLevelPanels[i].IsLocked)
{
correctionValues[i] = 0;
continue;
}
//if the probability value is 0 or the probability value is equal to the correction value
if (singleLevelPanels[i].ProbabilityValue < 0 || singleLevelPanels[i].ProbabilityValue > 1)
{
correctionValues[i] = 0;
}
else if (singleLevelPanels[i].ProbabilityValue == correctionValues[i])
{
underZeroExtraValueAfterCorrected += correctionValues[i];
}
else if (singleLevelPanels[i].ProbabilityValue - correctionValues[i] < 0 || singleLevelPanels[i].ProbabilityValue - correctionValues[i] > 1)
{
underZeroExtraValueAfterCorrected += correctionValues[i];
correctionValues[i] = singleLevelPanels[i].ProbabilityValue;
remainCorrectionNum++;
}
else
{
lastReCorrectionIndex = i;
reCorrectionIndex.Add(i);
}
}
foreach (int index in reCorrectionIndex)
{
float newAverageCorrectionValue = (extraValue - underZeroExtraValueAfterCorrected) / reCorrectionIndex.Count;
if (index == lastReCorrectionIndex)
{
correctionValues[index] = extraValue - (newAverageCorrectionValue * (reCorrectionIndex.Count - 1));
if (singleLevelPanels[index].ProbabilityValue - correctionValues[index] < 0)
{
correctionValues[index] = singleLevelPanels[index].ProbabilityValue;
remainCorrectionNum++;
}
}
else
{
correctionValues[index] = newAverageCorrectionValue;
if (singleLevelPanels[index].ProbabilityValue - correctionValues[index] < 0)
{
correctionValues[index] = singleLevelPanels[index].ProbabilityValue;
remainCorrectionNum++;
}
}
}
return (correctionValues, remainCorrectionNum);
}
/// <summary>
/// applicate correction value to each panel
/// </summary>
private void applyCorrectionValue(float[] correctionValues)
{
for (int i = 0; i < panelNum; i++)
{
if (singleLevelPanels[i].ProbabilityValue - correctionValues[i] < 0)
{
Debug.LogWarning("Probability value is less than 0");
Debug.Log(i);
Debug.Log(singleLevelPanels[i].ProbabilityValue);
Debug.Log(correctionValues[i]);
}
singleLevelPanels[i].SetProbability(singleLevelPanels[i].ProbabilityValue - correctionValues[i]);
}
}
}