Hi
A question: How to remove all spatials from my scene when exiting to main menu?
This is my ModelState, however, when I exit to main menu it looks fine - but when I create a new game - all spatials from the last game are still present in the scene. Am I doing something wrong here:
[java]package alpineterra.appstates;
import alpineterra.IModelFactory;
import alpineterra.Main;
import alpineterra.components.ModelType;
import alpineterra.components.Position;
import alpineterra.components.TimeProvider;
import com.jme3.app.Application;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.simsilica.es.Entity;
import com.simsilica.es.EntityData;
import com.simsilica.es.EntityId;
import com.simsilica.es.EntitySet;
import com.simsilica.lemur.event.BaseAppState;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
-
Watches entities with Position and ModelType components
-
and creates/destroys Spatials as needed as well as moving
-
them to the appropriate locations.
-
Spatials are created with a ModelFactory callback object
-
that can be game specific.
-
@author Asser Fahrenholz
*/
public class ModelState extends BaseAppState {static Logger log = LoggerFactory.getLogger( ModelState.class );
private EntityData ed;
private EntitySet entities;
private TimeProvider time;
private Map<EntityId, Spatial> models = new HashMap<>();
private Node modelRoot;
private IModelFactory factory;public ModelState( TimeProvider time, IModelFactory factory ) {
this.time = time;
this.factory = factory;
}public Node getModelRoot() {
return modelRoot;
}public Spatial getSpatial( EntityId entity ) {
// Make sure we are up to date
refreshModels();
return models.get(entity);
}public Collection<Spatial> spatials() {
return models.values();
}protected Spatial createSpatial( Entity e ) {
return factory.createModel(e);
}protected void addModels( Set<Entity> set ) {
for( Entity e : set ) { // See if we already have one Spatial s = models.get(e.getId()); if( s != null ) { log.error("Model already exists for added entity:" + e); continue; } s = createSpatial(e); models.put(e.getId(), s); updateModelSpatial(e, s); modelRoot.attachChild(s); }
}
protected void removeModels( Set<Entity> set ) {
for( Entity e : set ) { Spatial s = models.remove(e.getId()); if( s == null ) { log.error("Model not found for removed entity:" + e); continue; } s.removeFromParent(); }
}
protected void updateModelSpatial( Entity e, Spatial s ) {
/Position p = e.get(Position.class);
InterpolationControl ic = s.getControl(InterpolationControl.class);
if( ic != null ) {
ic.setTarget(p.getLocation(), p.getFacing(), p.getChangeTime(), p.getTime());
} else {
s.setLocalTranslation(p.getLocation());
s.setLocalRotation(p.getFacing());
}/Position p = e.get(Position.class); s.setLocalTranslation(p.getLocation()); s.setLocalRotation(p.getFacing());
}
protected void updateModels( Set<Entity> set ) {
for( Entity e : set ) { Spatial s = models.get(e.getId()); if( s == null ) { log.error("Model not found for updated entity:" + e); continue; } updateModelSpatial(e, s); }
}
protected void refreshModels() {
if( entities.applyChanges() ) {
removeModels(entities.getRemovedEntities());
addModels(entities.getAddedEntities());
updateModels(entities.getChangedEntities());
}
}@Override
protected void initialize( Application app ) {factory.setState(this); // Grab the set of entities we are interested in ed = getState(EntityDataState.class).getEntityData(); entities = ed.getEntities(Position.class, ModelType.class); // Create a root for all of the models we create modelRoot = new Node("Model Root");
}
@Override
protected void cleanup( Application app ) {
entities.applyChanges();
this.removeModels(entities);
modelRoot.removeFromParent();
// Release the entity set we grabbed previously
entities.release();
entities = null;
}@Override
protected void enable() {
((Main)getApplication()).getRootNode().attachChild(modelRoot);entities.applyChanges(); addModels(entities);
}
@Override
public void update( float tpf ) {
refreshModels();
}@Override
protected void disable() {
entities.applyChanges();
removeModels(entities);
modelRoot.removeFromParent();
}
}
[/java]