@Override
  public void performSwitch() {
    // get Game instance and the player
    Game game = Game.getInstance();
    MovableModel<?> player = game.getPlayerModel();

    // load level
    Level level;
    LevelAppState state = null;
    if (targetLevel == null) {
      level = game.getCurrentLevel();
    } else {
      Game.LevelInfo info = game.findLevel(targetLevel);
      level = game.loadLevel(info);
      state = new LevelAppState(level);
    }
    if (level == null) {
      LOG.log(java.util.logging.Level.SEVERE, "unable to load level {0}", targetLevel);
      return;
    }
    LOG.log(java.util.logging.Level.INFO, "level {0} loaded", targetLevel);

    // find target map switch
    MapSwitchModel target = null;
    for (BaseModel<?> m : level.getObjects().getModels()) {
      if (m instanceof MapSwitchModel && targetEntry.equals(m.getName())) {
        target = (MapSwitchModel) m;
        break;
      }
    }
    if (target == null) {
      LOG.log(java.util.logging.Level.SEVERE, "unable to find target entry {0}", targetEntry);
      return;
    }

    // collect players
    final CharacterModel<?> oldPlayer = game.getCurrentLevel().getPlayer();
    final CharacterModel<?> newPlayer = level.getPlayer();
    assert (oldPlayer != null);
    assert (newPlayer != null);

    // switch to the new level
    //		game.switchLevel(level);
    if (state != null) {
      HashMap<Object, Object> properties = new HashMap<Object, Object>();
      properties.put(KEY_SOURCE_SWITCH, this);
      properties.put(KEY_TARGET_SWITCH, target);
      // add hook that moves the player to the target switch
      final MapSwitchModel target2 = target;
      properties.put(
          LevelAppState.KEY_ACTIVATION_HOOKS,
          Collections.singletonList(
              new LevelAppState.ActivationHook() {
                @Override
                public void onActivated(
                    Map<Object, Object> properties,
                    ExtAppState lastState,
                    LevelAppState currentLevel) {
                  target2.entry(oldPlayer, newPlayer, AbstractMapSwitchModel.this);
                }
              }));
      // Call next level
      //			Game.getInstance().getApplication().getStateManager().callNext(state, properties);
      FadeAppState fade =
          new FadeAppState(state, FadeAppState.FADE_OUT_DOOR, FadeAppState.FADE_IN_DOOR);
      Game.getInstance().getApplication().getStateManager().pushStack(fade, properties);
    } else {
      // move player to the target switch
      target.entry(oldPlayer, newPlayer, this);
    }

    // done
    LOG.log(
        java.util.logging.Level.INFO,
        "player switched to {0} in {1}",
        new Object[] {targetEntry, targetLevel});
  }
Exemplo n.º 2
0
  @Override
  public void initApp() {
    //		addDefaultLight();
    addDebugActions();

    // init game
    GameTestImpl game = (GameTestImpl) Game.getInstance();
    game.setApplication(this);

    // create level
    Rect2i bounds = new Rect2i(-20, -20, 20, 20);
    Level level =
        TestUtils.createLevel(
            bounds,
            TestUtils.createRandomFlatDrawingMap(
                bounds.getWidth(),
                bounds.getHeight(),
                TestUtils.TEST_TILES_3,
                TestUtils.TEST_TILES_COUNT_X_3,
                TestUtils.TEST_TILES_COUNT_Y_3,
                new Point2i(0, 7),
                new Point2i(15, 7),
                new Point2i(16, 7),
                new Point2i(17, 7),
                new Point2i(18, 7),
                new Point2i(19, 7),
                new Point2i(20, 7),
                new Point2i(21, 7),
                new Point2i(22, 7)),
            TestUtils.createFlatMovingMap(bounds.getWidth(), bounds.getHeight()),
            MVCContainer.create(),
            MVCContainer.create());

    LevelAppState levelAppState = new LevelAppState(level);
    stateManager.callNext(levelAppState, new HashMap<>());

    // create player at the center
    //		createPlayer(level, 0, 0, 0);
    createAnimatedPlayer(level, 0, 0, 0, SINBAD);
    game.setPlayer(playerModel);

    // create positional audio view
    AudioView.Positional audioView = new JMEAudioView();
    ((JMEAudioView) audioView).setBufferMode(JMEAudioView.BufferMode.BUFFERED);
    ((JMEAudioView) audioView).setAudioFile(POINT_AUDIO_FILE);
    audioView.setPositional(true);
    audioView.setPosition(5, 7, 0);
    audioView.setLooping(true);
    audioView.setRefDistance(0.02f);
    audioView.setVolume(2);
    audioView.play();
    level.getObjects().addView(audioView);

    // create background audio view
    AudioView background =
        new JMEAudioView(BACKGROUND_AUDIO_FILE, JMEAudioView.BufferMode.BUFFERED);
    background.setLooping(true);
    background.setVolume(0.05f);
    background.play();
    level.getObjects().addView(background);

    // add light
    LightView.Ambient ambient = new JMELightView.Ambient();
    ambient.setColor(new ColorRGBA(0.2f, 0.2f, 0.2f, 1));
    level.getObjects().addView(ambient);
    LightView.Directional directional = new JMELightView.Directional();
    directional.setColor(new ColorRGBA(0.5f, 0.5f, 0.4f, 1));
    directional.setDirection(new Vector3f(0.0f, -0.5f, -0.4f).normalizeLocal());
    level.getObjects().addView(directional);
    // visualizate point audio
    LightView.Point point3 = new JMELightView.Point();
    point3.setColor(new ColorRGBA(5, 5, 5, 1));
    point3.setPosition(5, 7, 6);
    point3.setRadius(10);
    point3.setShadowEnabled(false);
    level.getObjects().addView(point3);

    // add FXAA-view
    JMEFXAAView fxaa = new JMEFXAAView();
    level.getObjects().addView(fxaa);
  }