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 screenHeight;
private FrameRate frameRate;
private Thread client;
private Client client;
private OrthographicCamera camera;
private GameInputProcessor gameInputProcessor;
private GameCharacter selectedCharacter;
@@ -49,6 +49,9 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
private GAMESTATE gamestate;
private Faction chosenFaction;
private long lastTimeCounted = 0;
private boolean gameOver = false;
private int turn = 0;
private boolean playersTurn = true;
private Map map;
public MapRenderer mapRenderer;
@@ -130,18 +133,14 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
mapRenderer.getGameTiles()[3][width - (i + 1)].visit(temp2);
if (chosenFaction == Faction.HACKER) {
System.out.println("adding " + temp);
this.team.addMember(temp);
this.enemyTeam.addMember(temp2);
} if (chosenFaction == Faction.MEGACORPORATION) {
System.out.println("adding " + temp2);
this.team.addMember(temp2);
this.enemyTeam.addMember(temp);
}
}
System.out.println(this.team);
System.out.println(this.enemyTeam);
this.setSelectedCharacter(this.team.get(0));
}
@@ -158,14 +157,25 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
private void connectToServer() {
client = new Thread(new Client("localhost", this));
client = new Client("localhost",this);
Thread t = new Thread(client);
try {
client.start();
t.start();
} catch (Exception e) {
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
*/
@@ -174,13 +184,23 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
if (this.gamestate == GAMESTATE.PLAYING) {
update();
// clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
clearRender();
mapRenderer.render();
frameRate.render();
renderText();
renderTurnText();
} 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);
} 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);
}
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
*/
@@ -213,6 +239,12 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
this.gameInputProcessor.update();
this.team.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
@@ -288,8 +320,38 @@ public class MainGame extends ApplicationAdapter implements DataCallback {
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
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 {
PLAYING,
SELECTING_FACTION
SELECTING_FACTION,
ENDED
}

View File

@@ -33,7 +33,16 @@ public class Client extends Controller {
@Override
public void run() {
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());
this.outputStream = new ObjectOutputStream(socket.getOutputStream());
register(in);
this.receiveThread = new Thread( () -> receive(in));
this.receiveThread = new Thread(() -> receive(in));
} catch (IOException e) {
this.connecting = false;
System.out.println("[CLIENT] there was an error connecting : " + e.getMessage());
@@ -67,7 +76,7 @@ public class Client extends Controller {
Data data = (Data) object;
if (data.getPayload() instanceof ConnectionData) {
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.isConnected = true;
}
@@ -81,10 +90,12 @@ public class Client extends Controller {
/**
* Sends a message to the server.
*
* @param data The message to send.
*/
public void send(Data data) {
try {
System.out.println("[CLIENT] writing data " + data);
this.outputStream.writeObject(data);
} catch (IOException e) {
e.printStackTrace();
@@ -93,12 +104,15 @@ public class Client extends Controller {
/**
* Receives a message from the server.
*
* @param in The inputStream
*/
public void receive(ObjectInputStream in) {
System.out.println("[CLIENT RECEIVE] connected: " + isConnected);
while (isConnected) {
try {
Object object = in.readObject();
System.out.println("[CLIENT] got object " + object);
if (object instanceof Data) {
callback.onDataReceived((Data) object);
}

View File

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

View File

@@ -39,4 +39,8 @@ public class Server {
public DataController getDataController() {
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) {
try {
System.out.println("[SERVERCLIENT] writing data " + data);
this.out.writeObject(data);
} catch (IOException e) {
e.printStackTrace();
@@ -40,6 +41,7 @@ public class ServerClient implements Runnable {
while (this.isConnected) {
try {
Object object = this.in.readObject();
System.out.println("[SERVERCLIENT] got object " + object);
if (object instanceof Data) {
Data data = (Data) object;
if (data.getPayload() instanceof ConnectionData) {
@@ -49,7 +51,9 @@ public class ServerClient implements Runnable {
//todo properly remove thread.
}
} 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) {

View File

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

View File

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