Initial commit: Tiny Tackle Heroes Unity project
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
using BumpCombat.Core;
|
||||
using System;
|
||||
using BumpCombat.Constants;
|
||||
using BumpCombat.Player;
|
||||
using UnityEngine;
|
||||
|
||||
namespace BumpCombat.Progression
|
||||
{
|
||||
[RequireComponent(typeof(PlayerStats))]
|
||||
public sealed class ExperienceSystem : MonoBehaviour
|
||||
{
|
||||
private PlayerStats playerStats;
|
||||
private float fractionalExperience;
|
||||
|
||||
public event Action<int, int, int> OnExperienceChanged;
|
||||
public event Action<int> OnLevelGained;
|
||||
public event Action OnExperienceOrbPickedUp;
|
||||
|
||||
public int Level { get; private set; } = 1;
|
||||
public int CurrentExperience { get; private set; }
|
||||
public int RequiredExperience => RequiredExperienceForLevel(Level);
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
playerStats = GetComponent<PlayerStats>();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
OnExperienceChanged?.Invoke(CurrentExperience, RequiredExperience, Level);
|
||||
}
|
||||
|
||||
public void AddExperience(int amount)
|
||||
{
|
||||
AddExperienceInternal(amount);
|
||||
}
|
||||
|
||||
public void AddMissionExperience(int amount)
|
||||
{
|
||||
AddExperienceInternal(amount);
|
||||
}
|
||||
|
||||
public void CollectExperienceOrb(int amount)
|
||||
{
|
||||
AddExperienceInternal(amount);
|
||||
OnExperienceOrbPickedUp?.Invoke();
|
||||
}
|
||||
|
||||
private void AddExperienceInternal(int amount)
|
||||
{
|
||||
fractionalExperience += Mathf.Max(0f, playerStats.Evaluate(
|
||||
CharacterStat.ExperienceGain,
|
||||
Mathf.Max(0, amount)));
|
||||
int wholeExperience = Mathf.FloorToInt(
|
||||
fractionalExperience + 0.0001f);
|
||||
fractionalExperience = Mathf.Max(
|
||||
0f,
|
||||
fractionalExperience - wholeExperience);
|
||||
CurrentExperience += wholeExperience;
|
||||
while (CurrentExperience >= RequiredExperience)
|
||||
{
|
||||
CurrentExperience -= RequiredExperience;
|
||||
Level++;
|
||||
OnLevelGained?.Invoke(Level);
|
||||
}
|
||||
|
||||
OnExperienceChanged?.Invoke(CurrentExperience, RequiredExperience, Level);
|
||||
}
|
||||
|
||||
public static int RequiredExperienceForLevel(int level)
|
||||
{
|
||||
LevelUpConstants tuning = GameplayConstants.Current.LevelUp;
|
||||
return tuning.BaseExperienceToNextLevel
|
||||
+ (Mathf.Max(1, level) - 1)
|
||||
* tuning.AdditionalExperiencePerLevel;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user