Enemy Behaviors

Enemy behaviors determine how enemies select the cards to play. The behavior should normally be predictable so that the intent can be shown to the player. If randomization is needed, its best to make it based on the location seed and the current turn, so that the intent is calculated the same way as the actual action.

To create a new behavior, you can duplicate one of the behavior scripts

The first function is the most important, because its what determines which card will be played, the other 3 functions are only used for card abilities that ask the enemy to make a selection.

The list of cards or characters, will already only contain the VALID targets, so you don't need to check again if the target is valid, only to check which one you want to prioritize.

Let's try to create a new behavior example that make the enemy attack the character with the lowest health, and use the ability that deals the most damage.

[CreateAssetMenu(fileName = "behavior", menuName = "TcgEngine/Behaviors/LowHP", order = 20)]
public class BehaviorLowHP : BehaviorData
{
    public override Card SelectPlayCard(Battle data, BattleCharacter character, List<Card> cards, int turn)
    {
        //Find card with highest damage
        int highest = 0;
        Card target = cards.Count > 0 ? cards[0] : null; //Play first card if no card deals damage
        foreach (Card card in cards)
        {
            foreach (AbilityData ability in card.GetAbilities())
            {
                int damage = card.GetAbilityValue(character);
                if (ability.HasEffect<EffectDamage>() && damage > highest)
                {
                    target = card;
                    highest = damage;
                }
            }
        }
        return target;
    }

    public override BattleCharacter SelectCharacterTarget(Battle data, BattleCharacter character, Card card, List<BattleCharacter> characters, int turn)
    {
        //Find lowest HP
        int lowest = 9999;
        BattleCharacter target = null;
        foreach (BattleCharacter tcharacter in characters)
        {
            if (tcharacter.GetHP() < lowest)
            {
                lowest = tcharacter.GetHP();
                target = tcharacter;
            }
        }
        return target;
    }

    public override Card SelectCardTarget(Battle data, BattleCharacter character, Card card, List<Card> cards, int turn)
    {
        return GetRandomCard(data, character, cards, turn);
    }

    public override Slot SelectSlotTarget(Battle data, BattleCharacter character, Card card, List<Slot> slots, int turn)
    {
        return GetRandomSlot(data, character, slots, turn);
    }

    public override string GetBehaviorText()
    {
        return "Attacks the champion with lowest health";
    }
}

Then, like other scriptable objects, you would need to create the data file for this event in the Resources folder by right click - TcgEngine/Behaviors/LowHP. Then you would need to link the behavior to an enemy data.

Last updated