Browse Source

refactoring: Created ArrayList for all enemy mobs

remotes/origin/fight-engine
fdai7430 2 years ago
parent
commit
2b44d99b7a
  1. 33
      src/main/java/org/bitbiome/commands/UseCommand.java

33
src/main/java/org/bitbiome/commands/UseCommand.java

@ -7,17 +7,22 @@ import org.bitbiome.classes.*;
import org.bitbiome.entities.*; import org.bitbiome.entities.*;
public class UseCommand implements CommandAPI { public class UseCommand implements CommandAPI {
ArrayList<Mob> enemies = new ArrayList<Mob>();
@Override @Override
public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine engine) { public void performCommand(Scanner scanner, boolean isRunning, String message, TravelEngine engine) {
ArrayList<Mob> allMobs = engine.getPlayer().getLocation().getMobList();
for(int i = 0; i<allMobs.size(); i++) {
Mob mob = allMobs.get(i);
if(!mob.isFriendly())
enemies.add(mob);
}
do { do {
System.out.println(getUseMessage(message.split(" ", 2)[1], engine)); System.out.println(getUseMessage(message.split(" ", 2)[1], engine));
if(engine.getPlayer().getHp() <= 0) if(engine.getPlayer().getHp() <= 0)
System.exit(0); System.exit(0);
Location loc = engine.getPlayer().getLocation();
ArrayList<Mob> mobs = loc.getMobList();
for(int i = 0; i<mobs.size(); i++) {
Mob mob = mobs.get(i);
if(!mob.isFriendly()) {
for(int i = 0; i<enemies.size(); i++) {
Mob mob = enemies.get(i);
float hp = engine.getPlayer().getHp(); float hp = engine.getPlayer().getHp();
hp -= mob.getDamage(); hp -= mob.getDamage();
System.out.println(mob.getName() + " attacked you for " + mob.getDamage() + " damage."); System.out.println(mob.getName() + " attacked you for " + mob.getDamage() + " damage.");
@ -28,8 +33,7 @@ public class UseCommand implements CommandAPI {
} }
engine.getPlayer().setHp(hp); engine.getPlayer().setHp(hp);
} }
}
} while(getNumOfEnemyMobs(engine) > 0);
} while(enemies.size() > 0);
} }
private String getUseMessage(String msg, TravelEngine engine) { private String getUseMessage(String msg, TravelEngine engine) {
@ -72,26 +76,17 @@ public class UseCommand implements CommandAPI {
} }
private String useItem(Item item, Mob target, Location location) { private String useItem(Item item, Mob target, Location location) {
if(target.isFriendly())
enemies.add(target);
float hp = target.getHp(); float hp = target.getHp();
float dmg = item.getDamage(); float dmg = item.getDamage();
if(dmg>=hp) { if(dmg>=hp) {
location.getMobList().remove(target); location.getMobList().remove(target);
enemies.remove(target);
return "You killed " + target.getName() + " with " + item.getName(); return "You killed " + target.getName() + " with " + item.getName();
} }
target.setHp(hp-dmg); target.setHp(hp-dmg);
target.setFriendly(false); target.setFriendly(false);
return "You used " + item.getName() + " on " + target.getName(); return "You used " + item.getName() + " on " + target.getName();
} }
private int getNumOfEnemyMobs(TravelEngine engine) {
Location loc = engine.getPlayer().getLocation();
ArrayList<Mob> mobs = loc.getMobList();
int count = 0;
for(int i = 0; i<mobs.size(); i++) {
Mob mob = mobs.get(i);
if(!mob.isFriendly())
count++;
}
return count;
}
} }
Loading…
Cancel
Save