Hot damn dude, that’s too much!
Here’s a better (and scalable) example:
class Action {
enum Conditions {
LESS_THAN, GREATER_THAN;
public boolean test(float v1, float v2) {
switch(this) {
case LESS_THAN: return v1 < v2;
case GREATER_THAN: return v1 > v2;
}
}
}
private boolean doneX, doneY;
private Condition cond;
private float condValue;
private float dx, dy;
public Action(float dx, float dy) {
this.dx = dx;
this.dy = dy;
}
public void act(Player player) {
player.x += dx;
player.y += dy;
}
public Action doneWhenX(Condition cond, float condValue) {
doneX = true; doneY = false;
this.cond = cond;
this.condValue = condValue;
return this;
}
public Action doneWhenY(Condition cond, float condValue) {
doneY = true; doneX = false;
this.cond = cond;
this.condValue = condValue;
return this;
}
public boolean isDone(Player player) {
if(cond == null)
throw new IllegalArgumentException("No condition specified.");
if(doneX)
return cond.test(player.x, condValue);
if(doneY)
return cond.test(player.y, condValue);
}
}
public class MyClass {
public static void main(String[] args) {
PriorityQueue<Action> actions = new PriorityQueue<>();
actions.add(new Action(0.04f,0).doneWhenX(Action.Condition.GREATER_THAN, 700));
//...
Action curAction = null;
// gameloop
while(true) {
//...
if(curAction == null || curAction.isDone())
curAction = actions.poll();
if(curAction != null)
curAction.act(player);
//...
}
}
}