From e2480880064c8fc97472f85aea5afaa942a3cc11 Mon Sep 17 00:00:00 2001 From: EmaMaker Date: Thu, 18 Jun 2020 21:33:42 +0200 Subject: [PATCH] Multiplayer: initial PowerUps support --- core/src/com/emamaker/amazeing/AMazeIng.java | 5 + .../amazeing/manager/GameManager.java | 118 ++++++++++++------ .../manager/managers/GameManagerClient.java | 25 +++- .../manager/managers/GameManagerLocal.java | 9 ++ .../manager/managers/GameManagerServer.java | 26 +++- .../amazeing/manager/network/GameClient.java | 26 +++- .../amazeing/manager/network/GameServer.java | 34 +++-- .../manager/network/NetworkCommon.java | 73 ++++++++--- .../manager/network/NetworkHandler.java | 7 +- .../actions/client/login/NACRemovePlayer.java | 5 +- .../powerup/NACPowerUpAssignRequestOk.java | 51 ++++++++ .../powerup/NACPowerUpRevokeRequestOk.java | 48 +++++++ .../client/powerup/NACPowerUpUseRequest.java | 72 +++++++++++ .../client/powerup/NACUpdatePowerUps.java | 83 ++++++++++++ .../actions/server/game/NASUpdateMap.java | 10 +- .../powerup/NASPowerUpAssignRequest.java | 74 +++++++++++ .../powerup/NASPowerUpRevokeRequest.java | 69 ++++++++++ .../powerup/NASPowerUpUseRequestOk.java | 53 ++++++++ .../server/powerup/NASUpdatePowerUps.java | 57 +++++++++ .../emamaker/amazeing/maze/MazeGenerator.java | 1 + .../settings/MazeSettingPowerUpSpawnFreq.java | 25 ++++ .../amazeing/maze/settings/MazeSettings.java | 10 +- .../emamaker/amazeing/player/MazePlayer.java | 3 +- .../amazeing/player/MazePlayerLocal.java | 5 +- .../amazeing/player/powerups/PowerUp.java | 25 ++-- .../player/powerups/PowerUpBallAndChain.java | 4 +- .../amazeing/player/powerups/PowerUpBomb.java | 14 +-- .../amazeing/player/powerups/PowerUpSlug.java | 2 +- .../amazeing/player/powerups/PowerUps.java | 2 + .../emamaker/amazeing/utils/MathUtils.java | 2 - 30 files changed, 819 insertions(+), 119 deletions(-) create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpAssignRequestOk.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpRevokeRequestOk.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpUseRequest.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACUpdatePowerUps.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpAssignRequest.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpRevokeRequest.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpUseRequestOk.java create mode 100644 core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASUpdatePowerUps.java create mode 100644 core/src/com/emamaker/amazeing/maze/settings/MazeSettingPowerUpSpawnFreq.java diff --git a/core/src/com/emamaker/amazeing/AMazeIng.java b/core/src/com/emamaker/amazeing/AMazeIng.java index 3552980..35fcbb0 100755 --- a/core/src/com/emamaker/amazeing/AMazeIng.java +++ b/core/src/com/emamaker/amazeing/AMazeIng.java @@ -101,6 +101,11 @@ public class AMazeIng extends Game { System.out.println("Setting up PowerUps"); new PowerUps(); } + + public void requestChangeToMap(int[][] todraw) { + if(server.isRunning()) server.gameManager.requestChangeToMap(todraw); + else if(!client.isRunning()) server.gameManager.requestChangeToMap(todraw); + } @Override public void render() { diff --git a/core/src/com/emamaker/amazeing/manager/GameManager.java b/core/src/com/emamaker/amazeing/manager/GameManager.java index f53e59e..e319b9f 100755 --- a/core/src/com/emamaker/amazeing/manager/GameManager.java +++ b/core/src/com/emamaker/amazeing/manager/GameManager.java @@ -43,9 +43,10 @@ public class GameManager { public ArrayList players = new ArrayList(); public ArrayList powerups = new ArrayList(); - ArrayList toDelete = new ArrayList(); + protected ArrayList toDeletePlayer = new ArrayList(); + protected ArrayList toDeletePowerUps = new ArrayList(); - PowerUp pup; + protected PowerUp pup; TextButton pauseBtn; Dialog pauseDlg; @@ -75,18 +76,18 @@ public class GameManager { if (pl != null) { for (MazePlayer p : players) if (!pl.contains(p)) - toDelete.add(p); + toDeletePlayer.add(p); // Check if new players have to be added for (MazePlayer p : pl) if (!players.contains(p)) players.add(p); - for (MazePlayer p : toDelete) { + for (MazePlayer p : toDeletePlayer) { p.dispose(); players.remove(p); } - toDelete.clear(); + toDeletePlayer.clear(); } } else { for (MazePlayer p : players) @@ -129,7 +130,7 @@ public class GameManager { // pauseBtn.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("data/pause.png")))); pauseDlg = new Dialog("Pause", main.uiManager.skin); - + pauseDlgText = new Label("What do you want to do?", main.uiManager.skin); pauseDlgResumeBtn = new TextButton("Resume", main.uiManager.skin); @@ -138,7 +139,7 @@ public class GameManager { pauseDlg.text(pauseDlgText); pauseDlg.button(pauseDlgQuitBtn); pauseDlg.button(pauseDlgResumeBtn); - + pauseBtn.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { @@ -165,12 +166,12 @@ public class GameManager { stage.addActor(pauseBtn); } - float cw, ch, d, labScale, buttonDim; + public void hudUpdate() { resetCamera(); setCamera(new Vector3((MazeSettings.MAZEX + 1) / 2, (MazeSettings.MAZEX + MazeSettings.MAZEZ) * 0.48f, - MazeSettings.MAZEZ / 2 - 1), new Vector3(0, -90, 0)); + MazeSettings.MAZEZ / 2 - 1.75f), new Vector3(0, -90, 0)); cw = Gdx.graphics.getWidth(); ch = Gdx.graphics.getHeight(); @@ -179,13 +180,13 @@ public class GameManager { buttonDim = d * 0.04f; pauseBtn.setSize(buttonDim, buttonDim); pauseBtn.setPosition((cw - pauseBtn.getWidth()) / 2, ch - pauseBtn.getHeight()); - pauseBtn.getLabel().setFontScale(labScale*0.9f); + pauseBtn.getLabel().setFontScale(labScale * 0.9f); - pauseDlg.setSize(cw*0.2f, ch*0.15f); - pauseDlg.setPosition((cw-pauseDlg.getWidth())/2, (ch-pauseDlg.getHeight())/2); - pauseDlgResumeBtn.getLabel().setFontScale(labScale*0.9f); - pauseDlgQuitBtn.getLabel().setFontScale(labScale*0.9f); - pauseDlgText.setFontScale(labScale*0.9f); + pauseDlg.setSize(cw * 0.2f, ch * 0.15f); + pauseDlg.setPosition((cw - pauseDlg.getWidth()) / 2, (ch - pauseDlg.getHeight()) / 2); + pauseDlgResumeBtn.getLabel().setFontScale(labScale * 0.9f); + pauseDlgQuitBtn.getLabel().setFontScale(labScale * 0.9f); + pauseDlgText.setFontScale(labScale * 0.9f); stage.act(); stage.draw(); @@ -210,6 +211,13 @@ public class GameManager { updatePlayer(p); } + public void updatePowerUps() { + for (PowerUp p : toDeletePowerUps) { + powerups.remove(p); + } + toDeletePowerUps.clear(); + } + public void renderPlayer(MazePlayer p) { if (getShowGame()) p.render(main.world.modelBatch, main.world.environment); @@ -224,24 +232,36 @@ public class GameManager { } public void assignPowerUps() { - if (players != null && !players.isEmpty()) - for (MazePlayer p : players) - assignPowerUp(p); - } - - public PowerUp assignPowerUp(MazePlayer p) { - PowerUp pup = null; - for (PowerUp p1 : powerups) { - if (checkPowerUp(p, p1)) { - pup = p1; - p.currentPowerUp = pup; - break; + if (players != null && !players.isEmpty()) { + for (MazePlayer p : players) { + for (PowerUp p1 : powerups) { + if (checkPowerUp(p, p1)) { + assignPowerUp(p, p1); + } + } } } - if (pup != null) - powerups.remove(pup); + } - return pup; + public void revokePowerUp(MazePlayer p) { + p.disablePowerUp(); + } + + public void assignPowerUp(MazePlayer p, PowerUp p1, boolean immediateUse) { + if (p.currentPowerUp == null) { + toDeletePowerUps.add(p1); + p.currentPowerUp = p1; + if (immediateUse) + p.usePowerUp(); + } + } + + public void assignPowerUp(MazePlayer p, PowerUp p1) { + this.assignPowerUp(p, p1, false); + } + + public void usePowerUp(MazePlayer p) { + p.usePowerUp(); } public void checkWin() { @@ -255,13 +275,13 @@ public class GameManager { public void setFinished() { anyoneWon = true; gameStarted = false; - + AMazeIng.getMain().multiplexer.removeProcessor(stage); for (MazePlayer p : players) p.disablePowerUp(); main.clearEffects(); } - + public void quitGameByBtn() { setFinished(); if (pauseDlg != null) { @@ -290,6 +310,7 @@ public class GameManager { public void inGameUpdate() { if (players != null) { updatePlayers(); + updatePowerUps(); } } @@ -306,12 +327,20 @@ public class GameManager { } public void spawnPowerUps() { - for (int i = 0; i < MazeSettings.START_POWERUPS; i++) { - int x = 1, z = 1; - do { - x = (Math.abs(rand.nextInt() - 1) % (mazeGen.w)); - z = (Math.abs(rand.nextInt() - 1) % (mazeGen.h)); - } while (thereIsPlayerInPos(x, z) || mazeGen.occupiedSpot(x, z)); + for (int i = 0; i < MazeSettings.START_POWERUPS; i++) + spawnRandomPowerUp(); + } + + public void spawnRandomPowerUp() { + int x = 1, z = 1, tries = 0; + int maxtries = MazeSettings.MAZEX * MazeSettings.MAZEZ + 2; + do { + x = (Math.abs(rand.nextInt() - 1) % (mazeGen.w)); + z = (Math.abs(rand.nextInt() - 1) % (mazeGen.h)); + tries++; + } while ((thereIsPlayerInPos(x, z) || mazeGen.occupiedSpot(x, z) || thereIsPowerUpInPos(x, z)) + && tries < maxtries); + if (tries < maxtries) { System.out.println("Spawning power-up in " + x + ", " + z); spawnPowerUp(x + .5f, z + .5f); } @@ -323,16 +352,22 @@ public class GameManager { powerups.add(p); } + public void spawnPowerUpByName(String name, float x, float z) { + PowerUp p = PowerUps.pickByName(name); + p.setPosition(x, 1.25f, z); + powerups.add(p); + } + public void clearPowerUps() { for (PowerUp p : powerups) if (p != null) p.dispose(); powerups.clear(); + toDeletePowerUps.clear(); } public void removePowerUp(PowerUp p) { - if (p != null) - powerups.remove(p); + toDeletePowerUps.add(p); } public String getPowerUpNameByPos(int x, int z) { @@ -404,7 +439,8 @@ public class GameManager { } public void requestChangeToMap(int[][] todraw) { - mazeGen.show(todraw); + if (gameStarted) + mazeGen.show(todraw); } public void resetCamera() { diff --git a/core/src/com/emamaker/amazeing/manager/managers/GameManagerClient.java b/core/src/com/emamaker/amazeing/manager/managers/GameManagerClient.java index 45cdfb2..112dea3 100644 --- a/core/src/com/emamaker/amazeing/manager/managers/GameManagerClient.java +++ b/core/src/com/emamaker/amazeing/manager/managers/GameManagerClient.java @@ -6,6 +6,7 @@ import com.emamaker.amazeing.AMazeIng; import com.emamaker.amazeing.manager.GameManager; import com.emamaker.amazeing.manager.GameType; import com.emamaker.amazeing.player.MazePlayer; +import com.emamaker.amazeing.player.powerups.PowerUp; import com.emamaker.amazeing.ui.screens.PreGameScreen; public class GameManagerClient extends GameManager { @@ -55,14 +56,29 @@ public class GameManagerClient extends GameManager { } } - // Protecting against myself since this feature doesn't exist yet + @Override + public void usePowerUp(MazePlayer p) { +// System.out.println("using powerup for " + p + " " + p.currentPowerUp); + main.client.requestUsePowerUp(p); + } + + // These features don't exist in clients @Override public void assignPowerUps() { } - @Override - public void checkWin() { + public void assignPowerUp(MazePlayer p, PowerUp p1) { } + @Override + public void assignPowerUp(MazePlayer p, PowerUp p1, boolean b) { + } + @Override + public void requestChangeToMap(int[][] todraw) { + } + @Override + public void revokePowerUp(MazePlayer p) { + } + @Override public void quitGameByBtn() { @@ -71,6 +87,5 @@ public class GameManagerClient extends GameManager { main.client.stop(); main.setScreen(main.uiManager.titleScreen); } - + } - diff --git a/core/src/com/emamaker/amazeing/manager/managers/GameManagerLocal.java b/core/src/com/emamaker/amazeing/manager/managers/GameManagerLocal.java index 9efe5cc..f7bed2e 100644 --- a/core/src/com/emamaker/amazeing/manager/managers/GameManagerLocal.java +++ b/core/src/com/emamaker/amazeing/manager/managers/GameManagerLocal.java @@ -5,10 +5,13 @@ import java.util.Set; import com.emamaker.amazeing.AMazeIng; import com.emamaker.amazeing.manager.GameManager; import com.emamaker.amazeing.manager.GameType; +import com.emamaker.amazeing.maze.settings.MazeSettings; import com.emamaker.amazeing.player.MazePlayer; public class GameManagerLocal extends GameManager { + long lastPowerUpTime = 0; + public GameManagerLocal() { super(AMazeIng.getMain(), GameType.LOCAL); setupHud(); @@ -33,6 +36,12 @@ public class GameManagerLocal extends GameManager { @Override public void inGameUpdate() { super.inGameUpdate(); + + if(System.currentTimeMillis() - lastPowerUpTime > MazeSettings.POWERUP_SPAWN_FREQUENCY) { + lastPowerUpTime = System.currentTimeMillis(); + spawnRandomPowerUp(); + } + assignPowerUps(); renderWorld(); diff --git a/core/src/com/emamaker/amazeing/manager/managers/GameManagerServer.java b/core/src/com/emamaker/amazeing/manager/managers/GameManagerServer.java index 1ec6704..10fda87 100644 --- a/core/src/com/emamaker/amazeing/manager/managers/GameManagerServer.java +++ b/core/src/com/emamaker/amazeing/manager/managers/GameManagerServer.java @@ -5,10 +5,14 @@ import java.util.Set; import com.emamaker.amazeing.AMazeIng; import com.emamaker.amazeing.manager.GameManager; import com.emamaker.amazeing.manager.GameType; +import com.emamaker.amazeing.maze.settings.MazeSettings; import com.emamaker.amazeing.player.MazePlayer; +import com.emamaker.amazeing.player.powerups.PowerUp; public class GameManagerServer extends GameManager { + long lastPowerUpTime = 0; + public GameManagerServer() { super(AMazeIng.getMain(), GameType.SERVER); } @@ -24,22 +28,32 @@ public class GameManagerServer extends GameManager { if (todraw != null) mazeGen.show(todraw); - } @Override public void inGameUpdate() { super.inGameUpdate(); + if(System.currentTimeMillis() - lastPowerUpTime > MazeSettings.POWERUP_SPAWN_FREQUENCY) { + lastPowerUpTime = System.currentTimeMillis(); + spawnRandomPowerUp(); + } + assignPowerUps(); } @Override - public void assignPowerUps() { -// if (players != null && !players.isEmpty()) -// for (MazePlayer p : players) { -// main.server.removePowerUp(assignPowerUp(p)); -// } + public void assignPowerUp(MazePlayer p, PowerUp p1, boolean immediateUse) { + if (p.currentPowerUp == null) { + toDeletePowerUps.add(p1); + p.currentPowerUp = p1; + main.server.assignPowerUpRequest(p, p1, immediateUse); + } } + @Override + public void revokePowerUp(MazePlayer p) { + main.server.revokePowerUpRequest(p); + } + } diff --git a/core/src/com/emamaker/amazeing/manager/network/GameClient.java b/core/src/com/emamaker/amazeing/manager/network/GameClient.java index 344f952..9452985 100755 --- a/core/src/com/emamaker/amazeing/manager/network/GameClient.java +++ b/core/src/com/emamaker/amazeing/manager/network/GameClient.java @@ -17,6 +17,10 @@ import com.emamaker.amazeing.manager.network.action.actions.client.login.NACRemo import com.emamaker.amazeing.manager.network.action.actions.client.player.NACUpdateOtherPlayerPos; import com.emamaker.amazeing.manager.network.action.actions.client.player.NACUpdatePlayerPosForced; import com.emamaker.amazeing.manager.network.action.actions.client.player.NACUpdatePlayersPos; +import com.emamaker.amazeing.manager.network.action.actions.client.powerup.NACPowerUpAssignRequestOk; +import com.emamaker.amazeing.manager.network.action.actions.client.powerup.NACPowerUpRevokeRequestOk; +import com.emamaker.amazeing.manager.network.action.actions.client.powerup.NACPowerUpUseRequest; +import com.emamaker.amazeing.manager.network.action.actions.client.powerup.NACUpdatePowerUps; import com.emamaker.amazeing.maze.settings.MazeSettings; import com.emamaker.amazeing.player.MazePlayer; import com.emamaker.amazeing.player.MazePlayerLocal; @@ -69,9 +73,13 @@ public class GameClient extends NetworkHandler { actions.add(new NACRemovePlayer(this)); actions.add(new NACGameStatusUpdate(this)); actions.add(new NACUpdateMap(this)); + actions.add(new NACUpdatePowerUps(this)); actions.add(new NACUpdatePlayerPosForced(this)); actions.add(new NACUpdatePlayersPos(this)); actions.add(new NACUpdateOtherPlayerPos(this)); + actions.add(new NACPowerUpRevokeRequestOk(this)); + actions.add(new NACPowerUpAssignRequestOk(this)); + actions.add(new NACPowerUpUseRequest(this)); } @Override @@ -82,14 +90,15 @@ public class GameClient extends NetworkHandler { @Override public void update() { super.update(); - + if (isRunning()) { - //Check if the server disconnected or it's timing out - if(!client.isConnected() || ((NACGameStatusUpdate.gotMessage && System.currentTimeMillis() - NACGameStatusUpdate.lastMsgTime > Constants.COMMUNICATION_TIMEOUT_MILLIS))) { + // Check if the server disconnected or it's timing out + if (!client.isConnected() || ((NACGameStatusUpdate.gotMessage && System.currentTimeMillis() + - NACGameStatusUpdate.lastMsgTime > Constants.COMMUNICATION_TIMEOUT_MILLIS))) { stop(true); main.uiManager.srvJoinScreen.showErrorDlg(1); } - //Normal client update + // Normal client update if (gameManager != null) { if (gameManager.gameStarted) { } else { @@ -126,11 +135,10 @@ public class GameClient extends NetworkHandler { p.dispose(); players.clear(); - if (!fromserver) { for (NetworkAction n : pendingActions) n.onParentClosing(); - + while (!pendingActions.isEmpty()) update(); } @@ -210,4 +218,10 @@ public class GameClient extends NetworkHandler { updateMobilePlayers = true; } + public void requestUsePowerUp(MazePlayer p) { + if (p.currentPowerUp != null) + ((NACPowerUpUseRequest) getActionByClass(NACPowerUpUseRequest.class)).startAction(null, null, p.uuid, + p.currentPowerUp.name); + } + } diff --git a/core/src/com/emamaker/amazeing/manager/network/GameServer.java b/core/src/com/emamaker/amazeing/manager/network/GameServer.java index efac57c..86e05ef 100755 --- a/core/src/com/emamaker/amazeing/manager/network/GameServer.java +++ b/core/src/com/emamaker/amazeing/manager/network/GameServer.java @@ -13,7 +13,12 @@ import com.emamaker.amazeing.manager.network.action.actions.server.login.NASLogi import com.emamaker.amazeing.manager.network.action.actions.server.login.NASRemovePlayer; import com.emamaker.amazeing.manager.network.action.actions.server.player.NASUpdatePlayerPos; import com.emamaker.amazeing.manager.network.action.actions.server.player.NASUpdatePlayerPosForced; +import com.emamaker.amazeing.manager.network.action.actions.server.powerup.NASPowerUpAssignRequest; +import com.emamaker.amazeing.manager.network.action.actions.server.powerup.NASPowerUpRevokeRequest; +import com.emamaker.amazeing.manager.network.action.actions.server.powerup.NASPowerUpUseRequestOk; +import com.emamaker.amazeing.manager.network.action.actions.server.powerup.NASUpdatePowerUps; import com.emamaker.amazeing.player.MazePlayer; +import com.emamaker.amazeing.player.powerups.PowerUp; import com.emamaker.amazeing.utils.MathUtils.Constants; import com.esotericsoftware.kryonet.Server; @@ -58,24 +63,30 @@ public class GameServer extends NetworkHandler { actions.add(new NASRemovePlayer(this)); actions.add(new NASGameStatusUpdate(this)); actions.add(new NASUpdateMap(this)); + actions.add(new NASUpdatePowerUps(this)); actions.add(new NASUpdatePlayerPosForced(this)); - actions.add(new NASUpdatePlayerPos(this)); + actions.add(new NASUpdatePlayerPos(this));; + actions.add(new NASPowerUpAssignRequest(this)); + actions.add(new NASPowerUpRevokeRequest(this)); + actions.add(new NASPowerUpUseRequestOk(this)); } @Override public void startDefaultActions() { getActionByClass(NASGameStatusUpdate.class).startAction(null, null); + getActionByClass(NASUpdatePowerUps.class).startAction(null, null); // getActionByClass(NAServerUpdatePlayers.class).startAction(null, null); } @Override public void update() { super.update(); - - if(isRunning()) { - //Check if there's some player not responding that needs to be removed - for(String s: players.keySet()) { - if(System.currentTimeMillis() - players.get(s).LAST_NETWORK_TIME > Constants.COMMUNICATION_TIMEOUT_MILLIS) { + + if (isRunning()) { + // Check if there's some player not responding that needs to be removed + for (String s : players.keySet()) { + if (System.currentTimeMillis() + - players.get(s).LAST_NETWORK_TIME > Constants.COMMUNICATION_TIMEOUT_MILLIS) { players.get(s).dispose(); players.remove(s); } @@ -83,7 +94,7 @@ public class GameServer extends NetworkHandler { } } } - + @Override public void periodicNonGameUpdate() { } @@ -136,4 +147,13 @@ public class GameServer extends NetworkHandler { positionUpdate.put(uuid, b); } + public void assignPowerUpRequest(MazePlayer p, PowerUp p1, boolean immediateUse) { + ((NASPowerUpAssignRequest) getActionByClass(NASPowerUpAssignRequest.class)).startAction(null, null, p.uuid, + p1.name, immediateUse); + } + public void revokePowerUpRequest(MazePlayer p) { + System.out.println("Revoking powerup to " + p); + ((NASPowerUpRevokeRequest) getActionByClass(NASPowerUpRevokeRequest.class)).startAction(null, null, p.uuid); + } + } diff --git a/core/src/com/emamaker/amazeing/manager/network/NetworkCommon.java b/core/src/com/emamaker/amazeing/manager/network/NetworkCommon.java index 79fa88b..918fce3 100755 --- a/core/src/com/emamaker/amazeing/manager/network/NetworkCommon.java +++ b/core/src/com/emamaker/amazeing/manager/network/NetworkCommon.java @@ -16,10 +16,18 @@ public class NetworkCommon { kryo.register(RemovePlayer.class); kryo.register(PlayerRemoved.class); - kryo.register(GameStatusUpdate.class); kryo.register(String[].class); + kryo.register(GameStatusUpdate.class); kryo.register(UpdateMap.class); + kryo.register(PowerUpUpdate.class); + kryo.register(PowerUpAssignRequest.class); + kryo.register(PowerUpAssignRequestOk.class); + kryo.register(PowerUpUseRequest.class); + kryo.register(PowerUpUseRequestOk.class); + kryo.register(PowerUpRevokeRequest.class); + kryo.register(PowerUpRevokeRequestOk.class); + kryo.register(UpdatePlayerPosition.class); kryo.register(UpdateForcedPlayerPosition.class); kryo.register(UpdateForcedPlayerPositionOk.class); @@ -27,21 +35,21 @@ public class NetworkCommon { kryo.register(ServerClosed.class); } + + /** PLAYER PACKETS **/ + /*LOGIN*/ public static class ClientLoginAO { } - public static class ServerLoginUUID { public String uuid; } - public static class ClientLoginAO2 { public String uuid; } - public static class ServerLoginAO2 { public String uuid; } - + /*REMOVAL*/ public static class RemovePlayer{ public String uuid; } @@ -49,20 +57,7 @@ public class NetworkCommon { public static class PlayerRemoved{ public String uuid; } - - //Game Status is used to broadcast to clients infos about the game and players - public static class GameStatusUpdate{ - public boolean gameStarted; - public boolean anyoneWon; - public String[] playersUUIDs; - } - - //Run-lenght encoding of the map - public static class UpdateMap{ - public String map; - } - - + /*PLAYER POSITION UPDATE*/ public static class UpdatePlayerPosition{ public String uuid; public float px, py, pz; @@ -77,7 +72,47 @@ public class NetworkCommon { public String uuid; } + /** SERVER TO CLIENT UPDATES **/ + //Game Status is used to broadcast to clients infos about the game and players + public static class GameStatusUpdate{ + public boolean gameStarted; + public boolean anyoneWon; + public String[] playersUUIDs; + } + + //Run-lenght encoding of the map + public static class UpdateMap{ + public String map; + } + + /** POWER UPS **/ + public static class PowerUpUpdate { + public String[] powerups; + } + public static class PowerUpAssignRequest{ + public String uuid, pupname; + public boolean immediateUse; + } + public static class PowerUpAssignRequestOk{ + public String uuid; + } + public static class PowerUpUseRequest{ + public String uuid, pupname; + } + public static class PowerUpUseRequestOk{ + public String uuid; + public boolean canuse; + } + public static class PowerUpRevokeRequest{ + public String uuid; + } + public static class PowerUpRevokeRequestOk{ + public String uuid; + } + + /** MISC **/ public static class ServerClosed{ } + } class ConnectionPlayer extends Connection { diff --git a/core/src/com/emamaker/amazeing/manager/network/NetworkHandler.java b/core/src/com/emamaker/amazeing/manager/network/NetworkHandler.java index ecab8d7..ab767d8 100644 --- a/core/src/com/emamaker/amazeing/manager/network/NetworkHandler.java +++ b/core/src/com/emamaker/amazeing/manager/network/NetworkHandler.java @@ -1,5 +1,8 @@ package com.emamaker.amazeing.manager.network; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; + import com.badlogic.gdx.math.Vector3; import com.emamaker.amazeing.AMazeIng; import com.emamaker.amazeing.manager.GameManager; @@ -8,10 +11,6 @@ import com.emamaker.amazeing.manager.network.NetworkCommon.UpdatePlayerPosition; import com.emamaker.amazeing.manager.network.action.NetworkAction; import com.emamaker.amazeing.player.MazePlayer; -import java.util.Arrays; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArrayList; - public abstract class NetworkHandler { public ConcurrentHashMap players = new ConcurrentHashMap(); diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/login/NACRemovePlayer.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/login/NACRemovePlayer.java index 9b4028e..4c8f532 100644 --- a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/login/NACRemovePlayer.java +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/login/NACRemovePlayer.java @@ -23,9 +23,8 @@ public class NACRemovePlayer extends NetworkAction { /* * Here startAction needs to be overrided to allow for the uuid argument to be - * passed from one instance to another and to remove the alreadyPending - * limitation. Here we need to be able to call multiple instances of this at the - * same time + * passed from one instance to another. This also removes the alreadyPending statement instead of setting + * the oneAtTheTie flag. */ public void startAction(String s_) { this.startAction(incomingConnection, incomingMsg, s_); diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpAssignRequestOk.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpAssignRequestOk.java new file mode 100644 index 0000000..b23b3d5 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpAssignRequestOk.java @@ -0,0 +1,51 @@ +package com.emamaker.amazeing.manager.network.action.actions.client.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpAssignRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpAssignRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.emamaker.amazeing.player.powerups.PowerUps; +import com.esotericsoftware.kryonet.Connection; + +public class NACPowerUpAssignRequestOk extends NetworkAction { + + protected NACPowerUpAssignRequestOk(NetworkHandler parent, Connection c, Object incomingMsg_, + Object responsePacket_, Object endPacket_, boolean oneTime) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + setOneAtTheTime(false); + } + + public NACPowerUpAssignRequestOk(NetworkHandler parent_) { + super(parent_, new NetworkCommon.PowerUpAssignRequest(), new NetworkCommon.PowerUpAssignRequestOk(), null, + true); + } + + @Override + public void resolveAction() { + super.resolveAction(); + + ((PowerUpAssignRequestOk) responsePacket).uuid = ((PowerUpAssignRequest) incomingMsg).uuid; + if (parent.players.containsKey(((PowerUpAssignRequest) incomingMsg).uuid)) { + parent.players.get(((PowerUpAssignRequest) incomingMsg).uuid).currentPowerUp = PowerUps.pickByName(((PowerUpAssignRequest) incomingMsg).pupname); + System.out.println("Assigning powerup to " + ((PowerUpAssignRequest) incomingMsg).uuid); + if(((PowerUpAssignRequest) incomingMsg).immediateUse) client().requestUsePowerUp(parent.players.get(((PowerUpAssignRequest) incomingMsg).uuid)); + } + + + client().client.sendUDP(responsePacket); + + } + + @Override + public void responseReceived(Connection conn, Object msg) { + super.responseReceived(conn, msg); + } + + @Override + public NetworkAction newInstance() { + return new NACPowerUpAssignRequestOk(client(), incomingConnection, incomingMsg, responsePacket, endPacket, + oneTime); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpRevokeRequestOk.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpRevokeRequestOk.java new file mode 100644 index 0000000..498aaa7 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpRevokeRequestOk.java @@ -0,0 +1,48 @@ +package com.emamaker.amazeing.manager.network.action.actions.client.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpRevokeRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpRevokeRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.esotericsoftware.kryonet.Connection; + +public class NACPowerUpRevokeRequestOk extends NetworkAction { + + protected NACPowerUpRevokeRequestOk(NetworkHandler parent, Connection c, Object incomingMsg_, + Object responsePacket_, Object endPacket_, boolean oneTime) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + setOneAtTheTime(false); + } + + public NACPowerUpRevokeRequestOk(NetworkHandler parent_) { + super(parent_, new NetworkCommon.PowerUpRevokeRequest(), new NetworkCommon.PowerUpRevokeRequestOk(), null, + true); + } + + @Override + public void resolveAction() { + super.resolveAction(); + + ((PowerUpRevokeRequestOk) responsePacket).uuid = ((PowerUpRevokeRequest) incomingMsg).uuid; + if (parent.players.containsKey(((PowerUpRevokeRequest) incomingMsg).uuid)) { + parent.players.get(((PowerUpRevokeRequest) incomingMsg).uuid).disablePowerUp(); + System.out.println("client: Revoked powerup for " + + parent.players.get(((PowerUpRevokeRequest) incomingMsg).uuid).uuid); + } + client().client.sendUDP(responsePacket); + + } + + @Override + public void responseReceived(Connection conn, Object msg) { + super.responseReceived(conn, msg); + } + + @Override + public NetworkAction newInstance() { + return new NACPowerUpRevokeRequestOk(client(), incomingConnection, incomingMsg, responsePacket, endPacket, + oneTime); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpUseRequest.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpUseRequest.java new file mode 100644 index 0000000..cd98fd1 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACPowerUpUseRequest.java @@ -0,0 +1,72 @@ +package com.emamaker.amazeing.manager.network.action.actions.client.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUseRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUseRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.esotericsoftware.kryonet.Connection; + +public class NACPowerUpUseRequest extends NetworkAction { + + String uuid, pupname; + + protected NACPowerUpUseRequest(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime, String uuid_, String pupname_) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + this.uuid = uuid_; + this.pupname = pupname_; + } + + public NACPowerUpUseRequest(NetworkHandler parent_) { + super(parent_, null, new NetworkCommon.PowerUpUseRequest(), new NetworkCommon.PowerUpUseRequestOk(), false); + } + + /* + * Here startAction needs to be overrided to allow for the uuid argument to be + * passed from one instance to another. This also removes the alreadyPending statement instead of setting + * the oneAtTheTie flag. + */ + public void startAction(String u_, String p_) { + this.startAction(incomingConnection, incomingMsg, u_, p_); + } + + public void startAction(Connection conn, Object msg, String uuid_, String pupname_) { + this.incomingConnection = conn; + this.incomingMsg = msg; + this.uuid = uuid_; + this.pupname = pupname_; + + parent.addToPending(newInstance()); + } + + @Override + public void resolveAction() { + super.resolveAction(); + ((PowerUpUseRequest) responsePacket).uuid = uuid; + ((PowerUpUseRequest) responsePacket).pupname = pupname; + client().client.sendUDP(responsePacket); + } + + /* + * We also have to override the responseReceived package, because this action + * has to finish only if the correct uuid has been received + */ + public void responseReceived(Connection conn, Object msg) { + this.endConnection = conn; + this.endMsg = msg; + if ( endMsg == null || ((PowerUpUseRequestOk) endMsg).uuid.equals(uuid)) { + if (endMsg != null && ((PowerUpUseRequestOk) endMsg).canuse) { + parent.players.get(uuid).usePowerUp(); + System.out.println("using powerup " + pupname + " on " + uuid); + } + detachFromParent(); + } + } + + @Override + public NetworkAction newInstance() { + return new NACPowerUpUseRequest(client(), incomingConnection, incomingMsg, responsePacket, endPacket, oneTime, uuid, pupname); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACUpdatePowerUps.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACUpdatePowerUps.java new file mode 100644 index 0000000..86ec6f5 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/client/powerup/NACUpdatePowerUps.java @@ -0,0 +1,83 @@ +package com.emamaker.amazeing.manager.network.action.actions.client.powerup; + +import java.util.Arrays; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUpdate; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.emamaker.amazeing.player.powerups.PowerUp; +import com.esotericsoftware.kryonet.Connection; + +public class NACUpdatePowerUps extends NetworkAction { + + String[] a; + String name; + float x, y, z; + boolean found = true; + + static String[] oldList = new String[0]; + + protected NACUpdatePowerUps(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + } + + public NACUpdatePowerUps(NetworkHandler parent_) { + super(parent_, new NetworkCommon.PowerUpUpdate(), null, null, true); + } + + @Override + public void resolveAction() { + super.resolveAction(); + + // This part works exactly the same way as remote player adding and removal + // works + + if (!Arrays.equals(((PowerUpUpdate) incomingMsg).powerups, oldList)) { + + //now check if the currently used powerups are in the list received from the server, if not they have to be removed because they are not present anymore in the game + for(PowerUp p : client().gameManager.powerups) { + found = false; + + //This could result quite computationally expensive and could be optimized in some way, but now we just need the thing working + for (String s : ((PowerUpUpdate) incomingMsg).powerups) { + a = s.split("-"); + name = a[0]; + x = Float.valueOf(a[1]); + y = Float.valueOf(a[2]); + z = Float.valueOf(a[3]); + + found = p.getPosition().x == x && p.getPosition().z == z && p.name.equals(name); + if(found) break; + } + if(!found) client().gameManager.removePowerUp(p); + } + + //check if there are new powerups to be added + for (String s : ((PowerUpUpdate) incomingMsg).powerups) { + a = s.split("-"); + name = a[0]; + x = Float.valueOf(a[1]); + y = Float.valueOf(a[2]); + z = Float.valueOf(a[3]); + if (!client().gameManager.thereIsPowerUpInPos((int) x, (int) z)) { + client().gameManager.spawnPowerUpByName(name, x, z); + } + } + + oldList = ((PowerUpUpdate) incomingMsg).powerups; + } + } + + @Override + public void responseReceived(Connection conn, Object msg) { + super.responseReceived(conn, msg); + } + + @Override + public NetworkAction newInstance() { + return new NACUpdatePowerUps(client(), incomingConnection, incomingMsg, responsePacket, endPacket, oneTime); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/game/NASUpdateMap.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/game/NASUpdateMap.java index 7c6a04a..9757b74 100644 --- a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/game/NASUpdateMap.java +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/game/NASUpdateMap.java @@ -8,6 +8,8 @@ import com.esotericsoftware.kryonet.Connection; public class NASUpdateMap extends NetworkAction { + static String map = "", oldMap = ""; + protected NASUpdateMap(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, Object endPacket_, boolean oneTime) { super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); @@ -20,8 +22,12 @@ public class NASUpdateMap extends NetworkAction { @Override public void resolveAction() { super.resolveAction(); - ((UpdateMap) responsePacket).map = server().gameManager.mazeGen.runLenghtEncode(); - server().server.sendToAllUDP(responsePacket); + map = server().gameManager.mazeGen.runLenghtEncode(); + if (!map.equals(oldMap)) { + ((UpdateMap)responsePacket).map = map; + server().server.sendToAllUDP(responsePacket); + oldMap = map; + } } @Override diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpAssignRequest.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpAssignRequest.java new file mode 100644 index 0000000..eb31c61 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpAssignRequest.java @@ -0,0 +1,74 @@ +package com.emamaker.amazeing.manager.network.action.actions.server.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpAssignRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpAssignRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.esotericsoftware.kryonet.Connection; + +public class NASPowerUpAssignRequest extends NetworkAction { + + String uuid, pupname; + boolean immediateUse; + + protected NASPowerUpAssignRequest(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime, String uuid_, String pupname_, boolean immediateUse_) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + this.uuid = uuid_; + this.pupname = pupname_; + this.immediateUse = immediateUse_; + } + + public NASPowerUpAssignRequest(NetworkHandler parent_) { + super(parent_, null, new NetworkCommon.PowerUpAssignRequest(), new NetworkCommon.PowerUpAssignRequestOk(), false); + } + + /* + * Here startAction needs to be overrided to allow for the uuid argument to be + * passed from one instance to another. This also removes the alreadyPending + * statement instead of setting the oneAtTheTie flag. + */ + public void startAction(String u_, String p_, boolean immediateUse_) { + this.startAction(incomingConnection, incomingMsg, u_, p_, immediateUse_); + } + + public void startAction(Connection conn, Object msg, String uuid_, String pupname_, boolean immediateUse_) { + this.incomingConnection = conn; + this.incomingMsg = msg; + this.uuid = uuid_; + this.pupname = pupname_; + this.immediateUse = immediateUse_; + + parent.addToPending(newInstance()); + } + + @Override + public void resolveAction() { + super.resolveAction(); + ((PowerUpAssignRequest) responsePacket).uuid = uuid; + ((PowerUpAssignRequest) responsePacket).pupname = pupname; + ((PowerUpAssignRequest) responsePacket).immediateUse = immediateUse; + System.out.println("requesting to assign powerup " + pupname + " to " + uuid); + + server().server.sendToAllUDP(responsePacket); + } + + /* + * We also have to override the responseReceived package, because this action + * has to finish only if the correct uuid has been received + */ + public void responseReceived(Connection conn, Object msg) { + this.endConnection = conn; + this.endMsg = msg; + if (endMsg == null || ((PowerUpAssignRequestOk) endMsg).uuid.equals(uuid)) + detachFromParent(); + } + + @Override + public NetworkAction newInstance() { + return new NASPowerUpAssignRequest(server(), incomingConnection, incomingMsg, responsePacket, endPacket, + oneTime, uuid, pupname, immediateUse); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpRevokeRequest.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpRevokeRequest.java new file mode 100644 index 0000000..ef738d4 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpRevokeRequest.java @@ -0,0 +1,69 @@ +package com.emamaker.amazeing.manager.network.action.actions.server.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpRevokeRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpRevokeRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.esotericsoftware.kryonet.Connection; + +public class NASPowerUpRevokeRequest extends NetworkAction { + + String uuid; + + protected NASPowerUpRevokeRequest(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime, String uuid_) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + this.uuid = uuid_; + } + + public NASPowerUpRevokeRequest(NetworkHandler parent_) { + super(parent_, null, new NetworkCommon.PowerUpRevokeRequest(), new NetworkCommon.PowerUpRevokeRequestOk(), + false); + } + + /* + * Here startAction needs to be overrided to allow for the uuid argument to be + * passed from one instance to another. This also removes the alreadyPending + * statement instead of setting the oneAtTheTie flag. + */ + public void startAction(String u_) { + this.startAction(incomingConnection, incomingMsg, u_); + } + + public void startAction(Connection conn, Object msg, String uuid_) { + this.incomingConnection = conn; + this.incomingMsg = msg; + this.uuid = uuid_; + + parent.addToPending(newInstance()); + } + + @Override + public void resolveAction() { + super.resolveAction(); + ((PowerUpRevokeRequest) responsePacket).uuid = uuid; + System.out.println("server: Revoking powerup for " + uuid); + parent.players.get(uuid).disablePowerUp(); + server().server.sendToAllUDP(responsePacket); + } + + /* + * We also have to override the responseReceived package, because this action + * has to finish only if the correct uuid has been received + */ + public void responseReceived(Connection conn, Object msg) { + this.endConnection = conn; + this.endMsg = msg; + if (endMsg == null || ((PowerUpRevokeRequestOk) endMsg).uuid.equals(uuid)) { + detachFromParent(); + } + } + + @Override + public NetworkAction newInstance() { + return new NASPowerUpRevokeRequest(server(), incomingConnection, incomingMsg, responsePacket, endPacket, + oneTime, uuid); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpUseRequestOk.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpUseRequestOk.java new file mode 100644 index 0000000..39c0058 --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASPowerUpUseRequestOk.java @@ -0,0 +1,53 @@ +package com.emamaker.amazeing.manager.network.action.actions.server.powerup; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUseRequest; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUseRequestOk; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.esotericsoftware.kryonet.Connection; + +public class NASPowerUpUseRequestOk extends NetworkAction { + + protected NASPowerUpUseRequestOk(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + setOneAtTheTime(false); + } + + public NASPowerUpUseRequestOk(NetworkHandler parent_) { + super(parent_, new NetworkCommon.PowerUpUseRequest(), new NetworkCommon.PowerUpUseRequestOk(), null, true); + } + + @Override + public void resolveAction() { + super.resolveAction(); + + ((PowerUpUseRequestOk) responsePacket).uuid = ((PowerUpUseRequest) incomingMsg).uuid; + ((PowerUpUseRequestOk) responsePacket).canuse = !parent.players + .containsKey(((PowerUpUseRequest) incomingMsg).uuid) + || parent.players.get(((PowerUpUseRequest) incomingMsg).uuid).currentPowerUp == null ? false + : parent.players.get(((PowerUpUseRequest) incomingMsg).uuid).currentPowerUp.name + .equals(((PowerUpUseRequest) incomingMsg).pupname); + + if (((PowerUpUseRequestOk) responsePacket).canuse) { + parent.players.get(((PowerUpUseRequest) incomingMsg).uuid).usePowerUp(); + } else { + server().revokePowerUpRequest(parent.players.get(((PowerUpUseRequest) incomingMsg).uuid)); + } + incomingConnection.sendUDP(responsePacket); + + } + + @Override + public void responseReceived(Connection conn, Object msg) { + super.responseReceived(conn, msg); + } + + @Override + public NetworkAction newInstance() { + return new NASPowerUpUseRequestOk(server(), incomingConnection, incomingMsg, responsePacket, endPacket, + oneTime); + } + +} diff --git a/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASUpdatePowerUps.java b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASUpdatePowerUps.java new file mode 100644 index 0000000..e898a8d --- /dev/null +++ b/core/src/com/emamaker/amazeing/manager/network/action/actions/server/powerup/NASUpdatePowerUps.java @@ -0,0 +1,57 @@ +package com.emamaker.amazeing.manager.network.action.actions.server.powerup; + +import java.util.ArrayList; + +import com.emamaker.amazeing.manager.network.NetworkCommon; +import com.emamaker.amazeing.manager.network.NetworkCommon.PowerUpUpdate; +import com.emamaker.amazeing.manager.network.NetworkHandler; +import com.emamaker.amazeing.manager.network.action.NetworkAction; +import com.emamaker.amazeing.player.powerups.PowerUp; +import com.esotericsoftware.kryonet.Connection; + +public class NASUpdatePowerUps extends NetworkAction { + + ArrayList powerups = new ArrayList(); + + protected NASUpdatePowerUps(NetworkHandler parent, Connection c, Object incomingMsg_, Object responsePacket_, + Object endPacket_, boolean oneTime) { + super(parent, c, incomingMsg_, responsePacket_, endPacket_, oneTime); + enableTimeout(false); + } + + public NASUpdatePowerUps(NetworkHandler parent_) { + super(parent_, null, new NetworkCommon.PowerUpUpdate(), null, false); + } + + @Override + public void resolveAction() { + super.resolveAction(); + + try { + powerups.clear(); + + if (server().gameManager.powerups != null && !server().gameManager.powerups.isEmpty()) { + for (PowerUp p : server().gameManager.powerups) { + powerups.add(p.name + "-" + p.getPosition().x + "-" + p.getPosition().y + "-" + p.getPosition().z); + } + + ((PowerUpUpdate) responsePacket).powerups = powerups.toArray(new String[powerups.size()]); + server().server.sendToAllUDP(responsePacket); + } + } catch (Exception e) { + e.printStackTrace(); + } + + } + + @Override + public void responseReceived(Connection conn, Object msg) { + super.responseReceived(conn, msg); + } + + @Override + public NetworkAction newInstance() { + return new NASUpdatePowerUps(server(), incomingConnection, incomingMsg, responsePacket, endPacket, oneTime); + } + +} diff --git a/core/src/com/emamaker/amazeing/maze/MazeGenerator.java b/core/src/com/emamaker/amazeing/maze/MazeGenerator.java index 83c38c0..d5fa63f 100755 --- a/core/src/com/emamaker/amazeing/maze/MazeGenerator.java +++ b/core/src/com/emamaker/amazeing/maze/MazeGenerator.java @@ -264,6 +264,7 @@ public class MazeGenerator { public int[][] changeMap(int[][] tmp, int x, int z, int type) { if (x > 0 && x < w - 1 && z > 0 && z < h - 1 && todraw[x][z] != 2) { + System.out.println("changing " + x + ", " + z + " to " + type); tmp[x][z] = type; } return tmp; diff --git a/core/src/com/emamaker/amazeing/maze/settings/MazeSettingPowerUpSpawnFreq.java b/core/src/com/emamaker/amazeing/maze/settings/MazeSettingPowerUpSpawnFreq.java new file mode 100644 index 0000000..6ce89eb --- /dev/null +++ b/core/src/com/emamaker/amazeing/maze/settings/MazeSettingPowerUpSpawnFreq.java @@ -0,0 +1,25 @@ +package com.emamaker.amazeing.maze.settings; + +import com.emamaker.amazeing.ui.UIManager; + +public class MazeSettingPowerUpSpawnFreq extends MazeSetting{ + + /* Game max. number of players settings*/ + + public MazeSettingPowerUpSpawnFreq(String name_, String[] options_, UIManager uiManager_) { + super(name_, options_, uiManager_); + } + public MazeSettingPowerUpSpawnFreq(String name_, String[] options_, int defaultOption, UIManager uiManager_) { + super(name_, options_, defaultOption, uiManager_); + } + + @Override + public void parseOptionString(String opt) { + if(opt.equals("Off")) { + MazeSettings.POWERUP_SPAWN_FREQUENCY = Integer.MAX_VALUE; + }else { + MazeSettings.POWERUP_SPAWN_FREQUENCY = Integer.valueOf(opt)*1000; + } + } + +} diff --git a/core/src/com/emamaker/amazeing/maze/settings/MazeSettings.java b/core/src/com/emamaker/amazeing/maze/settings/MazeSettings.java index bae4cbb..c6bb769 100755 --- a/core/src/com/emamaker/amazeing/maze/settings/MazeSettings.java +++ b/core/src/com/emamaker/amazeing/maze/settings/MazeSettings.java @@ -14,6 +14,7 @@ public class MazeSettings { public static int MAXPLAYERS_MOBILE = 1; public static int EPDIST = 5; public static int START_POWERUPS = 0; + public static int POWERUP_SPAWN_FREQUENCY = 0; public static ArrayList settings = new ArrayList(); public static MazeSetting setDim; @@ -21,6 +22,7 @@ public class MazeSettings { public static MazeSetting setPlayers_Mobile; public static MazeSetting setEpDist; public static MazeSetting setStartPowerups; + public static MazeSetting setPowerUpSpawnFreq; public static String[] maxPlayersDesktop = new String[] { "2", "4", "6", "8", "10", "15", "20" }; public static String[] maxPlayersMobile = new String[] { "1", "2", "3", "4" }; @@ -35,16 +37,20 @@ public class MazeSettings { setPlayers_Mobile = new MazeSettingMaxPlayersMobile("PLAYERS JOINING FROM THIS DEVICE: ", maxPlayersMobile, 0, AMazeIng.getMain().uiManager); - setStartPowerups = new MazeSettingStartPowerUps("POWERUPS AT START : ", - new String[] { "1", "2", "3", "4", "5", "8", "10", "15" }, 3, AMazeIng.getMain().uiManager); + setStartPowerups = new MazeSettingStartPowerUps("POWERUPS AT START: ", + new String[] { "1", "2", "3", "4", "5", "8", "10", "15" }, 7, AMazeIng.getMain().uiManager); setEpDist = new MazeSettingEPDIST("END POINT DISTANCE:", new String[] { "1", "2", "5", "10", "20" }, 2, AMazeIng.getMain().uiManager); + setPowerUpSpawnFreq = new MazeSettingPowerUpSpawnFreq("POWERUP SPAWN FREQUENCY:", + new String[] { "Off", "1", "2", "5", "10", "20", "30" }, 4, AMazeIng.getMain().uiManager); + settings.add(setDim); settings.add(setPlayers); settings.add(setEpDist); settings.add(setStartPowerups); + settings.add(setPowerUpSpawnFreq); } public static void saveStates() { diff --git a/core/src/com/emamaker/amazeing/player/MazePlayer.java b/core/src/com/emamaker/amazeing/player/MazePlayer.java index f586f6c..216ea3b 100755 --- a/core/src/com/emamaker/amazeing/player/MazePlayer.java +++ b/core/src/com/emamaker/amazeing/player/MazePlayer.java @@ -159,8 +159,7 @@ public abstract class MazePlayer implements Disposable { public void usePowerUp() { if (currentPowerUp != null) - if (currentPowerUp.usePowerUp(this)) - disablePowerUp(); + currentPowerUp.usePowerUp(this); } public void disablePowerUp() { diff --git a/core/src/com/emamaker/amazeing/player/MazePlayerLocal.java b/core/src/com/emamaker/amazeing/player/MazePlayerLocal.java index 22e8604..9dc3859 100755 --- a/core/src/com/emamaker/amazeing/player/MazePlayerLocal.java +++ b/core/src/com/emamaker/amazeing/player/MazePlayerLocal.java @@ -240,7 +240,7 @@ public class MazePlayerLocal extends MazePlayer { } if (Gdx.input.isKeyJustPressed(kpup)) - usePowerUp(); + main.currentGameManager.usePowerUp(this); } public void inputTouchscreen() { @@ -302,6 +302,9 @@ public class MazePlayerLocal extends MazePlayer { @Override public void setTransform(float x, float y, float z, float i, float j, float k, float l) { if (!isDisposed() && initedPhysics) { + //Just a little hack to avoid players disappearing in the void due to some strange things happening with physics when updating map and remote players + if(y < 1) y = 1.25f; + characterTransform.set(x, y, z, i, j, k, l); ghostObject.setWorldTransform(characterTransform); } diff --git a/core/src/com/emamaker/amazeing/player/powerups/PowerUp.java b/core/src/com/emamaker/amazeing/player/powerups/PowerUp.java index 92095cd..175afe8 100755 --- a/core/src/com/emamaker/amazeing/player/powerups/PowerUp.java +++ b/core/src/com/emamaker/amazeing/player/powerups/PowerUp.java @@ -19,6 +19,7 @@ import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.utils.Disposable; import com.emamaker.amazeing.AMazeIng; +import com.emamaker.amazeing.manager.managers.GameManagerServer; import com.emamaker.amazeing.player.MazePlayer; import com.emamaker.amazeing.utils.MathUtils; @@ -121,15 +122,16 @@ public class PowerUp implements Disposable { // Return true if the effect has been resolved public boolean usePowerUp(MazePlayer player) { - System.out.println(this.name + " ! "); + System.out.println(AMazeIng.getMain().currentGameManager + " " + this.name + " ! "); - if (effect != null) { + if (effect != null && !(AMazeIng.getMain().currentGameManager instanceof GameManagerServer)) { PooledEffect e = effectPool.obtain(); Vector3 pos = MathUtils.toScreenCoords(player.getPos()); e.setPosition(pos.x, pos.y); AMazeIng.getMain().effects.add(e); } + AMazeIng.getMain().currentGameManager.revokePowerUp(player); return true; } @@ -165,8 +167,10 @@ class PowerUpTemporized extends PowerUp { startTime = System.currentTimeMillis(); used = true; - e = effectPool.obtain(); - AMazeIng.getMain().effects.add(e); + if (!(AMazeIng.getMain().currentGameManager instanceof GameManagerServer)) { + e = effectPool.obtain(); + AMazeIng.getMain().effects.add(e); + } } if (System.currentTimeMillis() - startTime <= time) { @@ -175,16 +179,19 @@ class PowerUpTemporized extends PowerUp { return false; } else { used = false; -// System.out.println("finishing " + name); e = null; temporizedEffectExpired(player); + AMazeIng.getMain().currentGameManager.revokePowerUp(player); + System.out.println("finishing " + name); return true; } } public void temporizedEffect(MazePlayer player) { - Vector3 p = MathUtils.toScreenCoords(player.getPos()); - e.setPosition(p.x, p.y); + if (e != null && !(AMazeIng.getMain().currentGameManager instanceof GameManagerServer)) { + Vector3 p = MathUtils.toScreenCoords(player.getPos()); + e.setPosition(p.x, p.y); + } beingUsed = true; } @@ -219,8 +226,8 @@ class PowerUpGiver extends PowerUp { if (AMazeIng.getMain().currentGameManager.players.size() > 1) { while (p == player || p == null) p = AMazeIng.getMain().currentGameManager.getRandomPlayer(); - p.currentPowerUp = powerup; - p.usePowerUp(); + AMazeIng.getMain().currentGameManager.assignPowerUp(p, powerup, true); + AMazeIng.getMain().currentGameManager.revokePowerUp(player); } return true; diff --git a/core/src/com/emamaker/amazeing/player/powerups/PowerUpBallAndChain.java b/core/src/com/emamaker/amazeing/player/powerups/PowerUpBallAndChain.java index df6cfb7..1ce1945 100755 --- a/core/src/com/emamaker/amazeing/player/powerups/PowerUpBallAndChain.java +++ b/core/src/com/emamaker/amazeing/player/powerups/PowerUpBallAndChain.java @@ -7,7 +7,7 @@ import com.emamaker.amazeing.utils.TextureLoader; public class PowerUpBallAndChain extends PowerUpTemporized { public PowerUpBallAndChain() { - super("BALL AND CHAIN", TextureLoader.textureBallAndChain, true, 10, 1f, 1f, + super("BALL_AND_CHAIN", TextureLoader.textureBallAndChain, true, 10, 1f, 1f, Gdx.files.internal("data/particles/ball_and_chain.particle"), Gdx.files.internal("data/powerups")); } @@ -27,7 +27,7 @@ public class PowerUpBallAndChain extends PowerUpTemporized { class PowerUpGiveBallAndChain extends PowerUpGiver { public PowerUpGiveBallAndChain() { - super(new PowerUpBallAndChain(), "BALL AND CHAIN GIVER", TextureLoader.textureBallAndChain, false, null, null); + super(new PowerUpBallAndChain(), "BALL_AND_CHAIN_GIVER", TextureLoader.textureBallAndChain, false, null, null); } } diff --git a/core/src/com/emamaker/amazeing/player/powerups/PowerUpBomb.java b/core/src/com/emamaker/amazeing/player/powerups/PowerUpBomb.java index 138929d..a03dbd5 100755 --- a/core/src/com/emamaker/amazeing/player/powerups/PowerUpBomb.java +++ b/core/src/com/emamaker/amazeing/player/powerups/PowerUpBomb.java @@ -9,7 +9,7 @@ import com.emamaker.amazeing.utils.TextureLoader; public class PowerUpBomb extends PowerUp { - int radius = 1; + int radius = 2; AMazeIng main = AMazeIng.getMain(); @@ -36,13 +36,13 @@ public class PowerUpBomb extends PowerUp { for (int k = 0; k < main.currentGameManager.mazeGen.h; k++) tmptodraw[i][k] = main.currentGameManager.mazeGen.todraw[i][k]; - for (int i = px - radius; i < px + radius + 1; i++) { - for (int k = pz - radius; k < pz + radius + 1; k++) { - tmptodraw = main.currentGameManager.mazeGen.changeMap(tmptodraw, i, k, 0); + for (int i = px - radius - 1; i < px + radius + 2; i++) { + for (int k = pz - radius - 1; k < pz + radius + 2; k++) { + if(player.getPos().dst(i, 1, k) <= radius) tmptodraw = main.currentGameManager.mazeGen.changeMap(tmptodraw, i, k, 0); } } - - AMazeIng.getMain().currentGameManager.requestChangeToMap(tmptodraw); + + AMazeIng.getMain().requestChangeToMap(tmptodraw); return true; } @@ -52,7 +52,7 @@ class PowerUpBigBomb extends PowerUpBomb{ public PowerUpBigBomb() { super("BIG BOMB", TextureLoader.textureBomb, false, 1.5f, 1.5f, Gdx.files.internal("data/particles/explosion.particle"), Gdx.files.internal("data/particles")); - radius = 2; + radius = 3; } } diff --git a/core/src/com/emamaker/amazeing/player/powerups/PowerUpSlug.java b/core/src/com/emamaker/amazeing/player/powerups/PowerUpSlug.java index fbd50a0..0c78fba 100755 --- a/core/src/com/emamaker/amazeing/player/powerups/PowerUpSlug.java +++ b/core/src/com/emamaker/amazeing/player/powerups/PowerUpSlug.java @@ -28,7 +28,7 @@ public class PowerUpSlug extends PowerUpTemporized { class PowerUpGiveSlug extends PowerUpGiver { public PowerUpGiveSlug() { - super(new PowerUpSlug(), "SLUG GIVER", TextureLoader.textureSlug, false, 1f, 1f, null, null); + super(new PowerUpSlug(), "SLUG_GIVER", TextureLoader.textureSlug, false, 1f, 1f, null, null); } } diff --git a/core/src/com/emamaker/amazeing/player/powerups/PowerUps.java b/core/src/com/emamaker/amazeing/player/powerups/PowerUps.java index 1ca0d8d..a61fa6e 100755 --- a/core/src/com/emamaker/amazeing/player/powerups/PowerUps.java +++ b/core/src/com/emamaker/amazeing/player/powerups/PowerUps.java @@ -46,6 +46,8 @@ public class PowerUps { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); + } catch (NullPointerException e) { + e.printStackTrace(); } return null; } diff --git a/core/src/com/emamaker/amazeing/utils/MathUtils.java b/core/src/com/emamaker/amazeing/utils/MathUtils.java index 5a81f2a..cef6025 100644 --- a/core/src/com/emamaker/amazeing/utils/MathUtils.java +++ b/core/src/com/emamaker/amazeing/utils/MathUtils.java @@ -55,8 +55,6 @@ public class MathUtils extends net.dermetfan.gdx.math.MathUtils { float hue = value*maxHue + (1-value)*minHue; Color c = new Color(java.awt.Color.HSBtoRGB(hue, 1, 1f)); - System.out.println(r + ", " + g + ", " + b); - return new Color(c); }