Browse Source

Merging fight-engine into develop

remotes/origin/develop
fdai7430 2 years ago
parent
commit
c0283c3995
  1. 3
      .gitignore
  2. 3
      src/main/java/org/bitbiome/commands/CommandListener.java
  3. 8
      src/main/java/org/bitbiome/commands/HelpCommand.java
  4. 18
      src/main/java/org/bitbiome/commands/RunAwayCommand.java
  5. 164
      src/main/java/org/bitbiome/commands/UseCommand.java
  6. 12
      src/test/java/org/bitbiome/commands/RunAwayCommandTest.java
  7. 86
      src/test/java/org/bitbiome/commands/UseCommandTest.java

3
.gitignore

@ -26,4 +26,5 @@ replay_pid*
/.idea/
/out/
/target/
*.iml
*.iml
/.vscode/

3
src/main/java/org/bitbiome/commands/CommandListener.java

@ -16,6 +16,8 @@ public class CommandListener {
commands.put("exit", new QuitCommand());
commands.put("quit", new QuitCommand());
commands.put("use", new UseCommand());
commands.put("location", new LocationCommand());
commands.put("travel", new TravelCommand());
commands.put("inventory", new InventoryCommand());
@ -24,6 +26,7 @@ public class CommandListener {
commands.put("map", new MapCommand());
commands.put("gold", new GoldCommand());
commands.put("shop", new ShopCommand());
commands.put("runaway", new RunAwayCommand());
}
public HashMap<String, CommandAPI> returnCommands() {

8
src/main/java/org/bitbiome/commands/HelpCommand.java

@ -27,10 +27,6 @@ public class HelpCommand implements CommandAPI {
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " travel" + Colors.ANSI_RESET + " | Startet das Reise System |\n")
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " quiz" + Colors.ANSI_RESET + " | Startet das quiz im shop |\n")
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " blackjack" + Colors.ANSI_RESET + " | Startet blackjack im shop |\n")
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " location" + Colors.ANSI_RESET + " | Gibt deine Location aus |\n")
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " quiz" + Colors.ANSI_RESET + " | Startet das quiz im shop |\n")
@ -47,6 +43,10 @@ public class HelpCommand implements CommandAPI {
.append("|" + Colors.ANSI_GREEN + " map" + Colors.ANSI_RESET + " | Zeigt dir deinen Karte |\n")
.append("|--------------|-----------------------------|\n")
.append("|" + Colors.ANSI_GREEN + " gold" + Colors.ANSI_RESET + " | Gibt dein Gold an |\n")
.append("|______________|_____________________________|\n")
.append("|" + Colors.ANSI_GREEN + " use" + Colors.ANSI_RESET + " | Benutze ein Item |\n")
.append("|______________|_____________________________|\n")
.append("|" + Colors.ANSI_GREEN + " runaway" + Colors.ANSI_RESET + " | Fliehe aus einem Kampf |\n")
.append("|______________|_____________________________|\n");
return outputMessage.toString();

18
src/main/java/org/bitbiome/commands/RunAwayCommand.java

@ -0,0 +1,18 @@
package org.bitbiome.commands;
import java.util.Scanner;
import org.bitbiome.classes.TravelEngine;
public class RunAwayCommand implements CommandAPI {
@Override
public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine travelEngine) {
System.out.println(getRunAwayMessage());
}
public String getRunAwayMessage() {
return "You can only run away when you're fighting someone.";
}
}

164
src/main/java/org/bitbiome/commands/UseCommand.java

@ -0,0 +1,164 @@
package org.bitbiome.commands;
import java.util.ArrayList;
import java.util.Scanner;
import org.bitbiome.classes.*;
import org.bitbiome.entities.*;
public class UseCommand implements CommandAPI {
private ArrayList<Mob> enemies = new ArrayList<Mob>();
private boolean combat = false;
@Override
public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine engine) {
Player player = engine.getPlayer();
getEnemies(player.getLocation().getMobList());
System.out.println(use(scanner, engine));
while(enemies.size() > 0 && combat) {
combat(player, scanner, engine);
}
}
public void combat(Player player, Scanner scanner, TravelEngine engine) {
if(player.getHp() <= 0)
System.exit(0);
for(int i = 0; i<enemies.size(); i++) {
Mob mob = enemies.get(i);
float hp = player.getHp();
hp -= mob.getDamage();
System.out.println(mob.getName() + " hat dich angegriffen und " + mob.getDamage() + " Schaden zugefügt.");
if(hp <= 0) {
player.setHp(0);
System.out.println("Du bist gestorben.");
System.exit(0);
}
player.setHp(hp);
}
COMBAT_LOOP:do {
System.out.println("Was willst du tun?");
String input = scanner.nextLine();
switch(input) {
case "use":
System.out.println(use(scanner, engine));
break COMBAT_LOOP;
case "runaway":
if(runawaySucceeds(getRunawayChance())) {
System.out.println("Du bist erfolgreich geflüchtet.");
combat = false;
break COMBAT_LOOP;
}
System.out.println("Dein Fluchtversuch ist fehlgeschlagen.");
break;
case "help":
new HelpCommand().performCommand(scanner, true, "", engine);
break;
case "exit":
case "quit":
System.out.println("Du kannst nicht während eines Kampfes das Spiel verlassen.");
break;
case "location":
new LocationCommand().performCommand(scanner, true, "", engine);
break;
case "travel":
System.out.println("Du kannst nicht während eines Kampfes reisen.");
break;
default:
System.out.println("Unbekannter Befehl - Siehe " + Colors.ANSI_PURPLE + "help" + Colors.ANSI_RESET);
break;
}
} while(true);
}
public boolean runawaySucceeds(int chance) {
int random = (int)(Math.random()*100);
if(random >= chance)
return true;
return false;
}
public int getRunawayChance() {
int chance = 10;
for(int i = 0; i<enemies.size(); i++) {
Mob mob = enemies.get(i);
chance += (int)Math.round(Math.sqrt(mob.getHp() * mob.getDamage() / 3) * (110-chance) / 100) + 1;
}
if(chance > 100)
chance = 100;
return chance;
}
public String use(Scanner scanner, TravelEngine engine) {
System.out.println("Welches Item willst du benutzen?");
String item = scanner.nextLine();
System.out.println("Auf welchen Gegner willst du es benutzen? (Leer lassen, um es auf dich selbst anzuwenden)");
String target = scanner.nextLine();
if(target.equals(""))
return getUseMessage(item, engine);
else
return getUseMessage(item + " on " + target, engine);
}
public String getUseMessage(String msg, TravelEngine engine) {
Player player = engine.getPlayer();
String message[] = msg.split(" on ");
String itemName = message[0];
ArrayList<Item> inv = player.getInventory();
for(int i = 0; i<inv.size(); i++) {
Item item = inv.get(i);
if(item.getName().toLowerCase().equals(itemName.toLowerCase())) {
if(!item.doesDamage())
return "Damit kannst du nicht angreifen.";
if(message.length == 1)
return useItem(item, player);
String targetName = message[1];
Location loc = player.getLocation();
ArrayList<Mob> mobs = loc.getMobList();
for(int j = 0; j<mobs.size(); j++) {
Mob mob = mobs.get(j);
if(mob.getName().toLowerCase().equals(targetName.toLowerCase()))
return useItem(item, mob, loc);
}
return "Dieses Ziel ist nicht verfügbar.";
}
}
return "Du bist nicht im Besitz dieses Items.";
}
public String useItem(Item item, Player target) {
float hp = target.getHp();
hp -= Float.parseFloat(item.getDamage());
String retString = "Du hast " + item.getName() + " auf dich selbst angewandt";
if(hp <= 0) {
target.setHp(0);
return retString + " und bist gestorben.";
}
target.setHp(hp);
return retString + ".";
}
public String useItem(Item item, Mob target, Location location) {
combat = true;
if(target.isFriendly())
enemies.add(target);
float hp = target.getHp();
float dmg = Float.parseFloat(item.getDamage());
if(dmg>=hp) {
location.getMobList().remove(target);
enemies.remove(target);
return "Du hast " + target.getName() + " mit " + item.getName() + " getötet.";
}
target.setHp(hp-dmg);
target.setFriendly(false);
return "Du hast " + item.getName() + " auf " + target.getName() + " angewandt.";
}
public void getEnemies(ArrayList<Mob> allMobs) {
for(int i = 0; i<allMobs.size(); i++) {
Mob mob = allMobs.get(i);
if(!mob.isFriendly())
enemies.add(mob);
}
}
}

12
src/test/java/org/bitbiome/commands/RunAwayCommandTest.java

@ -0,0 +1,12 @@
package org.bitbiome.commands;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class RunAwayCommandTest {
@Test
public void testRunAwayCommand() {
assertEquals("You can only run away when you're fighting someone.", new RunAwayCommand().getRunAwayMessage());
}
}

86
src/test/java/org/bitbiome/commands/UseCommandTest.java

@ -0,0 +1,86 @@
package org.bitbiome.commands;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
import org.bitbiome.classes.TravelEngine;
import org.bitbiome.entities.Item;
import org.bitbiome.entities.Mob;
import org.bitbiome.entities.Player;
import org.junit.jupiter.api.Test;
public class UseCommandTest {
@Test
public void testGetRunawayChance() {
UseCommand com = new UseCommand();
assertEquals(10, com.getRunawayChance());
ArrayList<Mob> enemies = new ArrayList<Mob>();
enemies.add(new Mob("Wolf", false, 11, 10));
com.getEnemies(enemies);
assertEquals(17, com.getRunawayChance());
enemies.add(new Mob("Drache", false, 55, 20));
com.getEnemies(enemies);
assertEquals(41, com.getRunawayChance());
enemies.add(new Mob("Drache", false, 55, 20));
enemies.add(new Mob("Drache", false, 55, 20));
enemies.add(new Mob("Drache", false, 55, 20));
enemies.add(new Mob("Drache", false, 55, 20));
enemies.add(new Mob("Drache", false, 55, 20));
com.getEnemies(enemies);
assertEquals(96, com.getRunawayChance());
enemies = new ArrayList<Mob>();
enemies.add(new Mob("Cthulhu", false, 15000, 10000));
com.getEnemies(enemies);
assertEquals(100, com.getRunawayChance());
}
@Test
public void testRunawaySucceeds() {
UseCommand com = new UseCommand();
assertEquals(true, com.runawaySucceeds(0));
assertEquals(false, com.runawaySucceeds(101));
}
@Test
public void testUseItemMob() {
UseCommand com = new UseCommand();
Player player = new Player("test");
Mob mob = new Mob("Wolf", false, 11, 10);
player.getLocation().getMobList().add(mob);
Item sword = new Item("Sword", true, "10", 1, 1);
assertEquals("Du hast Sword auf Wolf angewandt.", com.useItem(sword, mob, player.getLocation()));
assertEquals(1, player.getLocation().getMobList().size());
assertEquals("Du hast Wolf mit Sword getötet.", com.useItem(sword, mob, player.getLocation()));
assertEquals(0, player.getLocation().getMobList().size());
}
@Test
public void testUseItemPlayer() {
UseCommand com = new UseCommand();
Player player = new Player("test");
Item sword = new Item("Sword", true, "10", 1, 1);
assertEquals("Du hast Sword auf dich selbst angewandt.", com.useItem(sword, player));
assertEquals(90, player.getHp());
Item sword2 = new Item("Heiliges Schwert der Engel", true, "1000", 1, 1);
assertEquals("Du hast Heiliges Schwert der Engel auf dich selbst angewandt und bist gestorben.", com.useItem(sword2, player));
}
@Test
public void testGetUseMessage() {
UseCommand com = new UseCommand();
TravelEngine engine = new TravelEngine(new Player("test"));
assertEquals("Du bist nicht im Besitz dieses Items.", com.getUseMessage("", engine));
assertEquals("Du bist nicht im Besitz dieses Items.", com.getUseMessage("Fell", engine));
engine.getPlayer().addToInventory(new Item("Fell", false, "0", 1, 1));
assertEquals("Damit kannst du nicht angreifen.", com.getUseMessage("Fell", engine));
engine.getPlayer().addToInventory(new Item("Heiliges Schwert der Erzengel", true, "3000", 1, 1));
assertEquals("Dieses Ziel ist nicht verfügbar.", com.getUseMessage("Heiliges Schwert der Erzengel on Wolf", engine));
assertEquals("Dieses Ziel ist nicht verfügbar.", com.getUseMessage("hEiLiGeS sChWeRt DeR eRzEnGeL on WoLf", engine));
engine.getPlayer().getLocation().getMobList().add(new Mob("Wolf", false, 11, 10));
assertEquals("Du hast Wolf mit Heiliges Schwert der Erzengel getötet.", com.getUseMessage("Heiliges Schwert der Erzengel on Wolf", engine));
assertEquals("Dieses Ziel ist nicht verfügbar.", com.getUseMessage("Heiliges Schwert der Erzengel on Wolf", engine));
engine.getPlayer().getLocation().getMobList().add(new Mob("Wolf", false, 11, 10));
assertEquals("Du hast Wolf mit Heiliges Schwert der Erzengel getötet.", com.getUseMessage("hEiLiGeS sChWeRt DeR eRzEnGeL on WoLf", engine));
}
}
Loading…
Cancel
Save