I'm not too sure if I understand the question correctly so forgive me if my answer if off, but this is how I would go about a turn-based event system.
Firstly, the actions that are represented by strings and ids can be converted into something more object-oriented which would be something like:
public abstract class Action {
public abstract void execute();
}
That way you don't have to use switch or if-else statements; you would have to extend the action class.
To sort these actions out I would probably use an enumeration as each instance implements Comparable
:
public enum Priority {
PRE_TURN, PHASE, POST_TURN
}
And implement it into the action class like so:
private Priority priority;
public Action(Priority priority) {
this.priority = priority;
}
public Priority getPriority() {
return priority;
}
Action
is an abstract class because there are no arguments in the execute
method. This means you will have to specify the necessary fields in the constructor of the extending class which acts as a work-around to the many different parameters. It also enforces you to specify its priority. An example would be:
public class BattlePhase extends Action {
private Entity e, Entity target;
private Attack attack;
public BattlePhase(Entity e) {
this(e, e.inLineTarget(), e.getCurrentAttack());
}
public BattlePhase(Entity e, Entity target, Attack attack) {
super(Priority.MEDIUM);
this.e = e;
this.target = target;
this.attack = attack;
}
@Override
public void execute() {
attack.perform(e, target);
}
}
Character c = scene.currentCharacter();
scene.queueAction(new BattleAction(c, selectedTarget, c.getMove(selectedMove)));
In the class that manages event you could use a priority queue that will sort actions added to the queue if you specify a 'Comparator'.
Queue<Action> actions = new PriorityQueue<>(10, new Comparator<Action>() {
public int compare(Action a1, Action a2) {
return a1.getPriority().compareTo(a2.getPriority());
}
});
(the first argument, 10, is the initial capacity)
Or you could use a List
and sort it when you add actions by calling the Collection#sort
Then your runEvent
method would be something like:
public void runEvent() {
Action a;
if ((a = actions.poll) != null) {
a.execute();
}
}
That's just the outline of it, you might want to add a procedure to sequence the actions so that they don't all run at once.