public void deactivate() {
   try {
     _entityManager.save(
         new File(
             Terasology.getInstance().getWorldSavePath(getActiveWorldProvider().getTitle()),
             ENTITY_DATA_FILE),
         EntityManager.SaveFormat.Binary);
   } catch (IOException e) {
     _logger.log(Level.SEVERE, "Failed to save entities", e);
   }
   dispose();
   _entityManager.clear();
 }
 private void loadPrefabs() {
   EntityPersister persister = _entityManager.getPersister();
   for (String prefabURI : AssetManager.list("prefab")) {
     _logger.info("Loading prefab " + prefabURI);
     try {
       if (!prefabURI.endsWith(".prefab")) {
         continue;
       }
       BufferedReader reader =
           new BufferedReader(new InputStreamReader(AssetManager.assetStream(prefabURI)));
       EntityData.Prefab prefabData = EntityDataJSONFormat.readPrefab(reader);
       if (prefabData != null) {
         persister.deserializePrefab(prefabData);
       }
     } catch (IOException e) {
       _logger.log(Level.WARNING, "Failed to load prefab '" + prefabURI + "'", e);
     }
   }
 }
  public void update(float delta) {
    /* GUI */
    updateUserInterface();

    for (UpdateSubscriberSystem updater : _componentSystemManager.iterateUpdateSubscribers()) {
      PerformanceMonitor.startActivity(updater.getClass().getSimpleName());
      updater.update(delta);
    }

    if (_worldRenderer != null && shouldUpdateWorld()) _worldRenderer.update(delta);

    if (!screenHasFocus()) _localPlayerSys.updateInput();

    if (screenHasFocus() || !shouldUpdateWorld()) {
      if (Mouse.isGrabbed()) {
        Mouse.setGrabbed(false);
        Mouse.setCursorPosition(Display.getWidth() / 2, Display.getHeight() / 2);
      }
    } else {
      if (!Mouse.isGrabbed()) Mouse.setGrabbed(true);
    }

    // TODO: This seems a little off - plus is more of a UI than single player game state concern.
    // Move somewhere
    // more appropriate?
    boolean dead = true;
    for (EntityRef entity : _entityManager.iteratorEntities(LocalPlayerComponent.class)) {
      dead = entity.getComponent(LocalPlayerComponent.class).isDead;
    }
    if (dead) {
      _statusScreen.setVisible(true);
      _statusScreen.updateStatus("Sorry! Seems like you have died. :-(");
    } else {
      _statusScreen.setVisible(false);
    }
  }
  /** Init. a new random world. */
  public void initWorld(String title, String seed) {
    final FastRandom random = new FastRandom();

    // Get rid of the old world
    if (_worldRenderer != null) {
      _worldRenderer.dispose();
      _worldRenderer = null;
    }

    if (seed == null) {
      seed = random.randomCharacterString(16);
    } else if (seed.isEmpty()) {
      seed = random.randomCharacterString(16);
    }

    Terasology.getInstance()
        .getLogger()
        .log(Level.INFO, "Creating new World with seed \"{0}\"", seed);

    // Init. a new world
    _worldRenderer = new WorldRenderer(title, seed, _entityManager, _localPlayerSys);

    File entityDataFile =
        new File(Terasology.getInstance().getWorldSavePath(title), ENTITY_DATA_FILE);
    _entityManager.clear();
    if (entityDataFile.exists()) {
      try {
        _entityManager.load(entityDataFile, EntityManager.SaveFormat.Binary);
      } catch (IOException e) {
        _logger.log(Level.SEVERE, "Failed to load entity data", e);
      }
    }

    LocalPlayer localPlayer = null;
    Iterator<EntityRef> iterator =
        _entityManager.iteratorEntities(LocalPlayerComponent.class).iterator();
    if (iterator.hasNext()) {
      localPlayer = new LocalPlayer(iterator.next());
    } else {
      PlayerFactory playerFactory = new PlayerFactory(_entityManager);
      localPlayer =
          new LocalPlayer(
              playerFactory.newInstance(
                  new Vector3f(_worldRenderer.getWorldProvider().nextSpawningPoint())));
    }
    _worldRenderer.setPlayer(localPlayer);

    // Create the first Portal if it doesn't exist yet
    _worldRenderer.initPortal();

    fastForwardWorld();
    CoreRegistry.put(WorldRenderer.class, _worldRenderer);
    CoreRegistry.put(IWorldProvider.class, _worldRenderer.getWorldProvider());
    CoreRegistry.put(LocalPlayer.class, _worldRenderer.getPlayer());
    CoreRegistry.put(Camera.class, _worldRenderer.getActiveCamera());
    CoreRegistry.put(BulletPhysicsRenderer.class, _worldRenderer.getBulletRenderer());

    for (ComponentSystem system : _componentSystemManager.iterateAll()) {
      system.initialise();
    }
  }
  public void init() {
    _hud = new UIHeadsUpDisplay();
    _hud.setVisible(true);

    _pauseMenu = new UIPauseMenu();
    _loadingScreen = new UILoadingScreen();
    _statusScreen = new UIStatusScreen();
    _inventoryScreen = new UIInventoryScreen();
    _metrics = new UIMetrics();

    _metrics.setVisible(true);

    _guiScreens.add(_metrics);
    _guiScreens.add(_hud);
    _guiScreens.add(_pauseMenu);
    _guiScreens.add(_loadingScreen);
    _guiScreens.add(_inventoryScreen);
    _guiScreens.add(_statusScreen);

    PojoEntityManager entityManager = new PojoEntityManager();

    entityManager.registerTypeHandler(BlockFamily.class, new BlockFamilyTypeHandler());
    entityManager.registerTypeHandler(Color4f.class, new Color4fTypeHandler());
    entityManager.registerTypeHandler(Quat4f.class, new Quat4fTypeHandler());
    entityManager.registerTypeHandler(
        Sound.class, new SoundTypeHandler(AudioManager.getInstance()));
    entityManager.registerTypeHandler(Vector3f.class, new Vector3fTypeHandler());
    entityManager.registerTypeHandler(Vector2f.class, new Vector2fTypeHandler());
    entityManager.registerTypeHandler(Vector3i.class, new Vector3iTypeHandler());

    // TODO: Use reflection pending mod support
    entityManager.registerComponentClass(ExplosionActionComponent.class);
    entityManager.registerComponentClass(PlaySoundActionComponent.class);
    entityManager.registerComponentClass(TunnelActionComponent.class);
    entityManager.registerComponentClass(AABBCollisionComponent.class);
    entityManager.registerComponentClass(BlockComponent.class);
    entityManager.registerComponentClass(BlockItemComponent.class);
    entityManager.registerComponentClass(BlockParticleEffectComponent.class);
    entityManager.registerComponentClass(CameraComponent.class);
    entityManager.registerComponentClass(CharacterMovementComponent.class);
    entityManager.registerComponentClass(CharacterSoundComponent.class);
    entityManager.registerComponentClass(HealthComponent.class);
    entityManager.registerComponentClass(InventoryComponent.class);
    entityManager.registerComponentClass(ItemComponent.class);
    entityManager.registerComponentClass(LightComponent.class);
    entityManager.registerComponentClass(LocalPlayerComponent.class);
    entityManager.registerComponentClass(LocationComponent.class);
    entityManager.registerComponentClass(MeshComponent.class);
    entityManager.registerComponentClass(PlayerComponent.class);
    entityManager.registerComponentClass(SimpleAIComponent.class);
    entityManager.registerComponentClass(AccessInventoryActionComponent.class);
    _entityManager = entityManager;

    _entityManager.setEventSystem(new PojoEventSystem(_entityManager));
    CoreRegistry.put(EntityManager.class, _entityManager);
    _componentSystemManager = new ComponentSystemManager();
    CoreRegistry.put(ComponentSystemManager.class, _componentSystemManager);

    PrefabManager prefabManager = new PojoPrefabManager();
    CoreRegistry.put(PrefabManager.class, prefabManager);
    entityManager.setPrefabManager(prefabManager);

    loadPrefabs();

    _componentSystemManager.register(new BlockEntityRegistry());
    _componentSystemManager.register(new CharacterMovementSystem());
    _componentSystemManager.register(new SimpleAISystem());
    _componentSystemManager.register(new ItemSystem());
    _componentSystemManager.register(new CharacterSoundSystem());
    _localPlayerSys = new LocalPlayerSystem();
    _componentSystemManager.register(_localPlayerSys);
    _componentSystemManager.register(new FirstPersonRenderer());
    _componentSystemManager.register(new HealthSystem());
    _componentSystemManager.register(new BlockEntitySystem());
    _componentSystemManager.register(new BlockParticleEmitterSystem());
    _componentSystemManager.register(new BlockDamageRenderer());
    _componentSystemManager.register(new InventorySystem());
    _componentSystemManager.register(new MeshRenderer());
    _componentSystemManager.register(new ExplosionAction());
    _componentSystemManager.register(new PlaySoundAction());
    _componentSystemManager.register(new TunnelAction());
    _componentSystemManager.register(new AccessInventoryAction());
  }