- Home /
How to handle undefined targets in a composition based ability system?
Hey all, I'm trying to code a composition based ability system for a turn-based game because im doing a game with a lot of different abilitys.
I have an AbilityController and an AbilityComponent. From those i plan to make all my abilitys. The Controller should just grab ALL Components attached to an ability and trigger them. The Components are something like DamageComponent, HealComponent, SpeedBuffComponent and so on. Controller:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AbilityController : MonoBehaviour
{
public void CastAbility()
{
AbilityComponent[] abilityComponents = gameObject.GetComponents<AbilityComponent>();
for(int i = 0; i< abilityComponents.Length; i++)
{
abilityComponents[i].CastAbilityComponent();
}
}
}
Component:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AbilityComponent : MonoBehaviour
{
public void CastAbilityComponent()
{
//do stuff
}
}
Now i want to make a system where the targets could be different for each Component.
Example:
Damage Enemy A
Debuff Enemy A+B+C
Heal PlayerCharavter A+B+C
For this i would need 3 TargetArrays but i do not want to hardcode 3 different arrays. Not sure how to handle this problem.
Answer by pauldarius98 · Feb 25, 2021 at 01:25 PM
You could define an interface called ITarget which is going to be implemented by all units (enemies or player). Then, your AbilityComponent should also receive a list of targets:
public class AbilityComponent : MonoBehaviour
{
public void CastAbilityComponent(params ITarget[] targets)
{
//do stuff
}
}
And now you can specify the targets that are going to be affected by each spell at runtime, without hardcoding anything
Answer by oskar-s · Feb 25, 2021 at 02:22 PM
Okay, thank you for the answer. But how can i then specify which of the targets is healed and which damaged if my ability can do both for example?
Your answer
Follow this Question
Related Questions
Need to code Enemy Attacking Portion of a Pokemon/Turned Based Combat System 2 Answers
Hack N' Slash RPG Combat System 2 Answers
Looking for RPG system (look inside) 0 Answers
Need help making a pokemon turn based system ! 1 Answer
Server software for multiplayer turn-based RPG like Pokemon 1 Answer