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