From 37c5a342732fa14f49084625ea62fc96a03f64ba Mon Sep 17 00:00:00 2001 From: Lost-Fly <114507453+Lost-Fly@users.noreply.github.com> Date: Tue, 7 May 2024 15:22:47 +0500 Subject: [PATCH] upd correct logic --- .../packandgo/client/MessageProcessor.java | 2 +- .../packandgo/Screens/HomeSc.java | 49 ++++++++++++++----- .../packandgo/Tools/hud/GameHud.java | 32 +++++++----- .../Tools/maptools/BodyHelperService.java | 5 +- .../Tools/maptools/TileMapHelper.java | 4 +- 5 files changed, 62 insertions(+), 30 deletions(-) diff --git a/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java b/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java index 3765108..6e67a80 100644 --- a/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java +++ b/android/src/com/aqwsxlostfly/packandgo/client/MessageProcessor.java @@ -67,7 +67,7 @@ private void processObject(JsonValue object) { float x = object.getFloat("x"); float y = object.getFloat("y"); - main.renderer.getGameScreen().updatePlayerArray(id, x, y); + main.renderer.getCurrentScreen().updatePlayerArray(id, x, y); break; default: throw new RuntimeException("Unknown message type " + type); diff --git a/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java b/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java index a3fa856..7f36ae9 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java +++ b/core/src/com/aqwsxlostfly/packandgo/Screens/HomeSc.java @@ -54,6 +54,8 @@ public class HomeSc implements Screen { Stage stage; Skin skin; + Timer timer = new Timer(); + Texture backgroundTexture; Label gameNameLabel; TextButton createRoomBtn; @@ -63,6 +65,7 @@ public class HomeSc implements Screen { TextButton connectBtn; TextButton joinGameBtn; Dialog lobbyDialog; + Dialog errorDialog; private ArrayList flyingObjects; private Texture chairTexture, bathTexture, wardrobeTexture, tableTexture, lampTexture; @@ -216,6 +219,7 @@ public void clicked(InputEvent event, float x, float y) { joinGameBtn.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { + Gdx.app.log("JOINCLICK", "CLICK"); connectToServer(setPasswordField.getText(), setIdField.getText(), false); } }); @@ -254,7 +258,7 @@ protected void result(Object object) { lobbyDialog.button(actionButtonDialog, true); - actionButtonDialog.addListener(new ClickListener() { // Устанавливаем слушателя для кнопки в диалоге + actionButtonDialog.addListener(new ClickListener() { @Override public void clicked(InputEvent event, float x, float y) { connectToServer(passwordFieldDialog.getText(), idFieldDialog.getText(), isCreating); @@ -282,7 +286,6 @@ private void connectToServer(String password, String roomId, boolean isCreating) String action = isCreating ? "createRoom" : "joinRoom"; SessionState sessionState = new SessionStateToSend(action, roomId, password); try { - setGameScreen(new PlayScreen()); Main.messageSender.sendMessage(sessionState); startConnectionTimer(isCreating, roomId, password); } catch (Exception e) { @@ -292,52 +295,59 @@ private void connectToServer(String password, String roomId, boolean isCreating) } private void startConnectionTimer(boolean isCreating, String roomId, String password) { - Timer timer = new Timer(); - timer.scheduleTask(new Timer.Task() { - @Override - public void run() { - showErrorDialog(CONNECTING, CONNECTING_TEXT, 3); - } - }, 0, 500); + showErrorDialog(CONNECTING, CONNECTING_TEXT, 1); timer.scheduleTask(new Timer.Task() { @Override public void run() { checkConnectionStatus(isCreating, roomId, password); } - }, 3, 500); + }, 1); + } private void checkConnectionStatus(boolean isCreating, String roomId, String password) { if (main.gameSession.getSessionMsg() != null) { + resetButtons(); switch (main.gameSession.getSessionMsg()) { case "connected_ok": Gdx.app.log(isCreating ? CREATE_ROOM : JOIN_ROOM, "Creating a room with ID: " + roomId + " and password: " + password + " response " + main.gameSession.getSessionMsg()); + setGameScreen(new PlayScreen()); changeToPlayScreen(); break; case "session_exists": + resetSessionState(); showErrorDialog(SESSION_EXISTS, SESSION_EXIST_TEXT, 5); break; case "does_not_exists": + resetSessionState(); showErrorDialog(JOIN_ERROR, DOES_NOT_EXIST_TEXT, 5); break; case "incorrect_password": + resetSessionState(); showErrorDialog(JOIN_ERROR, INCORRECT_PASSWORD_TEXT, 5); break; case "players_limit": + resetSessionState(); showErrorDialog(JOIN_ERROR, MAX_PLAYERS_TEXT, 5); break; default: + resetSessionState(); showErrorDialog(SOME_ERROR, SOME_ERROR_TEXT, 5); break; } } } + private void resetButtons() { + createRoomBtn.setDisabled(false); // Включить кнопку + joinRoomBtn.setDisabled(false); // Включить кнопку + } + private void showErrorDialog(String title, String message, float autoCloseAfterSeconds) { - Dialog errorDialog = new Dialog(title, skin) { + errorDialog = new Dialog(title, skin) { @Override protected void result(Object object) { hide(); @@ -348,7 +358,16 @@ protected void result(Object object) { errorDialog.getTitleTable().padTop(40).padLeft(25).padBottom(30); errorDialog.getContentTable().add(new Label(message, skin)).pad(40); - errorDialog.button("OK", true); + TextButton okButton = new TextButton("OK", skin); + errorDialog.button(okButton, true); + + okButton.addListener(new ClickListener() { + @Override + public void clicked(InputEvent event, float x, float y) { + errorDialog.hide(); + } + }); + errorDialog.show(stage); if (autoCloseAfterSeconds > 0) { @@ -357,6 +376,11 @@ protected void result(Object object) { Actions.run(errorDialog::hide) )); } + + } + + private void resetSessionState() { + main.gameSession.setSessionMsg(null); } @@ -384,7 +408,6 @@ public void dispose() { wardrobeTexture.dispose(); tableTexture.dispose(); lampTexture.dispose(); - } @Override diff --git a/core/src/com/aqwsxlostfly/packandgo/Tools/hud/GameHud.java b/core/src/com/aqwsxlostfly/packandgo/Tools/hud/GameHud.java index b2014c8..fc15ba5 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Tools/hud/GameHud.java +++ b/core/src/com/aqwsxlostfly/packandgo/Tools/hud/GameHud.java @@ -1,15 +1,13 @@ package com.aqwsxlostfly.packandgo.Tools.hud; -import static com.aqwsxlostfly.packandgo.AssetManagerSingleton.getAssetManager; - import com.aqwsxlostfly.packandgo.Heroes.Player; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.ui.Skin; import com.badlogic.gdx.scenes.scene2d.ui.TextButton; -import com.badlogic.gdx.scenes.scene2d.InputEvent; import com.badlogic.gdx.scenes.scene2d.utils.ClickListener; import com.badlogic.gdx.utils.viewport.ScreenViewport; @@ -30,12 +28,9 @@ public GameHud(Player player) { skin = new Skin(Gdx.files.internal("skin/uiskin.json")); stage = new Stage(new ScreenViewport()); - // Установка обработчика ввода в текущую сцену - //Gdx.input.setInputProcessor(stage); - // Создание и настройка кнопки takeBtn = new TextButton("Take", skin); - fightBtn = new TextButton("GIVE PUSSY", skin); + fightBtn = new TextButton("Kick", skin); takeBtn.setSize(250, 150); // Установка размера кнопки takeBtn.setPosition(Gdx.graphics.getWidth() -450, Gdx.graphics.getHeight() / 2 - 450); // Позиционирование кнопки fightBtn.setSize(250, 150); // Установка размера кнопки @@ -53,12 +48,10 @@ public GameHud(Player player) { } public void draw() { - // Отрисовка сцены (и всех элементов UI на ней) stage.draw(); } public void update(float delta) { - // Обновление сцены stage.act(delta); } @@ -68,26 +61,39 @@ private void addListeners() { @Override public void clicked(InputEvent event, float x, float y) { Texture currentTexture = player.textureMapObject.getTextureRegion().getTexture(); - // Обработка нажатия на кнопку Gdx.app.log("Button Clicked", "Take button was pressed"); if (currentTexture != tabouret){ Gdx.app.log("INFO","смена текстуры"); player.textureMapObject.getTextureRegion().setTexture(tabouret); } else{ - //player.textureMapObject.getTextureRegion().setTexture(null); + player.textureMapObject.getTextureRegion().setTexture(hero); + } + } + }); + + fightBtn.addListener(new ClickListener() { + + @Override + public void clicked(InputEvent event, float x, float y) { + Texture currentTexture = player.textureMapObject.getTextureRegion().getTexture(); + Gdx.app.log("Button Clicked", "Kick button was pressed"); + if (currentTexture != tabouret){ + Gdx.app.log("INFO","кнопка ударить"); + player.textureMapObject.getTextureRegion().setTexture(tabouret); + + } else{ player.textureMapObject.getTextureRegion().setTexture(hero); } } }); } - // Вызовите этот метод в вашем основном цикле приложения для обеспечения обновления UI + public void resize(int width, int height) { stage.getViewport().update(width, height, true); } - // Не забудьте освободить ресурсы public void dispose() { stage.dispose(); skin.dispose(); diff --git a/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/BodyHelperService.java b/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/BodyHelperService.java index c81461c..3ff09fb 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/BodyHelperService.java +++ b/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/BodyHelperService.java @@ -15,7 +15,7 @@ public class BodyHelperService { public static final short CATEGORY_WALL = 0x0002; - public static Body createBody(TextureMapObject textureObject, World world, boolean isStatic) { + public static Body createBodyPlayer(TextureMapObject textureObject, World world, boolean isStatic) { TextureRegion textureRegion = textureObject.getTextureRegion(); float width = textureRegion.getRegionWidth(); @@ -48,4 +48,7 @@ public static Body createBody(TextureMapObject textureObject, World world, boole return body; } + + + } \ No newline at end of file diff --git a/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/TileMapHelper.java b/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/TileMapHelper.java index 3705152..41db817 100644 --- a/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/TileMapHelper.java +++ b/core/src/com/aqwsxlostfly/packandgo/Tools/maptools/TileMapHelper.java @@ -49,7 +49,7 @@ public void beginContact(Contact contact) { Fixture fixtureA = contact.getFixtureA(); Fixture fixtureB = contact.getFixtureB(); - Gdx.app.log("Contact", "beginContact between " + fixtureA.getBody().getType()+ " and " + fixtureB.getBody().getUserData()); + Gdx.app.log("Contact", "beginContact between " + fixtureA.getBody().getType() + " and " + fixtureB.getBody().getUserData()); } @Override @@ -179,7 +179,7 @@ public Player getPlayer() { TextureMapObject textureMapObjectNew = new TextureMapObject(textureMapObjectPlayer.getTextureRegion()); - Body body = BodyHelperService.createBody(textureMapObjectNew, + Body body = BodyHelperService.createBodyPlayer(textureMapObjectNew, world, false); body.setTransform(textureMapObjectPlayer.getX(), textureMapObjectPlayer.getY(), 0);