I have to make a function that returns the spells a character has.
So I have 3 parameters: a level (an int from 1-50), an enum representing a character class (only 8 classes) and an enum representing a race (3-4 races).
This function has to return an array with spell IDs. The higher the level, the more spells a character has.
What I did so far is hard-coded everything, but when I have to modify something it is a mess. I don't know what kind of data-structure best suits my needs without recurring to horrible if
s that are hard to change/mantain.
Also, the language is C# and I am using Xna/.NET 4.0
public static int[] ListOfSpells(int level, CharacterClass chClass, CharacterRace chRace)
{
switch (chClass)
{
case CharacterClass.Mage:
return new int[] { 1, 2, 3 };
case CharacterClass.Knight:
return new int[] { 2, 5, 6 };
case CharacterClass.Paladin:
return new int[] { 3, 5, 6, 2 };
default:
return new int[] { };
}
}
// classes
public enum CharacterClass : short
{
Mage = 0x00,
Warlock,
Priest,
Monk,
Knight,
Assassin,
Paladin,
Hunter,
Warrior
}
// races
public enum CharacterRace : short
{
Human = 0x00,
Elf
}
I don't know what kind of data-structure best suits my needs without recurring to horrible ifs that are hard to change/mantain.
A simple trivial array, alternativels a table in a database.
Fields:
Then filter by matching condition.
And I suggest NOT returning an array - that is useless. make the method IEnumerable, then you CAN ToArray() it if needed, or just run along it.
A database if definitly what you need. You can maybe make it works with IEnumerable
/ Array
now, but database will gave you the flexibility that you want for the future of your game.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.