Refactor stats
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using Chickensoft.Collections;
|
||||
using System;
|
||||
using Zennysoft.Ma.Adapter;
|
||||
|
||||
namespace Zennysoft.Game.Ma;
|
||||
|
||||
public class ExperiencePointsComponent : IExperiencePointsComponent
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user