Hello! I'm working on an action side-scroller project and right now I'm trying to code the attack system. My questions are more like "what's the best way of doing this" than "how to do this". To give a little bit of context of what I'm trying to achieve:
- Player character can cast a spell by pressing the attack button;
- The equipped spell can be switched;
- Different spells have different properties (e.g. MP cost, element...)
I've created an array JSON file to store the spells and its properties and it's looking something like this (all values are placeholders):
name |
MP cost |
power |
element |
type |
Fireball |
5 |
2 |
fire |
basic |
Stalagmite |
30 |
10 |
earth |
attack |
Ice Spike |
20 |
12 |
water |
attack |
These values will always stay the same. There will be modifiers added to it (e.g. an equipment that raises fire damage), but those will be applied after getting the value from the array.
Every time the player attacks, I need to know the MP cost, power and element of the currently equipped spell to calculate damage, reduce player's max MP, etc. I'm thinking that searching an array every time the player attacks might cause an unnecessary amount of data processing. Is this a valid concern?
A possible solution that I thought was saving the current spell properties to player's instance variables or global variables. Every time the current spell is switched these values would be updated. Then, when the player attacks I can get these values directly from the variables instead of searching an array. Replicating these values don't sound like a good practice to me though, looking from a "clean code" perspective.
I guess my main questions about this are:
- Is searching an array during every attack an issue, considering it will happen often?
- Is array JSON files a good way of storing the spells list? Any other best option?
Another related question that I have is what's a good way of saving the player character's current properties, e.g. HP, MP, current equipments, learned spells, etc. Thought about creating a Dictionary specific for this, what do you think?
Thanks in advance for any help provided!