Start of networking
parent
a63a7fc835
commit
15979d1d5a
|
@ -6,6 +6,7 @@ import com.badlogic.gdx.Game;
|
|||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.InputMultiplexer;
|
||||
import com.badlogic.gdx.graphics.FPSLogger;
|
||||
import com.badlogic.gdx.math.Vector3;
|
||||
import com.badlogic.gdx.physics.bullet.Bullet;
|
||||
import com.emamaker.amazeing.manager.GameManager;
|
||||
import com.emamaker.amazeing.ui.UIManager;
|
||||
|
|
|
@ -51,10 +51,7 @@ public class GameManager {
|
|||
}
|
||||
}
|
||||
|
||||
if (anyoneWon) {
|
||||
// destroyPlayers();
|
||||
main.setScreen(main.uiManager.playersScreen);
|
||||
}
|
||||
if (anyoneWon) main.setScreen(main.uiManager.playersScreen);
|
||||
main.world.modelBatch.end();
|
||||
}
|
||||
}
|
||||
|
@ -95,14 +92,6 @@ public class GameManager {
|
|||
mazeGen.setMazeSize(MazeSettings.MAZEX, MazeSettings.MAZEZ);
|
||||
mazeGen.generateMaze();
|
||||
|
||||
// Set camera
|
||||
main.world.cam.lookAt(mazeGen.w / 2, 0, mazeGen.h / 2);
|
||||
main.world.cam.rotate(Vector3.X, 15);
|
||||
main.world.cam.update();
|
||||
|
||||
// Init the player with relative movement keys
|
||||
// players.add(generateNewPlayer(Keys.W, Keys.S, Keys.A, Keys.D));
|
||||
// players.add(generateNewPlayer(Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT));
|
||||
spreadPlayers();
|
||||
mazeGen.setupEndPoint();
|
||||
gameStarted = true;
|
||||
|
|
|
@ -5,6 +5,8 @@ import com.badlogic.gdx.Gdx;
|
|||
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
|
||||
import com.emamaker.amazeing.AMazeIng;
|
||||
import com.emamaker.amazeing.ui.screens.PlayerChooseScreen;
|
||||
import com.emamaker.amazeing.ui.screens.ServerJoinScreen;
|
||||
import com.emamaker.amazeing.ui.screens.ServerLaunchScreen;
|
||||
import com.emamaker.amazeing.ui.screens.SettingsScreen;
|
||||
import com.emamaker.amazeing.ui.screens.TitleScreen;
|
||||
|
||||
|
@ -16,6 +18,8 @@ public class UIManager {
|
|||
public TitleScreen titleScreen;
|
||||
public PlayerChooseScreen playersScreen;
|
||||
public SettingsScreen setScreen;
|
||||
public ServerJoinScreen srvJoinScreen;
|
||||
public ServerLaunchScreen srvLaunchScreen;
|
||||
|
||||
public UIManager(Game main_) {
|
||||
main = (AMazeIng)main_;
|
||||
|
@ -26,6 +30,9 @@ public class UIManager {
|
|||
titleScreen = new TitleScreen(this);
|
||||
playersScreen = new PlayerChooseScreen(this);
|
||||
setScreen = new SettingsScreen(this);
|
||||
srvJoinScreen = new ServerJoinScreen(this);
|
||||
srvLaunchScreen = new ServerLaunchScreen(this);
|
||||
|
||||
main.setScreen(titleScreen);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
package com.emamaker.amazeing.ui.screens;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Net.Protocol;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.net.Socket;
|
||||
import com.badlogic.gdx.net.SocketHints;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Container;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextArea;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||
import com.emamaker.amazeing.ui.UIManager;
|
||||
|
||||
public class ServerJoinScreen implements Screen{
|
||||
|
||||
Stage stage;
|
||||
UIManager uiManager;
|
||||
|
||||
public ServerJoinScreen(UIManager uiManager_) {
|
||||
|
||||
uiManager = uiManager_;
|
||||
|
||||
stage = new Stage(new ScreenViewport());
|
||||
Container<Table> tableContainer = new Container<Table>();
|
||||
Table table = new Table();
|
||||
|
||||
float cw = stage.getWidth();
|
||||
float ch = stage.getHeight();
|
||||
|
||||
tableContainer.setSize(cw, ch);
|
||||
tableContainer.setPosition(0, 0);
|
||||
|
||||
Label instLab = new Label("Enter ip address and port and connect to the server!", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("Main menu", uiManager.skin);
|
||||
TextButton connectBtn = new TextButton("Connect to the server!", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
final TextArea srvIp = new TextArea("Server IP Address", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("Server Port", uiManager.skin);
|
||||
|
||||
// Add actions to the buttons
|
||||
backBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.setScreen(uiManager.titleScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
helpBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
System.out.println("Make this appear help dialog (TODO)");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
connectBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
/*https://www.gamefromscratch.com/post/2014/03/11/LibGDX-Tutorial-10-Basic-networking.aspx*/
|
||||
SocketHints socketHints = new SocketHints();
|
||||
// Socket will time our in 4 seconds
|
||||
socketHints.connectTimeout = 4000;
|
||||
//create the socket and connect to the server entered in the text box ( x.x.x.x format ) on port 9021
|
||||
Socket socket = Gdx.net.newClientSocket(Protocol.TCP, srvIp.getText(), Integer.valueOf(srvPort.getText()), socketHints);
|
||||
try {
|
||||
// write our entered message to the stream
|
||||
socket.getOutputStream().write("AO\n".getBytes());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
Table firstRowTable = new Table();
|
||||
firstRowTable.add(backBtn).fillX().expandX().space(cw*0.005f);
|
||||
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw*0.25f);
|
||||
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
|
||||
firstRowTable.setOrigin(Align.center | Align.top);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(firstRowTable);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(srvIp).fillX().expandX();
|
||||
table.row().colspan(4);
|
||||
table.add(srvPort).fillX().expandX();
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(connectBtn).fillX().expandX();
|
||||
|
||||
tableContainer.setActor(table);
|
||||
stage.addActor(tableContainer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
uiManager.main.multiplexer.addProcessor(stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
Gdx.gl.glClearColor(0,0,0,0);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
stage.act();
|
||||
stage.draw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
stage.getViewport().update(width, height, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
uiManager.main.multiplexer.removeProcessor(stage);
|
||||
}
|
||||
@Override
|
||||
public void pause() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
}
|
||||
|
||||
}
|
|
@ -1,49 +1,162 @@
|
|||
package com.emamaker.amazeing.ui.screens;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import com.badlogic.gdx.Gdx;
|
||||
import com.badlogic.gdx.Net.Protocol;
|
||||
import com.badlogic.gdx.Screen;
|
||||
import com.badlogic.gdx.graphics.GL20;
|
||||
import com.badlogic.gdx.net.ServerSocket;
|
||||
import com.badlogic.gdx.net.ServerSocketHints;
|
||||
import com.badlogic.gdx.net.Socket;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputEvent;
|
||||
import com.badlogic.gdx.scenes.scene2d.InputListener;
|
||||
import com.badlogic.gdx.scenes.scene2d.Stage;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Container;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Label;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.Table;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextArea;
|
||||
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
|
||||
import com.badlogic.gdx.utils.Align;
|
||||
import com.badlogic.gdx.utils.viewport.ScreenViewport;
|
||||
import com.emamaker.amazeing.ui.UIManager;
|
||||
|
||||
public class ServerLaunchScreen implements Screen{
|
||||
|
||||
Stage stage;
|
||||
UIManager uiManager;
|
||||
|
||||
public ServerLaunchScreen(UIManager uiManager_) {
|
||||
|
||||
uiManager = uiManager_;
|
||||
|
||||
stage = new Stage(new ScreenViewport());
|
||||
Container<Table> tableContainer = new Container<Table>();
|
||||
Table table = new Table();
|
||||
|
||||
float cw = stage.getWidth();
|
||||
float ch = stage.getHeight();
|
||||
|
||||
tableContainer.setSize(cw, ch);
|
||||
tableContainer.setPosition(0, 0);
|
||||
|
||||
Label instLab = new Label("Enter the port the server should start on", uiManager.skin);
|
||||
TextButton backBtn = new TextButton("Main menu", uiManager.skin);
|
||||
TextButton connectBtn = new TextButton("Launch the server!", uiManager.skin);
|
||||
TextButton helpBtn = new TextButton("?", uiManager.skin);
|
||||
final TextArea srvPort = new TextArea("Server Port", uiManager.skin);
|
||||
|
||||
// Add actions to the buttons
|
||||
backBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.setScreen(uiManager.titleScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
helpBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
System.out.println("Make this appear help dialog (TODO)");
|
||||
return true;
|
||||
}
|
||||
});
|
||||
connectBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
/*https://www.gamefromscratch.com/post/2014/03/11/LibGDX-Tutorial-10-Basic-networking.aspx*/
|
||||
|
||||
// Now we create a thread that will listen for incoming socket connections
|
||||
new Thread(new Runnable(){
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
ServerSocketHints serverSocketHint = new ServerSocketHints();
|
||||
// 0 means no timeout. Probably not the greatest idea in production!
|
||||
serverSocketHint.acceptTimeout = 0;
|
||||
|
||||
// Create the socket server using TCP protocol and listening on 9021
|
||||
// Only one app can listen to a port at a time, keep in mind many ports are reserved
|
||||
// especially in the lower numbers ( like 21, 80, etc )
|
||||
ServerSocket serverSocket = Gdx.net.newServerSocket(Protocol.TCP, Integer.valueOf(srvPort.getText()), serverSocketHint);
|
||||
|
||||
// Loop forever
|
||||
while(true){
|
||||
// Create a socket
|
||||
Socket socket = serverSocket.accept(null);
|
||||
|
||||
// Read data from the socket into a BufferedReader
|
||||
BufferedReader buffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
try {
|
||||
// Read to the next newline (\n) and display that text on labelMessage
|
||||
System.out.println("Server received message!");
|
||||
System.out.println(buffer.readLine());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}).start(); // And, start the thread running
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
Table firstRowTable = new Table();
|
||||
firstRowTable.add(backBtn).fillX().expandX().space(cw*0.005f);
|
||||
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw*0.25f);
|
||||
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
|
||||
firstRowTable.setOrigin(Align.center | Align.top);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(firstRowTable);
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(srvPort).fillX().expandX();
|
||||
|
||||
table.row().colspan(4);
|
||||
table.add(connectBtn).fillX().expandX();
|
||||
|
||||
tableContainer.setActor(table);
|
||||
stage.addActor(tableContainer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void show() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
uiManager.main.multiplexer.addProcessor(stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void render(float delta) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
Gdx.gl.glClearColor(0,0,0,0);
|
||||
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
|
||||
stage.act();
|
||||
stage.draw();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resize(int width, int height) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
stage.getViewport().update(width, height, true);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
uiManager.main.multiplexer.removeProcessor(stage);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void pause() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void resume() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void hide() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,8 +27,10 @@ public class TitleScreen implements Screen {
|
|||
VerticalGroup mainScreenGroup = new VerticalGroup().space(5).pad(5).fill();
|
||||
TextButton setBut = new TextButton("Customize Game Settings", uiManager.skin);
|
||||
mainScreenGroup.addActor(setBut);
|
||||
TextButton servBut = new TextButton("Start Server and play online with friends (TODO)", uiManager.skin);
|
||||
mainScreenGroup.addActor(servBut);
|
||||
TextButton makeSrvBtn = new TextButton("Start Server and play online (WIP)", uiManager.skin);
|
||||
mainScreenGroup.addActor(makeSrvBtn);
|
||||
TextButton joinSrvBtn = new TextButton("Join Server and play online (WIP)", uiManager.skin);
|
||||
mainScreenGroup.addActor(joinSrvBtn);
|
||||
TextButton localBut = new TextButton("Start a game on the local machine", uiManager.skin);
|
||||
mainScreenGroup.addActor(localBut);
|
||||
TextButton quitBut = new TextButton("Quit game", uiManager.skin);
|
||||
|
@ -42,7 +44,6 @@ public class TitleScreen implements Screen {
|
|||
@Override
|
||||
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
// uiManager.main.gameManager.generateMaze();
|
||||
uiManager.main.setScreen(uiManager.playersScreen);
|
||||
return true;
|
||||
}
|
||||
|
@ -55,16 +56,26 @@ public class TitleScreen implements Screen {
|
|||
return true;
|
||||
}
|
||||
});
|
||||
servBut.addListener(new InputListener() {
|
||||
makeSrvBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
System.out.println("Make this appear server joining and setup screen (TODO)");
|
||||
hide();
|
||||
uiManager.main.setScreen(uiManager.srvLaunchScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
joinSrvBtn.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.setScreen(uiManager.srvJoinScreen);
|
||||
return true;
|
||||
}
|
||||
});
|
||||
setBut.addListener(new InputListener() {
|
||||
@Override
|
||||
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
|
||||
hide();
|
||||
uiManager.main.setScreen(uiManager.setScreen);
|
||||
return true;
|
||||
}
|
||||
|
@ -72,7 +83,8 @@ public class TitleScreen implements Screen {
|
|||
|
||||
//Add actors to the group
|
||||
mainScreenGroup.addActor(setBut);
|
||||
mainScreenGroup.addActor(servBut);
|
||||
mainScreenGroup.addActor(makeSrvBtn);
|
||||
mainScreenGroup.addActor(joinSrvBtn);
|
||||
mainScreenGroup.addActor(localBut);
|
||||
mainScreenGroup.addActor(quitBut);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue