Skip to content

Instantly share code, notes, and snippets.

@Kryzarel
Kryzarel / EasingFunctions.cs
Last active November 8, 2024 17:23
C# Easing Functions
using System;
namespace Kryz.Tweening
{
// Made with the help of this great post: https://joshondesign.com/2013/03/01/improvedEasingEquations
// --------------------------------- Other Related Links --------------------------------------------------------------------
// Original equations, bad formulation: https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
// A few equations, very simplified: https://gist.github.com/gre/1650294
// Easings.net equations, simplified: https://github.com/ai/easings.net/blob/master/src/easings/easingsFunctions.ts
namespace Kryz.CharacterStats
{
[Serializable]
public class CharacterStat
{
//...
}
}
@Kryzarel
Kryzarel / CharacterStat.cs
Created December 5, 2017 20:12
Added parameterless constructor
public CharacterStat()
{
statModifiers = new List<StatModifier>();
StatModifiers = statModifiers.AsReadOnly();
}
public CharacterStat(float baseValue) : this()
{
BaseValue = baseValue;
}
[Serializable]
public class CharacterStat
@Kryzarel
Kryzarel / CharacterStat_Extendable.cs
Created December 5, 2017 20:08
Changed all private variables, properties and methods to protected. Changed all methods methods to virtual. Showing only lines that were changed.
protected bool isDirty = true;
protected float lastBaseValue;
protected float _value;
public virtual float Value {}
protected readonly List<StatModifier> statModifiers;
public virtual void AddModifier(StatModifier mod);
public virtual bool RemoveModifier(StatModifier mod);
public virtual bool RemoveAllModifiersFromSource(object source);
@Kryzarel
Kryzarel / StatModType.cs
Created December 3, 2017 21:33
Changed default "indexes" of all values
public enum StatModType
{
Flat = 100,
PercentAdd = 200,
PercentMult = 300,
}
// Add this variable
private float lastBaseValue = float.MinValue;
// Change Value
public float Value {
get {
if(isDirty || lastBaseValue != BaseValue) {
lastBaseValue = BaseValue;
_value = CalculateFinalValue();
isDirty = false;
public readonly ReadOnlyCollection<StatModifier> StatModifiers; // Add this variable
public CharacterStat(float baseValue)
{
BaseValue = baseValue;
statModifiers = new List<StatModifier>();
StatModifiers = statModifiers.AsReadOnly(); // Add this line to the constructor
}
@Kryzarel
Kryzarel / RemoveModifier.cs
Created December 3, 2017 21:23
Updated to set isDirty only when Remove returns true
public bool RemoveModifier(StatModifier mod)
{
if (statModifiers.Remove(mod))
{
isDirty = true;
return true;
}
return false;
}
@Kryzarel
Kryzarel / Item.cs
Created December 3, 2017 21:21
Updated with RemoveAllModifiersFromSource()
public class Item
{
public void Equip(Character c)
{
// Create the modifiers and set the Source to "this"
// Note that we don't need to store the modifiers in variables anymore
c.Strength.AddModifier(new StatModifier(10, StatModType.Flat, this));
c.Strength.AddModifier(new StatModifier(0.1, StatModType.Percent, this));
}