diff --git a/TeamCode/src/main/java/codebase/actions/Action.java b/TeamCode/src/main/java/codebase/actions/Action.java new file mode 100644 index 0000000..bab8736 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/Action.java @@ -0,0 +1,9 @@ +package codebase.actions; + +import codebase.Loop; + +public interface Action extends Loop { + void init(); + + boolean isComplete(); +} diff --git a/TeamCode/src/main/java/codebase/actions/SequentialAction.java b/TeamCode/src/main/java/codebase/actions/SequentialAction.java new file mode 100644 index 0000000..74bff30 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SequentialAction.java @@ -0,0 +1,52 @@ +package codebase.actions; + +import java.util.Arrays; +import java.util.stream.Stream; + +public class SequentialAction implements Action { + + private ActionNode currentActionNode; + + public SequentialAction(Action first, Action... rest) { + this.currentActionNode = new ActionNode(Stream.concat(Stream.of(first), Arrays.stream(rest)).toArray(Action[]::new)); + } + + @Override + public void init() { + currentActionNode.action.init(); + } + + @Override + public boolean isComplete() { + return this.currentActionNode == null; + } + + @Override + public void loop() { + if (this.currentActionNode.action.isComplete()) { + this.currentActionNode = this.currentActionNode.next; + } + + if (this.currentActionNode == null) { + return; + } + + this.currentActionNode.action.loop(); + } +} + +class ActionNode { + public Action action; + public ActionNode next; + + public ActionNode(Action[] actions) { + this.action = actions[0]; + + if (actions.length == 1) { + this.next = null; + return; + } + + this.next = new ActionNode(Arrays.copyOfRange(actions, 1, actions.length)); + } +} \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java new file mode 100644 index 0000000..29b3090 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SimultaneousAction.java @@ -0,0 +1,61 @@ +package codebase.actions; + +import androidx.annotation.NonNull; + +import java.util.ArrayList; + +public class SimultaneousAction implements Action { + + private final ArrayList actions; + + public SimultaneousAction(Action first, Action... rest) { + this.actions = new ArrayList<>(); + + this.actions.add(first); + + for (Action action : rest) { + this.add(action, false); + } + } + + @Override + public void init() { + for (Action action : actions) { + action.init(); + } + } + + @Override + public boolean isComplete() { + for (Action action : actions) { + if (!action.isComplete()) { + return false; + } + } + return true; + } + + @Override + public void loop() { + for (Action action : actions) { + if (!action.isComplete()) { + action.loop(); + } + } + } + + public void add(@NonNull Action action, boolean init) { + String actionName = action.getClass().getName(); + + for (Action a : actions) { + if (a.getClass().getName().equals(actionName)) { + throw new IllegalArgumentException("You can't add multiple of the same class of Action to SimultaneousAction."); + } + } + actions.add(action); + + if (init) { + action.init(); + } + } +} \ No newline at end of file diff --git a/TeamCode/src/main/java/codebase/actions/SleepAction.java b/TeamCode/src/main/java/codebase/actions/SleepAction.java new file mode 100644 index 0000000..81185c8 --- /dev/null +++ b/TeamCode/src/main/java/codebase/actions/SleepAction.java @@ -0,0 +1,27 @@ +package codebase.actions; + +public class SleepAction implements Action { + + private final long ms; + + private long endMs; + + public SleepAction(long ms) { + this.ms = ms; + } + + + @Override + public void init() { + this.endMs = System.currentTimeMillis() + this.ms; + } + + @Override + public boolean isComplete() { + return System.currentTimeMillis() >= this.endMs; + } + + @Override + public void loop() { + } +}