Skip to content

Instantly share code, notes, and snippets.

@rafaskb
Created June 21, 2022 17:36
Show Gist options
  • Save rafaskb/91f009b5ef1f3e1d9216a9ebe2c2119d to your computer and use it in GitHub Desktop.
Save rafaskb/91f009b5ef1f3e1d9216a9ebe2c2119d to your computer and use it in GitHub Desktop.
Executes another action until it returns true and ensures such action is never run after its completed.
package com.grashers.core.ui.scene2d.actions;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.utils.Pool;
import com.grashers.core.app.Game;
/** Executes another action until it returns true and ensures such action is never run after its completed. */
public class ConsumableAction extends Action {
protected Action action;
private boolean ran;
// REMINDER: Reset members
public void setAction(Action action) {
this.action = action;
}
public Action getAction() {
return action;
}
@Override
public final boolean act(float delta) {
Pool<? extends Action> pool = getPool();
setPool(null); // Ensure this action can't be returned to the pool inside the delegate action.
try {
if(!ran) {
try {
ran = action.act(delta);
} catch(Exception e) {
Game.logger.warning("ConsumableAction", "Error while calling action.act", e);
}
}
return ran;
} finally {
setPool(pool);
}
}
@Override
public void restart() {
if(action != null) action.restart();
ran = false;
}
@Override
public void reset() {
super.reset();
action = null;
}
@Override
public void setActor(Actor actor) {
if(action != null) action.setActor(actor);
super.setActor(actor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment