186 lines
7.1 KiB
Java
186 lines
7.1 KiB
Java
package org.project.engine;
|
|
|
|
import org.project.common.models.EntityDiff;
|
|
import org.project.constants.appConstants.TextColor;
|
|
import org.project.constants.gameConstant.EntityState;
|
|
import org.project.constants.gameConstant.FightMoves;
|
|
import org.project.entity.Entity;
|
|
import org.project.entity.enemies.Enemy;
|
|
import org.project.entity.players.Player;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.Dictionary;
|
|
import java.util.Hashtable;
|
|
import java.util.Scanner;
|
|
|
|
public class SinglePlayerBattleEngine extends BattleEngine {
|
|
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 = player.copy();
|
|
enemyPast = enemy.copy();
|
|
}
|
|
|
|
@Override
|
|
public void StartBattle() {
|
|
EntityDiff playerDiff;
|
|
EntityDiff enemyDiff;
|
|
|
|
while (true) {
|
|
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);
|
|
}
|
|
|
|
if (player.GetState() == EntityState.DEAD) {
|
|
return;
|
|
}
|
|
|
|
playerPast = player.copy();
|
|
enemyPast = enemy.copy();
|
|
|
|
if (enemy.GetState() != EntityState.FROZEN) {
|
|
FightMoves botMove = enemy.PlayRound(player);
|
|
showMoveExplanation(botMove, enemy, playerPast);
|
|
|
|
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);
|
|
player.AddLevelProgress(enemy.LevelProgressAmount);
|
|
System.out.println(TextColor.Green + player.Name + " WON!" + TextColor.Reset);
|
|
return;
|
|
}
|
|
|
|
if (enemyPast.GetState() == EntityState.FROZEN) {
|
|
enemy.SetState(EntityState.ALIVE);
|
|
}
|
|
|
|
playerPast = player.copy();
|
|
enemyPast = enemy.copy();
|
|
}
|
|
}
|
|
|
|
private EntityDiff getPlayerDiff(Entity oldVersion, Entity newVersion) {
|
|
return new EntityDiff(
|
|
newVersion.Health - oldVersion.Health,
|
|
newVersion.GetStamina() - oldVersion.GetStamina(),
|
|
(newVersion.Shield == null && oldVersion.Shield != null),
|
|
(newVersion.Weapon == null && oldVersion.Weapon != null),
|
|
(newVersion.Shield != null ? newVersion.Shield.GetHealth() : 0) - (oldVersion.Shield != null ? oldVersion.Shield.GetHealth() : 0),
|
|
(newVersion.Costume != null ? newVersion.Costume.GetHealth() : 0) - (oldVersion.Costume != null ? oldVersion.Costume.GetHealth() : 0)
|
|
);
|
|
}
|
|
|
|
private void showFighter(EntityDiff diff, Entity entity) {
|
|
System.out.println(entity.Name + ":");
|
|
|
|
String color = diff.health() > 0 ? TextColor.Green : TextColor.Red;
|
|
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;
|
|
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);
|
|
}
|
|
|
|
if (diff.lostCostume()) {
|
|
System.out.println(" Costume: " + entity.Costume.getName() + " " + TextColor.Red + "LOST" + TextColor.Reset);
|
|
}
|
|
|
|
if (entity.Shield != null) {
|
|
color = diff.shieldHealth() > 0 ? TextColor.Green : TextColor.Red;
|
|
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;
|
|
sign = diff.costumeHealth() > 0 ? "+" : "-";
|
|
System.out.println(" Costume: " + entity.Costume.getName() + " " + color + sign + Math.abs(diff.shieldHealth()) + TextColor.Reset);
|
|
}
|
|
}
|
|
|
|
private FightMoves PlayerMovesMenu() {
|
|
Scanner scanner = new Scanner(System.in);
|
|
|
|
System.out.println(player.Name + " it's your turn");
|
|
ArrayList<FightMoves> moves = player.GetAvailableMoves();
|
|
|
|
int index = 0;
|
|
Dictionary<Integer, FightMoves> dictionary = new Hashtable<>();
|
|
|
|
for (FightMoves move : moves) {
|
|
String result = index + " > " + move.toString();
|
|
|
|
if (move == FightMoves.SPECIAL) {
|
|
result += " (" + player.GetSpecialAbilityDescription(enemy) + ")";
|
|
}
|
|
|
|
dictionary.put(index, move);
|
|
index++;
|
|
|
|
System.out.println(result);
|
|
}
|
|
|
|
boolean continueLoop;
|
|
while (true){
|
|
continueLoop = false;
|
|
int choice = scanner.nextInt();
|
|
FightMoves move = moves.get(choice);
|
|
|
|
switch (move) {
|
|
case FightMoves.LIGHT_ATTACK -> player.LightAttack(enemy);
|
|
case FightMoves.HEAVY_ATTACK -> player.HeavyAttack(enemy);
|
|
case FightMoves.SPECIAL -> player.SpecialAbility(enemy);
|
|
case FightMoves.HEAL -> player.HealMove();
|
|
case FightMoves.DEFEND -> player.defend();
|
|
default -> continueLoop = true;
|
|
}
|
|
|
|
if (!continueLoop) {
|
|
return move;
|
|
}
|
|
}
|
|
}
|
|
|
|
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");
|
|
case FightMoves.SPECIAL -> System.out.println(performer.GetSpecialAbilityResultDescription(receiver));
|
|
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);
|
|
}
|
|
}
|