diff --git a/.gitignore b/.gitignore index c15a9a2..1f00eaf 100644 --- a/.gitignore +++ b/.gitignore @@ -26,4 +26,5 @@ replay_pid* /.idea/ /out/ /target/ -*.iml \ No newline at end of file +*.iml +/.vscode/ \ No newline at end of file diff --git a/src/main/java/org/bitbiome/commands/CommandListener.java b/src/main/java/org/bitbiome/commands/CommandListener.java index dbda2c9..c2f9860 100644 --- a/src/main/java/org/bitbiome/commands/CommandListener.java +++ b/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 returnCommands() { diff --git a/src/main/java/org/bitbiome/commands/HelpCommand.java b/src/main/java/org/bitbiome/commands/HelpCommand.java index 14a1472..61051c0 100644 --- a/src/main/java/org/bitbiome/commands/HelpCommand.java +++ b/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(); diff --git a/src/main/java/org/bitbiome/commands/RunAwayCommand.java b/src/main/java/org/bitbiome/commands/RunAwayCommand.java new file mode 100644 index 0000000..0e5b9bf --- /dev/null +++ b/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."; + } + +} diff --git a/src/main/java/org/bitbiome/commands/UseCommand.java b/src/main/java/org/bitbiome/commands/UseCommand.java new file mode 100644 index 0000000..8c04710 --- /dev/null +++ b/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 enemies = new ArrayList(); +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= chance) + return true; + return false; + } + + public int getRunawayChance() { + int chance = 10; + for(int i = 0; i 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 inv = player.getInventory(); + + for(int i = 0; i mobs = loc.getMobList(); + for(int j = 0; j=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 allMobs) { + for(int i = 0; i enemies = new ArrayList(); + 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(); + 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)); + } +}