Skip to content

Commit

Permalink
Only remove relevant objects from the tiledmap manager
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbrain committed Jun 4, 2019
1 parent 710da88 commit a966169
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 0.5.17

* [[#98](https://github.com/bitbrain/braingdx/issues/98)] only remove relevant objects from the world when disposing the tiled manager

# Version 0.5.16

* remove `Iterable` interface from `GameWorld`
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/de/bitbrain/braingdx/tmx/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<String> getLayerIds() {
}

public void setLayerIds(List<String> layerIds) {
layerIds = Collections.unmodifiableList(layerIds);
this.layerIds = Collections.unmodifiableList(layerIds);
}

public void setNumberOfLayers(int numberOfLayers) {
Expand Down
31 changes: 17 additions & 14 deletions core/src/main/java/de/bitbrain/braingdx/tmx/StatePopulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.badlogic.gdx.maps.tiled.TiledMapTile;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.utils.GdxRuntimeException;
import de.bitbrain.braingdx.behavior.BehaviorManager;
import de.bitbrain.braingdx.event.GameEventManager;
import de.bitbrain.braingdx.graphics.GameObjectRenderManager;
Expand Down Expand Up @@ -64,39 +65,41 @@ public void populate(TiledMap tiledMap, State state, Camera camera, MapLayerRend
state.setNumberOfLayers(mapLayers.getCount());
handleMapProperties(tiledMap.getProperties(), state, config);
List<String> layerIds = new ArrayList<String>();
int lastTileLayerIndex = 0;
int lastTileLayerIndex = -1;
String lastLayerId = null;
for (int i = 0; i < mapLayers.getCount(); ++i) {
MapLayer mapLayer = mapLayers.get(i);
if (mapLayer instanceof TiledMapTileLayer) {
if (i > 0) {
lastTileLayerIndex++;
}
String layerId = handleTiledMapTileLayer((TiledMapTileLayer) mapLayer, i, tiledMap, camera, rendererFactory,
lastTileLayerIndex++;
lastLayerId = handleTiledMapTileLayer((TiledMapTileLayer) mapLayer, i, tiledMap, camera, rendererFactory,
config);
layerIds.add(layerId);
layerIds.add(lastLayerId);
populateStaticMapData(lastTileLayerIndex, (TiledMapTileLayer) mapLayer, state, config);
} else {
// Not a tiledlayer so consider it as an object layer
handleObjectLayer(lastTileLayerIndex, mapLayer, state, config);
handleObjectLayer(lastLayerId, lastTileLayerIndex, mapLayer, state, config);
}
}
state.setLayerIds(layerIds);

// Add debug layer
handleDebugTileLayer(state, camera, rendererFactory, config);
layerIds.add(handleDebugTileLayer(state, camera, rendererFactory));
state.setLayerIds(layerIds);
}

private void handleMapProperties(MapProperties properties, State state, TiledMapConfig config) {
state.setIndexDimensions(properties.get(config.get(Constants.WIDTH), Integer.class),
properties.get(config.get(Constants.HEIGHT), Integer.class));
}

private void handleObjectLayer(int layerIndex, MapLayer layer, State state, TiledMapConfig config) {
private void handleObjectLayer(String lastLayerId, int layerIndex, MapLayer layer, State state, TiledMapConfig config) {
if (layerIndex < 0) {
throw new GdxRuntimeException("Unable to load tiled map! At least a single tiled layer is required!");
}
MapObjects mapObjects = layer.getObjects();
for (int objectIndex = 0; objectIndex < mapObjects.getCount(); ++objectIndex) {
MapObject mapObject = mapObjects.get(objectIndex);
MapProperties objectProperties = mapObject.getProperties();
GameObject gameObject = gameWorld.addObject();
GameObject gameObject = gameWorld.addObject(lastLayerId);
final float x = objectProperties.get(config.get(Constants.X), Float.class);
final float y = objectProperties.get(config.get(Constants.Y), Float.class);
final float w = objectProperties.get(config.get(Constants.WIDTH), Float.class);
Expand Down Expand Up @@ -139,11 +142,11 @@ private void handleObjectLayer(int layerIndex, MapLayer layer, State state, Tile
}

private String handleDebugTileLayer(State state, Camera camera,
MapLayerRendererFactory rendererFactory, TiledMapConfig config) {
MapLayerRendererFactory rendererFactory) {
GameObjectRenderer renderer = rendererFactory.createDebug(api, state, camera);
String id = UUID.randomUUID().toString();
renderManager.register(id, renderer);
GameObject layerObject = gameWorld.addObject();
GameObject layerObject = gameWorld.addObject(id);
layerObject.setActive(false);
layerObject.setPersistent(true);
layerObject.setType(id);
Expand All @@ -158,7 +161,7 @@ private String handleTiledMapTileLayer(TiledMapTileLayer layer, int index, Tiled
GameObjectRenderer renderer = rendererFactory.create(index, tiledMap, camera);
String id = UUID.randomUUID().toString();
renderManager.register(id, renderer);
GameObject layerObject = gameWorld.addObject();
GameObject layerObject = gameWorld.addObject(id);
layerObject.setActive(false);
layerObject.setPersistent(true);
layerObject.setType(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public TiledMapAPI getAPI() {
private void clear() {
gameEventManager.publish(new TiledMapEvents.BeforeUnloadEvent(api));
behaviorManager.remove(gameObjectUpdater);
gameWorld.clear();
for (String id : state.getLayerIds()) {
gameWorld.clearGroup(id);
renderManager.unregister(id);
}
state.clear();
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/de/bitbrain/braingdx/world/GameWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ public GameObject addObject(Object group, boolean lazy) {
return addObject(group, null, lazy);
}

/**
* Adds a new game object to the game world and provides it.
*
* @return newly created game object
*/
public GameObject addObject(Mutator<GameObject> mutator, boolean lazy) {
return addObject(DEFAULT_GROUP_ID, mutator, lazy);
}

/**
* Adds a new game object to the game world and provides it.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

package de.bitbrain.braingdx.tmx;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import de.bitbrain.braingdx.behavior.BehaviorManager;
Expand All @@ -34,7 +32,6 @@
import org.mockito.runners.MockitoJUnitRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -461,6 +458,24 @@ public void load_withSimple3x3Map_shouldNotOverrideExistingCollision() {
assertThat(tiledMapManager.getAPI().isCollision(0, 1, 0)).isTrue();
}

@Test
public void load_withSimple3x3Map_shouldOnlyUnloadRelevantObjects() {
TiledMap map = new MockTiledMapBuilder(2, 2, 1)
.addLayer(new MockTiledTileLayerBuilder()
.addCell(0, 0)
.addCell(0, 1, true)
.addCell(1, 0)
.addCell(1, 1).build())
.addLayer(new MockObjectLayerBuilder().addObject(0, 0, "player").build())
.build();
tiledMapManager.load(map, camera, TiledMapType.ORTHOGONAL);
world.update(0f);
GameObject remainer = world.addObject();
assertThat(world.size()).isEqualTo(4);
tiledMapManager.dispose();
assertThat(world.getObjects()).containsExactly(remainer);
}

/**
* Creates collisions on different layers
* <p>
Expand Down

0 comments on commit a966169

Please sign in to comment.