implement game loop

This commit is contained in:
2026-05-07 22:00:42 +03:30
parent b7c3086fd0
commit add61e75aa
14 changed files with 188 additions and 32 deletions
+139 -21
View File
@@ -15,50 +15,168 @@ import org.project.location.Location;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Weapon wp = new Sword(12, 5);
ArrayList<Player> players = new ArrayList<>();
players.add(new Assassin("Pia", 100, 100, wp, new KnightArmor(0, 1)));
players.add(new Knight("Khan", 100, 100, wp, new KnightArmor(50, 1)));
players.add(new Wizard("Daalou", 100, 100, wp, new KnightArmor(50, 1)));
ArrayList<Enemy> enemies = new ArrayList<>();
enemies.add(new Goblin(100, 100, wp));
enemies.add(new Skeleton(50, 100, wp));
enemies.add(new Vampire(90, 110, wp));
List<Location> locations = new ArrayList<>();
ArrayList<Location> locations = new ArrayList<>();
locations.add(new Location("Jungle", enemies));
Player player;
Enemy enemy;
locations.add(new Location("China", new ArrayList<>(enemies.subList(0, 2))));
locations.add(new Location("Iran", new ArrayList<>(enemies.subList(1, 3))));
Player player = null;
Enemy enemy = null;
Location location = null;
int isTurn = 0;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
player = new Assassin("Pia", 100, 100, wp, new KnightArmor(50, 1));
System.out.println("You chose " + player.getName() + "(Assassin)");
player = choosePlayer(players);
while (true) {
location = chooseLocation(locations);
enemy = chooseEnemy(location);
System.out.println("wanna fight?\n1.Yes\n2.No");
int choice = sc.nextInt();
if (choice == 1) {
combat(player,enemy);
System.out.println("player AHP: " + player.getHP());
break;
case 2:
player = new Knight("Khan", 200, 100, wp, new KnightArmor(80, 1));
System.out.println("You chose " + player.getName() + "(Knight)");
break;
default:
player = new Wizard("Daalou", 90, 100, wp, new KnightArmor(100, 1));
System.out.println("You chose " + player.getName() + "(Wizard)");
System.out.println();
}
else {
continue;
}
}
break;
}
}
public void displayLocations(List<Location> locations) {
/////////////////////////////////////////////////////////////////////////////////////////
public static void displayLocations(List<Location> locations) {
for (Location location : locations) {
System.out.println(location);
}
}
}
public static Player choosePlayer(ArrayList<Player> players) {
System.out.print("1.Assassin\n2.Knight\n3.Wizard\nChoose your Character: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Player player = null;
switch (choice) {
case 1:
player = players.get(0);
break;
case 2:
player = players.get(1);
break;
default:
player = players.get(2);
break;
}
System.out.println("You chose " + player.getName() + "(" + player.getClass().getSimpleName() + ")");
return player;
}
public static Location chooseLocation(ArrayList<Location> locations) {
displayLocations(locations);
System.out.print("1.Jungle\n2.Iran\n3.China\nChoose Location: ");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
Location location = null;
switch (choice) {
case 1:
location = locations.get(0);
break;
case 2:
location = locations.get(1);
break;
case 3:
location = locations.get(2);
break;
}
System.out.println("You chose " + location.getName() );
return location;
}
public static Enemy chooseEnemy(Location location) {
int random = new Random().nextInt(location.getEnemies().size());
Enemy enemy = location.getEnemies().get(random);
System.out.println("The Enemey is: " + enemy.getClass().getSimpleName());
return enemy;
}
public static void combat(Player player, Enemy enemy) {
Scanner sc = new Scanner(System.in);
int turn = 0;
int choice;
while (player.isAlive() && enemy.isAlive()) {
if (turn == 0) {
turn = 1;
System.out.println("choose your action:\n1.light attack 2.heavy attack 3.defend 4.heal 5.special ability");
choice = sc.nextInt();
switch (choice) {
case 1: player.lightAttack(enemy);
break;
case 2: player.heavyAttack(enemy);
break;
case 3: player.defend();
break;
case 4: player.heal(10);
break;
case 5: player.specialAbility(enemy);
break;
default:
System.out.println("invalid choice");
}
System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
+ " HP | " + player.getMP() + "/" + player.getMaxMP() + " Mana" + "]");
System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
+ " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");
}
if (turn == 1) {
System.out.println("-".repeat(30));
System.out.println(enemy.getClass().getSimpleName() + "'s turn: ");
turn = 0;
choice = new Random().nextInt(3);
switch (choice) {
case 0: enemy.attack(player);
break;
case 1: enemy.defend();
break;
case 2: enemy.heal(10);
}
}
System.out.println("[" + player.getClass().getSimpleName() + " - " + player.getHP() + "/" + player.getMaxHP()
+ " HP | " + player.getMP() + "/" + player.getMaxMP() +" Mana" + "]");
System.out.println("[" + enemy.getClass().getSimpleName() + " - " + enemy.getHP() + "/" + enemy.getMaxHP()
+ " HP | " + enemy.getMP() + "/" + enemy.getMaxMP() + " Mana" + "]");
}
if (!player.isAlive()) {
System.out.println("You lost!");
}
else {
System.out.println("You Won!");
if (enemy.dropKey()) {
System.out.println(enemy.getClass().getSimpleName() + "'s key dropped!");
player.achiveKey(enemy);
}
}
}
}
@@ -27,9 +27,9 @@ public abstract class Enemy implements Entity {
if (!keyFound) {
hasKey = Math.random() < .2;
}
if (hasKey) {
keyFound = true;
}
// if (hasKey) {
// keyFound = true;
// }
}
//implement this in Main
private boolean checkTargetAbility(Player target) {
@@ -43,6 +43,7 @@ public abstract class Enemy implements Entity {
@Override
public void attack(Entity target) {
if (getMP() >= weapon.getManaCost()) {
System.out.println(this.getClass().getSimpleName() + ": attacking " + target.getClass().getSimpleName());
if (checkTargetAbility((Player) target)) {
System.out.println("target is using special ability");
return;
@@ -61,6 +62,7 @@ public abstract class Enemy implements Entity {
@Override
public void defend(){
System.out.println(this.getClass().getSimpleName() + " is defending");
setDefending(true);
};
@@ -71,7 +73,13 @@ public abstract class Enemy implements Entity {
@Override
public void takeDamage(int damage) {
if (isDefending()) {
setHP(getHP() - (int) ((1 - getDefendRate())) * damage);
}
setHP(getHP() - damage);
if (getHP() <= 0) {
setHP(0);
}
}
@Override
@@ -154,8 +162,12 @@ public abstract class Enemy implements Entity {
return getClass().getSimpleName();
}
public boolean getHasKey() {
return hasKey;
public boolean dropKey() {
if (hp <= 0 && hasKey) {
keyFound = true;
return true;
}
return false;
}
}
@@ -6,9 +6,11 @@ import org.project.item.weapons.Weapon;
public class Skeleton extends Enemy{
private boolean incarnation = false;
public Skeleton(int hp, int mp, Weapon weapon) {
super(hp,mp,weapon);
setDefendRate(.3);
}
@Override
@@ -41,8 +43,9 @@ public class Skeleton extends Enemy{
@Override
public void takeDamage(int damage) {
super.takeDamage(damage);
if (getHP() <= 0) {
if (getHP() <= 0 && !incarnation) {
setHP(getMaxHP() / 2);
incarnation = true;
}
}
@@ -14,11 +14,13 @@ public class Assassin extends Player {
@Override
public void lightAttack(Entity target) {
super.lightAttack(target);
attack(target);
}
@Override
public void heavyAttack(Entity target) {
super.heavyAttack(target);
if (getUsingSpecialAbility()) {
setAttackMultiplier(3);
attack(target);
@@ -14,11 +14,13 @@ public class Knight extends Player {
@Override
public void lightAttack(Entity target) {
super.lightAttack(target);
attack(target);
}
@Override
public void heavyAttack(Entity target) {
super.heavyAttack(target);
if (getMP() >= weapon.getManaCost()) {
setAttackMultiplier(1.8);
attack(target);
@@ -1,6 +1,8 @@
package org.project.entity.players;
import org.project.entity.Entity;
import org.project.entity.enemies.Goblin;
import org.project.entity.enemies.Skeleton;
import org.project.item.armors.Armor;
import org.project.item.weapons.Weapon;
@@ -17,9 +19,9 @@ public abstract class Player implements Entity, combatOptions {
private double defendRate;
private boolean isUsingSpecialAbility = false;
private double attackMultiplier = 1;
protected boolean hasGoblinKey = false;
protected boolean hasSkeletonKey = false;
protected boolean hasVampireKey = false;
private boolean hasGoblinKey = false;
private boolean hasSkeletonKey = false;
private boolean hasVampireKey = false;
public Player(String name, int hp, int mp, Weapon weapon, Armor armor) {
this.name = name;
@@ -44,13 +46,18 @@ public abstract class Player implements Entity, combatOptions {
};
@Override
public abstract void lightAttack(Entity target);
public void lightAttack(Entity target) {
System.out.println(this.getClass().getSimpleName() + " light attacking");
}
@Override
public abstract void heavyAttack(Entity target);
public void heavyAttack(Entity target) {
System.out.println(this.getClass().getSimpleName() + " heavy attacking");
}
@Override
public void defend(){
System.out.println(this.getClass().getSimpleName() + " is defending");
setDefending(true);
};
@@ -168,4 +175,14 @@ public abstract class Player implements Entity, combatOptions {
attackMultiplier = multiplier;
}
public boolean HasGoblinKey() { return hasGoblinKey; }
public boolean HasSkeletonKey() { return hasSkeletonKey; }
public boolean HasVampireKey() { return hasVampireKey; }
public void achiveKey(Entity target) {
if ( target instanceof Goblin) { hasGoblinKey = true; }
else if (target instanceof Skeleton) { hasSkeletonKey = true; }
else { hasVampireKey = true; }
}
}
@@ -13,11 +13,13 @@ public class Wizard extends Player{
@Override
public void lightAttack(Entity target) {
super.lightAttack(target);
attack(target);
}
@Override
public void heavyAttack(Entity target) {
super.heavyAttack(target);
if (getMP() >= weapon.getManaCost()) {
setAttackMultiplier(1.6);
attack(target);
Binary file not shown.