Working map sharing between server and clients working, apart from some graphics imperfections
parent
ab89f9e348
commit
8f02390188
|
@ -1,6 +1,8 @@
|
|||
package com.emamaker.amazeing.manager;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Net.Protocol;
|
||||
|
@ -11,7 +13,9 @@ import com.emamaker.amazeing.AMazeIng;
|
|||
public class GameClient {
|
||||
|
||||
volatile boolean clientRunning = false;
|
||||
volatile String nextMessage = "";
|
||||
public Socket socket;
|
||||
String s = "", s1 = "";
|
||||
public String addr;
|
||||
public int port;
|
||||
Thread clientThread;
|
||||
|
@ -30,31 +34,51 @@ public class GameClient {
|
|||
public void run() {
|
||||
clientRunning = true;
|
||||
while (clientRunning) {
|
||||
/*
|
||||
* https://www.gamefromscratch.com/post/2014/03/11/LibGDX-Tutorial-10-Basic-
|
||||
* networking.aspx
|
||||
*/
|
||||
if (socket == null) {
|
||||
SocketHints socketHints = new SocketHints();
|
||||
socketHints.connectTimeout = 0;
|
||||
socket = Gdx.net.newClientSocket(Protocol.TCP, addr, port, socketHints);
|
||||
System.out.println("Connected to server!");
|
||||
sendMessagetoClients("AO");
|
||||
} else {
|
||||
if (socket.isConnected()) {
|
||||
// Send messages to the server:
|
||||
// Receive messages from the server
|
||||
try {
|
||||
// write our entered message to the stream
|
||||
socket.getOutputStream().write("AO\n".getBytes());
|
||||
if (socket.getInputStream().available() > 0) {
|
||||
BufferedReader buffer = new BufferedReader(
|
||||
new InputStreamReader(socket.getInputStream()));
|
||||
|
||||
// Read to the next newline (\n) and display that text on labelMessage
|
||||
s = buffer.readLine().toString();
|
||||
System.out.println("Received message from server: " + socket + "!\n" + s);
|
||||
if(s.startsWith("Map")) {
|
||||
s1 = s.replace("Map", "");
|
||||
main.gameManager.generateMaze(main.gameManager.mazeGen.runLenghtDecode(s1));
|
||||
//System.out.println(s1);
|
||||
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Send messages to the client
|
||||
if (!nextMessage.equals(""))
|
||||
try {
|
||||
// write our entered message to the stream
|
||||
socket.getOutputStream().write((nextMessage + "\n").getBytes());
|
||||
} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
nextMessage = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
clientThread.start();
|
||||
}
|
||||
|
||||
public void sendMessagetoClients(String s) {
|
||||
nextMessage += (s + "\n");
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
|
|
@ -13,8 +13,8 @@ import com.emamaker.voxelengine.block.CellId;
|
|||
import com.emamaker.voxelengine.player.Player;
|
||||
|
||||
public class GameManager {
|
||||
|
||||
/*Local manager for local games and server host in multiplayer games*/
|
||||
|
||||
/* Local manager for local games and server host in multiplayer games */
|
||||
|
||||
public MazeGenerator mazeGen;
|
||||
public boolean gameStarted = false;
|
||||
|
@ -22,7 +22,6 @@ public class GameManager {
|
|||
public GameServer server;
|
||||
public GameClient client;
|
||||
AMazeIng main;
|
||||
|
||||
|
||||
ArrayList<MazePlayer> players = new ArrayList<MazePlayer>();
|
||||
|
||||
|
@ -39,13 +38,14 @@ public class GameManager {
|
|||
|
||||
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.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();
|
||||
main.world.render();
|
||||
|
||||
main.world.modelBatch.begin(main.world.cam);
|
||||
if(players != null) {
|
||||
if (players != null) {
|
||||
for (MazePlayer p : players) {
|
||||
p.render(main.world.modelBatch, main.world.environment);
|
||||
anyoneWon = false;
|
||||
|
@ -57,13 +57,14 @@ public class GameManager {
|
|||
}
|
||||
}
|
||||
|
||||
if (anyoneWon) main.setScreen(main.uiManager.playersScreen);
|
||||
if (anyoneWon)
|
||||
main.setScreen(main.uiManager.playersScreen);
|
||||
main.world.modelBatch.end();
|
||||
}
|
||||
}
|
||||
|
||||
ArrayList<MazePlayer> toDelete = new ArrayList<MazePlayer>();
|
||||
public void generateMaze(Set<MazePlayer> pl) {
|
||||
public void generateMaze(Set<MazePlayer> pl, int todraw[][]) {
|
||||
main.setScreen(null);
|
||||
anyoneWon = false;
|
||||
|
||||
|
@ -99,11 +100,16 @@ public class GameManager {
|
|||
|
||||
mazeGen.setMazeSize(MazeSettings.MAZEX, MazeSettings.MAZEZ);
|
||||
mazeGen.generateMaze();
|
||||
|
||||
|
||||
spreadPlayers();
|
||||
mazeGen.setupEndPoint();
|
||||
|
||||
if(todraw != null) {
|
||||
mazeGen.show(todraw);
|
||||
}
|
||||
|
||||
|
||||
gameStarted = true;
|
||||
|
||||
}
|
||||
|
||||
public void spreadPlayers() {
|
||||
|
@ -176,6 +182,18 @@ public class GameManager {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void generateMaze() {
|
||||
generateMaze(null, null);
|
||||
}
|
||||
|
||||
public void generateMaze(int todraw[][]) {
|
||||
generateMaze(null, todraw);
|
||||
}
|
||||
public void generateMaze(Set<MazePlayer> pl) {
|
||||
generateMaze(pl, null);
|
||||
}
|
||||
|
||||
|
||||
public void dispose() {
|
||||
client.stop();
|
||||
server.stop();
|
||||
|
|
|
@ -4,7 +4,6 @@ import java.io.BufferedReader;
|
|||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Net.Protocol;
|
||||
|
@ -18,7 +17,7 @@ import com.emamaker.amazeing.AMazeIng;
|
|||
public class GameServer {
|
||||
|
||||
volatile boolean serverRunning = false;
|
||||
String nextMessage = "oa";
|
||||
volatile String nextMessage = "";
|
||||
|
||||
public ServerSocket serverSocket = null;
|
||||
public int port;
|
||||
|
@ -58,18 +57,32 @@ public class GameServer {
|
|||
} catch (GdxRuntimeException se) {
|
||||
// System.out.println("No new clients connected in the last 100 milliseconds");
|
||||
}
|
||||
for (Socket s : clientSockets) {
|
||||
System.out.println(s);
|
||||
// Receive messages from the server
|
||||
try {
|
||||
BufferedReader buffer = new BufferedReader(new InputStreamReader(s.getInputStream()));
|
||||
|
||||
// Read to the next newline (\n) and display that text on labelMessage
|
||||
System.out.println("Server received message From: " + s + "!\n" + buffer.readLine());
|
||||
} catch (IOException e) {
|
||||
// e.printStackTrace();
|
||||
for (Socket s : clientSockets) {
|
||||
if (s.isConnected()) {
|
||||
// Receive messages from the client
|
||||
try {
|
||||
if (s.getInputStream().available() > 0) {
|
||||
BufferedReader buffer = new BufferedReader(
|
||||
new InputStreamReader(s.getInputStream()));
|
||||
|
||||
// Read to the next newline (\n) and display that text on labelMessage
|
||||
System.out
|
||||
.println("Server received message From: " + s + "!\n" + buffer.readLine());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
}
|
||||
// Send messages to the client
|
||||
if (!nextMessage.equals(""))
|
||||
try {
|
||||
// write our entered message to the stream
|
||||
s.getOutputStream().write((nextMessage + "\n").getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
nextMessage = "";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -77,15 +90,17 @@ public class GameServer {
|
|||
serverThread.start();
|
||||
}
|
||||
|
||||
public void sendMessagetoClients(String s) {
|
||||
nextMessage += (s + "\n");
|
||||
}
|
||||
|
||||
// Once the server has started accepting connections from other players, the
|
||||
// host should decide when to start the gmae
|
||||
// A proper ui should be added, but for now we can just start the game without
|
||||
// showing any players and just show the map across all the clients
|
||||
public void startGame() {
|
||||
// Once the server has started accepting connections from other players, the
|
||||
// host should decide when to start the gmae
|
||||
// A proper ui should be added, but for now we can just start the game without
|
||||
// showing any players and just show the map across all the clients
|
||||
// To spread the map we can just encode in a string the todraw[][] array in
|
||||
// MazeGenerator, in future run-lenght encoding should be considered
|
||||
main.gameManager.generateMaze(null);
|
||||
nextMessage = "Map" + Arrays.deepToString(main.gameManager.mazeGen.todraw);
|
||||
main.gameManager.generateMaze();
|
||||
sendMessagetoClients("Map" + main.gameManager.mazeGen.runLenghtEncode());
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.emamaker.amazeing.maze;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
import com.emamaker.amazeing.AMazeIng;
|
||||
|
@ -30,26 +31,21 @@ public class MazeGenerator {
|
|||
}
|
||||
|
||||
public void setMazeSize(int w_, int h_) {
|
||||
w = w_+(1-w%2);
|
||||
h = h_+(1-h%2);
|
||||
W = (w-1) / 2;
|
||||
H = (h-1) / 2;
|
||||
// W = w_;
|
||||
// H = h_;
|
||||
//
|
||||
// w = 2 * W + 1;
|
||||
// h = 2 * H + 1;
|
||||
|
||||
/*
|
||||
* w-1 = 2*W
|
||||
* W = w-1)
|
||||
*/
|
||||
|
||||
w = w_ + (1 - w % 2);
|
||||
h = h_ + (1 - h % 2);
|
||||
W = (w - 1) / 2;
|
||||
H = (h - 1) / 2;
|
||||
|
||||
cellsGrid = new Cell[W][H];
|
||||
todraw = new int[w][h];
|
||||
System.out.println(W + "*" + H + " --- " + w + "*" + h);
|
||||
}
|
||||
|
||||
/*
|
||||
* Implementation of a recursive backtracker to generated the maze. Mazes
|
||||
* generated in this way can always be solved.
|
||||
* https://en.wikipedia.org/wiki/Maze_generation_algorithm#Recursive_backtracker
|
||||
*/
|
||||
public void generateMaze() {
|
||||
// init cells
|
||||
for (int i = 0; i < W; i++) {
|
||||
|
@ -108,27 +104,98 @@ public class MazeGenerator {
|
|||
currentCell.current = true;
|
||||
}
|
||||
}
|
||||
prepareShow(todraw);
|
||||
prepareShow();
|
||||
}
|
||||
|
||||
//Setup end point in a random location. At a distance of EP_DIST from every player
|
||||
public void setupEndPoint(){
|
||||
//Randomly spawns all the players
|
||||
//For a spawn location to be valid, it has to be free from both walls and players
|
||||
|
||||
/*
|
||||
* Setup end point in a random location. At a distance of EP_DIST from every
|
||||
* player
|
||||
*/
|
||||
public void setupEndPoint() {
|
||||
// Randomly spawns all the players
|
||||
// For a spawn location to be valid, it has to be free from both walls and
|
||||
// players
|
||||
int x = 0, y = 0;
|
||||
|
||||
|
||||
do {
|
||||
x = (Math.abs(rand.nextInt()) % (w));
|
||||
y = (Math.abs(rand.nextInt()) % (h));
|
||||
//while there's a wall in current location pick new location
|
||||
}while(main.gameManager.areTherePlayersNearby(x, y, EP_DIST) || occupiedSpot(x, y));
|
||||
y = (Math.abs(rand.nextInt()) % (h));
|
||||
// while there's a wall in current location pick new location
|
||||
} while (main.gameManager.areTherePlayersNearby(x, y, EP_DIST) || occupiedSpot(x, y));
|
||||
WINX = x;
|
||||
WINZ = y;
|
||||
todraw[x][y] = 2;
|
||||
main.world.worldManager.setCell(WINX, 0, WINZ, CellId.ID_WOOD);
|
||||
}
|
||||
|
||||
public void prepareShow(int[][] todraw_) {
|
||||
/*
|
||||
* Run-lenght encodes ( https://en.wikipedia.org/wiki/Run-length_encoding) the
|
||||
* maze todraw configuration, so it can be easily passed to server clients. This
|
||||
* should be done once the game is been set up and the end point has been
|
||||
* 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
|
||||
* the different block, starting from A (Ascii 65) and adding the todraw[x][y]
|
||||
* index to the ascii value of A.
|
||||
* To even simplify decoding, the count number is encoded in a letter too, starting from
|
||||
* a (Ascii 97), so that every count takes up just to characters.
|
||||
*
|
||||
*/
|
||||
public String runLenghtEncode() {
|
||||
// todraw[x][y], where row number is x and the index of the block in that row is
|
||||
// y
|
||||
int currentBlock = 0;
|
||||
int count = 0;
|
||||
String s = "";
|
||||
for (int i = 0; i < w; i++) {
|
||||
for (int j = 0; j < h; j++) {
|
||||
if(todraw[i][j] != currentBlock || j == h-1) {
|
||||
s+=String.valueOf((char)(97+count))+String.valueOf((char)(65+currentBlock));
|
||||
count = 1;
|
||||
currentBlock = todraw[i][j];
|
||||
}else {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
s += "-";
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* Run length decodes the maze received from the server.
|
||||
* We know that the block types start from A (Ascii 65), so we can simply subtract 65
|
||||
* We know that the block count start from a (Ascii 97), so we can simply subtract 97
|
||||
* from the current index and get the block type, repeated for how many times the count number says
|
||||
*/
|
||||
public int[][] runLenghtDecode(String s) {
|
||||
int[][] todraw = null;
|
||||
int count, type, totalcount = 0;
|
||||
|
||||
//Split the various rows
|
||||
String[] rows = s.split("-");
|
||||
System.out.println(Arrays.deepToString(rows));
|
||||
//Mazes are always squares
|
||||
todraw = new int[rows.length][rows.length];
|
||||
|
||||
for(int i = 0; i < rows.length; i++) {
|
||||
totalcount = 0;
|
||||
for(int j = 0; j < rows[i].length(); j+=2) {
|
||||
count = ((int) (rows[i].charAt(j))) - 97;
|
||||
type = ((int) (rows[i].charAt(j+1))) - 65;
|
||||
|
||||
for(int k = totalcount; k < totalcount+count; k++) {
|
||||
todraw[i][k] = type;
|
||||
}
|
||||
totalcount += count;
|
||||
}
|
||||
}
|
||||
|
||||
return todraw;
|
||||
}
|
||||
|
||||
public void prepareShow() {
|
||||
for (int i = 0; i < w; i++) {
|
||||
for (int j = 0; j < h; j++) {
|
||||
todraw[i][j] = 1;
|
||||
|
@ -143,31 +210,38 @@ public class MazeGenerator {
|
|||
int y = 2 * j + 1;
|
||||
|
||||
// current cell
|
||||
todraw_[x][y] = 0;
|
||||
todraw[x][y] = 0;
|
||||
|
||||
// up wall
|
||||
if (!cellsGrid[i][j].walls[0])
|
||||
todraw_[x][y - 1] = 0;
|
||||
todraw[x][y - 1] = 0;
|
||||
// down wall
|
||||
if (!cellsGrid[i][j].walls[2])
|
||||
todraw_[x][y + 1] = 0;
|
||||
todraw[x][y + 1] = 0;
|
||||
// left wall
|
||||
if (!cellsGrid[i][j].walls[3])
|
||||
todraw_[x - 1][y] = 0;
|
||||
todraw[x - 1][y] = 0;
|
||||
// right all
|
||||
if (!cellsGrid[i][j].walls[1])
|
||||
todraw_[x + 1][y] = 0;
|
||||
todraw[x + 1][y] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//setupEndPoint();
|
||||
show(todraw);
|
||||
}
|
||||
|
||||
public void show(int[][] todraw_) {
|
||||
for (int j = 0; j < h; j++) {
|
||||
for (int i = 0; i < w; i++) {
|
||||
todraw[i][j] = todraw_[i][j];
|
||||
main.world.worldManager.setCell(i, 1, j, CellId.ID_AIR);
|
||||
|
||||
main.world.worldManager.setCell(i, 0, j, CellId.ID_GRASS);
|
||||
if (todraw_[i][j] == 1)
|
||||
|
||||
if (todraw[i][j] == 1)
|
||||
main.world.worldManager.setCell(i, 1, j, CellId.ID_LEAVES);
|
||||
// if (todraw[i][j] == 2)
|
||||
// main.world.worldManager.setCell(i, 0, j, CellId.ID_WOOD);
|
||||
if (todraw[i][j] == 2)
|
||||
main.world.worldManager.setCell(i, 0, j, CellId.ID_WOOD);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -181,11 +255,11 @@ public class MazeGenerator {
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
int cellAt(int x, int y) {
|
||||
return todraw[x][y];
|
||||
}
|
||||
|
||||
|
||||
public boolean occupiedSpot(int x, int y) {
|
||||
return cellAt(x, y) != 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue