RiftMayhem/Assets/Scripts/UI/ExperienceBarUI.cs

71 lines
2.4 KiB
C#

using Kryz.CharacterStats.Examples;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ExperienceBarUI : MonoBehaviour
{
[Header("Components:")]
[SerializeField] private TMP_Text level;
[SerializeField] private TMP_Text nextLevel;
[SerializeField] private TMP_Text experienceBar;
[SerializeField] private Image experienceBarFill;
[Header("Listeners:")]
[SerializeField] private GameEventListener_RiftPlayer onPlayerSpawned;
public bool isRepBar = false;
PlayerCharacterStats player;
private void Awake()
{
onPlayerSpawned.Response.AddListener(DependancyInjection);
}
private void DependancyInjection(RiftPlayer spawnedPlayer)
{
player = spawnedPlayer.GetComponent<PlayerCharacterStats>();
if (isRepBar)
{
player.reputationLevel.OnLevelUpEvent.AddListener(UpdateLevelText);
player.reputationLevel.OnExperienceChanged.AddListener(UpdateLevelText);
}
else
{
player.level.OnLevelUpEvent.AddListener(UpdateLevelText);
player.level.OnExperienceChanged.AddListener(UpdateLevelText);
}
UpdateLevelText();
}
public void UpdateLevelText()
{
if (level != null)
level.text = isRepBar ? player.reputationLevel.currentLevel.ToString() : player.level.currentLevel.ToString();
if (nextLevel != null)
nextLevel.text = isRepBar ? (player.reputationLevel.currentLevel + 1).ToString() : (player.level.currentLevel + 1).ToString();
if (experienceBar != null)
experienceBar.text = isRepBar ? $"{player.reputationLevel.GetCurrentExperience().ToString("F0")} / {player.reputationLevel.FinalExperienceThreshold.ToString("F0")}" : $"{player.level.GetCurrentExperience().ToString("F0")} / {player.level.FinalExperienceThreshold.ToString("F0")}";
SetCurrentFill();
}
private void SetCurrentFill()
{
if (experienceBarFill != null)
experienceBarFill.fillAmount = isRepBar ? player.reputationLevel.GetCurrentExperience() / player.reputationLevel.FinalExperienceThreshold : player.level.GetCurrentExperience() / player.level.FinalExperienceThreshold;
//sizeFill.x = 0;
//sizeFill.y = 0;
//sizeFill.z = startingWidth - (currentFill * startingWidth);
//sizeFill.w = 0;
//
//mask.padding = sizeFill;
}
}