SPAM Framework Documentation

Casting/invoking abilities

There's an example-component provided in the Components-folder that you can either use directly or look at for reference.

To cast abilities you first have to create an ability and add it to an invoker.

To make abilities effect targets you either have to add the pre-supplied Ability target-component to a GameObject or implement the IAbilityTarget-interface in one of your own MonoBehaviours.

The Cast method accepts a Vector3 that is a point in the direction that the ability should be cast.
If the ability is targeted there's also an overload that accepts an IAbilityTarget.

Cast handles everything related to cooldowns, effects, timings etc., and there's multiple early exits that safeguards against performance degradation if you continually call it every frame. In other words: it's no problem spam Cast to your heart's content!

When an ability is cast, it goes through all of the Ability stages in order.

Casting targeted abilities

To cast a targeted ability you first have to resolve the actual IAbilityTarget or position that you wish to cast it on. Your game will decide how targets are selected, but a simplified example is:

// class CastTargetedAbility : MonoBehaviour 

[SerializeField] private TargetedAbility _ability;

void Update() 
{
    var leftMousePressed = Input.GetMouseButtonDown(0);
    if(!leftMousePressed) return;

    CastTargetedAbility();
}

void CastTargetedAbility() 
{
    var mousePos = Input.mousePosition;  
    var ray = Camera.main.ScreenPointToRay(mousePos);  
    if (!Physics.Raycast(ray, out var hit, Mathf.Infinity)) return;

    if (_ability.RequiresTarget)  
    {  
        var target = hit.transform.GetComponent<IAbilityTarget>();  
        if (target != null)  
            _ability.Cast(target);  
        return;  
    }

    _ability.Cast(hit.point);
}

Casting other abilities

All other abilities except targeted only requires a point along the cast-direction. A simple starting point could be:

// class CastAbility : MonoBehaviour

[SerializeField] private AbilityBase _ability;

void Update() 
{
    var leftMousePressed = Input.GetMouseButtonDown(0);
    if(!leftMousePressed) return;

    CastAbility();
}

void CastAbility() 
{
    var direction = transform.forward;
    _ability.Cast(direction);
}

Backlinks: