SpellAPI is a Unity API that handle all the logic for the creation of spells in games. Spells are stateful objects that have it own lifecycle, providing events called by different commands the user send to it. It can be used generally for all the spells inside MOBA or RPG game styles, but it can be also used for anything that have cooldown.
Creating a new spell for your game is quite simple! First, let's instantiate a new Spell, passing the name, the spell type and the user (must be a SpellUser), and put it into a variable:
Spell fireball = new Spell("Fireball", Spell.SpellType.ACTIVE, user1);
The object above represents a runtime spell of a single user. When instantiating a spell, it will be attached automatically to the given user being able to be accessed through it.
We can set a cooldown for the spell - this is the reload time that avoid spamming:
fireball.Cooldown = 5;
Spells have it own state that vary according to the type passed in the constructor. In this example, we're using an ACTIVE spell that have the CASTING state. To define what the spell do when it is activated, first we define a function with a spell argument and with no return:
void CastFireball(Spell spell)
{
// Casting a fireball!
}
Now, let's connect the function in the ENTER_CASTING event:
fireball.events.Add(Spell.SpellEventName.ENTER_CASTING, CastFireball);
You can storage spells in a list to manage them - it depends on your own design:
List<Spell> runtimeSpells = new List<Spell>();
runtimeSpells.Add(fireball);
All the logic is already working, what means that when casted it will call CastFiberall function defined above and then reload for 5 seconds.
The spell is ready to use! But before, we need to define how... Is the user a NPC (non-playable character) or a Player?
If the user is a NPC, you'll send a command to cast the spell through the user and the magic will happen:
user.spells[0].ExecuteCommand(SpellCommand.CAST)
While the ExecuteCommand is used for NPCs, for players we use SendInput instead, and it's necessary to setup that inputs before using.
We bind different SpellInputs to different SpellCommands, so we can customize the behaviour of the spells. For example, we could cast some spell with key down and other with key up. In this case, the fireball is setted to cast when it receive a BTN_DOWN input:
fireball.SetKeyToCommand(Spell.SpellInput.BTN_DOWN, Spell.SpellCommand.CAST);
Let's setup the "Q" key to cast the Fireball and detect it in the PlayerController, assuming it inherit SpellUser:
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
spells[0].SendInput(Spell.SpellInput.BTN_DOWN);
}
Now, when the player press "Q", the fireball will cast!
This was a simple guide to help to get into the basic usage. The API becomes more powerful with other spell types, events and much more!
For more detail, check the API Reference.