예제 #1
0
  /** Spawns an entity into the world. Fires off a cancellable EntitySpawnEvent */
  public void spawnEntity(Entity e, int entityID) {
    if (e.isSpawned()) {
      throw new IllegalArgumentException("Cannot spawn an entity that is already spawned!");
    }

    SpoutRegion region = (SpoutRegion) e.getRegion();
    if (region == null) {
      throw new IllegalStateException("Cannot spawn an entity that has a null region!");
    }
    if (region.getEntityManager().isSpawnable((SpoutEntity) e)) {
      if (entityID > -1) {
        if (getEngine().getPlatform() == Platform.CLIENT) {
          ((SpoutEntity) e).setId(entityID);
        } else {
          throw new IllegalArgumentException("Can not set entity id's manually");
        }
      }
      EntitySpawnEvent event =
          getEngine()
              .getEventManager()
              .callEvent(new EntitySpawnEvent(e, e.getScene().getPosition()));
      if (event.isCancelled()) {
        return;
      }
      region.getEntityManager().addEntity((SpoutEntity) e);
      // Alert world components that an entity entered
      for (Component component : values()) {
        if (component instanceof WorldComponent) {
          ((WorldComponent) component).onSpawn(event);
        }
      }
      // Alert entity components that their owner spawned
      for (Component component : e.values()) {
        if (component instanceof EntityComponent) {
          ((EntityComponent) component).onSpawned(event);
        }
      }
    } else {
      throw new IllegalStateException("Cannot spawn an entity that already has an id!");
    }
  }