Skip to content

Commit de851ad

Browse files
committed
bug fix
1 parent 01f581f commit de851ad

18 files changed

Lines changed: 318 additions & 39 deletions

android/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ android {
3131
dependencies {
3232
implementation project(':core')
3333
implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
34+
implementation "com.journeyapps:zxing-android-embedded:4.3.0"
35+
implementation "androidx.core:core:1.13.1"
3436
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
3537
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
3638
natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"

android/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<uses-permission android:name="android.permission.INTERNET" />
55
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
66
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7+
<uses-permission android:name="android.permission.CAMERA" />
8+
<uses-feature android:name="android.hardware.camera" android:required="false" />
79

810
<application
911
android:allowBackup="true"
@@ -14,7 +16,7 @@
1416
android:name="com.codeheadsystems.mazer.android.AndroidLauncher"
1517
android:exported="true"
1618
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout"
17-
android:screenOrientation="landscape">
19+
android:screenOrientation="portrait">
1820
<intent-filter>
1921
<action android:name="android.intent.action.MAIN" />
2022
<category android:name="android.intent.category.LAUNCHER" />
Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,55 @@
11
package com.codeheadsystems.mazer.android;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.view.WindowManager;
6+
57
import com.badlogic.gdx.backends.android.AndroidApplication;
68
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
79
import com.codeheadsystems.mazer.MazerGame;
10+
import com.google.zxing.integration.android.IntentIntegrator;
11+
import com.google.zxing.integration.android.IntentResult;
812

913
public class AndroidLauncher extends AndroidApplication {
1014

15+
private AndroidPlatformServices platformServices;
16+
1117
@Override
1218
protected void onCreate(Bundle savedInstanceState) {
1319
super.onCreate(savedInstanceState);
1420

1521
// Keep screen on during gameplay
1622
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
1723

24+
platformServices = new AndroidPlatformServices(this);
25+
1826
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
1927
config.useAccelerometer = false;
2028
config.useCompass = false;
21-
config.useImmersiveMode = true; // hide system bars for fullscreen
22-
config.numSamples = 2; // light anti-aliasing
23-
initialize(new MazerGame(), config);
29+
config.useImmersiveMode = true;
30+
config.numSamples = 2;
31+
initialize(new MazerGame(platformServices), config);
32+
}
33+
34+
/**
35+
* Called by AndroidPlatformServices to launch the QR scanner.
36+
*/
37+
void launchQrScanner() {
38+
IntentIntegrator integrator = new IntentIntegrator(this);
39+
integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE);
40+
integrator.setPrompt("Scan the host's QR code to join");
41+
integrator.setOrientationLocked(true);
42+
integrator.setBeepEnabled(false);
43+
integrator.initiateScan();
44+
}
45+
46+
@Override
47+
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
48+
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
49+
if (result != null) {
50+
platformServices.onScanResult(result.getContents()); // null if cancelled
51+
} else {
52+
super.onActivityResult(requestCode, resultCode, data);
53+
}
2454
}
2555
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.codeheadsystems.mazer.android;
2+
3+
import com.codeheadsystems.mazer.platform.PlatformServices;
4+
5+
/**
6+
* Android implementation of PlatformServices.
7+
* Uses zxing-android-embedded for QR code scanning via the camera.
8+
*/
9+
public class AndroidPlatformServices implements PlatformServices {
10+
11+
private final AndroidLauncher activity;
12+
private QrScanCallback pendingCallback;
13+
14+
public AndroidPlatformServices(AndroidLauncher activity) {
15+
this.activity = activity;
16+
}
17+
18+
@Override
19+
public boolean isQrScanAvailable() {
20+
return true;
21+
}
22+
23+
@Override
24+
public void scanQrCode(QrScanCallback callback) {
25+
this.pendingCallback = callback;
26+
activity.launchQrScanner();
27+
}
28+
29+
/**
30+
* Called by AndroidLauncher when the scan result comes back.
31+
*/
32+
void onScanResult(String result) {
33+
if (pendingCallback != null) {
34+
QrScanCallback cb = pendingCallback;
35+
pendingCallback = null;
36+
com.badlogic.gdx.Gdx.app.postRunnable(() -> cb.onResult(result));
37+
}
38+
}
39+
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ buildscript {
44
google()
55
}
66
dependencies {
7-
classpath 'com.android.tools.build:gradle:9.1.0'
7+
classpath 'com.android.tools.build:gradle:8.7.3'
88
}
99
}
1010

core/src/main/java/com/codeheadsystems/mazer/MazerGame.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@
22

33
import com.badlogic.gdx.Game;
44
import com.badlogic.gdx.Gdx;
5+
import com.codeheadsystems.mazer.platform.DefaultPlatformServices;
6+
import com.codeheadsystems.mazer.platform.PlatformServices;
57
import com.codeheadsystems.mazer.screen.MenuScreen;
68

79
/**
810
* Main game entry point. Manages screen transitions and Android lifecycle.
911
*/
1012
public class MazerGame extends Game {
1113

14+
private final PlatformServices platformServices;
15+
16+
public MazerGame() {
17+
this(new DefaultPlatformServices());
18+
}
19+
20+
public MazerGame(PlatformServices platformServices) {
21+
this.platformServices = platformServices;
22+
}
23+
24+
public PlatformServices getPlatformServices() {
25+
return platformServices;
26+
}
27+
1228
@Override
1329
public void create() {
1430
setScreen(new MenuScreen(this));

core/src/main/java/com/codeheadsystems/mazer/input/DesktopInputProcessor.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
public class DesktopInputProcessor extends InputAdapter {
1212

1313
private final InputState inputState;
14-
private boolean firePressed;
1514

1615
public DesktopInputProcessor(InputState inputState) {
1716
this.inputState = inputState;
@@ -44,9 +43,9 @@ public void update() {
4443
inputState.turnAmount = 0f;
4544
}
4645

47-
// Fire: spacebar (edge-triggered)
48-
boolean spaceDown = Gdx.input.isKeyPressed(Input.Keys.SPACE);
49-
inputState.fire = spaceDown && !firePressed;
50-
firePressed = spaceDown;
46+
// Fire: spacebar (level-triggered, held = fire).
47+
// Server-side cooldown (1sec) prevents rapid fire.
48+
// Level-triggered ensures the fire signal isn't missed by throttled UDP sends.
49+
inputState.fire = Gdx.input.isKeyPressed(Input.Keys.SPACE);
5150
}
5251
}

core/src/main/java/com/codeheadsystems/mazer/net/NetworkManager.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import com.codeheadsystems.mazer.net.Protocol.LobbyUpdate;
55
import com.codeheadsystems.mazer.net.Protocol.PlayerInfo;
66

7+
import java.util.HashMap;
8+
import java.util.Map;
79
import java.util.function.Consumer;
810

911
/**
@@ -16,6 +18,7 @@ public class NetworkManager {
1618
private ClientConnection clientConnection;
1719
private boolean isHost;
1820
private int localPlayerId = -1;
21+
private final Map<Integer, String> playerNames = new HashMap<>();
1922

2023
// Callbacks set by screens
2124
private Consumer<LobbyUpdate> onLobbyUpdate;
@@ -123,11 +126,24 @@ public String getConnectString() {
123126
// --- Internal callbacks from HostServer/ClientConnection ---
124127

125128
void fireLobbyUpdate(LobbyUpdate update) {
129+
// Cache player names for later use (e.g., GameOverScreen)
130+
if (update.players != null) {
131+
for (PlayerInfo info : update.players) {
132+
playerNames.put(info.id, info.name);
133+
}
134+
}
126135
if (onLobbyUpdate != null) {
127136
onLobbyUpdate.accept(update);
128137
}
129138
}
130139

140+
/**
141+
* Returns the player name for the given ID, or "Player N" as fallback.
142+
*/
143+
public String getPlayerName(int playerId) {
144+
return playerNames.getOrDefault(playerId, "Player " + playerId);
145+
}
146+
131147
void fireGameStart(Protocol.StartGame msg) {
132148
if (onGameStart != null) {
133149
onGameStart.accept(msg);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.codeheadsystems.mazer.platform;
2+
3+
/**
4+
* Default (no-op) implementation for platforms that don't support scanning.
5+
*/
6+
public class DefaultPlatformServices implements PlatformServices {
7+
8+
@Override
9+
public boolean isQrScanAvailable() {
10+
return false;
11+
}
12+
13+
@Override
14+
public void scanQrCode(QrScanCallback callback) {
15+
callback.onResult(null);
16+
}
17+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.codeheadsystems.mazer.platform;
2+
3+
/**
4+
* Platform-specific services interface. Implemented per-platform (Android, Desktop).
5+
* Injected into MazerGame at launch.
6+
*/
7+
public interface PlatformServices {
8+
9+
/**
10+
* Returns true if QR code scanning is available on this platform.
11+
*/
12+
boolean isQrScanAvailable();
13+
14+
/**
15+
* Launches a QR code scanner. The result is delivered asynchronously
16+
* via the callback. Called on the GL thread.
17+
*
18+
* @param callback receives the scanned text, or null if cancelled/failed
19+
*/
20+
void scanQrCode(QrScanCallback callback);
21+
22+
interface QrScanCallback {
23+
void onResult(String scannedText);
24+
}
25+
}

0 commit comments

Comments
 (0)