removed 'runnable' functionality of GameEngine class

main
mike 2023-04-15 18:56:36 -04:00
parent a4eec6ecc6
commit 0228d6947a
2 changed files with 43 additions and 26 deletions

View File

@ -13,30 +13,24 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Random; import java.util.Random;
public class GameEngine implements Runnable { public class GameEngine {
public static final double GOLD_FACTOR = 5; public static final double GOLD_FACTOR = 5;
public static final double IRON_FACTOR = 1; public static final double IRON_FACTOR = 1;
public static final double WOOD_FACTOR = 0.1; public static final double WOOD_FACTOR = 0.1;
private Player player;
boolean running = true;
private float pillageFactor = 0.5f; private float pillageFactor = 0.5f;
private int currentTime; private int currentTime;
private final Random random = new Random(System.nanoTime()); private final Random random = new Random(System.nanoTime());
public Map map;
public GameDisplay view; public GameDisplay view;
public GameEngine() { public GameEngine() {
player = new Player();
map = generateInitialMap();
} }
public void attackVillage(Map map) { public void attackVillage(Map attacking, Map defending) {
// int defenseiveCounter = 1; // int defenseiveCounter = 1;
// int inhabCounter = 0; // int inhabCounter = 0;
// for (Building b : map.contains) // for (Building b : map.contains)
@ -53,31 +47,31 @@ public class GameEngine implements Runnable {
// this.map.getTownHall().addWood((int) (map.getTownHall().getCurrentWood() * pillageFactor)); // this.map.getTownHall().addWood((int) (map.getTownHall().getCurrentWood() * pillageFactor));
// this.map.getTownHall().addIron((int) (map.getTownHall().getCurrentIron() * pillageFactor)); // this.map.getTownHall().addIron((int) (map.getTownHall().getCurrentIron() * pillageFactor));
// this.map.getTownHall().addGold((int) (map.getTownHall().getCurrentGold() * pillageFactor)); // this.map.getTownHall().addGold((int) (map.getTownHall().getCurrentGold() * pillageFactor));
ChallengeAdapter adapter = new ChallengeAdapter(this.map); ChallengeAdapter adapter = new ChallengeAdapter(attacking);
adapter.attack(map); adapter.attack(defending);
} }
public Map generateInitialMap(){ public Map generateInitialMap(){
return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30); return new Map(new CasaDeNarino(1, VillageHallStages.villageStages[0]), 30);
} }
public Map generateMap() { public Map generateMap(Map map) {
Map initialMap = generateInitialMap(); Map initialMap = generateInitialMap();
CasaDeNarino hall = initialMap.getTownHall(); CasaDeNarino hall = initialMap.getTownHall();
// generate a similar town hall // generate a similar town hall
int levelChange = random.nextInt(2) - 1; int levelChange = random.nextInt(2) - 1;
int nextLevel = this.map.getTownHall().getLevel() + levelChange; int nextLevel = map.getTownHall().getLevel() + levelChange;
// only need to change if the new village level is higher than initial // only need to change if the new village level is higher than initial
if (nextLevel > 0) if (nextLevel > 0)
hall.upgrade(VillageHallStages.villageStages[nextLevel]); hall.upgrade(VillageHallStages.villageStages[nextLevel]);
hall.addWood(this.map.getTownHall().getCurrentWood() + random.nextInt(500) - 150); hall.addWood(map.getTownHall().getCurrentWood() + random.nextInt(500) - 150);
hall.addIron(this.map.getTownHall().getCurrentIron() + random.nextInt(500) - 150); hall.addIron(map.getTownHall().getCurrentIron() + random.nextInt(500) - 150);
hall.addGold(this.map.getTownHall().getCurrentGold() + random.nextInt(500) - 150); hall.addGold(map.getTownHall().getCurrentGold() + random.nextInt(500) - 150);
int buildingCount = this.map.contains.size(); int buildingCount = map.contains.size();
int saulGoodMines = 0; int saulGoodMines = 0;
int ironMines = 0; int ironMines = 0;
@ -86,7 +80,7 @@ public class GameEngine implements Runnable {
int cannons = 0; int cannons = 0;
// count buildings in our map // count buildings in our map
for (Building b : this.map.contains){ for (Building b : map.contains){
if (b instanceof SaulGoodMine) if (b instanceof SaulGoodMine)
saulGoodMines++; saulGoodMines++;
else if (b instanceof IronMine) else if (b instanceof IronMine)
@ -145,6 +139,35 @@ public class GameEngine implements Runnable {
return score; return score;
} }
public void updateMap(Map map) {
for (Building b : map.contains){
if ((b instanceof ResourceBuilding)) {
((ResourceBuilding) b).update(map.getTownHall());
}
}
}
public boolean build (Map map, String buildingArg) {
BuildingFactory bfactory = new BuildingFactory();
Building type = bfactory.getBuilding(buildingArg);
return map.build(new Tile(), type);
}
public boolean train (Map map, String inhabitantArgs) {
InhabitantFactory ifactory = new InhabitantFactory();
Inhabitant type = ifactory.getInhabitant(inhabitantArgs);
return map.train(type);
}
public boolean upgradeBuilding (Map map, int buildingIndex) {
return map.upgradeBuilding(buildingIndex);
}
public boolean upgradeInhabitant (Map map, int inhabitantIndex) {
return map.upgradeInhabitant(inhabitantIndex);
}
/*
@Override @Override
public void run() { public void run() {
String in; String in;
@ -156,11 +179,6 @@ public class GameEngine implements Runnable {
Map exploringMap = null; Map exploringMap = null;
boolean deleteMyHeart = true; boolean deleteMyHeart = true;
while (running) { while (running) {
for (Building b : this.map.contains){
if ((b instanceof ResourceBuilding)) {
((ResourceBuilding) b).update(this.map.getTownHall());
}
}
try { try {
if ((in = view.nextInput()) != null) { if ((in = view.nextInput()) != null) {
String[] args = in.split(" "); String[] args = in.split(" ");
@ -249,8 +267,7 @@ public class GameEngine implements Runnable {
if (deleteMyHeart) if (deleteMyHeart)
exploringMap = null; exploringMap = null;
} }
save("test.xml", this.map); } */
}
public void save(String file, Map map){ public void save(String file, Map map){
try (XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) { try (XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(Files.newOutputStream(Paths.get(file))))) {

View File

@ -56,7 +56,7 @@ public class Map implements Serializable {
public boolean upgradeBuilding(int buildingIndex) { public boolean upgradeBuilding(int buildingIndex) {
if (buildingIndex >= contains.size()) return false; if (buildingIndex >= contains.size() || buildingIndex < 0) return false;
Building b = contains.get(buildingIndex); Building b = contains.get(buildingIndex);
@ -93,7 +93,7 @@ public class Map implements Serializable {
public boolean upgradeInhabitant(int inhabitantIndex) { public boolean upgradeInhabitant(int inhabitantIndex) {
if (inhabitantIndex >= inhabitants.size()) return false; if (inhabitantIndex >= inhabitants.size() || inhabitantIndex < 0) return false;
Inhabitant i = inhabitants.get(inhabitantIndex); Inhabitant i = inhabitants.get(inhabitantIndex);