121 lines
2.5 KiB
C#
121 lines
2.5 KiB
C#
|
|
using System;
|
||
|
|
using UnityEngine;
|
||
|
|
using UnityEngine.UI;
|
||
|
|
|
||
|
|
public class LevelSystem : MonoBehaviour
|
||
|
|
{
|
||
|
|
[Header("Progression")]
|
||
|
|
public int level = 1;
|
||
|
|
public int currentExp = 0;
|
||
|
|
public int expToNextLevel = 100;
|
||
|
|
|
||
|
|
[Header("Dependencies")]
|
||
|
|
public TalentManager talentManager;
|
||
|
|
public NotificationUI notificationUI;
|
||
|
|
|
||
|
|
[Header("HUD")]
|
||
|
|
public Text levelText;
|
||
|
|
public Text expText;
|
||
|
|
public Image expFillImage;
|
||
|
|
|
||
|
|
public event Action OnExperienceChanged;
|
||
|
|
public event Action OnLevelChanged;
|
||
|
|
|
||
|
|
private void Awake()
|
||
|
|
{
|
||
|
|
if (talentManager == null)
|
||
|
|
{
|
||
|
|
talentManager = FindObjectOfType<TalentManager>();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (notificationUI == null)
|
||
|
|
{
|
||
|
|
notificationUI = FindObjectOfType<NotificationUI>();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Start()
|
||
|
|
{
|
||
|
|
RefreshUI();
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Update()
|
||
|
|
{
|
||
|
|
if (Input.GetKeyDown(KeyCode.X))
|
||
|
|
{
|
||
|
|
AddExperience(50);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void AddExperience(int amount)
|
||
|
|
{
|
||
|
|
if (amount <= 0)
|
||
|
|
{
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
currentExp += amount;
|
||
|
|
Notify($"Получено опыта: {amount}");
|
||
|
|
|
||
|
|
while (currentExp >= expToNextLevel)
|
||
|
|
{
|
||
|
|
currentExp -= expToNextLevel;
|
||
|
|
LevelUp();
|
||
|
|
}
|
||
|
|
|
||
|
|
RefreshUI();
|
||
|
|
OnExperienceChanged?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
public void LevelUp()
|
||
|
|
{
|
||
|
|
level = Mathf.Max(1, level + 1);
|
||
|
|
expToNextLevel = Mathf.Max(1, Mathf.RoundToInt(expToNextLevel * 1.25f + 25f));
|
||
|
|
|
||
|
|
if (talentManager != null)
|
||
|
|
{
|
||
|
|
talentManager.AddTalentPoints(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
Notify($"Уровень {level}! Получено очко талантов");
|
||
|
|
RefreshUI();
|
||
|
|
OnLevelChanged?.Invoke();
|
||
|
|
}
|
||
|
|
|
||
|
|
public float GetExpNormalized()
|
||
|
|
{
|
||
|
|
if (expToNextLevel <= 0)
|
||
|
|
{
|
||
|
|
return 0f;
|
||
|
|
}
|
||
|
|
|
||
|
|
return Mathf.Clamp01((float)currentExp / expToNextLevel);
|
||
|
|
}
|
||
|
|
|
||
|
|
private void RefreshUI()
|
||
|
|
{
|
||
|
|
if (levelText != null)
|
||
|
|
{
|
||
|
|
levelText.text = $"Уровень {level}";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (expText != null)
|
||
|
|
{
|
||
|
|
expText.text = $"{currentExp} / {expToNextLevel} XP";
|
||
|
|
}
|
||
|
|
|
||
|
|
if (expFillImage != null)
|
||
|
|
{
|
||
|
|
expFillImage.fillAmount = GetExpNormalized();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private void Notify(string message)
|
||
|
|
{
|
||
|
|
if (notificationUI != null)
|
||
|
|
{
|
||
|
|
notificationUI.Show(message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|