Skip to content

Commit

Permalink
a bit better keyboard control
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikhael-Danilov committed Jan 8, 2025
1 parent 4633b1f commit f04c230
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Integer, Long> keyDownTimes = new HashMap<>();
private static final long AUTO_FIRE_INTERVAL = 250;

public Game(Class<? extends Scene> c) {
super();
instance = this;
Expand All @@ -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() {
Expand All @@ -78,7 +80,6 @@ public static boolean smallResScreen() {
public static void syncAdsState() {
}


public static void vibrate(int milliseconds) {
}

Expand All @@ -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() {
}

Expand All @@ -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();
}

Expand All @@ -130,6 +132,16 @@ public void render() {

gameLoop.onFrame();

// Check for auto-fire events
long currentTime = System.currentTimeMillis();
for (Map.Entry<Integer, Long> 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();
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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()) {
Expand All @@ -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) {
Expand Down Expand Up @@ -290,4 +302,4 @@ public static void updateFpsLimit() {
int[] limit = {30, 60, 120};
Gdx.graphics.setForegroundFPS(limit[GamePreferences.fps_limit()]);
}
}
}

0 comments on commit f04c230

Please sign in to comment.