Search Unity

Discussion Elemental Interaction System

Discussion in 'Game Design' started by NDSno1, Feb 6, 2023.

  1. NDSno1

    NDSno1

    Joined:
    Dec 20, 2014
    Posts:
    223
    Hello,
    I'm working on a RPG game with elemental trait interactions similar to pokemon. Currently I can only think of a CheckInteraction() function everytime there is an attack or interaction, an a bunch of switch statement to calculate the damage output. Is there a “cleaner” way to do this?
    I'm only doing 5 elements (or rather, agents, they are strictly not elements, I'm only calling them elements to simplify the idea) based on the Chinese Traditional thinking system: wood, fire, earth, water, and metal. All of them will have interactions with others like so. I'll take Fire for example:

    • Being supported: Fire is supported by Wood:
      • When attacking each other, damage output of both is reduced.
      • When combined as one attack, damage output of both is increased.
    • Neutral: Fire is neutral againts Fire:
      • When attacking each other, damage output of both is unchanged.
      • When combined, damage output of both is increased.
    • Being contrasted: Fire is contrasted by Water:
      • When attacking each other, damage output of Water is increased, damage output of Fire is decreased.
      • When combining, damage output of both is decreased.
    • Support: Fire supports Earth:
      • When attacking each other, damage output of both is reduced.
      • When combined, damage output of both is increased
    • Contrast: Fire contrasts Metal:
      • When attacking each other, damage output of Fire is increased, damage output of Metal is decreased.
      • When combining, damage output of both is decreased.
    There will be other interactions as well, such as when one heals other, stats bonuses when 2 supporting elements goes together in a pair. But all of them falls on the above idea.

    Everthing can be sumarized by the chart below:

    Thank you very much in advance. I know this sounds like homework, but it is really just my hobby project to understand how games with element like pokemon works.

     
  2. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    hello,
    you could have something like this:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [CreateAssetMenu(menuName = "Element")]
    6. public class Element : ScriptableObject
    7. {
    8.     public string _name;
    9.     [SerializeField] private List<Element> weakAgainst = new List<Element>();
    10.     [SerializeField] private List<Element> strongAgainst = new List<Element>();
    11.  
    12.     public bool IsStrongToElement(Element element)
    13.     {
    14.         return strongAgainst.Contains(element);
    15.     }
    16.  
    17.     public bool IsWeakToElement(Element element)
    18.     {
    19.         return weakAgainst.Contains(element);
    20.     }
    21. }
    then create an element scriptable object for each element and drag and drop the elements into the list depending if they are strong or weak against certain element
    just adust the code for your needs
    upload_2023-3-28_23-2-47.png
    this all the elements created as scriptable objects
    then add the different elements to the lists:
    upload_2023-3-28_23-3-44.png
    and then later you can check with:

    Code (CSharp):
    1. if(pokemon.element.IsStrongToElement(otherpokemon.element) //200% damage
    2. {
    3. pokemon.currentHP -= otherpokemon.damage * 2;
    4. } else if(pokemon.element.IsWeakToElement(otherpokemon.element) //50% damage
    5. {
    6. pokemon.currentHP -= ptherpokemon.damage / 2;
    7. }