![]() I had actually read the Flash-oriented article you mentioned, but the code translation was a bother and I gave up on it.Įdit: I've been working to extend this so that it can represent other kinds of stats, like Resources (health, mana) which have a MaxValue. You clearly put a lot of effort into it, and it's really educational. Any time more than one object is removed, doing it from last to first always results in less shifts.Īnd now we can add and remove our (hypothetical) item's modifiers like so: Even if the objects that we need to remove are in the middle of the list (where shifts are inevitable), it's still a good idea to traverse the list in reverse (unless your specific use case demands otherwise). This is why we do the removal in reverse. We just remove the object at index 9 and everything else stays the same. However, if we remove the last object, then nothing has to be shifted. As you can imagine, this is quite inefficient. That happens because index 0 is now empty, so the object at index 1 will move to index 0, the object at index 2 will move to index 1, and so on. Let's say we have a list with 10 objects, when we remove the first one, the remaining 9 objects will be shifted up. To explain this let's look at what happens when we remove the first object from a list vs when we remove the last object: Let's add a third type of modifier by changing our StatModType enum to this: Because the first will double our original value (going from 100% to 200%), and the second will double it again (going from 200% to 400%).īut what if we would like to have certain modifiers stack additively? Meaning that the previous example would result in a 200% bonus instead of 400%. Right now our percentage modifiers stack multiplicatively with each other, i.e., if we add two 100% modifiers to a stat, we won't get 200%, we'll get 400%. Feel free to take a break and/or pat yourself on the back for reaching this point In our case, the comparison function is CompareModifierOrder().Ĭhapter 1 ends here, we already have a pretty good basis for a stat system. The function returns 1.ģ) Both objects are equal in "priority". The function returns -1.Ģ) The first object should come after the second. For each pair of objects there's 3 possible situations:ġ) The first object ( a) should come before the second object ( b). The comparison function will be used by the Sort() method to compare pairs of objects in the list. If we don't supply a comparison function, it uses the default comparer (whatever that does). The criteria it uses to sort the list should be supplied by us, in the form of a comparison function. ![]() Sort() is a C# method for all lists that, as the name implies, sorts the list. This way we don't need to change the CalculateFinalValue() method because everything will already be in the correct order. The easiest way to do this is to sort the statModifiers list whenever we add a new modifier. Now we need a way to apply modifiers according to their order when calculating the final stat value. So by default we get the most common behavior, but we can also do other things, like forcing a special modifier to apply a flat value after everything else. With these changes, we can set the order for each modifier, but if we don't, flat modifiers will apply before percentage modifiers. In order to retrieve the index of an enum element, we just cast it to int. If you hover your mouse over an enum element, you can see the index of that element in the tooltip (at least in Visual Studio). You can assign a custom index if you want, but we don't need to do that.yet. By default, the first element is 0, the second is 1, etc. ![]() ![]() In C#, every enum element is automatically assigned an index. In this case we defined a constructor that needs only the value and the type, it then calls the constructor that also needs the order, but passes the int representation of type as the default order. In C#, to call a constructor from another constructor, you essentially "extend" the constructor you want to call.
0 Comments
Leave a Reply. |