From f04c23091608bf359200288fc062a1ab69f74320 Mon Sep 17 00:00:00 2001 From: Mikhael Danilov Date: Thu, 9 Jan 2025 01:35:22 +0300 Subject: [PATCH] a bit better keyboard control --- .../libgdx/java/android/view/KeyEvent.java | 4 +- .../java/com/nyrds/platform/game/Game.java | 44 ++++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/RemixedDungeonDesktop/src/libgdx/java/android/view/KeyEvent.java b/RemixedDungeonDesktop/src/libgdx/java/android/view/KeyEvent.java index ba4ff0910..9c9fc42c7 100644 --- a/RemixedDungeonDesktop/src/libgdx/java/android/view/KeyEvent.java +++ b/RemixedDungeonDesktop/src/libgdx/java/android/view/KeyEvent.java @@ -18,8 +18,8 @@ public class KeyEvent { public static final int ACTION_DOWN = 5; public static final int ACTION_UP = 6; - private int code; - private int action; + private final int code; + private final int action; public KeyEvent(int keyCode, int action) { code = keyCode; diff --git a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java index 4a4cad69b..e7f19597a 100644 --- a/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java +++ b/RemixedDungeonDesktop/src/libgdx/java/com/nyrds/platform/game/Game.java @@ -29,11 +29,12 @@ import com.watabou.pixeldungeon.scenes.GameScene; import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; import java.util.zip.Deflater; import lombok.Getter; - public class Game implements ApplicationListener, InputProcessor { private static Game instance; @@ -43,9 +44,11 @@ public class Game implements ApplicationListener, InputProcessor { protected GameLoop gameLoop; public Iap iap = new Iap(); - public PlayGames playGames = new PlayGames(); + private final Map keyDownTimes = new HashMap<>(); + private static final long AUTO_FIRE_INTERVAL = 250; + public Game(Class c) { super(); instance = this; @@ -66,9 +69,8 @@ public static void shutdown() { public static void toast(final String text, final Object... args) { } - static public void runOnMainThread(Runnable runnable) { - GameLoop.pushUiTask( runnable ); + GameLoop.pushUiTask(runnable); } public static boolean smallResScreen() { @@ -78,7 +80,6 @@ public static boolean smallResScreen() { public static void syncAdsState() { } - public static void vibrate(int milliseconds) { } @@ -93,6 +94,7 @@ public static void openUrl(String prompt, String address) { public static void sendEmail(String emailUri, String subject) { Gdx.net.openURI("mailto:" + emailUri + "?subject=" + subject); } + static public void openPlayStore() { } @@ -107,8 +109,8 @@ public void create() { @Override public void resize(int width, int height) { - GameLoop.width=width; - GameLoop.height=height; + GameLoop.width = width; + GameLoop.height = height; GameLoop.setNeedSceneRestart(); } @@ -130,6 +132,16 @@ public void render() { gameLoop.onFrame(); + // Check for auto-fire events + long currentTime = System.currentTimeMillis(); + for (Map.Entry entry : keyDownTimes.entrySet()) { + int keycode = entry.getKey(); + long lastFireTime = entry.getValue(); + if (currentTime - lastFireTime >= AUTO_FIRE_INTERVAL) { + GameLoop.instance().keysEvents.add(new KeyEvent(keycode, KeyEvent.ACTION_DOWN)); + keyDownTimes.put(keycode, currentTime); // Update the last fire time + } + } if (Gdx.input.isButtonPressed(Input.Buttons.RIGHT)) { takeScreenshot(); @@ -190,8 +202,9 @@ public void toggleFullscreen() { @Override public boolean keyDown(int keycode) { GameLoop.instance().keysEvents.add(new KeyEvent(keycode, KeyEvent.ACTION_DOWN)); + keyDownTimes.put(keycode, System.currentTimeMillis()); // Record the time when the key was pressed - if (keycode==Input.Keys.F11) { + if (keycode == Input.Keys.F11) { toggleFullscreen(); } @@ -201,6 +214,7 @@ public boolean keyDown(int keycode) { @Override public boolean keyUp(int keycode) { GameLoop.instance().keysEvents.add(new KeyEvent(keycode, KeyEvent.ACTION_UP)); + keyDownTimes.remove(keycode); // Remove the key from the map when it's released return true; } @@ -211,19 +225,19 @@ public boolean keyTyped(char character) { @Override public boolean touchDown(int screenX, int screenY, int pointer, int button) { - gameLoop.motionEvents.add( new PointerEvent(screenX, screenY, pointer, button, PointerEvent.Type.TOUCH_DOWN)); + gameLoop.motionEvents.add(new PointerEvent(screenX, screenY, pointer, button, PointerEvent.Type.TOUCH_DOWN)); return true; } @Override public boolean touchUp(int screenX, int screenY, int pointer, int button) { - gameLoop.motionEvents.add( new PointerEvent(screenX, screenY, pointer, button, PointerEvent.Type.TOUCH_UP)); + gameLoop.motionEvents.add(new PointerEvent(screenX, screenY, pointer, button, PointerEvent.Type.TOUCH_UP)); return true; } @Override public boolean touchDragged(int screenX, int screenY, int pointer) { - gameLoop.motionEvents.add( new PointerEvent(screenX, screenY, pointer, 0, PointerEvent.Type.TOUCH_DRAGGED)); + gameLoop.motionEvents.add(new PointerEvent(screenX, screenY, pointer, 0, PointerEvent.Type.TOUCH_DRAGGED)); return true; } @@ -239,14 +253,13 @@ public boolean mouseMoved(int screenX, int screenY) { @Override public boolean scrolled(float amountX, float amountY) { - if(GameLoop.scene() instanceof GameScene) { + if (GameLoop.scene() instanceof GameScene) { GamePreferences.zoom(GamePreferences.zoom() + amountY / 20); Camera.main.zoom((float) (GameScene.defaultZoom + GamePreferences.zoom())); } return true; } - public static boolean deleteFile(String path) { FileHandle file = Gdx.files.local(path); if (file.exists()) { @@ -257,8 +270,7 @@ public static boolean deleteFile(String path) { } public final void runOnUiThread(Runnable action) { - - action.run(); + action.run(); } static public void requestInternetPermission(InterstitialPoint returnTo) { @@ -290,4 +302,4 @@ public static void updateFpsLimit() { int[] limit = {30, 60, 120}; Gdx.graphics.setForegroundFPS(limit[GamePreferences.fps_limit()]); } -} +} \ No newline at end of file