Skip to content

Commit

Permalink
Merge pull request #42 from SverreNystad/host-and-join-game
Browse files Browse the repository at this point in the history
Host and join game
  • Loading branch information
jmnorheim authored Apr 16, 2024
2 parents feb748f + 6a6e74d commit 7bfa7d7
Show file tree
Hide file tree
Showing 19 changed files with 475 additions and 438 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.softwarearchitecture.ecs.GraphicsController;
import com.softwarearchitecture.ecs.InputController;
import com.softwarearchitecture.ecs.SoundController;
import com.softwarearchitecture.game_server.GameServer;
import com.softwarearchitecture.game_server.ServerMessagingController;

public class Controllers {
Expand All @@ -11,12 +12,14 @@ public class Controllers {
public final SoundController soundController;
public final ServerMessagingController serverMessagingController;
public final ClientMessagingController clientMessagingController;
public final GameServer gameServer;

public Controllers(GraphicsController graphicsController, InputController inputController, SoundController soundController, ServerMessagingController serverMessagingController, ClientMessagingController clientMessagingController) {
public Controllers(GraphicsController graphicsController, InputController inputController, SoundController soundController, ServerMessagingController serverMessagingController, ClientMessagingController clientMessagingController, GameServer gameServer) {
this.graphicsController = graphicsController;
this.inputController = inputController;
this.soundController = soundController;
this.serverMessagingController = serverMessagingController;
this.clientMessagingController = clientMessagingController;
this.gameServer = gameServer;
}
}
18 changes: 14 additions & 4 deletions core/src/com/softwarearchitecture/game_client/GameClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

public class GameClient {
private ScreenManager screenManager;
private UUID yourId;

public GameClient(Controllers defaultControllers) throws IllegalArgumentException {
yourId = UUID.randomUUID();
private Controllers defaultControllers;

public GameClient(Controllers defaultControllers, UUID yourId) throws IllegalArgumentException {
this.defaultControllers = defaultControllers;
screenManager = ScreenManager.getInstance();
screenManager.nextState(new Menu(defaultControllers, yourId));

Expand All @@ -24,5 +23,16 @@ public void update() {
float deltaTime = 1f; // TODO: Implement deltatime

ECSManager.getInstance().update(deltaTime);

// Check if the player is in a multiplayer game
UUID gameId = null;
if (screenManager.getGameId() != null) {
gameId = screenManager.getGameId();
defaultControllers.clientMessagingController.requestGameState(gameId);
}
if (defaultControllers.gameServer.getGameId() != null) {
gameId = defaultControllers.gameServer.getGameId();
defaultControllers.clientMessagingController.requestGameState(gameId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.softwarearchitecture.ecs.components.SpriteComponent;
import com.softwarearchitecture.ecs.components.ButtonComponent.ButtonEnum;
import com.softwarearchitecture.game_client.TexturePack;
import com.softwarearchitecture.game_server.GameState;
import com.softwarearchitecture.math.Rectangle;
import com.softwarearchitecture.math.Vector2;

Expand All @@ -29,10 +30,47 @@ public class ButtonFactory {
public static Entity createAndAddButtonEntity(ButtonEnum button, Vector2 position, Vector2 size, Observer observer,
int z_index) throws IllegalArgumentException {
// factory that makes buttons based on the state enum
String texture = chooseTexture(button);
Runnable callback = () -> {
observer.onAction(button);
};

ButtonComponent buttonComponent = new ButtonComponent(position, size, button, z_index, callback);
PositionComponent positionComponent = new PositionComponent(position, z_index);
SpriteComponent spriteComponent = new SpriteComponent(texture, size);

Entity buttonEntity = new Entity();
buttonEntity.addComponent(ButtonComponent.class, buttonComponent);
buttonEntity.addComponent(PositionComponent.class, positionComponent);
buttonEntity.addComponent(SpriteComponent.class, spriteComponent);

ECSManager.getInstance().addEntity(buttonEntity);
return buttonEntity;
}

public static Entity createAndAddButtonEntity(ButtonEnum button, Vector2 position, Vector2 size, JoinGameObserver observer, GameState game, int z_index) {
String texture = chooseTexture(button);

// Here, we pass the game to the joinGame method directly in the lambda
Runnable callback = () -> observer.onJoinGame(game);

ButtonComponent buttonComponent = new ButtonComponent(position, size, button, z_index, callback);
PositionComponent positionComponent = new PositionComponent(position, z_index);
SpriteComponent spriteComponent = new SpriteComponent(texture, size);

Entity buttonEntity = new Entity();
buttonEntity.addComponent(ButtonComponent.class, buttonComponent);
buttonEntity.addComponent(PositionComponent.class, positionComponent);
buttonEntity.addComponent(SpriteComponent.class, spriteComponent);

ECSManager.getInstance().addEntity(buttonEntity);
return buttonEntity;
}

private static String chooseTexture(ButtonEnum button) {
String texture = TexturePack.BUTTON_PLACEHOLDER;
switch (button) {
case OPTIONS:
// create options buttons
texture = TexturePack.BUTTON_OPTION;
break;
case GAME_MENU:
Expand Down Expand Up @@ -89,23 +127,9 @@ public static Entity createAndAddButtonEntity(ButtonEnum button, Vector2 positio
default:
throw new IllegalArgumentException("Invalid button type");
}
Runnable callback = () -> {
observer.onAction(button);
};

ButtonComponent buttonComponent = new ButtonComponent(position, size, button, z_index, callback);
PositionComponent positionComponent = new PositionComponent(position, z_index);
SpriteComponent spriteComponent = new SpriteComponent(texture, size);

Entity buttonEntity = new Entity();
buttonEntity.addComponent(ButtonComponent.class, buttonComponent);
buttonEntity.addComponent(PositionComponent.class, positionComponent);
buttonEntity.addComponent(SpriteComponent.class, spriteComponent);

ECSManager.getInstance().addEntity(buttonEntity);
return buttonEntity;
return texture;
}

public static List<Rectangle> FindUVButtonPositions(int numberOfButtons, Vector2 containerUVPosition,
float containerUVWidth, float containerUVHeight) {
List<Rectangle> rectangles = new ArrayList<Rectangle>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class ChooseMap extends State implements Observer {

private boolean isHost;
/**
* Generic state is a state that can be used for multiple purposes
* for different types of menus.
Expand All @@ -28,6 +29,7 @@ public class ChooseMap extends State implements Observer {
*/
public ChooseMap(Controllers defaultControllers, UUID yourId, boolean isMultiplayer) {
super(defaultControllers, yourId);
isHost = isMultiplayer;
}

@Override
Expand Down Expand Up @@ -99,15 +101,16 @@ public void onAction(ButtonEnum type) {
// Switches the state of the game based on the button type

// TODO: MAKE BUTTON ACTIONS DYNAMIC CHOOSING MAPS
String map = "";
switch (type) {
case ABYSS:
System.out.println("Join button pressed");
screenManager.nextState(new InGame(defaultControllers, yourId, "abyss"));
System.out.println("Abyss map button pressed");
map = "abyss";
break;

case TEST:
System.out.println("Host button pressed");
screenManager.nextState(new InGame(defaultControllers, yourId, "test"));
System.out.println("Test map button pressed");
map = "test";
break;

case BACK:
Expand All @@ -117,8 +120,28 @@ public void onAction(ButtonEnum type) {

default:
throw new IllegalArgumentException("Invalid button type");

}
if (isHost) {
startServer(map);
}

screenManager.nextState(new InGame(defaultControllers, yourId, map));
}

private void startServer(String mapName) {
// Start the server
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Starting server on a new thread");
defaultControllers.gameServer.run(mapName);
} catch (Exception e) {
System.out.println("Error running server: " + e.getMessage());
e.printStackTrace();
}
}
}, "ServerThread").start();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.softwarearchitecture.game_client.states;

import com.softwarearchitecture.game_server.GameState;

public interface JoinGameObserver {

public void onJoinGame(GameState gameName);
}
25 changes: 13 additions & 12 deletions core/src/com/softwarearchitecture/game_client/states/JoinLobby.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import com.softwarearchitecture.ecs.components.ButtonComponent.ButtonEnum;
import com.softwarearchitecture.math.Vector2;

public class JoinLobby extends State implements Observer {
public class JoinLobby extends State implements Observer, JoinGameObserver {

private final int buttonZIndex = 3;

private final float gap = 0.2f;

public JoinLobby(Controllers defaultControllers, UUID yourId) {
super(defaultControllers, yourId);
}
Expand Down Expand Up @@ -56,12 +57,15 @@ protected void activate() {
// Create buttons for joining a game based on the available games
List<GameState> games = defaultControllers.clientMessagingController.getAllAvailableGames();
System.out.println("Games: " + games);
float translateY = 0.7f;
for (GameState game : games) {
System.out.println("Game: " + game.playerOne + " " + game.playerTwo);
ButtonEnum buttonType = ButtonEnum.JOIN;
Vector2 buttonWidth = new Vector2(0.2f, 0.2f);
float buttonX = 0.5f - buttonWidth.x / 2;
ButtonFactory.createAndAddButtonEntity(buttonType, new Vector2(buttonX, 0.5f), buttonWidth, this, buttonZIndex);
Vector2 position = new Vector2(buttonX, translateY);
ButtonFactory.createAndAddButtonEntity(buttonType, position, buttonWidth, this, game, buttonZIndex);
translateY -= gap;
}
}

Expand All @@ -78,23 +82,20 @@ public void onAction(ButtonEnum type) {
case MULTI_PLAYER:
screenManager.nextState(new Multiplayer(defaultControllers, yourId));
break;
case JOIN:
System.out.println("Join button pressed");
screenManager.nextState(new InGame(defaultControllers, yourId, "abyss"));
break;

default:
break;
}
}


private void joinGame(GameState game) {
@Override
public void onJoinGame(GameState game) {

// Send a message to the server to join the game
UUID gameID = game.playerOne.getId();
UUID gameID = game.gameID;
boolean didJoin = defaultControllers.clientMessagingController.joinGame(gameID, yourId);
// Wait for the server to respond
if (didJoin) {
screenManager.setGameId(gameID);
// Change the state to the game
screenManager.nextState(new InGame(defaultControllers, yourId, getGameName(game)));
} else {
Expand All @@ -105,6 +106,6 @@ private void joinGame(GameState game) {

private String getGameName(GameState game) {
// TODO: Get the name of the game
return "abyss";
return game.mapName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.UUID;

public class ScreenManager {
/**
Expand All @@ -11,10 +12,8 @@ public class ScreenManager {
private Boolean stateChanged;
private Deque<State> stateStack = new ArrayDeque<>();
private static ScreenManager instance = new ScreenManager();

private ScreenManager() {
}

private UUID gameId;

public static ScreenManager getInstance() {
return instance;
}
Expand Down Expand Up @@ -46,4 +45,12 @@ public void previousState() {
public void flushPreviousStates() {
this.stateStack.clear();
}

public void setGameId(UUID gameId) {
this.gameId = gameId;
}

public UUID getGameId() {
return this.gameId;
}
}
Loading

0 comments on commit 7bfa7d7

Please sign in to comment.