示例#1
0
  protected void update(float delta) {
    switch (state) {
      case INITIALIZING:
        break;
      case STARTING:
        break;
      case PLAYING:
        spawner.update(delta);
        for (int i = 0; i < conf.playersAlive.size(); i++) {
          PlayerFish player = conf.playersAlive.get(i);
          if (!player.isAlive()) {
            conf.playersAlive.remove(player);
            i--;
          }
        }

        if (conf.playersAlive.size() == 1 && conf.aiconf.getAIs().size() > 1) {
          final PlayerFish winner = conf.playersAlive.get(0);
          state = State.OVER;
          getGameContext().update(0f);
          getGameContext().setPaused(true);
          GlobalTween.add(
              CommonTweens.zoomAtGameObject(winner, getCamera(), 3.0f, 3.0f)
                  .setCallback(
                      new TweenCallback() {

                        @Override
                        public void onEvent(int type, BaseTween<?> source) {
                          if (type == TweenCallback.COMPLETE) {
                            winner.incrementScore();
                            winner.destroy();
                            conf.playersAlive.clear();
                            getGameContext().setPaused(false);
                            getGameContext().update(0f);
                            if (winner.getScore() >= conf.winLimit) {
                              game.setScreen(new WinnerScreen(game, winner.getName()));
                            } else {
                              game.setScreen(new PlayScreen(game, conf));
                            }
                          }
                        }
                      }));
        }

        break;
      case OVER:
        break;
      case WINNER:
        break;
      default:
        break;
    }
  }
示例#2
0
  @Override
  public void show() {
    state = State.INITIALIZING;
    setBackground(Assets.bg);
    getBackgroundBounds().x = -100;
    getBackgroundBounds().y = -100;
    getBackgroundBounds().width = EatFishAndAI.WIDTH + 200;
    getBackgroundBounds().height = EatFishAndAI.HEIGHT + 200;

    spawner = new DummySpawner();
    spawner.setGameContext(getGameContext());

    gameNameLayout = new GlyphLayout();
    gameNameLayout.setText(Assets.font30, conf.gamename);

    // Preset predator ai
    // int offset = 100;
    // getGameContext().spawn(
    // new PredatorFish(EatFishAndAI.WIDTH - offset,
    // EatFishAndAI.HEIGHT - offset));
    // getGameContext().spawn(
    // new PredatorFish(EatFishAndAI.WIDTH - offset, offset));
    // getGameContext().spawn(
    // new PredatorFish(offset, EatFishAndAI.HEIGHT - offset));
    // getGameContext().spawn(new PredatorFish(offset, offset));
    List<AIFactory<?>> factories = conf.aiconf.getAIs();
    int count = factories.size(), radius = 150;
    float angle = (float) (2 * Math.PI / count);
    float angleOffset = (float) (Math.PI / 4);
    float randomOffset = (float) ((int) (Math.random() * 4) * Math.PI / 2);
    System.out.println(randomOffset);
    boolean newGame = conf.players.isEmpty();
    for (int i = 0; i < conf.aiconf.getAIs().size(); i++) {
      PlayerFish player;
      if (newGame) {
        player =
            new PlayerFish(
                (float)
                    (centerX
                        - PlayerFish.WIDTH / 2
                        + Math.cos(i * angle + angleOffset + randomOffset) * radius),
                (float)
                    (centerY
                        - PlayerFish.HEIGHT / 2
                        + Math.sin(i * angle + angleOffset + randomOffset) * radius));
        conf.players.add(player);
      } else {
        player = conf.players.get(i);
        player.killAI();
        player.setX(
            (float)
                (centerX
                    - PlayerFish.WIDTH / 2
                    + Math.cos(i * angle + angleOffset + randomOffset) * radius));
        player.setY(
            (float)
                (centerY
                    - PlayerFish.HEIGHT / 2
                    + Math.sin(i * angle + angleOffset + randomOffset) * radius));
        player.setVelocityX(0);
        player.setVelocityY(0);
        player.setMaxSpeed(PlayerFish.MAX_SPEED);
        player.setScale(PlayerFish.STARTING_SCALE);
        player.setAlive(true);
      }

      getGameContext().spawn(player);
      player.setGameContext(getGameContext());
      player.attachAI(factories.get(i).newInstance());
      player.start();
    }

    conf.playersAlive = new ArrayList<>(conf.players);

    getGameContext().spawn(new RoundScore(conf, playerListX, playerListY));

    CustomTextButton stopButton =
        new CustomTextButton(EatFishAndAI.WIDTH - CustomTextButton.WIDTH, 0, "STOP");

    stopButton.setButtonListener(
        new ButtonListener() {

          @Override
          public void handle(ButtonAction ba) {
            if (ba.type == TYPE.RELEASE) {
              game.setScreen(new MenuScreen((EatFishAndAI) game));
            }
          }
        });

    CustomTextButton killButton =
        new CustomTextButton(EatFishAndAI.WIDTH - 2 * CustomTextButton.WIDTH, 0, "KILL");

    killButton.setButtonListener(
        new ButtonListener() {

          @Override
          public void handle(ButtonAction ba) {
            if (ba.type == TYPE.RELEASE) {

              PlayerFish biggest = conf.playersAlive.get(0);
              for (PlayerFish o : conf.playersAlive) {
                if (o.getScale() > biggest.getScale()) {
                  biggest = o;
                }
              }

              while (conf.playersAlive.size() > 1) {
                int randomIndex = (int) (Math.random() * conf.playersAlive.size());
                PlayerFish fish = conf.playersAlive.get(randomIndex);
                if (fish.equals(biggest)) {
                  continue;
                }
                conf.playersAlive.remove(fish);
                fish.destroy();
              }
            }
          }
        });

    getGameContext().spawn(killButton);
    getGameContext().spawn(stopButton);

    state = State.STARTING;

    Timeline all = Timeline.createSequence();
    for (PlayerFish player : conf.playersAlive) {
      all.push(CommonTweens.zoomAtGameObject(player, getCamera(), 3.0f, 1.0f));
    }
    all.push(CommonTweens.zoomAtPoint(centerX, centerY, getCamera(), 1.0f, 1.0f));
    all.setCallback(
        new TweenCallback() {

          @Override
          public void onEvent(int type, BaseTween<?> source) {
            if (type == TweenCallback.COMPLETE) {
              state = State.PLAYING;
              getGameContext().setPaused(false);
            }
          }
        });
    GlobalTween.add(all);

    getGameContext().spawn(new OverallScore(conf, EatFishAndAI.WIDTH - 300, EatFishAndAI.HEIGHT));

    getGameContext().update(1);
    getGameContext().setPaused(true);
  }