feature-attributes #7

Merged
zephyr merged 10 commits from feature-attributes into main 2023-07-30 10:58:19 -05:00
2 changed files with 24 additions and 31 deletions
Showing only changes of commit 37436793ce - Show all commits

View File

@ -2,35 +2,26 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct Attribute
public class Attribute : ScriptableObject
zephyr marked this conversation as resolved Outdated

Missing SO suffix

Missing SO suffix

I think this should go back to a struct rather than an SO. But it also seems like that makes it so the editor can't see them. Might need to implement a custom editor?

I think this should go back to a struct rather than an SO. But it also seems like that makes it so the editor can't see them. Might need to implement a custom editor?
{
[SerializeField]
private long _base;
public long Base { get; set; }
public long BaseBonus { get; set; }
public long PermyriadBonus { get; set; }
public long FlatBonus { get; set; }
public Attribute(long stat)
{
_base = stat;
}
/// <summary>
/// Permanently increase the stat value.
/// </summary>
/// <param name="by">Increase amount.</param>
public void LevelUp(long by)
{
_base += by;
}
public long Value => Calc(Base, BaseBonus, PermyriadBonus, FlatBonus);
/// <summary>
/// Calculates the stat value with accumulated buff and debuff modifiers.
/// </summary>
/// <param name="baseValue">Base value.</param>
/// <param name="baseBonus">Additive bonus applied before multiplicative scaling.</param>
/// <param name="permyriadBonus">Multiplicative scaling in hundredths of a percent. E.g., 625 permyriad corresponds to 6.25%.</param>
/// <param name="flatBonus">Additive bonus applied after multiplicative scaling.</param>
/// <returns>(base + baseBonus) * (100% + permyriadBonus) + flatBonus, bounded below by 0.</returns>
public long Calc(long baseBonus, long permyriadBonus, long flatBonus)
public static long Calc(long baseValue, long baseBonus, long permyriadBonus, long flatBonus)
{
var r = _base + baseBonus;
var r = baseValue + baseBonus;
var m = permyriadBonus + 10000;
r = r * (m / 10000) + (r / 10000) * (m % 10000) + flatBonus;
if (r <= 0)

View File

@ -7,19 +7,21 @@ using UnityEngine.TestTools;
public class AttributeTest
{
[Test]
[TestCase(1000000, 0, 0, 0, 0, ExpectedResult = 1000000)]
[TestCase(1000000, 1, 0, 0, 0, ExpectedResult = 1000001)]
[TestCase(1000000, 0, 2, 0, 0, ExpectedResult = 1000002)]
[TestCase(1000000, 0, 0, 10000, 0, ExpectedResult = 2000000)]
[TestCase(1000000, 0, 0, 0, 3, ExpectedResult = 1000003)]
[TestCase(0, 1000000, 1000000, 5000, 1000, ExpectedResult = 3001000)]
[TestCase(1000000, 0, 0, -10000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, 0, -20000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, 0, -20000, 50, ExpectedResult = 0)]
public long Calc(long startStat, long levelUp, long baseBonus, long permyriadBonus, long flatBonus)
[TestCase(1000000, 0, 0, 0, ExpectedResult = 1000000)]
[TestCase(1000000, 1, 0, 0, ExpectedResult = 1000001)]
[TestCase(1000000, 0, 2, 0, ExpectedResult = 1000200)]
[TestCase(1000000, 0, 0, 10000, ExpectedResult = 1010000)]
[TestCase(0, 1000000, 5000, 1000000, ExpectedResult = 2500000)]
[TestCase(1000000, 0, -10000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, -20000, 0, ExpectedResult = 0)]
[TestCase(1000000, 0, -20000, 0, ExpectedResult = 0)]
public long Value(long startStat, long baseBonus, long permyriadBonus, long flatBonus)
{
var attr = new Attribute(startStat);
attr.LevelUp(levelUp);
return attr.Calc(baseBonus, permyriadBonus, flatBonus);
var attr = ScriptableObject.CreateInstance<Attribute>();
attr.Base = startStat;
attr.BaseBonus = baseBonus;
attr.PermyriadBonus = permyriadBonus;
attr.FlatBonus = flatBonus;
return attr.Value;
}
}