first commit
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using OTGIntegrated.Core;
|
||||
using OTGIntegrated.Talents;
|
||||
using UnityEngine;
|
||||
|
||||
namespace OTGIntegrated.Stats
|
||||
{
|
||||
public sealed class LevelSystem : MonoBehaviour
|
||||
{
|
||||
[Header("Progression")]
|
||||
public int level = 1;
|
||||
public int currentExp = 0;
|
||||
public int expToNextLevel = 100;
|
||||
|
||||
private TalentManager talentManager;
|
||||
|
||||
public event Action OnExperienceChanged;
|
||||
public event Action OnLevelChanged;
|
||||
|
||||
public void Initialize(TalentManager talentManager)
|
||||
{
|
||||
this.talentManager = talentManager;
|
||||
level = Mathf.Max(1, level);
|
||||
currentExp = Mathf.Max(0, currentExp);
|
||||
expToNextLevel = Mathf.Max(1, expToNextLevel);
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(KeyCode.X))
|
||||
{
|
||||
AddExperience(50);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddExperience(int amount)
|
||||
{
|
||||
if (amount <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
currentExp += amount;
|
||||
GameEvents.RaiseExperienceGained(amount);
|
||||
GameEvents.RaiseNotification($"Получено опыта: {amount}");
|
||||
|
||||
while (currentExp >= expToNextLevel)
|
||||
{
|
||||
currentExp -= expToNextLevel;
|
||||
LevelUp();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
GameEvents.RaiseLevelUp(level);
|
||||
GameEvents.RaiseNotification($"Уровень повышен: {level}");
|
||||
OnLevelChanged?.Invoke();
|
||||
}
|
||||
|
||||
public float GetNormalizedExp()
|
||||
{
|
||||
return expToNextLevel <= 0 ? 0f : Mathf.Clamp01((float)currentExp / expToNextLevel);
|
||||
}
|
||||
|
||||
public void SetProgress(int savedLevel, int savedExp, int savedExpToNext)
|
||||
{
|
||||
level = Mathf.Max(1, savedLevel);
|
||||
currentExp = Mathf.Max(0, savedExp);
|
||||
expToNextLevel = Mathf.Max(1, savedExpToNext);
|
||||
OnExperienceChanged?.Invoke();
|
||||
OnLevelChanged?.Invoke();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user