Skip to content

Instantly share code, notes, and snippets.

@cocopon cocopon/Action.pde
Last active Aug 29, 2015

Embed
What would you like to do?
/*
* Action.pde
* (c) 2015 cocopon.
*
* License: MIT
*/
import java.util.Arrays;
public class ActionRunner {
private ArrayList<Action> actions_;
public ActionRunner() {
actions_ = new ArrayList();
}
public int getActionCount() {
return actions_.size();
}
public void add(Action action) {
actions_.add(action);
}
public void remove(Action action) {
for (int i = 0; i < getActionCount(); i++) {
if (action == actions_.get(i)) {
actions_.remove(i);
return;
}
}
}
public void update() {
int actionCount = getActionCount();
if (actionCount == 0) {
return;
}
for (int i = actionCount - 1; i >= 0; i--) {
Action action = actions_.get(i);
action.update();
if (action.isCompleted()) {
actions_.remove(i);
}
}
}
}
public class ActionDelegate {
public void onStart() {}
public void onUpdate(float progress) {}
public void onUpdate(float progress, float t) {}
public void onEnd() {}
}
public class ActionConfig {
private static final int DEFAULT_DURATION = 20;
public float duration;
public float delay;
public ActionDelegate delegate;
public ActionConfig(float delay, float duration, ActionDelegate delegate) {
this.delay = delay;
this.duration = duration;
this.delegate = delegate;
}
public ActionConfig(float duration, ActionDelegate delegate) {
this(0, duration, delegate);
}
public ActionConfig(ActionDelegate delegate) {
this(DEFAULT_DURATION, delegate);
}
public ActionConfig() {
this(null);
}
}
public class Action {
protected float t_;
protected ActionConfig config_;
public Action(ActionConfig config) {
t_ = 0;
config_ = config;
}
public float getTotalTime() {
return config_.delay + config_.duration;
}
public float getProgress() {
return min((t_ - config_.delay) / config_.duration, 1.0);
}
public boolean isCompleted() {
return t_ >= getTotalTime();
}
public void update() {
if (isCompleted()) {
return;
}
if (t_ == 0) {
setUp_();
}
++t_;
if (t_ < config_.delay) {
return;
}
updateInternal();
if (config_.delegate != null) {
config_.delegate.onUpdate(getProgress());
config_.delegate.onUpdate(getProgress(), t_);
}
if (isCompleted()) {
if (config_.delegate != null) {
config_.delegate.onEnd();
}
}
}
private void setUp_() {
if (config_.delegate != null) {
config_.delegate.onStart();
}
setUpInternal();
}
protected void setUpInternal() {
}
protected void updateInternal() {
}
}
public class SerialAction extends Action {
private ArrayList<Action> actions_;
public SerialAction(ActionConfig config, ArrayList<Action> actions) {
super(config);
actions_ = actions;
// Overwrite duration
float totalTime = 0;
for (Action action : actions_) {
totalTime += action.getTotalTime();
}
config_.duration = totalTime;
}
public SerialAction(ActionConfig config, Action[] actions) {
this(config, new ArrayList(Arrays.asList(actions)));
}
protected void updateInternal() {
for (Action action : actions_) {
if (action.isCompleted()) {
continue;
}
action.update();
break;
}
}
}
public class ParallelAction extends Action {
private ArrayList<Action> actions_;
public ParallelAction(ActionConfig config, ArrayList<Action> actions) {
super(config);
actions_ = actions;
// Overwrite duration
float maxTime = 0;
for (Action action : actions_) {
maxTime = max(maxTime, action.getTotalTime());
}
config_.duration = maxTime;
}
public ParallelAction(ActionConfig config, Action[] actions) {
this(config, new ArrayList(Arrays.asList(actions)));
}
protected void updateInternal() {
for (Action action : actions_) {
action.update();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.