RiftMayhem/Assets/Scripts/UI/ExperienceBarUI.cs

80 lines
2.6 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;
float startingWidth;
float currentFill;
RectTransform rectFill;
RectMask2D mask;
Vector4 sizeFill = new Vector4();
private void Awake()
{
onPlayerSpawned.Response.AddListener(DependancyInjection);
rectFill = experienceBarFill.GetComponent<RectTransform>();
mask = experienceBarFill.GetComponent<RectMask2D>();
startingWidth = rectFill.rect.width;
}
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()
{
level.text = isRepBar ? player.reputationLevel.currentLevel.ToString() : player.level.currentLevel.ToString();
nextLevel.text = isRepBar ? (player.reputationLevel.currentLevel + 1).ToString() : (player.level.currentLevel + 1).ToString();
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()
{
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;
}
}