revert game engine

main
Brett 2023-04-13 23:40:41 -04:00
parent 3ce19169cc
commit d5bbf1a930
2 changed files with 28 additions and 38 deletions

View File

@ -8,7 +8,7 @@ import ca.cosc3p91.a4.userinterface.GameDisplay;
import ca.cosc3p91.a4.util.ChallengeAdapter; import ca.cosc3p91.a4.util.ChallengeAdapter;
import java.beans.XMLEncoder; import java.beans.XMLEncoder;
import java.io.*; import java.io.BufferedOutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Random; import java.util.Random;
@ -21,25 +21,19 @@ public class GameEngine implements Runnable {
private Player player; private Player player;
boolean running = true; boolean running = true;
public boolean execFlag = true;
private float pillageFactor = 0.5f; private float pillageFactor = 0.5f;
private int currentTime; private int currentTime;
private final InputStream input;
private final OutputStream output;
public ObjectOutputStream mapOutput;
private final Random random = new Random(System.nanoTime()); private final Random random = new Random(System.nanoTime());
public Map map; public Map map;
public GameDisplay view; public GameDisplay view;
public GameEngine(InputStream commandStream, OutputStream outputBuf) { public GameEngine() {
player = new Player(); player = new Player();
map = generateInitialMap(); map = generateInitialMap();
input = commandStream;
output = outputBuf;
} }
public void attackVillage(Map map) { public void attackVillage(Map map) {
@ -155,23 +149,21 @@ public class GameEngine implements Runnable {
public void run() { public void run() {
String in; String in;
view = new GameDisplay(input); view = new GameDisplay();
view.printVillageState(this.map,"Current Village State"); view.printVillageState(this.map,"Current Village State");
view.printGameMenu(); view.printGameMenu();
Map exploringMap = null; Map exploringMap = null;
boolean deleteMyHeart = true; boolean deleteMyHeart = true;
while (running) { while (running) {
execFlag = true;
for (Building b : this.map.contains){ for (Building b : this.map.contains){
if ((b instanceof ResourceBuilding)) { if ((b instanceof ResourceBuilding)) {
((ResourceBuilding) b).update(this.map.getTownHall()); ((ResourceBuilding) b).update(this.map.getTownHall());
} }
} }
try { try {
if ((in = view.nextInput()) != null && !in.isEmpty()) { if ((in = view.nextInput()) != null) {
String[] args = in.split(" "); String[] args = in.split(" ");
if (in.charAt(0) == '0') continue;
view.printLastInput(); view.printLastInput();
// reset the map if they aren't exploring // reset the map if they aren't exploring
@ -180,53 +172,52 @@ public class GameEngine implements Runnable {
switch (in.charAt(0)) { switch (in.charAt(0)) {
case '1': case '1':
if (args.length < 2) { if (args.length < 2) {
output.write("Args must include type".getBytes()); System.err.println("Args must include type!");
} else { } else {
BuildingFactory bfactory = new BuildingFactory(); BuildingFactory bfactory = new BuildingFactory();
Building type = bfactory.getBuilding(args[1]); Building type = bfactory.getBuilding(args[1]);
if (type == null) { if (type == null)
output.write("Args are not a valid building!".getBytes()); System.err.println("Args are not a valid building!");
} else if (this.map.build(new Tile(), type) ) { else if (this.map.build(new Tile(), type) ) {
output.write((type.getClass().getSimpleName()+" successfully built\n").getBytes()); System.out.println(type.getClass().getSimpleName()+" successfully built\n");
} else { } else
output.write(("Missing resources to build "+type.getClass().getSimpleName()).getBytes()); System.out.println("Missing resources to build "+type.getClass().getSimpleName());
}
} }
break; break;
case '2': case '2':
if (args.length < 2) { if (args.length < 2) {
output.write("Args must include type".getBytes()); System.err.println("Args must include type!");
} else { } else {
InhabitantFactory ifactory = new InhabitantFactory(); InhabitantFactory ifactory = new InhabitantFactory();
Inhabitant type = ifactory.getInhabitant(args[1]); Inhabitant type = ifactory.getInhabitant(args[1]);
if (type == null) { if (type == null)
output.write("Args are not a valid inhabitant!".getBytes()); System.err.println("Args are not a valid inhabitant!");
} else if (this.map.train(type) ) { else if (this.map.train(type) ) {
output.write(("successfully trained a(n) "+type.getClass().getSimpleName()).getBytes()); System.out.println("successfully trained a(n) "+type.getClass().getSimpleName());
} else output.write(("Missing resources to train "+type.getClass().getSimpleName()).getBytes()); } else System.out.println("Missing gold to train "+type.getClass().getSimpleName());
} }
break; break;
case '3': case '3':
if (args.length < 2) { if (args.length < 2) {
output.write("Args must include type".getBytes()); System.err.println("Args must include type!");
} else { } else {
int unitIndex = Integer.parseInt(args[1].substring(1)); int unitIndex = Integer.parseInt(args[1].substring(1));
if (unitIndex < 0) { if (unitIndex < 0) {
output.write("Invalid Index".getBytes()); System.err.println("Invalid Index");
break; break;
} }
if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) { if (args[1].contains("i") && (unitIndex < map.inhabitants.size()) ) {
if ( map.upgradeInhabitant(unitIndex) ) { if ( map.upgradeInhabitant(unitIndex) ) {
output.write(("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName()).getBytes()); System.out.println("successfully upgraded a(n) "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
} else output.write(("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName()).getBytes()); } else System.out.println("Missing Resources to upgrade "+map.inhabitants.get(unitIndex).getClass().getSimpleName());
} else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) { } else if (args[1].contains("b") && (unitIndex < map.contains.size()) ) {
if ( map.upgradeBuilding(unitIndex) ) { if ( map.upgradeBuilding(unitIndex) ) {
output.write(("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName()).getBytes()); System.out.println("successfully upgraded a(n) "+map.contains.get(unitIndex).getClass().getSimpleName());
} else output.write(("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName()).getBytes()); } else System.out.println("Missing Resources to upgrade "+map.contains.get(unitIndex).getClass().getSimpleName());
} else { } else {
output.write("Args is not a valid unit".getBytes()); System.err.println("Args are not a valid unit!");
} }
} }
break; break;
@ -257,7 +248,6 @@ public class GameEngine implements Runnable {
} }
if (deleteMyHeart) if (deleteMyHeart)
exploringMap = null; exploringMap = null;
execFlag = false;
} }
save("test.xml", this.map); save("test.xml", this.map);
} }

View File

@ -17,8 +17,8 @@ public class Server implements Runnable {
private final HashMap<Long, ConnectedClient> clients = new HashMap<>(); private final HashMap<Long, ConnectedClient> clients = new HashMap<>();
private long clientAssignmentID = 0; private long clientAssignmentID = 0;
private long lastMessageID = 0; private long lastMessageID = 0;
private DatagramSocket socket; private final DatagramSocket socket;
private Thread ioThread; private final Thread ioThread;
private GameEngine mainEngine; private GameEngine mainEngine;
@ -43,7 +43,7 @@ public class Server implements Runnable {
long clientID = stream.readLong(); long clientID = stream.readLong();
if (packetID == PacketTable.CONNECT){ if (packetID == PacketTable.CONNECT){
clients.put(++clientAssignmentID, new ConnectedClient(socket, clientID receivePacket.getAddress(), receivePacket.getPort())); clients.put(++clientAssignmentID, new ConnectedClient(socket, clientID, receivePacket.getAddress(), receivePacket.getPort()));
} else if (packetID == PacketTable.DISCONNECT){ } else if (packetID == PacketTable.DISCONNECT){
clients.put(clientID, null); clients.put(clientID, null);
} else { } else {
@ -69,7 +69,7 @@ public class Server implements Runnable {
byte[] sendData = new byte[1284]; byte[] sendData = new byte[1284];
stream_in = new ByteArrayInputStream(receiveData); stream_in = new ByteArrayInputStream(receiveData);
stream_out = new ByteArrayOutputStream(1284); stream_out = new ByteArrayOutputStream(1284);
new Thread(mainEngine = new GameEngine(stream_in,stream_out)).start(); new Thread(mainEngine = new GameEngine()).start();
while(true) { while(true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket); serverSocket.receive(receivePacket);