Skip to content

Commit

Permalink
Add active flag to the IUpdateable interface
Browse files Browse the repository at this point in the history
This allows to exclude Updatable implementations from the update mechanism if they overwrite the default implementation of the isActive() method and define an appropriate condition (e.g. game is paused/freezed/...)

Issue #267
  • Loading branch information
steffen-wilke committed Jun 30, 2019
1 parent e2675a1 commit 90737bb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
25 changes: 20 additions & 5 deletions src/de/gurkenlabs/litiengine/IUpdateable.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package de.gurkenlabs.litiengine;

import de.gurkenlabs.litiengine.configuration.ClientConfiguration;

/**
* The Interface IUpdateable provides the functionality to update an instance
* from the game loop. The instance needs to register itself.
* The Interface IUpdateable provides the functionality to automatically update an instance that implements it
* from the game loop. The instance needs to be registered on the loop.
*
* @see ILoop#attach(IUpdateable)
* @see ILoop#detach(IUpdateable)
*/
public interface IUpdateable {

/**
* This method is called by the game loop on all objects that need to update
* their attributes. It is called on every tick, means, it is called
* Game.GameLoop.TICKS_PER_SECOND times per second.
* This method is called by the game loop on all objects that are attached to the loop.
* It's called on every tick of the loop and the frequency can be configured using the <code>ClientConfiguration</code>.
*
* @see ClientConfiguration#setUpdaterate(int)
*/
public void update();

/**
* This flag controls whether this instance is currently active and thereby needs to be updated by the game loop.
*
* @return True if this instance should be updated; otherwise false.
*/
public default boolean isActive() {
return true;
}
}
2 changes: 1 addition & 1 deletion src/de/gurkenlabs/litiengine/UpdateLoop.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ protected long getExpectedDelta() {
protected void update() {
for (IUpdateable updatable : this.getUpdatables()) {
try {
if (updatable != null) {
if (updatable != null && updatable.isActive()) {
updatable.update();
}
} catch (final Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public void setShowGameMetrics(final boolean showGameMetrics) {
/**
* Sets the updaterate. On a very good machine the max update rate is sth.
* around 500 but such a high value will never be beneficial for the player.
*
* <p>
* This defaults to a value of 60.
* </p>
*
* @param updaterate
* the new updaterate
Expand Down

0 comments on commit 90737bb

Please sign in to comment.