Skip to content

Commit

Permalink
Further reduce memory footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain committed May 31, 2019
1 parent 1e575a1 commit 29a798c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Version 0.5.13

* only create animation states dynamically whenever they are required
* only compute tmx layer indices whenever they are required

# Version 0.5.12

* only create `AStarPathFinder` when it is actually used to reduce memory footprint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.badlogic.gdx.math.Vector2;
import de.bitbrain.braingdx.graphics.GameObjectRenderManager;
import de.bitbrain.braingdx.util.Enabler;
import de.bitbrain.braingdx.util.Factory;
import de.bitbrain.braingdx.world.GameObject;

/**
Expand Down Expand Up @@ -42,6 +43,14 @@ private class AnimationState {
public float stateTime;
}

private final Factory<AnimationState> animationStateFactory = new Factory<AnimationState>() {

@Override
public AnimationState create() {
return new AnimationState();
}
};

private final AnimationConfig config;
private final AnimationCache animationCache;
private final Sprite sprite;
Expand Down Expand Up @@ -91,7 +100,7 @@ public AnimationRenderer offset(float x, float y) {

private TextureRegion retrieveRegionFor(GameObject object, float delta) {
Object currentAnimationType = animationTypeResolver.getAnimationType(object);
AnimationState state = (AnimationState) object.getOrSetAttribute(AnimationState.class, new AnimationState());
AnimationState state = object.getOrSetAttribute(AnimationState.class, animationStateFactory);
state.stateTime += delta;
Animation<TextureRegion> animation = animationCache.getAnimation(currentAnimationType);
boolean animationEnabled = animationEnabler.isEnabledFor(object);
Expand Down
41 changes: 38 additions & 3 deletions core/src/main/java/de/bitbrain/braingdx/tmx/GameObjectUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.badlogic.gdx.math.Vector2;
import de.bitbrain.braingdx.behavior.BehaviorAdapter;
import de.bitbrain.braingdx.event.GameEventManager;
import de.bitbrain.braingdx.util.Factory;
import de.bitbrain.braingdx.world.GameObject;

/**
Expand All @@ -32,6 +33,35 @@
*/
class GameObjectUpdater extends BehaviorAdapter {

private class IndexFactory implements Factory<Integer> {

float value;
float cellSize;

public IndexFactory() {

}

@Override
public Integer create() {
return IndexCalculator.calculateIndex(value, cellSize);
}
}

private class LayerIndexFactory implements Factory<Integer> {

GameObject object;

@Override
public Integer create() {
return api.lastLayerIndexOf(object);
}
}

private final IndexFactory indexFactory = new IndexFactory();

private final LayerIndexFactory layerIndexFactory = new LayerIndexFactory();

private final TiledMapAPI api;

private final State state;
Expand Down Expand Up @@ -81,13 +111,18 @@ private void updateCollision(GameObject object) {
// and last position is not occupied
Vector2 lastPosition = object.getLastPosition();
currentPosition.set(object.getLeft(), object.getTop());
int lastLayerIndex = (Integer) object.getOrSetAttribute("lastLayerIndex", api.lastLayerIndexOf(object));
layerIndexFactory.object = object;
int lastLayerIndex = object.getOrSetAttribute("lastLayerIndex", layerIndexFactory);
int currentLayerIndex = api.layerIndexOf(object);
if (lastLayerIndex != currentLayerIndex || !currentPosition.equals(lastPosition)) {
Gdx.app.debug("TiledMapAPI", "Updating collision of " + object);
// Object has moved, now check if last position is already occupied
int lastTileX = (Integer) object.getOrSetAttribute("lastTileX", IndexCalculator.calculateIndex(lastPosition.x, api.getCellWidth()));
int lastTileY = (Integer) object.getOrSetAttribute("lastTileY", IndexCalculator.calculateIndex(lastPosition.y, api.getCellHeight()));
indexFactory.value = lastPosition.x;
indexFactory.cellSize = api.getCellWidth();
int lastTileX = object.getOrSetAttribute("lastTileX", indexFactory);
indexFactory.value = lastPosition.y;
indexFactory.cellSize = api.getCellHeight();
int lastTileY = object.getOrSetAttribute("lastTileY", indexFactory);
GameObject occupant = api.getGameObjectAt(lastTileX, lastTileY, lastLayerIndex);

// clear last collision
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/de/bitbrain/braingdx/util/Factory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.bitbrain.braingdx.util;

public interface Factory<T> {

T create();
}
10 changes: 6 additions & 4 deletions core/src/main/java/de/bitbrain/braingdx/world/GameObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Pool;
import de.bitbrain.braingdx.util.Factory;
import de.bitbrain.braingdx.util.Mutator;

import java.util.HashMap;
Expand Down Expand Up @@ -269,13 +270,14 @@ public Object getAttribute(Object key) {
return attributes.get(key);
}

public Object getOrSetAttribute(Object key, Object defaultValue) {
Object value = attributes.get(key);
public <T> T getOrSetAttribute(Object key, Factory<T> defaultValueFactory) {
T value = (T) attributes.get(key);
if (value != null) {
return value;
}
setAttribute(key, defaultValue);
return defaultValue;
value = defaultValueFactory.create();
setAttribute(key, value);
return value;
}

public float getZIndex() {
Expand Down

0 comments on commit 29a798c

Please sign in to comment.