Fixed a huge bug with local games and separation between local and network games

master
EmaMaker 2020-06-08 13:44:49 +02:00
parent 7db5e8777f
commit 49f7d36b8e
11 changed files with 59 additions and 52 deletions

View File

@ -101,10 +101,10 @@ public class AMazeIng extends Game {
@Override
public void dispose() {
world.dispose();
gameManager.dispose();
client.stop();
server.stop();
world.dispose();
}
@Override

View File

@ -219,7 +219,6 @@ public class GameManager {
z = (Math.abs(rand.nextInt() - 1) % (mazeGen.h));
} while (thereIsPlayerInPos(x, z) || mazeGen.occupiedSpot(x, z));
p.setPos(x + 0.5f, 2f, z + 0.5f);
System.out.println(p.getPos().x + ", " + p.getPos().z);
}
}

View File

@ -17,11 +17,11 @@ public class GameManagerLocal extends GameManager {
public void generateMaze(Set<MazePlayer> pl, int todraw[][]) {
super.generateMaze(pl, todraw);
addTouchScreenInput();
spreadPlayers();
mazeGen.setupEndPoint();
clearPowerUps();
spawnPowerUps();
addTouchScreenInput();
if (todraw != null && getShowGame())
mazeGen.show(todraw);

View File

@ -84,10 +84,12 @@ public class GameClient extends NetworkHandler {
public void update() {
super.update();
if (gameManager != null) {
if (gameManager.gameStarted) {
} else {
checkForNewPlayers();
if (isRunning()) {
if (gameManager != null) {
if (gameManager.gameStarted) {
} else {
checkForNewPlayers();
}
}
}
}
@ -104,7 +106,7 @@ public class GameClient extends NetworkHandler {
@Override
public void stop() {
if (running) {
if (isRunning()) {
for (String s : localPlayers) {
((NACRemovePlayer) getActionByClass(NACRemovePlayer.class)).startAction(null, null, s);
}

View File

@ -20,7 +20,7 @@ import com.esotericsoftware.kryonet.Server;
public class GameServer extends NetworkHandler {
public Server server;
public ConcurrentHashMap<String, Boolean> positionUpdate = new ConcurrentHashMap<String, Boolean>();
// Returns true if the server started successfully
@ -84,8 +84,8 @@ public class GameServer extends NetworkHandler {
this.gameManager.generateMaze(new HashSet<MazePlayer>(players.values()));
for (String s : players.keySet()) {
((NASUpdatePlayerPosForced) getActionByClass(NASUpdatePlayerPosForced.class))
.startAction(null, null, s);
((NASUpdatePlayerPosForced) getActionByClass(NASUpdatePlayerPosForced.class)).startAction(null, null,
s);
}
return true;
@ -95,23 +95,26 @@ public class GameServer extends NetworkHandler {
@Override
public void stop() {
for (MazePlayer p : players.values())
p.dispose();
players.clear();
if (isRunning()) {
getActionByClass(NASServerClosed.class).startAction(null, null);
server.stop();
running = false;
for (MazePlayer p : players.values()) {
p.dispose();
players.clear();
getActionByClass(NASServerClosed.class).startAction(null, null);
server.stop();
running = false;
}
}
}
public boolean canUpdatePos(String u) {
return positionUpdate.containsKey(u) && positionUpdate.get(u);
}
public void setUpdatePos(String uuid, boolean b) {
if(positionUpdate.containsKey(uuid)) positionUpdate.replace(uuid, b);
else positionUpdate.put(uuid, b);
if (positionUpdate.containsKey(uuid))
positionUpdate.replace(uuid, b);
else
positionUpdate.put(uuid, b);
}
}

View File

@ -44,17 +44,19 @@ public abstract class NetworkHandler {
*/
public void update() {
updatePending();
if (isRunning()) {
updatePending();
if (gameManager != null)
gameManager.update();
if (gameManager != null)
gameManager.update();
if (gameManager != null && System.currentTimeMillis() - time > UPDATE_PERIOD) {
if (gameManager.gameStarted)
periodicGameUpdate();
else
periodicNonGameUpdate();
time = System.currentTimeMillis();
if (gameManager != null && System.currentTimeMillis() - time > UPDATE_PERIOD) {
if (gameManager.gameStarted)
periodicGameUpdate();
else
periodicNonGameUpdate();
time = System.currentTimeMillis();
}
}
}

View File

@ -24,7 +24,7 @@ public class NACUpdatePlayersPos extends NetworkAction {
if (parent.gameManager.gameStarted)
for (String s : client().localPlayers) {
if (parent.players.containsKey(s) && ((MazePlayerLocal) parent.players.get(s)).pressed) {
if (parent.players.containsKey(s) && ((MazePlayerLocal) parent.players.get(s)).getPressed()) {
responsePacket = parent.updatePlayer(s, parent.players.get(s), false);
client().client.sendUDP(responsePacket);
}

View File

@ -20,7 +20,7 @@ public class NASServerClosed extends NetworkAction {
public void resolveAction() {
super.resolveAction();
System.out.println("Server stopped?");
server().server.sendToAllTCP(responsePacket);
server().server.sendToAllUDP(responsePacket);
}
@Override

View File

@ -76,14 +76,14 @@ public abstract class MazePlayer implements Disposable {
}
public void setPos(float x, float y, float z) {
if (!disposed) {
if (!isDisposed()) {
pos.set(x, y, z);
toUpdatePos = true;
}
}
public void setTransform(float x, float y, float z, float i, float j, float k, float l) {
if (!disposed) {
if (!isDisposed()) {
pos.set(x, y, z);
rot.set(i, j, k, l);
if (show)
@ -92,7 +92,7 @@ public abstract class MazePlayer implements Disposable {
}
protected void updateFromTmpPos() {
if (toUpdatePos && initedPhysics) {
if (!isDisposed() && toUpdatePos && initedPhysics) {
setTransform(pos.x, pos.y, pos.z, 0, 0, 0, 0);
toUpdatePos = false;
}
@ -118,6 +118,7 @@ public abstract class MazePlayer implements Disposable {
public void initPhysics() {
initedPhysics = true;
disposed = false;
}
public void update() {

View File

@ -22,7 +22,6 @@ public class MazePlayerLocal extends MazePlayer {
/*
* Player controlled on local machine with mouse and kbd, touch or controller
* (in a remote future=
*/
btConvexShape ghostShape;
@ -197,7 +196,7 @@ public class MazePlayerLocal extends MazePlayer {
((btDiscreteDynamicsWorld) (main.world.dynamicsWorld)).addAction(characterController);
}
public boolean pressed = false;
boolean pressed = false;
public void inputs() {
pressed = false;
@ -242,10 +241,8 @@ public class MazePlayerLocal extends MazePlayer {
characterController.setWalkDirection(walkDirection);
}
if (Gdx.input.isKeyJustPressed(kpup)) {
pressed = true;
if (Gdx.input.isKeyJustPressed(kpup))
usePowerUp();
}
}
public void inputTouchscreen() {
@ -282,15 +279,13 @@ public class MazePlayerLocal extends MazePlayer {
@Override
public void update() {
super.update();
if (initedPhysics)
inputs();
if(initedPhysics && !isDisposed()) inputs();
}
@Override
protected void updateFromTmpPos() {
super.updateFromTmpPos();
if (initedPhysics)
pos.set(ghostObject.getWorldTransform().getTranslation(new Vector3()));
if(initedPhysics && !isDisposed()) pos.set(ghostObject.getWorldTransform().getTranslation(new Vector3()));
}
// @Override
@ -306,16 +301,17 @@ public class MazePlayerLocal extends MazePlayer {
@Override
public void setTransform(float x, float y, float z, float i, float j, float k, float l) {
if (!disposed && initedPhysics) {
if (!isDisposed() && initedPhysics) {
characterTransform.set(x, y, z, i, j, k, l);
ghostObject.setWorldTransform(characterTransform);
System.out.println(characterTransform.getTranslation(new Vector3()).toString());
}
}
@Override
public void dispose() {
super.dispose();
if (!isDisposed()) {
mazePlayerModel.dispose();
main.world.dynamicsWorld.removeAction(characterController);
main.world.dynamicsWorld.removeCollisionObject(ghostObject);
characterController.dispose();
@ -324,5 +320,9 @@ public class MazePlayerLocal extends MazePlayer {
disposed = true;
}
}
public boolean getPressed() {
return pressed;
}
}
}

View File

@ -83,13 +83,14 @@ public class PreGameScreen extends MyScreen {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
hide();
if (type == GameType.SERVER) {
uiManager.main.server.stop();
uiManager.main.setScreen(uiManager.srvLaunchScreen);
} else if (type == GameType.CLIENT) {
if (uiManager.main.client.isRunning()) {
uiManager.main.client.stop();
uiManager.main.setScreen(uiManager.srvJoinScreen);
}
if (uiManager.main.server.isRunning()) {
uiManager.main.server.stop();
uiManager.main.setScreen(uiManager.srvLaunchScreen);
}
return true;
}
});
@ -226,7 +227,6 @@ public class PreGameScreen extends MyScreen {
}
}
public void setGameType(GameType t) {
type = t;
show();