Skip to content

Commit

Permalink
Merge pull request #74 from FIUS/feature/sleep-method
Browse files Browse the repository at this point in the history
Add sleep method in BasicEntity
  • Loading branch information
buehlefs authored Oct 4, 2019
2 parents c6bd683 + ad42190 commit e953a41
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package de.unistuttgart.informatik.fius.icge.simulation.entity;

import java.lang.ref.WeakReference;
import java.util.concurrent.CompletableFuture;

import de.unistuttgart.informatik.fius.icge.simulation.Playfield;
import de.unistuttgart.informatik.fius.icge.simulation.Position;
Expand Down Expand Up @@ -116,6 +117,24 @@ protected Simulation getSimulation() {
return this.getPlayfield().getSimulation();
}

/**
* Pause execution for {@code ticks} simulation ticks.
*
* @param ticks
* numberof simulation ticks to pause; must be > 0
* @throws IllegalArgumentException
* if ticks is <= 0
*/
public void sleep(int ticks) {
if (ticks <= 0) throw new IllegalArgumentException("The number of ticks must be > 0 !");
final CompletableFuture<Void> endOfOperation = new CompletableFuture<>();
try {
this.getSimulation().getSimulationClock().scheduleOperationInTicks(ticks, endOfOperation);
} finally {
endOfOperation.complete(null);
}
}

@Override
public String toString() {
return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode());
Expand Down

0 comments on commit e953a41

Please sign in to comment.