Search Unity

Stealing spells properly!

Discussion in 'Scripting' started by FeastSC2, Sep 4, 2017.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    I'm relatively new to programming,
    I want to have a player that can steal the spells of the enemies, there surely will be a lot of spells in the game.

    It feels like adding components to the player is a good idea because I will need almost exactly the same code as the enemy, I want the stolen spell to behave in the same fashion as when the enemy uses it when possible.

    So since my player will have to be able to use any spell, surely all the spell components must have a same method like Activate() to start their effect, that sounds like I have to create an interface for all stealable spells.

    Lastly, I might have a component say "ProjectileManager" that could cast lots of different spells, but when I'm stealing a spell from an enemy I need to be able to copy his ProjectileManager and not just create a new one that doesn't have the enemy's particular settings, is there an easy way to do this?

    Does that sound good or is it better to just add prefabs? Maybe prefabs are more flexible?
    Any suggestion is welcome!
     
  2. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    Here's how I'd do it.

    Have all your spells inherit from a "Spell" class in which you put an activate method that takes 2 parameters, the caster gameobject and a target gameobject for spells that require a target.
    https://unity3d.com/fr/learn/tutorials/topics/scripting/inheritance

    You should design your spells to be independent from whatever gameobject casts it.

    e.g. Imagine a fireball that goes from the caster's hand to the target. For this fireball to work, it needs to know what the caster's hand position is and who the target is. The damage, speed, effects and everything else can all be handled internally in the fireball class.

    Since not all spells will require the same informations, it's probably best that you let each spells send their own requests for the data they need. They just need to know who they should ask it to.

    So, when calling that fireball, you're passing the caster and target as parameters. The fireball wants to know the exact casting point. You could have your player + enemies hold a class which stores their casting points (hand for the player, mouth for a dragon, whatever) and have the fireball class ask that class for the casting point.
     
    Last edited: Sep 4, 2017
  3. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yes I understand thanks for the input, however since I possibly could have a spell that doesn't need a target I'm thinking I should have all my Activate() methods take no arguments. I think I won't even end up creating that Activate method, because it might be too different when an AI casts it compared to the player, so I'll create a method within the component that handles how the player casts it, a method the enemy won't use.

    Also, since I'd like to make spells that don't necessarily need to be cast by player input, things like absorb fire if the player got attacked by a fire spell, I could have my player always cast those 3 methods in the correct positions in the code. The problem I see with this is that my spell class with the ISpell interface won't have access to the player's info despite being for him too so it might be necessary sometimes.

    The spells are likely to be quite varied so the implementation will have to be somewhat generalized.

    Code (CSharp):
    1. public interface ISpell
    2. {
    3.     void PlayerCast(); // normal cast spell
    4.  
    5.     void PlayerOnImpactCast(); // spells that activate when there was an impact with the player
    6.  
    7.     void PlayerAutoCast(); // spells that are cast automatically every x time?
    8. }
     
  4. ADNCG

    ADNCG

    Joined:
    Jun 9, 2014
    Posts:
    994
    I edited my post as you posted. Anyway, what are those differences between the player and the enemy? If you can sum them up, then you can create a class that holds the information regarding those differences and have the spells query that information.
     
  5. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    978
    Yes I saw :)

    Well for starters, I don't know for sure what those differences will be yet since my whole game is not planned out. But in general it's going to be a difference in that my AI finds the target directly and the player will have to aim.
    For a projectile, it might be that I have to change the method of firing a bit, the AI finds the target and finds a proper angle to shoot, but for the player it's gonna be as simple as aiming.

    Having a linking class between the player and the spell might be a good idea