ThunderfootMOB ThunderfootMOB - 5 months ago 12
Java Question

How to create a turn based fight system with multiple enemies?

I'm trying to learn Java at home using online tutorials and documentation. I have very little experience learning a programming language.

I'm trying to create a "turn based" fighting system with multiple enemies. I have created a system using 1 enemy and it works fine, but I can't figure out how to tell the system which enemy I am fighting.

Im not sure how in depth to go but I will try and be as thorough as possible.I am using JetBrains IntelliJ IDE, To try and keep things organized here is the layout (~Packages, *Classes) Also i'm keeping everything public for the most part for now, but IntelliJ lets me know what should and can be set to Private etc. Any tips for posting in the future would be great aswell :P

~Assets
*BareBones (public abstract class)

*Enemy0 (extends BareBones)

*Enemy1 (extends BareBones)

*PlayerChar (extends BareBones)

~BattlePlans

*FightSystem

*Tutorial

~MainPack

*Game

*MainClass

Alright, Now for the code

public abstract class BareBones {

private int health;
private int attPower;

public int getHealth() {
return health;
}

public void setHealth(int health) {
this.health = health;
}

public int getAttPower() {
return attPower;
}

public void setAttPower(int attPower) {
this.attPower = attPower;
}
}


Enemy0, Enemy1, PlayerChar are all just "public class (name) extends BareBones{"

MAIN CLASS:

public class MainClass {
public static void main(String[] args) {
Game game = new Game();
game.run();
}
}


GAME:

import Assets.PlayerChar;
import BattlePlans.Tutorial;
import java.util.Scanner;

public class Game {

//Scanner for user input
public static Scanner userInput = new Scanner(System.in);
//Instantiate the user's character
public static PlayerChar userHero = new PlayerChar();

void run() {
//Set player stats
userHero.setHealth(100);
userHero.setAttPower(10);

//Begin instruction
System.out.print("Welcome, this is a \"Turn Based Combat\" engine");
contDialogue();
System.out.print("Let's get through the basics, with your first battle!");
contDialogue();
Tutorial.tutorial();


}

//Method for pauses in between dialogue
public static void contDialogue() {
System.out.print("\nPress [ENTER] to continue...");
userInput.nextLine();
}
}


TUTORIAL:

public class Tutorial {
//Instantiate our fight system
static FightSystem fightSystem = new FightSystem();
//Instantiate our first enemy
static Enemy0 enemy0 = new Enemy0();
//Are we in battle?
private static boolean fighting = true;

//Create our tutorial method
public static void tutorial(){

//Assign our enemy its stats
enemy0.setHealth(100);
enemy0.setAttPower(5);

//Tutorial Dialogue
System.out.print("\n1 = Attack");

//Fight System
while (fighting) {
if (Game.userHero.getHealth() <= 0 || enemy0.getHealth() <= 0) {
fighting = false;
break;
}
if (Game.userInput.hasNextInt()) {
int input = Game.userInput.nextInt();
if (!(input == 1 || input == 2 || input == 3 || input == 4)) {
FightSystem.fightInput();
} else if (input == 1) {
fightSystem.fight1();
}
}
}
if (!fighting) {
if (Game.userHero.getHealth() <= 0) {
youLost();
} else if (enemy0.getHealth() <= 0) {
youWon();
}
}
}

private static void youWon() {
System.out.print("\nCongratulations!\nYou have defeated the enemy!");
Game.contDialogue();
Game.userInput.nextLine();
}

private static void youLost() {
System.out.print("\nSorry but you have lost!");
}
}


FIGHT SYSTEM:

public class FightSystem {

public static void fightInput() {
System.out.print("\n1 = Attack");
}

private void statusUpdate() {
System.out.print("\n------------------------------------------");
System.out.print("\n(HP = " + Game.userHero.getHealth() + "|| (ENEMY HP = " + Tutorial.enemy0.getHealth() + ")");
}

private void enemyAttack() {
System.out.print("\nThe enemy's attack does " + Tutorial.enemy0.getAttPower() + " damage!");
Game.userHero.setHealth(Game.userHero.getHealth() - Tutorial.enemy0.getAttPower());
}

public void fight1() {
System.out.print("\nYour attack does " + Game.userHero.getAttPower() + " damage to the enemy!");
Tutorial.enemy0.setHealth(Tutorial.enemy0.getHealth() - Game.userHero.getAttPower());
enemyAttack();
statusUpdate();
}
}

Answer

You could put your enemies into an ArrayList, and use the indices in the list as the fight number:

public ArrayList<Enemy> enemies;

public Tutorial()
{
    enemies = new ArrayList<Enemy>();
    // other constructor stuff
}

The fight function can look something like this then:

public void fight(int i)
{
    System.out.print("\nYour attack does " + Combat.userHero.getAttPower() + " damage to the enemy!");
    Tutorial.enemies[i].setHealth(Tutorial.enemies[i].getHealth() - Combat.userHero.getAttPower());
    // other fight stuff
}   

Then in the game loop you can modify the fight checking code to something like:

if (!(input == 1 || input == 2 || input == 3 || input == 4)) {
    FightSystem.fightInput();
} else {
    fightSystem.fight(input);
}

If you want to create different kinds of enemies, you should look into polymorphism and inheritance.