HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

"Simple" generic inheritance

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
inheritancegenericsimple

Problem

I'm sure that my intentions are highly erroneous, and I really just want to see what you guys would suggest I do instead. I have a class I made for my game which helps store various values regarding resistances, aversions, attribute modifications for buffs and debuffs, and damage mitigation and damage amplification vs creatures.

```
using System;
using System.Collections.Generic;

namespace GrimoireEngine.Framework.Collections
{

public enum ModificationType
{
Increase,
Decrease
}

public class ModificationTable where TEnum : struct, IConvertible, IComparable, IFormattable
{
private static TEnum[] _keys;
private int[] _values;
public const int DefaultModification = MinimumModification;
public const int MinimumModification = 0;
public const int MaximumModification = 100;
private static IEqualityComparer _comparer;

static ModificationTable()
{
_keys = (TEnum[])Enum.GetValues(typeof(TEnum));
_comparer = EqualityComparer.Default;
}

public TEnum[] Keys
{
get
{
return _keys;
}
}

public int this[TEnum enumType]
{
get
{
return _values[GetIndex(enumType)];
}
set
{
_values[GetIndex(enumType)] = value;
}
}

private int GetIndex(TEnum enumType)
{
return _comparer.GetHashCode(enumType);
}

public ModificationTable(int defaultValue = DefaultModification)
{
if (_values == null)
{
_values = new int[this.Keys.Length];
}
if (defaultValue != DefaultModification)
{
SetAll(defaultValue);
}
}

public void SetAll(int value)
{
for (int i = 0; i MaximumModification) ?

Solution

I might be missing something but why do you need your ModificationType at all? Let's look at your maths here:

value * (1.0f + (Get(type) / 100f))
value - ((value * (Get(type) / 100f)))


Let's define value as v and (Get(type) / 100f) as r.

v*(1+r) // Increase
v-(v*r) // Decrease


Rearranging your top formula gives you:

v+(v*r) // Increase


Which means your increase and decrease functions are the same apart from the sign (which is what you'd expect).

I think you can drop the ModificationType and instead have a range of -100 to 100 for the modification value.

Code Snippets

value * (1.0f + (Get(type) / 100f))
value - ((value * (Get(type) / 100f)))
v*(1+r) // Increase
v-(v*r) // Decrease
v+(v*r) // Increase

Context

StackExchange Code Review Q#152199, answer score: 6

Revisions (0)

No revisions yet.