- Joined
- Aug 1, 2013
- Messages
- 4,658
Hi all.
I am making a game but as I will have numberless spells, actions, etc
I thought that I could create a system that is similar to the trigger system of WC3.
I got some stuff but I am not sure about everything.
(Code is written in Java... which is not the language of the game though.)
The Globals are very easy as they are just variables that are accessable by every class:
I think that this class is pretty much the parent of the triggers:
Triggers are simply called like this.
The concept of the events is just like that.
This class should represent a specific trigger just like we have our triggers.
The events are unit created and unit killed (dies).
The condition is always true (AKA no conditions).
The execute will not be a problem either.
Now the problem:
How do I initialize all triggers?
Do I have to make a new object of every "trigger" in the init method of Trigger?
Or is there any good thing that I can run every class somehow?
(This should be possible on both Java and C#)
If anyone has a better concept of a trigger system then please tell me.
I am making a game but as I will have numberless spells, actions, etc
I thought that I could create a system that is similar to the trigger system of WC3.
I got some stuff but I am not sure about everything.
(Code is written in Java... which is not the language of the game though.)
The Globals are very easy as they are just variables that are accessable by every class:
Code:
public class Constants
{
public static int udg_TempInteger;
public static int udg_TempInteger2 = 10;
public static int udg_TempInteger3;
public static double udg_TempDouble;
public static double udg_TempDouble2 = 26.78;
public static double udg_TempDouble3;
public static String udg_String;
public static String udg_String2 = "2000";
public static String udg_String3;
public static String myString1;
}
I think that this class is pretty much the parent of the triggers:
Code:
public abstract class Trigger
{
public static ArrayList<ArrayList<Trigger>> TRIGGER_LIST;
public static final int EVENT_UNIT_CREATED = 1;
public static final int EVENT_UNIT_KILLED = 2;
public static final int EVENT_ABILITY_CAST = 3;
public static final int EVENT_UNIT_WALKED = 4;
//This method is called when the game starts to initialize triggers.
public static void init()
{
TRIGGER_LIST = new ArrayList<>();
for (int i = 1; i < 5; i++)
{
TRIGGER_LIST.add(new ArrayList<Trigger>());
}
}
//This method is called when an event is triggered.
public static void call(int event)
{
for (Trigger t : TRIGGER_LIST.get(event))
{
t.conditionalExecute();
}
}
//Simple constructor that registers the trigger in the proper events
public Trigger(int[] event)
{
for (int i : event)
{
TRIGGER_LIST.get(i).add(this);
}
}
//These 3 methods are obvious.
public final void conditionalExecute()
{
if (condition())
{
execute();
}
}
public abstract boolean condition();
public abstract void execute();
}
Triggers are simply called like this.
The concept of the events is just like that.
Code:
public class Unit
{
public Unit()
{
Trigger.call(Trigger.EVENT_UNIT_CREATED);
}
}
This class should represent a specific trigger just like we have our triggers.
The events are unit created and unit killed (dies).
The condition is always true (AKA no conditions).
The execute will not be a problem either.
Code:
public class TriggerTest extends Trigger
{
public TriggerTest()
{
super(new int[]
{
Trigger.EVENT_UNIT_CREATED,
Trigger.EVENT_UNIT_KILLED
});
}
@Override
public boolean condition()
{
return true;
}
@Override
public void execute()
{
System.out.println("TriggerTest executed!");
System.out.println("Unit is either created or killed.");
}
}
Now the problem:
How do I initialize all triggers?
Do I have to make a new object of every "trigger" in the init method of Trigger?
Or is there any good thing that I can run every class somehow?
(This should be possible on both Java and C#)
If anyone has a better concept of a trigger system then please tell me.