-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Have added curves for plotting scaling values
This can be used for things such as player stat scaling on level ups or other use cases.
- Loading branch information
Showing
13 changed files
with
232 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/OpenRpg.CurveFunctions/Curves/LogisticCurveFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using OpenRpg.CurveFunctions.Extensions; | ||
|
||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class LogisticCurveFunction : ICurveFunction | ||
{ | ||
public float Slope { get; } | ||
public float VerticalSize { get; } | ||
public float YShift { get; } | ||
public float XShift { get; } | ||
|
||
public LogisticCurveFunction(float slope, float xShift, float yShift, float verticalSize) | ||
{ | ||
Slope = slope; | ||
XShift = xShift; | ||
YShift = yShift; | ||
VerticalSize = verticalSize; | ||
} | ||
|
||
public float Plot(float value) | ||
{ | ||
var outputValue = (float)(Slope / (1 + Math.Exp(-10.0 * VerticalSize * (value - 0.5 - XShift))) + YShift); | ||
return this.SanitizeValue(outputValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using OpenRpg.CurveFunctions.Extensions; | ||
|
||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class LogitCurveFunction : ICurveFunction | ||
{ | ||
public float Slope { get; } | ||
public float YShift { get; } | ||
public float XShift { get; } | ||
|
||
public LogitCurveFunction(float slope, float xShift, float yShift) | ||
{ | ||
Slope = slope; | ||
XShift = xShift; | ||
YShift = yShift; | ||
} | ||
|
||
public float Plot(float value) | ||
{ | ||
var outputValue = (float)(Slope * Math.Log((value - XShift) / (1.0 - (value - XShift))) / 5.0 + 0.5 + YShift); | ||
return this.SanitizeValue(outputValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using OpenRpg.CurveFunctions.Extensions; | ||
|
||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class NormalCurveFunction : ICurveFunction | ||
{ | ||
public float Slope { get; } | ||
public float YShift { get; } | ||
public float XShift { get; } | ||
public float Exponent { get; } | ||
|
||
public NormalCurveFunction(float slope, float xShift, float yShift, float exponent) | ||
{ | ||
Slope = slope; | ||
XShift = xShift; | ||
YShift = yShift; | ||
Exponent = exponent; | ||
} | ||
|
||
public float Plot(float value) | ||
{ | ||
var outputValue = (float)(Slope * Math.Exp(-30.0 * Exponent * (value - XShift - 0.5) * (value - XShift - 0.5)) + YShift); | ||
return this.SanitizeValue(outputValue); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/OpenRpg.CurveFunctions/Curves/PassThroughlCurveFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class PassThroughlCurveFunction : ICurveFunction | ||
{ | ||
public float Plot(float value) | ||
{ return value; } | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/OpenRpg.CurveFunctions/Curves/PolynomialCurveFunction.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using OpenRpg.CurveFunctions.Extensions; | ||
|
||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class PolynomialCurveFunction : ICurveFunction | ||
{ | ||
public float Slope { get; } | ||
public float Exponent { get; } | ||
public float YShift { get; } | ||
public float XShift { get; } | ||
|
||
public PolynomialCurveFunction(float slope, float xShift, float yShift, float exponent) | ||
{ | ||
Slope = slope; | ||
XShift = xShift; | ||
YShift = yShift; | ||
Exponent = exponent; | ||
} | ||
|
||
public float Plot(float value) | ||
{ | ||
var outputValue = Slope * (float)Math.Pow((value - XShift), Exponent) + YShift; | ||
return this.SanitizeValue(outputValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System; | ||
using OpenRpg.CurveFunctions.Extensions; | ||
|
||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class SineCurveFunction : ICurveFunction | ||
{ | ||
public float Slope { get; } | ||
public float YShift { get; } | ||
public float XShift { get; } | ||
|
||
public SineCurveFunction(float slope, float xShift, float yShift) | ||
{ | ||
Slope = slope; | ||
XShift = xShift; | ||
YShift = yShift; | ||
} | ||
|
||
public float Plot(float value) | ||
{ | ||
var outputValue = (float)(0.5 * Slope * Math.Sin(2.0 * Math.PI * (value - XShift)) + 0.5 + YShift); | ||
return this.SanitizeValue(outputValue); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace OpenRpg.CurveFunctions.Curves | ||
{ | ||
public class StepCurveFunction : ICurveFunction | ||
{ | ||
public float StepValue { get; } | ||
public float MinValue { get; } | ||
public float MaxValue { get; } | ||
|
||
public StepCurveFunction(float stepValue, float minValue = 0.0f, float maxValue = 1.0f) | ||
{ | ||
StepValue = stepValue; | ||
MinValue = minValue; | ||
MaxValue = maxValue; | ||
} | ||
|
||
public float Plot(float value) | ||
{ return value < StepValue ? MinValue : MaxValue; } | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/OpenRpg.CurveFunctions/Extensions/IEvaluatorExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace OpenRpg.CurveFunctions.Extensions | ||
{ | ||
public static class ICurveFunctionExtensions | ||
{ | ||
public static float SanitizeValue(this ICurveFunction curve, float value) | ||
{ | ||
if(float.IsInfinity(value)) { return 0.0f; } | ||
if(float.IsNaN(value)) { return 0.0f; } | ||
if(value < 0 ) { return 0.0f; } | ||
if(value > 1.0f ) { return 1.0f; } | ||
return value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenRpg.CurveFunctions | ||
{ | ||
public interface ICurveFunction | ||
{ | ||
float Plot(float value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Version>0.0.0</Version> | ||
<TargetFrameworks>netstandard2.0;net46</TargetFrameworks> | ||
<Title>OpenRpg.CurveFunctions</Title> | ||
<Authors>Grofit (LP)</Authors> | ||
<PackageLicenseUrl>https://github.com/openrpg/OpenRpg/blob/master/LICENSE</PackageLicenseUrl> | ||
<PackageProjectUrl>https://github.com/openrpg/OpenRpg</PackageProjectUrl> | ||
<Description>Adds premade curve functions for scaling values or other use cases</Description> | ||
<PackageTags>rpg game-development xna monogame unity godot</PackageTags> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using OpenRpg.CurveFunctions.Curves; | ||
|
||
namespace OpenRpg.CurveFunctions | ||
{ | ||
public class PresetCurves | ||
{ | ||
public static ICurveFunction Constant = new PolynomialCurveFunction(0f, 0, 0.5f, 0); | ||
public static ICurveFunction Linear = new PolynomialCurveFunction(1.0f, 0, 0, 1.0f); | ||
public static ICurveFunction InverseLinear = new PolynomialCurveFunction(-1.0f, 1.0f, 0, 1.0f); | ||
public static ICurveFunction StandardCooldown = new PolynomialCurveFunction(1.0f, 0f, 0, 6.0f); | ||
public static ICurveFunction StandardRuntime = new PolynomialCurveFunction(-1.0f, 0f, 1.0f, 6.0f); | ||
public static ICurveFunction QuadraticLowerLeft = new PolynomialCurveFunction(1.0f, 1.0f, 0.0f, 4.0f); | ||
public static ICurveFunction QuadraticLowerRight = new PolynomialCurveFunction(1.0f, 0.0f, 0.0f, 4.0f); | ||
public static ICurveFunction QuadraticUpperLeft = new PolynomialCurveFunction(-1.0f, 1.0f, 1.0f, 4.0f); | ||
public static ICurveFunction QuadraticUpperRight = new PolynomialCurveFunction(-1.0f, 0f, 1.0f, 4.0f); | ||
public static ICurveFunction Logistic = new LogisticCurveFunction(1.0f, 0f, 0.0f, 1.0f); | ||
public static ICurveFunction InverseLogistic = new LogisticCurveFunction(-1.0f, 0f, 1.0f, 1.0f); | ||
public static ICurveFunction Logit = new LogitCurveFunction(1.0f, 0, 0); | ||
public static ICurveFunction InverseLogit = new LogitCurveFunction(-1.0f, 0, 0); | ||
public static ICurveFunction BellCurve = new NormalCurveFunction(1.0f, 0, 0, 1.0f); | ||
public static ICurveFunction InverseBellCurve = new NormalCurveFunction(-1.0f, 0, 1.0f, 1.0f); | ||
public static ICurveFunction SineWave = new SineCurveFunction(1.0f, 0, 0); | ||
public static ICurveFunction InverseSineWave = new SineCurveFunction(-1.0f, 0, 0); | ||
public static ICurveFunction GreaterThanHalf = new StepCurveFunction(0.5f); | ||
public static ICurveFunction LessThanHalf = new StepCurveFunction(0.5f, 1.0f, 0.0f); | ||
public static ICurveFunction PassThrough = new PassThroughlCurveFunction(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters