Server now starts a client locally to play. Fixed run length encoding bug. Fixed issues with map generation, however there seem to be still issues with dimensions
parent
40bdc9a594
commit
5b512c77bb
|
@ -37,41 +37,7 @@ public class GameManager {
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean anyoneWon = false;
|
boolean anyoneWon = false;
|
||||||
|
|
||||||
public void update() {
|
|
||||||
if (gameStarted) {
|
|
||||||
main.world.cam.position.set(mazeGen.w / 2, (MazeSettings.MAZEX + MazeSettings.MAZEZ) * 0.45f,
|
|
||||||
mazeGen.h / 2);
|
|
||||||
main.world.cam.lookAt(MazeSettings.MAZEX / 2, 0, MazeSettings.MAZEX / 2);
|
|
||||||
main.world.cam.update();
|
|
||||||
if (getShowGame())
|
|
||||||
main.world.render();
|
|
||||||
|
|
||||||
main.world.modelBatch.begin(main.world.cam);
|
|
||||||
if (players != null) {
|
|
||||||
for (MazePlayer p : players) {
|
|
||||||
if (getShowGame())
|
|
||||||
p.render(main.world.modelBatch, main.world.environment);
|
|
||||||
|
|
||||||
anyoneWon = false;
|
|
||||||
if(type != GameType.CLIENT) {
|
|
||||||
if (checkWin(p)) {
|
|
||||||
anyoneWon = true;
|
|
||||||
gameStarted = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (anyoneWon)
|
|
||||||
main.setScreen(main.uiManager.playersScreen);
|
|
||||||
main.world.modelBatch.end();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ArrayList<MazePlayer> toDelete = new ArrayList<MazePlayer>();
|
ArrayList<MazePlayer> toDelete = new ArrayList<MazePlayer>();
|
||||||
|
|
||||||
public void generateMaze(Set<MazePlayer> pl, int todraw[][]) {
|
public void generateMaze(Set<MazePlayer> pl, int todraw[][]) {
|
||||||
main.setScreen(null);
|
main.setScreen(null);
|
||||||
anyoneWon = false;
|
anyoneWon = false;
|
||||||
|
@ -110,6 +76,7 @@ public class GameManager {
|
||||||
|
|
||||||
mazeGen.setMazeSize(MazeSettings.MAZEX, MazeSettings.MAZEZ);
|
mazeGen.setMazeSize(MazeSettings.MAZEX, MazeSettings.MAZEZ);
|
||||||
mazeGen.generateMaze();
|
mazeGen.generateMaze();
|
||||||
|
mazeGen.runLenghtEncode();
|
||||||
|
|
||||||
if (type != GameType.CLIENT) {
|
if (type != GameType.CLIENT) {
|
||||||
spreadPlayers();
|
spreadPlayers();
|
||||||
|
@ -119,9 +86,41 @@ public class GameManager {
|
||||||
if (todraw != null && showGame == true) {
|
if (todraw != null && showGame == true) {
|
||||||
mazeGen.show(todraw);
|
mazeGen.show(todraw);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void update() {
|
||||||
|
if (gameStarted) {
|
||||||
|
main.world.cam.position.set(mazeGen.w / 2, (MazeSettings.MAZEX + MazeSettings.MAZEZ) * 0.45f,
|
||||||
|
mazeGen.h / 2);
|
||||||
|
main.world.cam.lookAt(MazeSettings.MAZEX / 2, 0, MazeSettings.MAZEX / 2);
|
||||||
|
main.world.cam.update();
|
||||||
|
if (getShowGame())
|
||||||
|
main.world.render();
|
||||||
|
|
||||||
|
main.world.modelBatch.begin(main.world.cam);
|
||||||
|
if (players != null) {
|
||||||
|
for (MazePlayer p : players) {
|
||||||
|
if (getShowGame())
|
||||||
|
p.render(main.world.modelBatch, main.world.environment);
|
||||||
|
|
||||||
|
anyoneWon = false;
|
||||||
|
if(type != GameType.CLIENT) {
|
||||||
|
if (checkWin(p)) {
|
||||||
|
anyoneWon = true;
|
||||||
|
gameStarted = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (anyoneWon)
|
||||||
|
main.setScreen(main.uiManager.playersScreen);
|
||||||
|
main.world.modelBatch.end();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void spreadPlayers() {
|
public void spreadPlayers() {
|
||||||
for (MazePlayer p : players) {
|
for (MazePlayer p : players) {
|
||||||
int x = 1, z = 1;
|
int x = 1, z = 1;
|
||||||
|
|
|
@ -108,7 +108,8 @@ public class GameClient {
|
||||||
UpdatePlayerTransform msg = (UpdatePlayerTransform) object;
|
UpdatePlayerTransform msg = (UpdatePlayerTransform) object;
|
||||||
if (!msg.uuid.equals(uuid)) {
|
if (!msg.uuid.equals(uuid)) {
|
||||||
remotePlayers.get(msg.uuid).setPlaying();
|
remotePlayers.get(msg.uuid).setPlaying();
|
||||||
remotePlayers.get(msg.uuid).setTransform(msg.tx, msg.ty, msg.tz, msg.rx, msg.ry, msg.rz, msg.rw);
|
remotePlayers.get(msg.uuid).setTransform(msg.tx, msg.ty, msg.tz, msg.rx, msg.ry, msg.rz,
|
||||||
|
msg.rw);
|
||||||
System.out.println("R: " + msg.tx + ", " + msg.ty + ", " + msg.tz);
|
System.out.println("R: " + msg.tx + ", " + msg.ty + ", " + msg.tz);
|
||||||
System.out.println("Updating remote player with uuid " + msg.uuid.toString());
|
System.out.println("Updating remote player with uuid " + msg.uuid.toString());
|
||||||
}
|
}
|
||||||
|
@ -175,10 +176,13 @@ public class GameClient {
|
||||||
|
|
||||||
players.add(player);
|
players.add(player);
|
||||||
|
|
||||||
|
if (main.getScreen() != null) {
|
||||||
main.getScreen().hide();
|
main.getScreen().hide();
|
||||||
main.setScreen(null);
|
main.setScreen(null);
|
||||||
|
}
|
||||||
|
|
||||||
for(MazePlayer p : players) p.setPlaying();
|
for (MazePlayer p : players)
|
||||||
|
p.setPlaying();
|
||||||
System.out.println(Arrays.toString(players.toArray()));
|
System.out.println(Arrays.toString(players.toArray()));
|
||||||
|
|
||||||
gameManager.generateMaze(new HashSet<MazePlayer>(players));
|
gameManager.generateMaze(new HashSet<MazePlayer>(players));
|
||||||
|
@ -186,7 +190,7 @@ public class GameClient {
|
||||||
}
|
}
|
||||||
if (!map.equals("")) {
|
if (!map.equals("")) {
|
||||||
System.out.println("Setting map");
|
System.out.println("Setting map");
|
||||||
gameManager.mazeGen.runLenghtDecode(map);
|
gameManager.mazeGen.show(gameManager.mazeGen.runLenghtDecode(map));
|
||||||
map = "";
|
map = "";
|
||||||
}
|
}
|
||||||
if (gameManager != null)
|
if (gameManager != null)
|
||||||
|
|
|
@ -139,6 +139,10 @@ public class GameServer {
|
||||||
server.bind(port);
|
server.bind(port);
|
||||||
server.start();
|
server.start();
|
||||||
System.out.println("Server registered and running on port " + port);
|
System.out.println("Server registered and running on port " + port);
|
||||||
|
|
||||||
|
//Also launch the client to have a player play on host
|
||||||
|
main.client.start("localhost", port);
|
||||||
|
System.out.println("Local client ready to play!");
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,10 +31,10 @@ public class MazeGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setMazeSize(int w_, int h_) {
|
public void setMazeSize(int w_, int h_) {
|
||||||
w = w_;
|
w = w_ - 1;
|
||||||
h = h_;
|
h = h_ - 1;
|
||||||
W = (w - 1) / 2;
|
W = w / 2;
|
||||||
H = (h - 1) / 2;
|
H = h / 2;
|
||||||
|
|
||||||
cellsGrid = new Cell[W][H];
|
cellsGrid = new Cell[W][H];
|
||||||
todraw = new int[w][h];
|
todraw = new int[w][h];
|
||||||
|
@ -135,53 +135,59 @@ public class MazeGenerator {
|
||||||
* placed. We'll normally use a number for the count of equal blocks next to
|
* placed. We'll normally use a number for the count of equal blocks next to
|
||||||
* each other We'll use letters instead for the todraw[][] numbers to represent
|
* each other We'll use letters instead for the todraw[][] numbers to represent
|
||||||
* the different block, starting from A (Ascii 65) and adding the todraw[x][y]
|
* the different block, starting from A (Ascii 65) and adding the todraw[x][y]
|
||||||
* index to the ascii value of A.
|
* index to the ascii value of A. To even simplify decoding, the count number is
|
||||||
* To even simplify decoding, the count number is encoded in a letter too, starting from
|
* encoded in a letter too, starting from a (Ascii 97), so that every count
|
||||||
* a (Ascii 97), so that every count takes up just to characters.
|
* takes up just to characters.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public String runLenghtEncode() {
|
public String runLenghtEncode() {
|
||||||
// todraw[x][y], where row number is x and the index of the block in that row is
|
// todraw[x][y], where row number is x and the index of the block in that row is
|
||||||
// y
|
// y
|
||||||
int currentBlock = 0;
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
String s = "";
|
String s = "";
|
||||||
for (int i = 0; i < w; i++) {
|
for (int i = 0; i < w; i++) {
|
||||||
for (int j = 0; j < h; j++) {
|
for (int j = 0; j < h; j++) {
|
||||||
if(todraw[i][j] != currentBlock || j == h-1) {
|
// https://www.geeksforgeeks.org/run-length-encoding/
|
||||||
s+=String.valueOf((char)(97+count))+String.valueOf((char)(65+currentBlock));
|
|
||||||
count = 1;
|
count = 1;
|
||||||
currentBlock = todraw[i][j];
|
while (j < h - 1 && todraw[i][j] == todraw[i][j + 1]) {
|
||||||
}else {
|
|
||||||
count++;
|
count++;
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
|
s += String.valueOf((char)(count + 97));
|
||||||
|
s += String.valueOf((char)(todraw[i][j] + 65));
|
||||||
|
// System.out.println("Got block " + todraw[i][j] + " for " + count + "times");
|
||||||
}
|
}
|
||||||
|
// System.out.println("Going next column");
|
||||||
s += "-";
|
s += "-";
|
||||||
}
|
}
|
||||||
|
System.out.println(s);
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Run length decodes the maze received from the server.
|
* Run length decodes the maze received from the server. We know that the block
|
||||||
* We know that the block types start from A (Ascii 65), so we can simply subtract 65
|
* types start from A (Ascii 65), so we can simply subtract 65 We know that the
|
||||||
* We know that the block count start from a (Ascii 97), so we can simply subtract 97
|
* block count start from a (Ascii 97), so we can simply subtract 97 from the
|
||||||
* from the current index and get the block type, repeated for how many times the count number says
|
* current index and get the block type, repeated for how many times the count
|
||||||
|
* number says
|
||||||
*/
|
*/
|
||||||
public int[][] runLenghtDecode(String s) {
|
public int[][] runLenghtDecode(String s) {
|
||||||
int[][] todraw_ = null;
|
int[][] todraw_ = null;
|
||||||
int count, type, totalcount = 0;
|
int count, type, totalcount = 0, width = 0;
|
||||||
|
|
||||||
// Split the various rows
|
// Split the various rows
|
||||||
String[] rows = s.split("-");
|
String[] rows = s.split("-");
|
||||||
System.out.println(Arrays.deepToString(rows));
|
System.out.println(Arrays.deepToString(rows));
|
||||||
//Mazes are always squares
|
|
||||||
setMazeSize(rows.length, rows.length);
|
|
||||||
//Temporarely patch to the calculation errors in setMazeSize
|
|
||||||
todraw_=new int[rows.length][rows.length];
|
|
||||||
|
|
||||||
for(int i = 0; i < rows.length; i++) {
|
count = ((int) (rows[0].charAt(0))) - 97;
|
||||||
|
width += count;
|
||||||
|
|
||||||
|
// Mazes are always squares
|
||||||
|
setMazeSize(width + 1, rows.length + 1);
|
||||||
|
// Temporarely patch to the calculation errors in setMazeSize
|
||||||
|
todraw_ = new int[w][h];
|
||||||
|
|
||||||
|
for (int i = 0; i < width; i++) {
|
||||||
totalcount = 0;
|
totalcount = 0;
|
||||||
for (int j = 0; j < rows[i].length(); j += 2) {
|
for (int j = 0; j < rows[i].length(); j += 2) {
|
||||||
count = ((int) (rows[i].charAt(j))) - 97;
|
count = ((int) (rows[i].charAt(j))) - 97;
|
||||||
|
@ -193,7 +199,6 @@ public class MazeGenerator {
|
||||||
totalcount += count;
|
totalcount += count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
show(todraw_);
|
|
||||||
return todraw_;
|
return todraw_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -259,7 +264,10 @@ public class MazeGenerator {
|
||||||
}
|
}
|
||||||
|
|
||||||
int cellAt(int x, int y) {
|
int cellAt(int x, int y) {
|
||||||
|
if (x < w && y < h)
|
||||||
return todraw[x][y];
|
return todraw[x][y];
|
||||||
|
else
|
||||||
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean occupiedSpot(int x, int y) {
|
public boolean occupiedSpot(int x, int y) {
|
||||||
|
|
|
@ -102,14 +102,19 @@ public class MazePlayerLocal extends MazePlayer {
|
||||||
((btDiscreteDynamicsWorld) (main.world.dynamicsWorld)).addAction(characterController);
|
((btDiscreteDynamicsWorld) (main.world.dynamicsWorld)).addAction(characterController);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
boolean pressed = false;
|
||||||
|
|
||||||
public void inputs() {
|
public void inputs() {
|
||||||
|
pressed = false;
|
||||||
// If the left or right key is pressed, rotate the character and update its
|
// If the left or right key is pressed, rotate the character and update its
|
||||||
// physics update accordingly.
|
// physics update accordingly.
|
||||||
if (Gdx.input.isKeyPressed(ksx)) {
|
if (Gdx.input.isKeyPressed(ksx)) {
|
||||||
|
pressed = true;
|
||||||
characterTransform.rotate(0, 1, 0, 2.5f);
|
characterTransform.rotate(0, 1, 0, 2.5f);
|
||||||
ghostObject.setWorldTransform(characterTransform);
|
ghostObject.setWorldTransform(characterTransform);
|
||||||
}
|
}
|
||||||
if (Gdx.input.isKeyPressed(kdx)) {
|
if (Gdx.input.isKeyPressed(kdx)) {
|
||||||
|
pressed = true;
|
||||||
characterTransform.rotate(0, 1, 0, -2.5f);
|
characterTransform.rotate(0, 1, 0, -2.5f);
|
||||||
ghostObject.setWorldTransform(characterTransform);
|
ghostObject.setWorldTransform(characterTransform);
|
||||||
}
|
}
|
||||||
|
@ -118,10 +123,14 @@ public class MazePlayerLocal extends MazePlayer {
|
||||||
// Set the walking direction accordingly (either forward or backward)
|
// Set the walking direction accordingly (either forward or backward)
|
||||||
walkDirection.set(0, 0, 0);
|
walkDirection.set(0, 0, 0);
|
||||||
|
|
||||||
if (Gdx.input.isKeyPressed(kup))
|
if (Gdx.input.isKeyPressed(kup)) {
|
||||||
|
pressed = true;
|
||||||
walkDirection.add(characterDirection);
|
walkDirection.add(characterDirection);
|
||||||
if (Gdx.input.isKeyPressed(kdown))
|
}
|
||||||
|
if (Gdx.input.isKeyPressed(kdown)) {
|
||||||
|
pressed = false;
|
||||||
walkDirection.add(-characterDirection.x, -characterDirection.y, -characterDirection.z);
|
walkDirection.add(-characterDirection.x, -characterDirection.y, -characterDirection.z);
|
||||||
|
}
|
||||||
walkDirection.scl(3f * Gdx.graphics.getDeltaTime());
|
walkDirection.scl(3f * Gdx.graphics.getDeltaTime());
|
||||||
// And update the character controller
|
// And update the character controller
|
||||||
characterController.setWalkDirection(walkDirection);
|
characterController.setWalkDirection(walkDirection);
|
||||||
|
@ -129,12 +138,14 @@ public class MazePlayerLocal extends MazePlayer {
|
||||||
// And fetch the new transformation of the character (this will make the model
|
// And fetch the new transformation of the character (this will make the model
|
||||||
// be rendered correctly)
|
// be rendered correctly)
|
||||||
ghostObject.getWorldTransform(characterTransform);
|
ghostObject.getWorldTransform(characterTransform);
|
||||||
|
|
||||||
|
if (pressed)
|
||||||
|
main.client.updateLocalPlayer(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update() {
|
public void update() {
|
||||||
inputs();
|
inputs();
|
||||||
main.client.updateLocalPlayer(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue