-
Notifications
You must be signed in to change notification settings - Fork 3
13 actions #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
13 actions #14
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
204cdcd
add Action Interface, SequentialAction, SimultaneousAction, and Sleep…
Consolegamer2 eef381c
add init option to add method of SimultaneousAction for adding action…
liambridgers 8f1cc30
Fix spacing in SleepAction, change SequentialAction and SimultaneousA…
liambridgers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package codebase.actions; | ||
|
|
||
| import codebase.Loop; | ||
|
|
||
| public interface Action extends Loop { | ||
| void init(); | ||
|
|
||
| boolean isComplete(); | ||
| } |
52 changes: 52 additions & 0 deletions
52
TeamCode/src/main/java/codebase/actions/SequentialAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
liambridgers marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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)); | ||
| } | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
TeamCode/src/main/java/codebase/actions/SimultaneousAction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package codebase.actions; | ||
|
|
||
| import androidx.annotation.NonNull; | ||
|
|
||
| import java.util.ArrayList; | ||
|
|
||
| public class SimultaneousAction implements Action { | ||
|
|
||
| private final ArrayList<Action> 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(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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() { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.