/** remove the given entity */
  public Entity remove(Entity entity) {
    if (!(this.contains(entity))) {
      Logger.get().log(Level.WARNING, "Tried to remove an entity which wasnt in the world");
      return (null);
    }

    // remove from global list
    this._entities.remove(entity.getWorldID());

    // remove from type list
    ArrayList<Entity> type_list = this.getEntitiesByType(entity);
    if (type_list != null) {
      type_list.remove(entity);
      if (type_list.size() == 0) {
        this._entities_by_type.remove(entity.getClass());
      }
    }

    // remove from model list
    Model model = this.getEntityModel(entity);
    ArrayList<Entity> model_list = this.getEntitiesByModel(model);
    if (model_list != null) {
      model_list.remove(entity);
      if (model_list.size() == 0) {
        this._entities_by_model.remove(model);
      }
    }

    return (entity);
  }
  /** add an entity to the storage */
  public Entity add(Entity entity) {

    if (this.contains(entity)) {
      return (entity);
    }

    // generate entity unique id
    Integer id = this.generateNextEntityID(entity);

    // prepare the entity
    entity.setWorldID(id);

    // add it to the list
    this._entities.put(id, entity);

    // spawn the entity
    entity.setWorld(this.getWorld());
    entity.onSpawn(this.getWorld());

    // add it to the type list
    this.addEntityToTypeList(entity);

    // add it to the model list
    this.addEntityToModelList(entity);

    // invoke events
    this.invokeEvent(new EventEntitySpawn(entity));

    return (entity);
  }
 private void addEntityToTypeList(Entity entity) {
   ArrayList<Entity> type_list = this.getEntitiesByType(entity.getClass());
   if (type_list == null) {
     type_list = new ArrayList<Entity>(1);
     this._entities_by_type.put(entity.getClass(), type_list);
   }
   type_list.add(entity);
 }
  /** generate unique id for the given entity (assuming the entity isnt already stored */
  private Integer generateNextEntityID(Entity entity) {

    // get the entity id if it already has one
    if (entity.getWorldID() != Entity.DEFAULT_WORLD_ID && !(this.contains(entity.getWorldID()))) {
      return (entity.getWorldID());
    }

    // else generate one
    Integer id = entity.hashCode();
    while (this.contains(id)) {
      ++id;
    }
    return (id);
  }
  @Override
  public void onUpdate(Entity entity) {
    this._move.set(entity.getViewVector());
    this._move.y = 0;
    this._move.normalise();
    this._move.scale(entity.getSpeed());
    entity.move(this._move);

    if (entity instanceof EntityModeled) {
      EntityModeled model = (EntityModeled) entity;
      ModelInstance instance = model.getModelInstance();
      if (instance != null && !instance.isPlayingAnyAnimations()) {
        instance.getAnimationInstance(0).startPlay();
      }
    }
  }
  /** remove every entities which uses the given model (can be null) */
  public Collection<Entity> removeEntitiesByModel(Model model) {

    ArrayList<Entity> entities = this.getEntitiesByModel(model);

    if (entities == null || entities.size() == 0) {
      Logger.get()
          .log(
              Logger.Level.DEBUG,
              "Tried to remove entity with model: "
                  + model
                  + " but no entities with this model where found.");
      return (null);
    }

    for (Entity entity : entities) {
      this._entities.remove(entity.getWorldID());
      this._entities_by_type.remove(entity.getClass());
    }

    this._entities_by_model.remove(model);

    return (entities);
  }
 public ArrayList<Entity> getEntitiesByType(Entity entity) {
   return (this.getEntitiesByType(entity.getClass()));
 }
 public Entity add(Entity entity, float x, float y, float z) {
   entity.setPosition(x, y, z);
   return (this.add(entity));
 }