This commit is contained in:
Sem van der Hoeven
2020-06-07 13:43:05 +02:00
parent 7abe8e22a1
commit 1be186e113
8 changed files with 124 additions and 25 deletions

View File

@@ -37,7 +37,7 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
float screenWidth; float screenWidth;
float screenHeight; float screenHeight;
private FrameRate frameRate; private FrameRate frameRate;
private Thread client; private Client client;
private OrthographicCamera camera; private OrthographicCamera camera;
private GameInputProcessor gameInputProcessor; private GameInputProcessor gameInputProcessor;
private GameCharacter selectedCharacter; private GameCharacter selectedCharacter;
@@ -49,6 +49,9 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
private GAMESTATE gamestate; private GAMESTATE gamestate;
private Faction chosenFaction; private Faction chosenFaction;
private long lastTimeCounted = 0; private long lastTimeCounted = 0;
private boolean gameOver = false;
private int turn = 0;
private boolean playersTurn = true;
private Map map; private Map map;
public MapRenderer mapRenderer; public MapRenderer mapRenderer;
@@ -130,18 +133,14 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
mapRenderer.getGameTiles()[3][width - (i + 1)].visit(temp2); mapRenderer.getGameTiles()[3][width - (i + 1)].visit(temp2);
if (chosenFaction == Faction.HACKER) { if (chosenFaction == Faction.HACKER) {
System.out.println("adding " + temp);
this.team.addMember(temp); this.team.addMember(temp);
this.enemyTeam.addMember(temp2); this.enemyTeam.addMember(temp2);
} if (chosenFaction == Faction.MEGACORPORATION) { } if (chosenFaction == Faction.MEGACORPORATION) {
System.out.println("adding " + temp2);
this.team.addMember(temp2); this.team.addMember(temp2);
this.enemyTeam.addMember(temp); this.enemyTeam.addMember(temp);
} }
} }
System.out.println(this.team);
System.out.println(this.enemyTeam);
this.setSelectedCharacter(this.team.get(0)); this.setSelectedCharacter(this.team.get(0));
} }
@@ -158,14 +157,25 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
private void connectToServer() { private void connectToServer() {
client = new Thread(new Client("localhost", this)); client = new Client("localhost",this);
Thread t = new Thread(client);
try { try {
client.start(); t.start();
} catch (Exception e) { } catch (Exception e) {
System.out.println("There was an error connecting : " + e.getMessage()); System.out.println("There was an error connecting : " + e.getMessage());
} }
} }
private void clearRender() {
clearRender(0,0,0,1);
}
private void clearRender(float r, float g, float b, float alpha) {
Gdx.gl.glClearColor(r/255f, g/255f, b/255f, alpha);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
/** /**
* render method that is called after the update method * render method that is called after the update method
*/ */
@@ -174,13 +184,23 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
if (this.gamestate == GAMESTATE.PLAYING) { if (this.gamestate == GAMESTATE.PLAYING) {
update(); update();
// clear screen // clear screen
Gdx.gl.glClearColor(0, 0, 0, 1); clearRender();
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mapRenderer.render(); mapRenderer.render();
frameRate.render(); frameRate.render();
renderText(); renderText();
renderTurnText();
} else if (this.gamestate == GAMESTATE.SELECTING_FACTION) { } else if (this.gamestate == GAMESTATE.SELECTING_FACTION) {
clearRender(67, 168, 186,1);
renderString("FACTION SELECT\nPress 1 for mega corporation, press 2 for hackers", Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f); renderString("FACTION SELECT\nPress 1 for mega corporation, press 2 for hackers", Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
} else if (this.gamestate == GAMESTATE.ENDED) {
clearRender(67, 168, 186,1);
String text = "Game ended!\n";
if (this.enemyTeam.isDead()) {
text += "Congratulations! You won!";
} else if (this.team.isDead()) {
text += "Too bad! You lost!";
}
renderString(text, Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f);
} }
} }
@@ -203,6 +223,12 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
textRenderer.render(text, x - layout.width / 2f, x - layout.height / 2f); textRenderer.render(text, x - layout.width / 2f, x - layout.height / 2f);
} }
private void renderTurnText() {
String text = playersTurn ? "Your turn, moves left: " + (3 - this.turn) : "Other player's turn";
layout.setText(font,text);
textRenderer.render(text, (Gdx.graphics.getWidth() / 2f) - layout.width / 2f,Gdx.graphics.getHeight() - 3);
}
/** /**
* update method that does all calculation before something is being drawn * update method that does all calculation before something is being drawn
*/ */
@@ -213,6 +239,12 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
this.gameInputProcessor.update(); this.gameInputProcessor.update();
this.team.update(Gdx.graphics.getDeltaTime()); this.team.update(Gdx.graphics.getDeltaTime());
this.enemyTeam.update(Gdx.graphics.getDeltaTime()); this.enemyTeam.update(Gdx.graphics.getDeltaTime());
if (this.team.isDead() || this.enemyTeam.isDead()) {
this.setGameOver(true);
}
if (this.isGameOver()) {
this.setGamestate(GAMESTATE.ENDED);
}
} }
@Override @Override
@@ -288,8 +320,38 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
return team; return team;
} }
public void setGameOver(boolean gameOver) {
this.gameOver = gameOver;
}
public boolean isGameOver() {
return gameOver;
}
public void increaseTurn() {
this.turn++;
if (turn == 3) {
this.turn = 0;
this.setPlayersTurn(false);
}
}
public boolean isPlayersTurn() {
return this.playersTurn;
}
public void setPlayersTurn(boolean playersTurn) {
this.playersTurn = playersTurn;
}
public void send(Data data) {
System.out.println("[MAINGAME] sending data " + data);
this.client.send(data);
}
@Override @Override
public void onDataReceived(Data data) { public void onDataReceived(Data data) {
System.out.println("[MAINGAME] Got data: " + data.toString());
} }
} }

View File

@@ -2,5 +2,6 @@ package netwerkprog.game.client.game;
public enum GAMESTATE { public enum GAMESTATE {
PLAYING, PLAYING,
SELECTING_FACTION SELECTING_FACTION,
ENDED
} }

View File

@@ -33,7 +33,16 @@ public class Client extends Controller {
@Override @Override
public void run() { public void run() {
this.connect(); this.connect();
this.receiveThread.start(); try {
if (this.receiveThread != null){
System.out.println("[CLIENT RUN] starting receive thread");
this.receiveThread.start();
}
else System.out.println("[CLIENT] couldnt connect to server, the receiving thread was null!");
} catch (Exception e) {
System.out.println("[CLIENT] error connecting to server: " + e.getMessage() + ", cause: " + e.getCause().toString());
}
} }
/** /**
@@ -47,7 +56,7 @@ public class Client extends Controller {
ObjectInputStream in = new ObjectInputStream(socket.getInputStream()); ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
this.outputStream = new ObjectOutputStream(socket.getOutputStream()); this.outputStream = new ObjectOutputStream(socket.getOutputStream());
register(in); register(in);
this.receiveThread = new Thread( () -> receive(in)); this.receiveThread = new Thread(() -> receive(in));
} catch (IOException e) { } catch (IOException e) {
this.connecting = false; this.connecting = false;
System.out.println("[CLIENT] there was an error connecting : " + e.getMessage()); System.out.println("[CLIENT] there was an error connecting : " + e.getMessage());
@@ -67,7 +76,7 @@ public class Client extends Controller {
Data data = (Data) object; Data data = (Data) object;
if (data.getPayload() instanceof ConnectionData) { if (data.getPayload() instanceof ConnectionData) {
ConnectionData connectionData = (ConnectionData) data.getPayload(); ConnectionData connectionData = (ConnectionData) data.getPayload();
if (connectionData.getAction().equals("Connect") && connectionData.getMessage().equals("Confirm")){ if (connectionData.getAction().equals("Connect") && connectionData.getMessage().equals("Confirm")) {
this.connecting = false; this.connecting = false;
this.isConnected = true; this.isConnected = true;
} }
@@ -81,10 +90,12 @@ public class Client extends Controller {
/** /**
* Sends a message to the server. * Sends a message to the server.
*
* @param data The message to send. * @param data The message to send.
*/ */
public void send(Data data) { public void send(Data data) {
try { try {
System.out.println("[CLIENT] writing data " + data);
this.outputStream.writeObject(data); this.outputStream.writeObject(data);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -93,12 +104,15 @@ public class Client extends Controller {
/** /**
* Receives a message from the server. * Receives a message from the server.
*
* @param in The inputStream * @param in The inputStream
*/ */
public void receive(ObjectInputStream in) { public void receive(ObjectInputStream in) {
System.out.println("[CLIENT RECEIVE] connected: " + isConnected);
while (isConnected) { while (isConnected) {
try { try {
Object object = in.readObject(); Object object = in.readObject();
System.out.println("[CLIENT] got object " + object);
if (object instanceof Data) { if (object instanceof Data) {
callback.onDataReceived((Data) object); callback.onDataReceived((Data) object);
} }

View File

@@ -9,6 +9,7 @@ import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.TimeUtils; import com.badlogic.gdx.utils.TimeUtils;
import netwerkprog.game.client.MainGame; import netwerkprog.game.client.MainGame;
import netwerkprog.game.client.game.GAMESTATE; import netwerkprog.game.client.game.GAMESTATE;
import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.game.Faction; import netwerkprog.game.util.game.Faction;
import netwerkprog.game.util.game.GameCharacter; import netwerkprog.game.util.game.GameCharacter;
@@ -144,8 +145,6 @@ public class GameInputProcessor implements InputProcessor {
Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0); Vector3 touchPoint = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
camera.unproject(touchPoint); camera.unproject(touchPoint);
if (mainGame.getGamestate() == GAMESTATE.PLAYING) { if (mainGame.getGamestate() == GAMESTATE.PLAYING) {
for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) { for (int row = 0; row < mainGame.mapRenderer.getGameTiles().length; row++) {
for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) { for (int col = 0; col < mainGame.mapRenderer.getGameTiles()[0].length; col++) {
GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col]; GameTile gameTile = mainGame.mapRenderer.getGameTiles()[row][col];
@@ -153,19 +152,25 @@ public class GameInputProcessor implements InputProcessor {
if (button == Input.Buttons.LEFT) { if (button == Input.Buttons.LEFT) {
// moving selected character // moving selected character
if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) { if (mainGame.isPlayersTurn()) {
if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) { if (mainGame.hasCharacterSelected() && !gameTile.containsCharacter()) {
removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.visit(mainGame.getSelectedCharacter()); if (gameTile.getSymbol() != '#' && mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row); removeCharacterFromTile(mainGame.getSelectedCharacter());
gameTile.visit(mainGame.getSelectedCharacter());
mainGame.mapRenderer.setSurroundedTilesOfCurrentCharacter(col, row);
mainGame.increaseTurn();
mainGame.send(new Data("move"));
}
} }
}
// clicking on enemy // clicking on enemy
if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) { if (mainGame.hasCharacterSelected() && gameTile.containsCharacter() && gameTile.getCharacter().getFaction() != mainGame.getChosenFaction()) {
if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) { if (mainGame.mapRenderer.getSurroundedTilesOfCurrentCharacter().contains(gameTile)) {
gameTile.getCharacter().damage(10); gameTile.getCharacter().damage(10);
mainGame.increaseTurn();
}
} }
} }
// set selected character // set selected character

View File

@@ -39,4 +39,8 @@ public class Server {
public DataController getDataController() { public DataController getDataController() {
return dataController; return dataController;
} }
public void setDataController(DataController dataController) {
this.dataController = dataController;
}
} }

View File

@@ -29,6 +29,7 @@ public class ServerClient implements Runnable {
public void writeData(Data data) { public void writeData(Data data) {
try { try {
System.out.println("[SERVERCLIENT] writing data " + data);
this.out.writeObject(data); this.out.writeObject(data);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@@ -40,6 +41,7 @@ public class ServerClient implements Runnable {
while (this.isConnected) { while (this.isConnected) {
try { try {
Object object = this.in.readObject(); Object object = this.in.readObject();
System.out.println("[SERVERCLIENT] got object " + object);
if (object instanceof Data) { if (object instanceof Data) {
Data data = (Data) object; Data data = (Data) object;
if (data.getPayload() instanceof ConnectionData) { if (data.getPayload() instanceof ConnectionData) {
@@ -49,7 +51,9 @@ public class ServerClient implements Runnable {
//todo properly remove thread. //todo properly remove thread.
} }
} else { } else {
callback.onDataReceived((Data) this.in.readObject()); // callback.onDataReceived((Data) this.in.readObject());
System.out.println("[SERVERCLIENT] got data: " + data + ", sending callback");
callback.onDataReceived(data);
} }
} }
} catch (IOException e) { } catch (IOException e) {

View File

@@ -1,5 +1,6 @@
package netwerkprog.game.server.controllers; package netwerkprog.game.server.controllers;
import netwerkprog.game.client.MainGame;
import netwerkprog.game.util.data.CharacterData; import netwerkprog.game.util.data.CharacterData;
import netwerkprog.game.util.data.Data; import netwerkprog.game.util.data.Data;
import netwerkprog.game.util.data.DataCallback; import netwerkprog.game.util.data.DataCallback;
@@ -50,6 +51,7 @@ public class DataController implements DataCallback {
@Override @Override
public void onDataReceived(Data data) { public void onDataReceived(Data data) {
System.out.println("[DATACONTROLLER] got data: " + data);
switch (data.getType()) { switch (data.getType()) {
case "Character" : case "Character" :
if (data.getPayload() instanceof CharacterData) { if (data.getPayload() instanceof CharacterData) {

View File

@@ -29,4 +29,11 @@ public class Data implements Serializable {
public Data getPayload() { public Data getPayload() {
return payload; return payload;
} }
@Override
public String toString() {
return "Data{" +
"objectType='" + objectType + '\'' +
'}';
}
} }