Files
HW-04-JAVA-KNIGHT-Ramtin/Java-Knight/src/main/java/org/project/engine/SinglePlayerEngine.java
T
2026-07-11 15:26:55 +03:30

330 lines
14 KiB
Java

package org.project.engine;
import org.project.constants.appConstants.ConsoleColor;
import org.project.constants.appConstants.TextColor;
import org.project.constants.gameConstant.*;
import org.project.entity.players.Player;
import org.project.item.Accessory.Accessory;
import org.project.item.Accessory.Shield;
import org.project.item.Item;
import org.project.location.*;
import org.project.util.DisplayItem;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Scanner;
public class SinglePlayerEngine extends MainEngine {
private Player player;
public SinglePlayerEngine() {
super();
}
@Override
public void run() {
Scanner scanner = new Scanner(System.in);
System.out.println(TextColor.Cyan + GetIntro() + ConsoleColor.Reset);
System.out.println();
System.out.println("How should me call you, hero? ");
String name = scanner.nextLine();
System.out.println("Greetings, " + name + ". May we know your role? \n");
player = RoleMenu(name);
System.out.println("Very well, " + TextColor.Cyan + player.Name + ConsoleColor.Reset + " your journey begins!");
System.out.println();
while (true) {
boolean canFightDragon = player.GetInventoryByClass(ItemClass.KEY).size() == 3;
Location location = Location.RandomLocation();
boolean continueLoop = false;
do {
System.out.println(GetLocationIntro(location));
System.out.println("What do you do, " + player.Name + "?");
System.out.println(
"""
0 > FIGHT!
1 > Go to the next location
2 > Visit the market place in the town
3 > Check inventory"""
);
if (canFightDragon) {
System.out.println("4 > Go to the castle to face the dragon");
}
int choice = scanner.nextInt();
switch (choice) {
case 0 -> {
System.out.println(TextColor.Red + "You choice to FIGHT!" + TextColor.Reset);
BattleEngine engine = new SinglePlayerBattleEngine(player, location.enemy);
engine.StartBattle();
continueLoop = false;
}
case 1 -> {
continueLoop = false;
}
case 2 -> {
MarketPlace();
continueLoop = false;
}
case 3 -> {
InventoryView();
continueLoop = true;
}
case 4 -> {
if (canFightDragon) {
location = new Castle();
continueLoop = false;
}
else {
continueLoop = true;
}
}
default -> continueLoop = true;
}
} while (continueLoop);
if (player.GetState() == EntityState.DEAD) {
System.out.println(TextColor.Red + "Game Over!" + TextColor.Reset);
break;
}
if (player.GetInventoryByClass(ItemClass.KEY).size() == 4) {
System.out.println(TextColor.Blue + "You WON! You saved Javanest!" + TextColor.Reset);
break;
}
}
}
public String GetIntro() {
return "For centuries, the land of Javanest lived in peace, until a magical Dragon attacked, plunging the realm into absolute darkness. With a wicked curse, the Dragon transformed the innocent people into horrific monsters: Goblins, Skeletons, and Vampires. Retreating to its impenetrable Castle, the Dragon divided the three keys to its lair and hid them among these cursed creatures. Now, it is your duty to step up and save the land. You must battle these monsters, recover the unique key from each monster type, and finally slay the Dragon to break the curse and restore peace to Javanest!";
}
public String GetLocationIntro(Location location) {
return switch (location) {
case Jungle jungle ->
player.Name + " enters the woods, after a few steps, they see a green goblin searching for food a few steps away";
case GraveYard graveYard ->
player.Name + " goes inside the grave ward, they cannot see a more than 10 steps ahead because of the fog in the air. After lurking through out the graves, the shadow appears... It's a skeleton!";
case DarkCaves darkCaves ->
player.Name + " dives into the darkness. As our hero was going deeper, a black smoke appears, a vampire is nearby...";
case Castle castle ->
player.Name + " Goes through the gates, the dragon looks at " + player.Name + "... It knows that an epic battle awaits both it them";
case null, default -> "";
};
}
public void MarketPlace() {
Scanner scanner = new Scanner(System.in);
System.out.println("You've entered the marketplace, what would you like to do?");
boolean wantsToStay = true;
while (wantsToStay) {
System.out.println("Money = " + player.GetMoney() + "\n");
System.out.println("""
0 > Show Inventory
1 > Sell Unnecessary goods
2 > Sell the accessories
3 > Sell the shields
4 > Sell the costumes
5 > Sell the Weapons
6 > Buy shields
7 > Buy shields
8 > Buy shields
9 > Exit
""");
int choice = scanner.nextInt();
switch (choice) {
case 0 -> DisplayItem.DisplayItems(player.GetInventory());
case 1 -> {
player.SellByClass(ItemClass.UNNECESSARY);
System.out.println(TextColor.Green + "Unnecessary goods sold" + TextColor.Reset);
}
case 2 -> {
player.SellByClass(ItemClass.ACCESSORY);
System.out.println(TextColor.Green + "accessories sold" + TextColor.Reset);
}
case 3 -> {
player.SellByClass(ItemClass.SHIELD);
System.out.println(TextColor.Green + "shields sold" + TextColor.Reset);
}
case 4 -> {
player.SellByClass(ItemClass.COSTUME);
System.out.println(TextColor.Green + "costumes sold" + TextColor.Reset);
}
case 5 -> {
player.SellByClass(ItemClass.WEAPON);
System.out.println(TextColor.Green + "weapons sold" + TextColor.Reset);
}
case 6 -> {
System.out.println("Money = " + player.GetMoney() + "\n");
System.out.println(TextColor.Blue + "Current shield: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Shield);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Shields.getShields()));
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(items);
System.out.println("Please set the number of the accessory you want to buy, set any other number to abort:");
Integer index = scanner.nextInt();
Item shield = dictionary.get(index);
Accessory accessory = (Accessory) shield;
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
System.out.println(TextColor.Red + "You do not have enough money" + TextColor.Reset);
}
}
}
case 7 -> {
System.out.println("Money = " + player.GetMoney() + "\n");
System.out.println(TextColor.Blue + "Current weapon: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Weapon);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Weapons.getWeapons()));
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(items);
System.out.println("Please set the number of the accessory you want to buy, set any other number to abort:");
Integer index = scanner.nextInt();
Item weapon = dictionary.get(index);
Accessory accessory = (Accessory) weapon;
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
System.out.println(TextColor.Red + "You do not have enough money" + TextColor.Reset);
}
}
}
case 8 -> {
System.out.println("Money = " + player.GetMoney() + "\n");
System.out.println(TextColor.Blue + "Current costume: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Costume);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Costumes.getCostumes()));
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(items);
System.out.println("Please set the number of the accessory you want to buy, set any other number to abort:");
Integer index = scanner.nextInt();
Item costume = dictionary.get(index);
Accessory accessory = (Accessory) costume;
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
System.out.println(TextColor.Red + "You do not have enough money" + TextColor.Reset);
}
}
}
case 9 -> wantsToStay = false;
}
}
}
public void InventoryView() {
Scanner scanner = new Scanner(System.in);
System.out.println("You open your inventory, what would you like to do?");
boolean wantsToStay = true;
while (wantsToStay) {
System.out.println("Money = " + player.GetMoney() + "\n");
System.out.println("""
0 > Show Inventory
1 > Show Shields
2 > Show Weapons
3 > Show Costumes
4 > Exit
""");
int choice = scanner.nextInt();
switch (choice) {
case 0 -> DisplayItem.DisplayItems(player.GetInventory());
case 1 -> {
System.out.println(TextColor.Blue + "Current shield: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Shield);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(player.GetInventoryByClass(ItemClass.SHIELD));
System.out.println("Please set the number of the accessory you want to wear, set any other number to abort:");
Integer index = scanner.nextInt();
Item shield = dictionary.get(index);
if (shield != null) {
((Accessory) shield).AttachTo(player);
}
}
case 2 -> {
System.out.println(TextColor.Blue + "Current weapon: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Weapon);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(player.GetInventoryByClass(ItemClass.WEAPON));
System.out.println("Please set the number of the accessory you want to wear, set any other number to abort:");
Integer index = scanner.nextInt();
Item weapon = dictionary.get(index);
if (weapon != null) {
((Accessory) weapon).AttachTo(player);
}
}
case 3 -> {
System.out.println(TextColor.Blue + "Current costume: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Costume);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
Dictionary<Integer, Item> dictionary = DisplayItem.MenuDisplay(player.GetInventoryByClass(ItemClass.COSTUME));
System.out.println("Please set the number of the accessory you want to wear, set any other number to abort:");
Integer index = scanner.nextInt();
Item costume = dictionary.get(index);
if (costume != null) {
((Accessory) costume).AttachTo(player);
}
}
case 4 -> wantsToStay = false;
}
}
}
}