Last active
May 7, 2020 06:35
-
-
Save h1ddengames/e831cbfd01e4f0baadb6de3e500c5902 to your computer and use it in GitHub Desktop.
Gross Misuse of C# Generics
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
// Created by h1ddengames | |
using System; | |
using System.Collections.Generic; | |
namespace h1ddengames { | |
[Serializable] | |
public class ImprovedEntityStats { | |
public Stat<int> hp; | |
public Stat<int> mp; | |
public Stat<float> strength; | |
public Stat<float> intelligence; | |
public Stat<float> luck; | |
public Stat<float> dexterity; | |
} | |
public class Stat<T> : IStat<T> { | |
public T value; | |
public IStat<T> IncrementStat(T amount) { | |
GenericMath<T>.Add(value, amount); | |
return this; | |
} | |
public IStat<T> DecrementStat(T amount) { | |
GenericMath<T>.Subtract(value, amount); | |
return this; | |
} | |
public T GetStat() { | |
return value; | |
} | |
public IStat<T> SetStat(T value) { | |
this.value = value; | |
return this; | |
} | |
} | |
public interface IStat<T> { | |
IStat<T> IncrementStat(T amount); | |
IStat<T> DecrementStat(T amount); | |
T GetStat(); | |
IStat<T> SetStat(T value); | |
} | |
public class GenericMath<T> { | |
public static T Add(T value1, T value2) { | |
dynamic a = value1; | |
dynamic b = value2; | |
return a + b; | |
} | |
public static T Add(params T[] values) { | |
dynamic result = values[0]; | |
for(int i = 1; i < values.Length; i++) { | |
result += values[i]; | |
} | |
return result; | |
} | |
public static T Subtract(T value1, T value2) { | |
dynamic a = value1; | |
dynamic b = value2; | |
return a - b; | |
} | |
public static T Subtract(params T[] values) { | |
dynamic result = values[0]; | |
for(int i = 1; i < values.Length; i++) { | |
result -= values[i]; | |
} | |
return result; | |
} | |
public static T Multiply(T value1, T value2) { | |
dynamic a = value1; | |
dynamic b = value2; | |
return a * b; | |
} | |
public static T Multiply(params T[] values) { | |
dynamic result = values[0]; | |
for(int i = 1; i < values.Length; i++) { | |
result *= values[i]; | |
} | |
return result; | |
} | |
public static T Divide(T value1, T value2) { | |
dynamic a = value1; | |
dynamic b = value2; | |
return a / b; | |
} | |
public static T Divide(params T[] values) { | |
dynamic result = values[0]; | |
for(int i = 1; i < values.Length; i++) { | |
dynamic n = 0; | |
if(values[i] != n) { | |
result /= values[i]; | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment