Example #1
0
  // Controls movement of NPCs towards their set location
  public void NPCMove(Stage s, ArrayList<NPC> npc, FireControl fc) throws IOException {

    // For each NPC
    for (NPC n : npc) {

      // Set Random Movement - when no target is in sight
      if (n.getGotox() == 0 && n.getGotoy() == 0) {

        if (n.getDirection() == 0) {
          if (PlayerInput.checkUp(n, s) == true) {

            n.findwalk();
            n.setY(n.getY() + npcmove);
            n.setMoveticks(n.getMoveticks() - 1);
          } else n.setDirection(rand.nextInt(4));
        }

        if (n.getDirection() == 1) {
          if (PlayerInput.checkDown(n, s) == true) {
            n.findwalk();
            n.setY(n.getY() - npcmove);
            n.setMoveticks(n.getMoveticks() - 1);
          } else n.setDirection(rand.nextInt(4));
        }

        if (n.getDirection() == 2) {
          if (PlayerInput.checkLeft(n, s) == true) {
            n.findwalk();
            n.setX(n.getX() - npcmove);
            n.setMoveticks(n.getMoveticks() - 1);
          } else n.setDirection(rand.nextInt(4));
        }

        if (n.getDirection() == 3) {
          if (PlayerInput.checkRight(n, s) == true) {
            n.findwalk();
            n.setX(n.getX() + npcmove);
            n.setMoveticks(n.getMoveticks() - 1);
          } else n.setDirection(rand.nextInt(4));
        }
      }

      // Otherwise move towards player and fire
      else {

        if (n.calcDistance(n.getX() - npcmove, n.getGotox()) < n.getDistancex()
            && PlayerInput.checkLeft(n, s) == true) {
          n.findwalk();
          NPCFire(n, fc);
          n.setX(n.getX() - npcmove);
        }
        if (n.calcDistance(n.getX() + npcmove, n.getGotox()) < n.getDistancex()
            && PlayerInput.checkRight(n, s) == true) {

          n.findwalk();
          NPCFire(n, fc);
          n.setX(n.getX() + npcmove);
        }

        if (n.calcDistance(n.getY() + npcmove, n.getGotoy()) < n.getDistancey()
            && PlayerInput.checkUp(n, s) == true) {
          n.findwalk();
          NPCFire(n, fc);
          n.setY(n.getY() + npcmove);
        }

        if (n.calcDistance(n.getY() - npcmove, n.getGotoy()) < n.getDistancey()
            && PlayerInput.checkDown(n, s) == true) {

          n.findwalk();
          NPCFire(n, fc);
          n.setY(n.getY() - npcmove);
        }
      }
    }
  }
Example #2
0
  public void NPCFire(NPC n, FireControl fc) throws IOException {

    // Is player above the npc
    if (n.getY() <= n.getGotoy()
        && ((n.getX() + n.getWidth() / 2 <= n.getGotox() + 25)
            && n.getX() + n.getWidth() / 2 >= n.getGotox()
            && n.getFirer() < n.getDistancey())) {

      // System.out.println("Shooting!");
      fc.addShot(
          new Bullet(
              n.getX() + n.getWidth() / 2,
              n.getY() + n.getHeight() + 4,
              n.getGun().getShotwidth(),
              n.getGun().getShotwidth(),
              0,
              n.getGun().getShotspeed(),
              n,
              n.getGun().getRicochet()));
    }

    if (n.getY() >= n.getGotoy()
        && ((n.getX() + n.getWidth() / 2 <= n.getGotox() + 25)
            && n.getX() + n.getWidth() / 2 >= n.getGotox()
            && n.getFirer() < n.getDistancey())) {

      // System.out.println("Shooting!");
      fc.addShot(
          new Bullet(
              n.getX() + n.getWidth() / 2,
              n.getY() - 4,
              n.getGun().getShotwidth(),
              n.getGun().getShotwidth(),
              1,
              n.getGun().getShotspeed(),
              n,
              n.getGun().getRicochet()));
    }

    if ((n.getY() + n.getHeight() / 2 >= n.getGotoy())
        && n.getY() + n.getHeight() / 2 <= n.getGotoy() + 25
        && ((n.getX() >= n.getGotox()) && n.getFirer() <= n.getDistancex())) {

      // System.out.println("Shooting!");
      fc.addShot(
          new Bullet(
              n.getX() - 4,
              n.getY() + n.getHeight() / 2,
              n.getGun().getShotwidth(),
              n.getGun().getShotwidth(),
              2,
              n.getGun().getShotwidth(),
              n,
              n.getGun().getRicochet()));
    }

    if ((n.getY() + n.getHeight() / 2 >= n.getGotoy())
        && n.getY() + n.getHeight() / 2 <= n.getGotoy() + 25
        && ((n.getX() < n.getGotox()) && n.getFirer() <= n.getDistancex())) {

      // System.out.println("Shooting!");
      fc.addShot(
          new Bullet(
              n.getX() + n.getWidth() + 4,
              n.getY() + n.getHeight() / 2,
              n.getGun().getShotwidth(),
              n.getGun().getShotwidth(),
              3,
              n.getGun().getShotspeed(),
              n,
              n.getGun().getRicochet()));
    }
  }