Files
GameJamDungeon/Zennysoft.Game.Godot.Implementation/Components/ExperiencePointsComponent.cs

55 lines
1.6 KiB
C#

using Chickensoft.Collections;
namespace Zennysoft.Game.Implementation.Components;
public class ExperiencePointsComponent
{
public IAutoProp<int> CurrentExp => _currentExp;
public IAutoProp<int> ExpToNextLevel => _expToNextLevel;
public IAutoProp<double> ExpGainRate => _expGainRate;
public IAutoProp<int> Level => _level;
private readonly AutoProp<int> _currentExp;
private readonly AutoProp<int> _expToNextLevel;
private readonly AutoProp<double> _expGainRate;
private readonly AutoProp<int> _level;
public ExperiencePointsComponent()
{
var firstLevelExpRequirement = ExpToNextLevelCalculation(1);
_expToNextLevel = new AutoProp<int>(firstLevelExpRequirement);
_currentExp = new AutoProp<int>(0);
_expGainRate = new AutoProp<double>(1.0);
_level = new AutoProp<int>(1);
}
public void Gain(int baseExpGain)
{
var modifiedExpGain = baseExpGain * _expGainRate.Value;
var newCurrentExpTotal = modifiedExpGain + _currentExp.Value;
while (modifiedExpGain + _currentExp.Value >= _expToNextLevel.Value)
LevelUp();
var cappedAmount = Math.Min(baseExpGain + _currentExp.Value, _expToNextLevel.Value);
_currentExp.OnNext(cappedAmount);
}
public void LevelUp()
{
_level.OnNext(_level.Value + 1);
var expToNextLevel = ExpToNextLevelCalculation(_level.Value);
_currentExp.OnNext(_currentExp.Value - _expToNextLevel.Value);
_expToNextLevel.OnNext(expToNextLevel);
}
private int ExpToNextLevelCalculation(int nextLevel)
{
return (int)(6.5 * nextLevel + 4.5 * Math.Pow(nextLevel, 2) + Math.Pow(nextLevel, 3));
}
}