-
-
Save stevesoltys/197e7a604f8bfe495ae070c2db0c89c1 to your computer and use it in GitHub Desktop.
This file contains 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
package org.apollo.game.action; | |
import org.apollo.game.model.Position; | |
import org.apollo.game.model.entity.Entity; | |
import org.apollo.game.model.entity.Mob; | |
import org.apollo.game.model.entity.obj.GameObject; | |
import java.util.Collections; | |
import java.util.Set; | |
/** | |
* An {@link Action} which fires when a distance requirement is met. | |
* | |
* @param <T> The type of {@link Mob}. | |
* @author Blake | |
* @author Graham | |
* @author Steve Soltys | |
*/ | |
public abstract class DistancedAction<T extends Mob> extends Action<T> { | |
/** | |
* The delay once the threshold is reached. | |
*/ | |
protected final int delay; | |
/** | |
* The entity. | |
*/ | |
protected final Entity entity; | |
/** | |
* A flag indicating if this action fires immediately after the threshold is reached. | |
*/ | |
private final boolean immediate; | |
/** | |
* A flag indicating if the distance has been reached yet. | |
*/ | |
private boolean reached; | |
/** | |
* Creates a new DistancedAction. | |
* | |
* @param delay The delay between executions once the distance threshold is reached. | |
* @param immediate Whether or not this action fires immediately after the distance threshold is reached. | |
* @param mob The mob. | |
* @param entity The entity that we are approaching. | |
*/ | |
public DistancedAction(int delay, boolean immediate, T mob, Entity entity) { | |
super(0, true, mob); | |
this.delay = delay; | |
this.immediate = immediate; | |
this.entity = entity; | |
} | |
@Override | |
public final void execute() { | |
if (reached) { // Don't check again in case the player has moved away since it was reached | |
executeAction(); | |
} else if (withinDistance()) { | |
reached = true; | |
setDelay(delay); | |
if (immediate) { | |
executeAction(); | |
} | |
} | |
} | |
/** | |
* Executes the actual action. Called when the distance requirement is met. | |
*/ | |
protected abstract void executeAction(); | |
/** | |
* Gets the set of positions that will trigger this action. | |
* | |
* @return The set of trigger positions. | |
*/ | |
protected Set<Position> getTriggerPositions() { | |
return entity.getBounds().getInteractionPositions(); | |
} | |
/** | |
* Checks whether or not the mob is within distance of the Entity. | |
* | |
* @return A flag indicating whether or not the mob is within distance. | |
*/ | |
private boolean withinDistance() { | |
return getTriggerPositions().contains(mob.getPosition()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment