Пример #1
0
  public GameState() {
    super("Game State");

    // Init the players and establish player relations

    myPlayer = new HumanPlayer("Human");

    ai1 = new AIPlayer("foo 1.0");
    ai2 = new AIPlayer("foo 2.0");
    ai3 = new AIPlayer("foo 3.0");
    ai4 = new AIPlayer("foo 4.0");

    myPlayer.addEnemies(ai1, ai2, ai3, ai4);
    ai1.addEnemies(ai2, ai3, ai4);
    ai2.addEnemies(ai1, ai3, ai4);
    ai3.addEnemies(ai1, ai2, ai4);
    ai4.addEnemies(ai1, ai2, ai3);

    ai1.addEnemy(myPlayer);

    activePlayers = new LinkedList<Player>();
    defeatedPlayers = new ArrayList<Player>();

    activePlayers.add(myPlayer);
    activePlayers.add(ai1);
    activePlayers.add(ai2);
    activePlayers.add(ai3);
    activePlayers.add(ai4);

    // Set the movement margins
    GamePanel panel = GamePanel.getInstance();
    int panelWidth = panel.getWidth();
    int panelHeight = panel.getHeight();

    // Prepare the HUD rectangle
    hudPanel.setBounds(
        0, panelHeight - Settings.MINI_MAP_HEIGHT, Settings.FIELD_WIDTH, panelHeight);
    miniMapRect.setBounds(
        panelWidth - Settings.MINI_MAP_WIDTH,
        panelHeight - Settings.MINI_MAP_HEIGHT,
        Settings.MINI_MAP_WIDTH,
        Settings.MINI_MAP_HEIGHT);

    // Init the control groups, and link them to my player
    ControlGroup.initGroups(this, myPlayer);

    // Initialize the grid
    int width = Settings.FIELD_WIDTH;
    int height = Settings.FIELD_HEIGHT;
    int numRows = 10;
    int numCols = 10;
    grid = Grid.initGrid(width, height, numRows, numCols);

    // Generate the images
    initImages(grid);

    /*
     *  Create the initial units
     *
     *  Each player starts with 1 hive and 10 larva
     *  The hives are evenly spaced around the circumference of a circle centred in the field centre
     */

    boolean createInitialUnits = true;

    if (createInitialUnits) {

      int numStartingLarvae = 10;

      float angle = 0;
      float deltaAngle = 2 * (float) Math.PI / activePlayers.size();

      int centreX = Settings.FIELD_WIDTH / 2;
      int centreY = Settings.FIELD_HEIGHT / 2;

      float radius = Settings.FIELD_WIDTH / 2 - 100;

      for (Player p : activePlayers) {

        int x = (int) (Math.cos(angle) * radius + centreX);
        int y = (int) (Math.sin(angle) * radius + centreY);

        p.spawnHive(x, y, true);

        for (int i = 0; i < numStartingLarvae; i++) {
          p.spawnLarvae(x + i, y + i);
        }

        angle += deltaAngle;
      }
    }
  }
Пример #2
0
  public void keyPressed(KeyEvent e) {

    int keyCode = e.getKeyCode();
    KeyTracker.press(keyCode);

    // Check for control group access
    if (KeyEvent.VK_0 <= keyCode && keyCode <= KeyEvent.VK_9) {
      int index = keyCode - KeyEvent.VK_0;
      ControlGroup.accessGroup(index);
    }

    switch (keyCode) {

        // For debugging:

      case KeyEvent.VK_F:
        if (KeyTracker.isPressed(KeyEvent.VK_SHIFT)) {

          myPlayer.spawnHive(mouseGlobal.x(), mouseGlobal.y(), false);
        } else {
          myPlayer.spawnLarvae(mouseGlobal.x(), mouseGlobal.y());
        }
        break;

      case KeyEvent.VK_E:
        if (KeyTracker.isPressed(KeyEvent.VK_SHIFT)) {

          ai1.spawnHive(mouseGlobal.x(), mouseGlobal.y(), false);
        } else {
          ai1.spawnLarvae(mouseGlobal.x(), mouseGlobal.y());
        }
        break;

        // For actual use
      case KeyEvent.VK_A:
        myPlayer.selectMyEntities();
        break;

      case KeyEvent.VK_D:
        myPlayer.deselectMyEntities();
        break;

      case KeyEvent.VK_H:
        if (myPlayer.getNumLarvaeSelected() >= Pod.SIZE) {

          placingHive = !placingHive;

          // TODO
          // placeHive = !placeHive;
          // Show a mock hive to allow the player to place it somewhere
          // The colour of the mock hive indicates whether or not it is in a valid location
          // Run those larvae towards the mock hive location
          // once they all get there, create a hive at the location (if it is still a valid
          // location)
        }

        break;

      case KeyEvent.VK_ESCAPE:

        // TODO: use escape to escape from hive creation, or other actions

        // NB: returning 0 indicates a normal exit (any other number indicates an error)
        GamePanel.getInstance().changeToScreen(new MenuState());
        break;

      default:
        break;
    }
  }