Skip to content
Snippets Groups Projects
Commit 0a86d962 authored by Sixten Müller's avatar Sixten Müller
Browse files

feat: lobby works gor players and host now

parent 6fee2ad9
No related branches found
No related tags found
2 merge requests!22Resolve "Create game model",!18Draft: Resolve "Create game model"
......@@ -84,7 +84,7 @@ public class AndroidInterfaceClass implements FirebaseInterface {
@Override
public void fetchPlayers(String pin, PlayerListUpdateCallback callback) {
/*DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
DatabaseReference lobbyRef = database.getReference("lobbies").child(pin);
lobbyRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
......@@ -98,6 +98,6 @@ public class AndroidInterfaceClass implements FirebaseInterface {
public void onCancelled(@NonNull DatabaseError databaseError) {
callback.onError(databaseError.getMessage());
}
});*/
});
}
}
......@@ -51,8 +51,6 @@ public class JoinGameController {
// Stay on JoinGameState and display an error message if joining lobby failed
Gdx.app.postRunnable(() -> joinGameView.setErrorMessage("Lobby does not exist"));
});
state.getStateManager().setState(new WaitForHostState(state.getStateManager(), _FBIC, pin, nickname));
} else {
// Stay on JoinGameState and display an error message if inputs are not valid
joinGameView.setErrorMessage("DB error");
......
......@@ -8,6 +8,8 @@ import com.wordbattle.game.network.FirebaseInterface;
import com.wordbattle.game.states.LobbyState;
import com.wordbattle.game.view.LobbyView;
import java.util.List;
public class LobbyController {
private LobbyState state;
private LobbyView lobbyView;
......@@ -21,12 +23,27 @@ public class LobbyController {
this.pin = pin;
this._FBIC = _FBIC;
this.lobbyView = new LobbyView(state.getCam(), pin);
_FBIC.createNewLobby(hostNickname, pin);
fetchPlayers();
}
private void fetchPlayers() {
_FBIC.fetchPlayers(pin, new FirebaseInterface.PlayerListUpdateCallback() {
@Override
public void onPlayerListUpdated(List<String> playerNames) {
System.out.println("Fetched players: " + playerNames);
lobbyView.setPlayerNames(playerNames);
}
@Override
public void onError(String error) {
System.err.println("Failed to fetch players: " + error);
}
});
}
public void handleInput() {
......
......@@ -34,8 +34,9 @@ public class WaitForHostController {
this.nickname = nickname;
this.pin = pin;
this._FBIC = _FBIC;
fetchPlayers();
this.waitForHostView = new WaitForHostView(state.getCam(), _FBIC, nickname, pin);
fetchPlayers();
}
......
......@@ -11,13 +11,15 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.wordbattle.game.WordBattle;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import java.util.ArrayList;
import java.util.List;
public class LobbyView {
private Texture backgroundTexture;
private BitmapFont font;
......@@ -28,6 +30,8 @@ public class LobbyView {
private Texture startGameTexture; // Texture for the "START GAME" button background
private Rectangle startTheGameBounds;
private List<String> playerNames = new ArrayList<>();
public LobbyView(OrthographicCamera cam, String pin) {
this.cam = cam;
......@@ -74,10 +78,20 @@ public class LobbyView {
float waitingForWidth = "WAITING FOR".length() * 14; // Approximate width per character is 14
float playersToJoinWidth = "PLAYERS TO JOIN...".length() * 14;
if (playerNames.size() < 2 ) {
// Draw "WAITING FOR" in the middle of the screen
font.draw(spriteBatch, "WAITING FOR", (WordBattle.WIDTH - waitingForWidth) / 2, 500);
// Draw "PLAYERS TO JOIN..." in the middle of the screen
font.draw(spriteBatch, "PLAYERS TO JOIN...", (WordBattle.WIDTH - playersToJoinWidth) / 2, 450);
}
else {
// Draw the list of players
float startY = WordBattle.HEIGHT / 2f; // Adjust Y as necessary
for (String playerName : playerNames) {
font.draw(spriteBatch, playerName, WordBattle.WIDTH / 2f, startY);
startY -= font.getLineHeight(); // Space the player names out
}
}
// Draw PIN
font.draw(spriteBatch, "PIN: " + pin, 160, 750);
......@@ -118,4 +132,7 @@ public class LobbyView {
}
public void setPlayerNames(List<String> playerNames) {
this.playerNames = playerNames;
}
}
......@@ -80,7 +80,18 @@ public class WaitForHostView {
spriteBatch.begin();
spriteBatch.draw(backgroundTexture, 0, 0, WordBattle.WIDTH, WordBattle.HEIGHT);
font.draw(spriteBatch, "Test123", 300, 300);
// Text on screen
font.draw(spriteBatch, "Pin: " + pin, WordBattle.WIDTH / 2f, WordBattle.HEIGHT - 50);
font.draw(spriteBatch, "Waiting for host to start the game...", WordBattle.WIDTH / 2f, 50);
// Draw the list of players
float startY = WordBattle.HEIGHT / 2f; // Adjust Y as necessary
for (String playerName : playerNames) {
font.draw(spriteBatch, playerName, WordBattle.WIDTH / 2f, startY);
startY -= font.getLineHeight(); // Space the player names out
}
spriteBatch.end();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment