fix engines bugs

This commit is contained in:
2026-07-16 16:59:03 +03:30
parent 71f1f272f2
commit 6f39911a34
5 changed files with 173 additions and 86 deletions
@@ -7,6 +7,10 @@ public abstract class BattleEngine {
public Entity Player2;
public BattleEngine(Entity player1, Entity player2) {
player1.ResetStamina();
player1.RestoreHealth();
player2.RestoreHealth();
player2.ResetStamina();
Player1 = player1;
Player2 = player2;
}
@@ -43,6 +43,7 @@ public abstract class MainEngine {
while (true) {
for (PlayerRoles role : roles) {
System.out.println(index + " > " + role.name().toLowerCase());
index++;
}
int choice = scanner.nextInt();
@@ -13,8 +13,8 @@ import java.util.Hashtable;
import java.util.Scanner;
public class MultiPlayerBattleEngine extends BattleEngine {
private Player player1;
private Player player2;
private final Player player1;
private final Player player2;
private Player player1Past;
private Player player2Past;
@@ -22,54 +22,67 @@ public class MultiPlayerBattleEngine extends BattleEngine {
super(player1, player2);
this.player1 = (Player) Player1;
this.player2 = (Player) Player2;
player1Past = null;
player2Past = null;
player1Past = player1.copy();
player2Past = player2.copy();
}
@Override
public void StartBattle() {
EntityDiff playerDiff;
EntityDiff enemyDiff;
while (true) {
FightMoves player1Move = PlayerMovesMenu(player1, player2);
showMoveExplanation(player1Move, player1, player2);
if (player1.GetState() != EntityState.FROZEN) {
FightMoves player1Move = PlayerMovesMenu(player1, player2);
showMoveExplanation(player1Move, player1, player2Past);
playerDiff = getPlayerDiff(player1Past, player1);
enemyDiff = getPlayerDiff(player2Past, player2);
System.out.println();
showFighter(playerDiff, player1);
showFighter(enemyDiff, player2);
System.out.println();
}
if (player1.GetState() == EntityState.DEAD) {
System.out.println(TextColor.Green + player2.Name + " WON!" + TextColor.Reset);
return;
}
if (player1Past.GetState() == EntityState.FROZEN) {
player1.SetState(EntityState.ALIVE);
}
player1Past = player1;
player2Past = player2;
player1Past = player1.copy();
player2Past = player2.copy();
EntityDiff playerDiff = getPlayerDiff(player1Past, player1);
EntityDiff enemyDiff = getPlayerDiff(player2Past, player2);
showFighter(playerDiff, player1);
showFighter(enemyDiff, player2);
if (player2.GetState() != EntityState.FROZEN)
{
FightMoves player2Move = PlayerMovesMenu(player2, player1);
showMoveExplanation(player2Move, player2, player1Past);
FightMoves player2Move = PlayerMovesMenu(player2, player1);
showMoveExplanation(player2Move, player2, player1);
playerDiff = getPlayerDiff(player1Past, player1);
enemyDiff = getPlayerDiff(player2Past, player2);
if (player2Past.GetState() == EntityState.FROZEN) {
player2.SetState(EntityState.ALIVE);
System.out.println();
showFighter(playerDiff, player1);
showFighter(enemyDiff, player2);
System.out.println();
}
player1Past = player1;
player2Past = player2;
playerDiff = getPlayerDiff(player1Past, player1);
enemyDiff = getPlayerDiff(player2Past, player2);
showFighter(playerDiff, player1);
showFighter(enemyDiff, player2);
if (player2.GetState() == EntityState.DEAD) {
System.out.println(TextColor.Green + player1.Name + " WON!" + TextColor.Reset);
return;
}
else if (player1.GetState() == EntityState.DEAD) {
System.out.println(TextColor.Green + player2.Name + " WON!" + TextColor.Reset);
return;
if (player2Past.GetState() == EntityState.FROZEN) {
player2.SetState(EntityState.ALIVE);
}
player1Past = player1.copy();
player2Past = player2.copy();
}
}
@@ -88,10 +101,12 @@ public class MultiPlayerBattleEngine extends BattleEngine {
System.out.println(entity.Name + ":");
String color = diff.health() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Health: " + entity.Health + " " + color + diff.health() + TextColor.Reset);
String sign = diff.health() > 0 ? "+" : "-";
System.out.println(" Health: " + entity.Health + " " + color + sign + Math.abs(diff.health()) + TextColor.Reset);
color = diff.stamina() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Stamina: " + entity.GetStamina() + " " + color + diff.stamina() + TextColor.Reset);
sign = diff.stamina() > 0 ? "+" : "-";
System.out.println(" Stamina: " + entity.GetStamina() + " " + color + sign + Math.abs(diff.stamina()) + TextColor.Reset);
if (diff.lostShield()) {
System.out.println(" Shield: " + entity.Shield.getName() + " " + TextColor.Red + "LOST" + TextColor.Reset);
@@ -103,12 +118,14 @@ public class MultiPlayerBattleEngine extends BattleEngine {
if (entity.Shield != null) {
color = diff.shieldHealth() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Shield: " + entity.Shield.getName() + " " + color + diff.shieldHealth() + TextColor.Reset);
sign = diff.shieldHealth() > 0 ? "+" : "-";
System.out.println(" Shield: " + entity.Shield.getName() + " " + color + sign + Math.abs(diff.shieldHealth()) + TextColor.Reset);
}
if (entity.Costume != null) {
color = diff.costumeHealth() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Costume: " + entity.Costume.getName() + " " + color + diff.shieldHealth() + TextColor.Reset);
sign = diff.costumeHealth() > 0 ? "+" : "-";
System.out.println(" Costume: " + entity.Costume.getName() + " " + color + sign + Math.abs(diff.shieldHealth()) + TextColor.Reset);
}
}
@@ -156,6 +173,7 @@ public class MultiPlayerBattleEngine extends BattleEngine {
}
private void showMoveExplanation(FightMoves move, Entity performer, Entity receiver) {
System.out.print(TextColor.Cyan);
switch (move) {
case FightMoves.LIGHT_ATTACK -> System.out.println(performer.Name + " performs a light attack");
case FightMoves.HEAVY_ATTACK -> System.out.println(performer.Name + " performs a heavy attack");
@@ -163,5 +181,6 @@ public class MultiPlayerBattleEngine extends BattleEngine {
case FightMoves.HEAL -> System.out.println(performer.Name + " heals");
case FightMoves.DEFEND -> System.out.println(performer.Name + " get their guard up!");
}
System.out.print(TextColor.Reset);
}
}
@@ -14,53 +14,60 @@ import java.util.Hashtable;
import java.util.Scanner;
public class SinglePlayerBattleEngine extends BattleEngine {
private Player player;
private Enemy enemy;
private final Player player;
private final Enemy enemy;
private Player playerPast;
private Enemy enemyPast;
public SinglePlayerBattleEngine(Player player, Enemy enemy) {
super(player, enemy);
this.player = (Player) Player1;
this.enemy = (Enemy) Player2;
playerPast = null;
enemyPast = null;
playerPast = player.copy();
enemyPast = enemy.copy();
}
@Override
public void StartBattle() {
EntityDiff playerDiff;
EntityDiff enemyDiff;
while (true) {
FightMoves playerMove = PlayerMovesMenu();
showMoveExplanation(playerMove, player, enemy);
if (player.GetState() != EntityState.FROZEN) {
FightMoves playerMove = PlayerMovesMenu();
showMoveExplanation(playerMove, player, enemyPast);
playerDiff = getPlayerDiff(playerPast, player);
enemyDiff = getPlayerDiff(enemyPast, enemy);
System.out.println();
showFighter(playerDiff, player);
showFighter(enemyDiff, enemy);
System.out.println();
}
if (playerPast.GetState() == EntityState.FROZEN) {
player.SetState(EntityState.ALIVE);
}
playerPast = player;
enemyPast = enemy;
EntityDiff playerDiff = getPlayerDiff(playerPast, player);
EntityDiff enemyDiff = getPlayerDiff(enemyPast, enemy);
showFighter(playerDiff, player);
showFighter(enemyDiff, enemy);
FightMoves botMove = enemy.PlayRound(player);
showMoveExplanation(botMove, enemy, player);
if (enemyPast.GetState() == EntityState.FROZEN) {
enemy.SetState(EntityState.ALIVE);
if (player.GetState() == EntityState.DEAD) {
return;
}
playerPast = player;
enemyPast = enemy;
playerPast = player.copy();
enemyPast = enemy.copy();
playerDiff = getPlayerDiff(playerPast, player);
enemyDiff = getPlayerDiff(enemyPast, enemy);
if (enemy.GetState() != EntityState.FROZEN) {
FightMoves botMove = enemy.PlayRound(player);
showMoveExplanation(botMove, enemy, playerPast);
showFighter(playerDiff, player);
showFighter(enemyDiff, enemy);
playerDiff = getPlayerDiff(playerPast, player);
enemyDiff = getPlayerDiff(enemyPast, enemy);
System.out.println();
showFighter(playerDiff, player);
showFighter(enemyDiff, enemy);
System.out.println();
}
if (enemy.GetState() == EntityState.DEAD) {
enemy.DropLoot(player);
@@ -68,9 +75,13 @@ public class SinglePlayerBattleEngine extends BattleEngine {
System.out.println(TextColor.Green + player.Name + " WON!" + TextColor.Reset);
return;
}
else if (player.GetState() == EntityState.DEAD) {
return;
if (enemyPast.GetState() == EntityState.FROZEN) {
enemy.SetState(EntityState.ALIVE);
}
playerPast = player.copy();
enemyPast = enemy.copy();
}
}
@@ -89,10 +100,12 @@ public class SinglePlayerBattleEngine extends BattleEngine {
System.out.println(entity.Name + ":");
String color = diff.health() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Health: " + entity.Health + " " + color + diff.health() + TextColor.Reset);
String sign = diff.health() > 0 ? "+" : "-";
System.out.println(" Health: " + entity.Health + " " + color + sign + Math.abs(diff.health()) + TextColor.Reset);
color = diff.stamina() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Stamina: " + entity.GetStamina() + " " + color + diff.stamina() + TextColor.Reset);
sign = diff.stamina() > 0 ? "+" : "-";
System.out.println(" Stamina: " + entity.GetStamina() + " " + color + sign + Math.abs(diff.stamina()) + TextColor.Reset);
if (diff.lostShield()) {
System.out.println(" Shield: " + entity.Shield.getName() + " " + TextColor.Red + "LOST" + TextColor.Reset);
@@ -104,12 +117,14 @@ public class SinglePlayerBattleEngine extends BattleEngine {
if (entity.Shield != null) {
color = diff.shieldHealth() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Shield: " + entity.Shield.getName() + " " + color + diff.shieldHealth() + TextColor.Reset);
sign = diff.shieldHealth() > 0 ? "+" : "-";
System.out.println(" Shield: " + entity.Shield.getName() + " " + color + sign + Math.abs(diff.shieldHealth()) + TextColor.Reset);
}
if (entity.Costume != null) {
color = diff.costumeHealth() > 0 ? TextColor.Green : TextColor.Red;
System.out.println(" Costume: " + entity.Costume.getName() + " " + color + diff.shieldHealth() + TextColor.Reset);
sign = diff.costumeHealth() > 0 ? "+" : "-";
System.out.println(" Costume: " + entity.Costume.getName() + " " + color + sign + Math.abs(diff.shieldHealth()) + TextColor.Reset);
}
}
@@ -157,6 +172,7 @@ public class SinglePlayerBattleEngine extends BattleEngine {
}
private void showMoveExplanation(FightMoves move, Entity performer, Entity receiver) {
System.out.print(TextColor.Cyan);
switch (move) {
case FightMoves.LIGHT_ATTACK -> System.out.println(performer.Name + " performs a light attack");
case FightMoves.HEAVY_ATTACK -> System.out.println(performer.Name + " performs a heavy attack");
@@ -164,5 +180,6 @@ public class SinglePlayerBattleEngine extends BattleEngine {
case FightMoves.HEAL -> System.out.println(performer.Name + " heals");
case FightMoves.DEFEND -> System.out.println(performer.Name + " get their guard up!");
}
System.out.print(TextColor.Reset);
}
}
@@ -29,10 +29,10 @@ public class SinglePlayerEngine extends MainEngine {
System.out.println(TextColor.Cyan + GetIntro() + ConsoleColor.Reset);
System.out.println();
System.out.println("How should me call you, hero? ");
System.out.println("How should we call you, hero? ");
String name = scanner.nextLine();
System.out.println("Greetings, " + name + ". May we know your role? \n");
System.out.println("Greetings, " + TextColor.Cyan + name + TextColor.Reset +". May we know your role? \n");
player = RoleMenu(name);
@@ -46,7 +46,7 @@ public class SinglePlayerEngine extends MainEngine {
boolean continueLoop = false;
do {
System.out.println(GetLocationIntro(location));
System.out.println(TextColor.Cyan + GetLocationIntro(location) + TextColor.Reset);
System.out.println("What do you do, " + player.Name + "?");
System.out.println(
"""
@@ -113,7 +113,7 @@ public class SinglePlayerEngine extends MainEngine {
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!";
player.Name + " goes inside the grave ward, they cannot see further 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 ->
@@ -125,7 +125,7 @@ public class SinglePlayerEngine extends MainEngine {
public void MarketPlace() {
Scanner scanner = new Scanner(System.in);
System.out.println("You've entered the marketplace, what would you like to do?");
System.out.println(TextColor.Cyan + "You've entered the marketplace, what would you like to do?" + TextColor.Reset);
boolean wantsToStay = true;
while (wantsToStay) {
System.out.println("Money = " + player.GetMoney() + "\n");
@@ -137,15 +137,21 @@ public class SinglePlayerEngine extends MainEngine {
4 > Sell the costumes
5 > Sell the Weapons
6 > Buy shields
7 > Buy shields
8 > Buy shields
7 > Buy weapon
8 > Buy costume
9 > Exit
""");
int choice = scanner.nextInt();
switch (choice) {
case 0 -> DisplayItem.DisplayItems(player.GetInventory());
case 0 -> {
if (player.GetInventory().isEmpty()) {
System.out.println(TextColor.Red + "You do not have anything at the moment" + TextColor.Reset);
break;
}
DisplayItem.DisplayItems(player.GetInventory());
}
case 1 -> {
player.SellByClass(ItemClass.UNNECESSARY);
System.out.println(TextColor.Green + "Unnecessary goods sold" + TextColor.Reset);
@@ -171,8 +177,13 @@ public class SinglePlayerEngine extends MainEngine {
System.out.println(TextColor.Blue + "Current shield: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Shield);
DisplayItem.DisplayItems(item);
if (player.Shield == null) {
System.out.println(TextColor.Red + "You do not have a Shield" + TextColor.Reset);
}
else {
item.add(player.Shield);
DisplayItem.DisplayItems(item);
}
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Shields.getShields()));
@@ -185,7 +196,8 @@ public class SinglePlayerEngine extends MainEngine {
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
if (player.Shield != null) player.Shield.DeattachFrom(player, true);
accessory.AttachTo(player, false);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
@@ -198,8 +210,13 @@ public class SinglePlayerEngine extends MainEngine {
System.out.println(TextColor.Blue + "Current weapon: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Weapon);
DisplayItem.DisplayItems(item);
if (player.Weapon == null) {
System.out.println(TextColor.Red + "You do not have a Weapon" + TextColor.Reset);
}
else {
item.add(player.Weapon);
DisplayItem.DisplayItems(item);
}
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Weapons.getWeapons()));
@@ -212,7 +229,8 @@ public class SinglePlayerEngine extends MainEngine {
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
if (player.Weapon != null) player.Weapon.DeattachFrom(player, true);
accessory.AttachTo(player, false);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
@@ -225,8 +243,13 @@ public class SinglePlayerEngine extends MainEngine {
System.out.println(TextColor.Blue + "Current costume: ");
ArrayList<Item> item = new ArrayList<>();
item.add(player.Costume);
DisplayItem.DisplayItems(item);
if (player.Costume == null) {
System.out.println(TextColor.Red + "You do not have a Costume" + TextColor.Reset);
}
else {
item.add(player.Costume);
DisplayItem.DisplayItems(item);
}
System.out.println(TextColor.Reset);
ArrayList<Item> items = new ArrayList<>(Arrays.asList(Costumes.getCostumes()));
@@ -239,7 +262,8 @@ public class SinglePlayerEngine extends MainEngine {
if (accessory != null) {
if (player.GetMoney() >= accessory.GetPrice()) {
accessory.AttachTo(player);
if (player.Costume != null) player.Costume.DeattachFrom(player, true);
accessory.AttachTo(player, false);
player.ModifyMoney(accessory.GetPrice(), false);
}
else {
@@ -255,7 +279,8 @@ public class SinglePlayerEngine extends MainEngine {
public void InventoryView() {
Scanner scanner = new Scanner(System.in);
System.out.println("You open your inventory, what would you like to do?");
System.out.println(TextColor.Cyan + "You open your inventory, what would you like to do?\n" + TextColor.Reset);
boolean wantsToStay = true;
while (wantsToStay) {
System.out.println("Money = " + player.GetMoney() + "\n");
@@ -270,10 +295,20 @@ public class SinglePlayerEngine extends MainEngine {
int choice = scanner.nextInt();
switch (choice) {
case 0 -> DisplayItem.DisplayItems(player.GetInventory());
case 0 -> {
if (player.GetInventory().isEmpty()) {
System.out.println(TextColor.Red + "You do not have anything at the moment" + TextColor.Reset);
break;
}
DisplayItem.DisplayItems(player.GetInventory());
}
case 1 -> {
System.out.println(TextColor.Blue + "Current shield: ");
ArrayList<Item> item = new ArrayList<>();
if (player.Shield == null) {
System.out.println(TextColor.Red + "You do not have a Shield" + TextColor.Reset);
break;
}
item.add(player.Shield);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
@@ -285,12 +320,17 @@ public class SinglePlayerEngine extends MainEngine {
Item shield = dictionary.get(index);
if (shield != null) {
((Accessory) shield).AttachTo(player);
if (player.Shield != null) player.Shield.DeattachFrom(player, true);
((Accessory) shield).AttachTo(player, true);
}
}
case 2 -> {
System.out.println(TextColor.Blue + "Current weapon: ");
ArrayList<Item> item = new ArrayList<>();
if (player.Weapon == null) {
System.out.println(TextColor.Red + "You do not have a Weapon" + TextColor.Reset);
break;
}
item.add(player.Weapon);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
@@ -302,12 +342,17 @@ public class SinglePlayerEngine extends MainEngine {
Item weapon = dictionary.get(index);
if (weapon != null) {
((Accessory) weapon).AttachTo(player);
if (player.Weapon != null) player.Weapon.DeattachFrom(player, true);
((Accessory) weapon).AttachTo(player, true);
}
}
case 3 -> {
System.out.println(TextColor.Blue + "Current costume: ");
ArrayList<Item> item = new ArrayList<>();
if (player.Costume == null) {
System.out.println(TextColor.Red + "You do not have a Costume" + TextColor.Reset);
break;
}
item.add(player.Costume);
DisplayItem.DisplayItems(item);
System.out.println(TextColor.Reset);
@@ -319,7 +364,8 @@ public class SinglePlayerEngine extends MainEngine {
Item costume = dictionary.get(index);
if (costume != null) {
((Accessory) costume).AttachTo(player);
if (player.Costume != null) player.Costume.DeattachFrom(player, true);
((Accessory) costume).AttachTo(player, true);
}
}
case 4 -> wantsToStay = false;