- Home /
Spell-casting combo system
I am working on a game which has unique spells composed of different elements. There are various elements (eg. water, fire, cold, arcane, shield, earth, life, lightning) and they are each bound to key. My goal is to be able to queue up to three elements and then cast when I click. The spell changes depending on the combination. For example, if I do fire, fire, fire it casts a very damaging fire spell but if I do something like fire, fire, earth it will do a fiery rock with less fire damage but it has some physical damage in the form of a rock now. The most efficient way I can think of accomplishing this is to assign each element to a key. I press three keys and then when ready I press the "cast" key. It would then combine each key into a single string (eg. if I pressed F for fire, E for earth and S for arcane it would combine them into a single string of "FES"). Then it would sort the string albabetically into "EFS" and compare it to a dictionary of all spells and their three letter input string. From there it would know which spell it is and cast it.
I also would like to make it so that there are certain elements which combine to make new elements like water and cold combining to make ice and fire and water combining to make steam. I also want there to be certain elements which can't go together like earth and lightning or arcane and life. That way I can use the opposites to remove an element from the spell. For example if I am making a spell and I press A for arcane, F for fire and I accidentally press E for earth as my final element but I want to remove the earth and replace it with a different element I can press the opposite of earth (lightning) to remove it from the spell.
That is the concept but I don't know how to actually put it into unity in C#. I'm new to C# but I have alot of past python experience
Answer by cbjunior · Jun 25, 2017 at 01:33 PM
From a programmatic perspective, I would recommend making an enum type to represent the different elements, then adding them to a collection type of fixed size. Something like this:
public static enum ElementType
{
FIRE, EARTH, AIR, WATER, LIGHTNING, ARCANE, ICE
}
You would put this enum into a class that itself reads the key input and adds the respective enum value to a collection, say an ArrayList. Each time an element is added it is checked against the other values in the collection, and if there's a conflict then remove the conflicting element. Once the fire key is pressed, check the current configuration against a static collection of effects and generate the appropriate effect. I don't want to give you too much example code because it's better for you to work through it to learn. I would recommend looming up how enums and collection types work in C#.
Thanks for the tips!
This is what I have so far
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spells : $$anonymous$$onoBehaviour {
class Empty
{}
public ArrayList elementQ = new ArrayList();
public enum ElementType
{
WATER, LIFE, SHIELD, COLD, LIGHTNING, ARCANE, EARTH, FIRE
}
public ElementType elementType;
// Use this for initialization
void Start () {
}
void AddElement(ElementType element)
{
print(elementQ);
elementQ.Add(element);
}
// Update is called once per frame
void FixedUpdate () {
if (Input.Get$$anonymous$$eyDown("q"))
AddElement(ElementType.WATER);
else if (Input.Get$$anonymous$$eyDown("w"))
AddElement(ElementType.LIFE);
else if (Input.Get$$anonymous$$eyDown("e"))
AddElement(ElementType.SHIELD);
else if (Input.Get$$anonymous$$eyDown("r"))
AddElement(ElementType.COLD);
else if (Input.Get$$anonymous$$eyDown("a"))
AddElement(ElementType.LIGHTNING);
else if (Input.Get$$anonymous$$eyDown("s"))
AddElement(ElementType.ARCANE);
else if (Input.Get$$anonymous$$eyDown("d"))
AddElement(ElementType.EARTH);
else if (Input.Get$$anonymous$$eyDown("f"))
AddElement(ElementType.FIRE);
}
}
The issue I'm having right now is that when I try to print the contents of the array list it prints this ins$$anonymous$$d
"elementQ" it prints "System.Collections.ArrayList" "UnityEngine.$$anonymous$$onoBehaviour:print(Object)"
I think this is because the contents of the list aren't strings or something along those lines but I can't figure out how to fix it. I tried doing print(elementQ.ToString);
but it didn't work.
Any more advice would be greatly appreciated :)
EDIT: I also can't figure out how to compare the items in the list to check if they are conflicting.
@lucateil You need to enumerate through the items in the ArrayList and check each one, as well as when you want to print. You could also try using a List<>
ins$$anonymous$$d because that will allow you to specify a type:
List<ElementType> elementList = new List<ElementType>();
private void PrintElements()
{
if (elementList != null)
{
foreach (ElementType e in elementList)
{
print (e.ToString ());
}
}
}