コード例 #1
0
 public void command(Game game, Scanner sc, String... args) throws IllegalArgumentException {
   Command cmd = game.getCommand(args[0].toLowerCase());
   if (cmd == null) {
     throw new IllegalArgumentException("Invalid command");
   }
   float time = cmd.onCalled(this, game, sc, args);
   Location loc = this.location;
   for (Animal a : loc.getPredators().keySet()) {
     if (Main.rand.nextFloat() < loc.getPredators().get(a)) {
       System.out.printf("A %s ambushes you!\n", a.getName(), args[0]);
       game.time += this.fight(sc, game, a, false);
       break;
     }
   }
   game.time += time;
   while (game.time >= 24) {
     game.day += 1;
     game.time -= 24;
   }
   hunger = cap(hunger - 2 * time, 0, MAXHUNGER);
   thirst = cap(thirst - 2 * time, 0, MAXTHIRST);
   if (hunger < 5) {
     hp -= (5 - hunger) * 5 * time;
   } else if (hunger > 20) {
     hp += 10;
     if (hp > MAXHEALTH) {
       hp = MAXHEALTH;
     }
   }
   if (thirst == 0) {
     hp -= 50 * time;
   }
 }
コード例 #2
0
 /**
  * Fight mode
  *
  * @param animal The target
  * @param isHiding Is sneak attacking
  * @return How long the fight lasts
  */
 public float fight(Scanner sc, Game game, Animal animal, boolean isHiding) {
   float timeElapsed = 0f, enemyHp = animal.getHealth();
   Item weapon;
   List<Item> weapons = new ArrayList<Item>();
   for (Item i : this.inventory) {
     if (i.damage > 0) {
       weapons.add(i);
     }
   }
   if (weapons.size() == 1) {
     weapon = weapons.get(0);
     System.out.printf("You use your %s.\n", weapon.name);
   } else if (weapons.size() == 0) {
     System.out.println("You have no weapons, so you use your fists.");
     weapon = Game.iFists;
   } else {
     System.out.println("Which weapon do you use?");
     while (true) {
       Item w = game.getItem(sc.nextLine());
       if (w != null) {
         weapon = w;
         break;
       } else {
         System.out.println("Invalid item, try again.");
       }
     }
   }
   System.out.println("");
   while (true) {
     /*
      * hiding, ambush animals
      * attack
      * run
      */
     int choice = Main.choice(sc, "What do you do?", "You... ", "Attack", "Run");
     if (choice == 1) {
       System.out.printf("You attack the %s for %s damage.\n", animal.getName(), weapon.damage);
       enemyHp -= weapon.damage;
     } else if (choice == 2) {
       System.out.printf("You run from the %s.\n", animal.getName());
       break;
     }
     // enemy turn
     if (animal.getDamage() > 0) {
       System.out.printf(
           "The %s damages you for %s damage.\n", animal.getName(), animal.getDamage());
       this.hp -= animal.getDamage();
     }
     if (this.hp <= 0) {
       System.out.printf("The %s defeated you in combat.\n", animal.getName());
       break;
     }
     if (enemyHp <= 0) {
       Map<Item, Integer> loot = animal.getLoot();
       try {
         for (Item i : loot.keySet()) {
           this.invAdd(i, loot.get(i));
         }
         System.out.printf(
             "You killed the %s, salvaging %s from its body.\n",
             animal.getName(), Main.listItems(loot));
       } catch (InvOutOfVolumeException | InvOutOfWeightException e) {
         System.out.printf(
             "You killed the %s, but could not carry everything.\n", animal.getName());
       }
       break;
     }
     timeElapsed += 0.05f;
   }
   return timeElapsed;
 }