Skip to content

Commit

Permalink
Added extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mackysoft committed May 20, 2020
1 parent 81ab472 commit 748405c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
6 changes: 4 additions & 2 deletions Assets/MackySoft/MackySoft.Modiferty/Scripts/IModifier.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
namespace MackySoft.Modiferty {

public interface IModifier<T> {

public interface IModifier {
/// <summary>
/// <para> Priority of evaluation. </para>
/// <para> See: <see cref="ModifierList{T}.Evaluate(T)"/> </para>
/// </summary>
int Priority { get; }
}

public interface IModifier<T> : IModifier {
/// <summary>
/// Evaluate the value.
/// </summary>
/// <param name="value"> Value to be modified. </param>
T Evaluate (T value);

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Generic;

namespace MackySoft.Modiferty {
public static class ModifierListExtensions {

public static bool HasModifier<TModifier> (this IEnumerable<IModifier> source) where TModifier : IModifier {
foreach (IModifier modifier in source) {
if (modifier is TModifier) {
return true;
}
}
return false;
}

public static TModifier GetModifier<TModifier> (this IEnumerable<IModifier> source) where TModifier : IModifier {
foreach (IModifier modifier in source) {
if (modifier is TModifier result) {
return result;
}
}
return default;
}

public static IEnumerable<TModifier> GetModifiers<TModifier> (this IEnumerable<IModifier> source) where TModifier : IModifier {
foreach (IModifier modifier in source) {
if (modifier is TModifier result) {
yield return result;
}
}
}

public static bool TryGetModifier<TModifier> (this IEnumerable<IModifier> source,out TModifier result) where TModifier : IModifier {
result = source.GetModifier<TModifier>();
return result != null;
}

}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
using System;
using UnityEngine;

namespace MackySoft.Modiferty {

/// <summary>
/// Create complex modifier improvise.
/// </summary>
[Serializable]
public class CreateModifier<T> : IModifier<T> {

public Func<T,T> ModifyMethod { get; set; }
public int Priority { get; set; }
[SerializeField]
int m_Priority;

[NonSerialized]
Func<T,T> m_Evaluator;

public Func<T,T> Evaluator {
get => m_Evaluator;
set => m_Evaluator = value ?? throw new NullReferenceException($"You cannot set the {nameof(Evaluator)} to null.");
}

public int Priority { get => m_Priority; set => m_Priority = value; }

public CreateModifier (Func<T,T> modifyMethod) {
ModifyMethod = modifyMethod;
/// <param name="evaluator"> Evaluator can not be null. </param>
public CreateModifier (Func<T,T> evaluator,int priority = 0) {
Evaluator = evaluator ?? throw new ArgumentNullException(nameof(evaluator));
Priority = priority;
}

public T Evaluate (T value) {
return ModifyMethod.Invoke(value);
return Evaluator.Invoke(value);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public ModifiableInt (int baseValue) : base(baseValue) {
#region Operator Modifiers

[Serializable]
public class AdditiveModifier : IModifier<int> {
public class AdditiveModifierInt : IModifier<int> {

[SerializeField]
int m_Amount;
Expand All @@ -24,7 +24,7 @@ public class AdditiveModifier : IModifier<int> {

public int Priority { get => m_Priority; set => m_Priority = value; }

public AdditiveModifier (int amount) {
public AdditiveModifierInt (int amount) {
Amount = amount;
}

Expand All @@ -34,7 +34,7 @@ public int Evaluate (int value) {
}

[Serializable]
public class SubtractiveModifier : IModifier<int> {
public class SubtractiveModifierInt : IModifier<int> {

[SerializeField]
int m_Amount;
Expand All @@ -46,7 +46,7 @@ public class SubtractiveModifier : IModifier<int> {

public int Priority { get => m_Priority; set => m_Priority = value; }

public SubtractiveModifier (int amount) {
public SubtractiveModifierInt (int amount) {
Amount = amount;
}

Expand All @@ -56,7 +56,7 @@ public int Evaluate (int value) {
}

[Serializable]
public class MultiplyModifier : IModifier<int> {
public class MultiplyModifierInt : IModifier<int> {

[SerializeField]
int m_Multiply = 1;
Expand All @@ -68,7 +68,7 @@ public class MultiplyModifier : IModifier<int> {

public int Priority { get => m_Priority; set => m_Priority = value; }

public MultiplyModifier (int multiply) {
public MultiplyModifierInt (int multiply) {
Multiply = multiply;
}

Expand All @@ -79,7 +79,7 @@ public int Evaluate (int value) {
}

[Serializable]
public class DivisionModifier : IModifier<int> {
public class DivisionModifierInt : IModifier<int> {

[SerializeField]
int m_Division = 2;
Expand All @@ -91,7 +91,7 @@ public class DivisionModifier : IModifier<int> {

public int Priority { get => m_Priority; set => m_Priority = value; }

public DivisionModifier (int division) {
public DivisionModifierInt (int division) {
Division = division;
}

Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ public class Character : MonoBehaviour {

public class PowerUpItem : MonoBehaviour {

public AdditiveModifier additiveDamage = new AdditiveModifier(1);
public AdditiveModifierInt additivePower = new AdditiveModifierInt(1);

public void Modify (Character target){
target.attackPower.Modifiers.Add(additiveDamage);
// target.attackPower.Add(additiveDamage);

target.attackPower.Modifiers.Add(additivePower);

// Same as below.
// target.attackPower.Add(additivePower);
}

}
Expand Down Expand Up @@ -97,7 +98,7 @@ A variety of other unique modifiers are also available.
The given value ignored and the specified value returned.

```cs
var setModifier = new SetModifier(0);
var setModifier = new SetModifierInt(0);

character.attackPower.Add(setModifier);

Expand Down

0 comments on commit 748405c

Please sign in to comment.