Skip to content

Commit

Permalink
Cache the animation controller instance
Browse files Browse the repository at this point in the history
  • Loading branch information
steffen-wilke committed Jan 3, 2021
1 parent 2afebd4 commit a319301
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/de/gurkenlabs/litiengine/entities/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public double getAngle() {

@Override
public IEntityAnimationController<?> animations() {
return this.getController(IEntityAnimationController.class);
return this.controllers.getAnimationController();
}

@Override
Expand Down
16 changes: 15 additions & 1 deletion src/de/gurkenlabs/litiengine/entities/EntityControllers.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.gurkenlabs.litiengine.entities;

import de.gurkenlabs.litiengine.graphics.animation.IEntityAnimationController;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -10,14 +12,22 @@
*/
public final class EntityControllers {
private Map<Class<? extends IEntityController>, IEntityController> controllers;
private IEntityAnimationController animationController;

EntityControllers() {
this.controllers = new ConcurrentHashMap<>();
}

public IEntityAnimationController getAnimationController() {
if (this.animationController == null) {
this.animationController = this.getController(IEntityAnimationController.class);
}

return this.animationController;
}

@SuppressWarnings("unchecked")
public <T extends IEntityController> T getController(Class<T> clss) {

T explicitController = this.getExplicitController(clss);
if (explicitController != null) {
return explicitController;
Expand All @@ -39,6 +49,7 @@ public <T extends IEntityController> void clearControllers(Class<T> clss) {
IEntityController controller = this.controllers.get(typeKey.get());
controller.detach();
this.controllers.remove(typeKey.get());
this.animationController = null;
}
}

Expand All @@ -48,11 +59,14 @@ public <T extends IEntityController> void addController(T controller) {
if (controller.getEntity().isLoaded()) {
controller.attach();
}

this.animationController = null;
}

public <T extends IEntityController> void setController(Class<T> clss, T controller) {
this.clearControllers(clss);
this.addController(controller);
this.animationController = null;
}

public void detachAll() {
Expand Down

0 comments on commit a319301

Please sign in to comment.