Example #1
0
  public GameContext getContext() {

    if (allContexts == null) {
      return mainContext;
    }

    synchronized (this) {
      if (allContexts == null) {
        return mainContext;
      }

      ThreadGroup currentThreadGroup = Thread.currentThread().getThreadGroup();
      for (int i = 0; i < allContexts.size(); i++) {
        GameContext context = (GameContext) allContexts.get(i);
        ThreadGroup contextThreadGroup = context.getThreadGroup();
        if (contextThreadGroup == currentThreadGroup
            || contextThreadGroup.parentOf(currentThreadGroup)) {
          return context;
        }
      }

      if (initContext != null && Thread.currentThread() == initContextThread) {
        return initContext;
      }

      return (GameContext) (allContexts.get(GameContext.nextContextID - 1));
    }
  }
Example #2
0
  public synchronized void unregisterApp(GameView app) {
    if (app == null || !isRegistered(app)) {
      return;
    }

    if (mainContext != null && mainContext.getView() == app) {
      mainContext = null;
    }

    if (allContexts != null) {
      for (int i = 0; i < allContexts.size(); i++) {
        GameContext context = (GameContext) allContexts.get(i);
        if (context.getView() == app) {
          allContexts.remove(i);
          break;
        }
      }

      if (mainContext == null) {
        mainContext = (GameContext) allContexts.get(0);
      }

      if (allContexts.size() == 1) {
        allContexts = null;
      }
    }

    if (getNumRegisteredApps() == 0) {
      timer.stop();
    }
  }
Example #3
0
  public void start() {

    // set up the console handler

    console.setHandler(new ConsoleHandler());

    // load the map

    map = MapLoader.loadMap("map_basic.txt");

    // grab some entities

    StaticEntity chip = new StaticEntity();
    chip.setPosition(new Coord(9, 5));
    chip.addTexture(GameContext.getTextureLoader().getTexture("gtest/resources/chipA.png"));
    chip.addTexture(GameContext.getTextureLoader().getTexture("gtest/resources/chipB.png"));
    entities.add(chip);

    // create the player
    player = new Player(new Coord(0, 12));
    player.setMapContext(map);
    entities.add(player);

    // set up camera
    GameContext.getCamera().setFollowing(player);

    // set up openGL

    GL11.glEnable(GL11.GL_TEXTURE_2D);
    GL11.glDisable(GL11.GL_DEPTH_TEST);
    GL11.glDisable(GL11.GL_LIGHTING);
    GL11.glEnable(GL11.GL_BLEND);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

    // game loop

    while (!Display.isCloseRequested()) {

      Date d = new Date();
      long startTime = d.getTime();

      draw();
      update();
      handleInput();

      d = new Date();
      long endTime = d.getTime();

      try {
        Thread.sleep(50 - (endTime - startTime));
      } catch (Exception e) {
      }

      if ((endTime - startTime) != 0) {
        GameContext.addToLog("fps", "" + 1000 / (endTime - startTime));
      }
    }

    Display.destroy();
  }
Example #4
0
 public static IHandler getSystemHandler() {
   GameContext context = INSTANCE.getContext();
   if (context != null) {
     return context.getView().getHandler();
   } else {
     return null;
   }
 }
Example #5
0
  private synchronized GameContext getAppContext(GameView app) {
    if (mainContext != null && mainContext.getView() == app) {
      return mainContext;
    }

    if (allContexts != null) {
      for (int i = 0; i < allContexts.size(); i++) {
        GameContext context = (GameContext) allContexts.get(i);
        if (context.getView() == app) {
          return context;
        }
      }
    }

    return null;
  }
Example #6
0
  @Override
  public void simpleInitApp() {
    flyCam.setEnabled(false);
    _gameContext = new GameContext(assetManager, rootNode, cam);
    _gameContext.setEnvironmentType(EnvironmentType.SimpleStarField);
    _gameContext.setPlayerType(ShipType.Voyager);
    _gameContext.setStatus(GameStatusType.Playing);

    inputManager.addMapping(InputMappings.left, new KeyTrigger(KeyInput.KEY_LEFT));
    inputManager.addMapping(InputMappings.right, new KeyTrigger(KeyInput.KEY_RIGHT));
    inputManager.addMapping(InputMappings.accelerate, new KeyTrigger(KeyInput.KEY_UP));
    inputManager.addMapping(InputMappings.decelerate, new KeyTrigger(KeyInput.KEY_DOWN));
    inputManager.addMapping(InputMappings.fire, new KeyTrigger(KeyInput.KEY_SPACE));

    inputManager.addListener(
        actionListener,
        InputMappings.left,
        InputMappings.right,
        InputMappings.accelerate,
        InputMappings.decelerate,
        InputMappings.fire);
  }
Example #7
0
    @Override
    public void handle(String message) {

      String[] tokens = message.split(" ");

      if (tokens[0].equals("load") && tokens.length == 2) {

        map = MapLoader.loadMap(tokens[1]);

      } else if (tokens[0].equals("scale") && tokens.length == 2) {

        scale = Integer.parseInt(tokens[1].trim());

      } else if (tokens[0].equals("debug") && tokens.length == 2) {

        GameContext.setDebugMode(tokens[1].equals("on"));

      } else if (tokens[0].equals("reset") && tokens.length == 1) {

        GameContext.getCamera().setPosition(new Coord(0, 0));
        GameContext.getCamera().setFollowing(player);
        editorMode = false;
        scale = 90;
        entities.remove(editorTag);
        editorTag = null;

      } else if (tokens[0].equals("editor") && tokens.length == 1) {

        GameContext.getCamera().getPosition().snap();
        editorTag = new EditorTagEntity(GameContext.getCamera().getPosition());
        entities.add(editorTag);
        GameContext.getCamera().setFollowing(editorTag);
        editorMode = true;

      } else if (tokens[0].equals("save") && tokens.length == 2) {

        MapLoader.saveMap(map, tokens[1].trim());

      } else if (tokens[0].equals("move") && tokens.length == 2) {

        player.move(Integer.parseInt(tokens[1]));
      }
    }
Example #8
0
  /** リザルト画面を生成する。 */
  public ResultScreen() {
    // スペースキー押下時にタイトル画面に切り替えるようにする。
    setOnKeyTyped(
        event -> {
          // KeyTyped イベントの場合は KeyCode を得られないので,Character で判定する。
          if (event.getCharacter().equals(" ")) {
            Main.changeScreen(0);
          }
        });
    setFocusTraversable(true);

    // ゲーム情報の最終値を取得する。
    long score = GameContext.getScore();
    long lifeBonus = GameContext.getLifeCount() * Configuration.SCORE_PER_LIFE;
    GameContext.addScore(lifeBonus);
    long totalScore = GameContext.getScore();

    // ランクを計算する。
    String rank;
    if (totalScore >= Configuration.SCORE_BORDER_OF_RANK_S) {
      rank = "S";
    } else if (totalScore >= Configuration.SCORE_BORDER_OF_RANK_A) {
      rank = "A";
    } else if (totalScore >= Configuration.SCORE_BORDER_OF_RANK_B) {
      rank = "B";
    } else if (totalScore >= Configuration.SCORE_BORDER_OF_RANK_C) {
      rank = "C";
    } else if (totalScore >= Configuration.SCORE_BORDER_OF_RANK_D) {
      rank = "D";
    } else {
      rank = "E";
    }

    // 画面に表示するテキストを生成する。
    Text resultCaptionText = createText("CONGRATULATION!!", 100, Color.GREENYELLOW);
    Text scoreCaptionText = createText("SCORE", 50, Color.GREENYELLOW);
    Text scoreText = createText(Long.toString(score), "monospace", 50, Color.GREENYELLOW);
    Text lifeBonusCaptionText = createText("LIFE BONUS", 50, Color.GREENYELLOW);
    Text lifeBonusText = createText(Long.toString(lifeBonus), "monospace", 50, Color.GREENYELLOW);
    Text totalScoreCaptionText = createText("TOTAL SCORE", 50, Color.GREENYELLOW);
    Text totalScoreText = createText(Long.toString(totalScore), "monospace", 50, Color.GREENYELLOW);
    Text rankCaptionText = createText("RANK", 50, Color.GREENYELLOW);
    Text rankText = createText(rank, 50, Color.GREENYELLOW);

    // 区切り線を生成する。
    VBox[] partitionPanes = new VBox[3];
    IntStream.range(0, partitionPanes.length)
        .forEach(
            i -> {
              partitionPanes[i] = new VBox();
              partitionPanes[i].setPrefHeight(Configuration.PARTITION_HEIGHT);
              partitionPanes[i].setStyle("-fx-background-color: greenyellow;");
            });

    // テキスト,および区切り線をグリッドペインに配置する。
    GridPane gridPane = new GridPane();
    GridPane.setHalignment(resultCaptionText, HPos.CENTER);
    GridPane.setHalignment(scoreText, HPos.RIGHT);
    GridPane.setHalignment(lifeBonusText, HPos.RIGHT);
    GridPane.setHalignment(totalScoreText, HPos.RIGHT);
    GridPane.setHalignment(rankText, HPos.CENTER);
    GridPane.setHgrow(scoreCaptionText, Priority.ALWAYS);
    gridPane.add(resultCaptionText, 0, 0, 2, 1);
    gridPane.add(partitionPanes[0], 0, 1, 2, 1);
    gridPane.add(scoreCaptionText, 0, 2);
    gridPane.add(scoreText, 1, 2);
    gridPane.add(lifeBonusCaptionText, 0, 3);
    gridPane.add(lifeBonusText, 1, 3);
    gridPane.add(partitionPanes[1], 0, 4, 2, 1);
    gridPane.add(totalScoreCaptionText, 0, 5);
    gridPane.add(totalScoreText, 1, 5);
    gridPane.add(rankCaptionText, 0, 6);
    gridPane.add(rankText, 1, 6);
    gridPane.add(partitionPanes[2], 0, 7, 2, 1);

    // グリッドペインをスタックペインに配置する。
    // こうすることで,グリッドペインが画面の中央に配置される。
    StackPane stackPane = new StackPane(new Group(gridPane));
    stackPane.setPrefSize(Configuration.SCREEN_WIDTH, Configuration.SCREEN_HEIGHT);
    stackPane.setStyle("-fx-background-color: black;");

    // 画面にスタックペインを配置する。
    getChildren().add(stackPane);
  }
Example #9
0
 @Override
 public void simpleUpdate(float tpf) {
   _gameContext.processUpdate(tpf);
 }
Example #10
0
 public void onAnalog(String name, float keyPressed, float tpf) {
   _gameContext.getUpdater().onAnalog(name, keyPressed, tpf);
 }
Example #11
0
  private void draw() {

    // Clear the screen and depth buffer
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();

    // set up GameContext.getCamera()
    GL11.glPushMatrix();

    GL11.glScalef(scale, scale, scale);
    GL11.glTranslatef(
        -GameContext.getCamera().getPosition().getx(),
        -GameContext.getCamera().getPosition().gety(),
        0);
    GL11.glTranslatef(4f, 3f, 0);

    map.draw();

    if (editorMode) {
      Quad q = new Quad(editorTag.getPosition(), map.getLookupTile(currentEditorTile).getTexture());
      GameContext.getPipe().addDrawable(q);
    }

    for (Entity ae : entities) {
      ae.draw();
    }

    GameContext.getPipe().renderContent();

    GL11.glPopMatrix();

    // overlay console text

    GL11.glPushMatrix();
    GL11.glScalef(2f, 2f, 2f);

    if (console.isEnabled()) {
      gtest.drawing.util.drawString(new Coord(0, 0), "> " + console.getText());
    }

    if (GameContext.isDebugMode()) {
      gtest.drawing.util.drawString(
          new Coord(0, 285), "tiles drawn " + GameContext.getFromLog("tilesLastDrawn"));
      gtest.drawing.util.drawString(
          new Coord(0, 275), "textures bound " + GameContext.getFromLog("textureBinds"));
      gtest.drawing.util.drawString(new Coord(0, 265), "FPS " + GameContext.getFromLog("fps"));
    }

    GL11.glPopMatrix();

    Display.update();

    // clean up

    GameContext.getPipe().clear();
    GameContext.addToLog("tilesLastDrawn", "0");
    GameContext.addToLog("textureBinds", "0");
  }