Skip to content
Snippets Groups Projects
Commit 1a4e94ff authored by Thea Slemdal Bergersen's avatar Thea Slemdal Bergersen
Browse files

Merge branch '19-starting-game-loading-page' into 'main'

Start Game Loading Page

Closes #19

See merge request !12
parents 9420bc34 8b893ad7
No related branches found
No related tags found
1 merge request!12Start Game Loading Page
package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.wordbattle.game.states.ConnectingLobbyState;
import com.wordbattle.game.states.StartingGameLoadingState;
import com.wordbattle.game.view.LobbyView;
public class ConnectingLobbyController {
private ConnectingLobbyState state;
private LobbyView lobbyView;
private String pin;
public ConnectingLobbyController(ConnectingLobbyState state) {
this.state = state;
this.lobbyView = new LobbyView(state.getCam(),state.getPin());
}
public void handleInput() {
if (Gdx.input.justTouched()) {
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
state.getCam().unproject(touchPos); // convert from screen coordinates to world coordinates
if (lobbyView.getCreateGameBounds().contains(touchPos.x, touchPos.y)) {
state.getStateManager().setState(new StartingGameLoadingState(state.getStateManager()));
}
}
}
public void update(float dt) {
// Logic for updating the connecting lobby state
// For example, checking if all players have joined or handling any networking tasks
handleInput();
}
public void dispose() {
// Clean up any resources when exiting the state
lobbyView.dispose();
}
}
package com.wordbattle.game.controller;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.wordbattle.game.states.StartingGameLoadingState;
import com.wordbattle.game.view.StartingGameLoadingView;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class StartingGameLoadingController {
private StartingGameLoadingState state;
private StartingGameLoadingView loadingView;
public StartingGameLoadingController(StartingGameLoadingState state){
this.state = state;
this.loadingView = new StartingGameLoadingView(state.getCam());
}
public void handleInput() {
if (Gdx.input.justTouched()) {
Vector3 touchPos = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);
state.getCam().unproject(touchPos); // convert from screen coordinates to world coordinates
state.getCam().unproject(touchPos); // Transform touch coordinates
System.out.println("World touch: " + touchPos.x + ", " + touchPos.y);
}
}
public void update(float dt) {
handleInput();
}
public void render(SpriteBatch sb){
loadingView.render(sb);
}
public void dispose() {
// Clean up any resources when exiting the state
loadingView.dispose();
}
public void exit() {
dispose();
}
}
......@@ -9,6 +9,7 @@ public abstract class BaseState implements GameState {
protected StateManager gsm;
protected OrthographicCamera cam;
protected Viewport viewport;
protected String pin;
public BaseState(StateManager gsm) {
this.gsm = gsm;
......@@ -29,6 +30,9 @@ public abstract class BaseState implements GameState {
public OrthographicCamera getCam() {
return cam;
}
public String getPin() {
return pin;
}
// Implement other GameState methods or leave them abstract for subclasses to implement
// ...
......
......@@ -44,6 +44,10 @@ public class ConnectingLobbyState extends BaseState {
@Override
public void dispose() {
lobbyView.dispose();
controller.dispose(); // eller lobbyView
}
public StateManager getStateManager() {
return gsm;
}
}
package com.wordbattle.game.states;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.wordbattle.game.WordBattle;
import com.wordbattle.game.controller.StartingGameLoadingController;
import com.wordbattle.game.view.StartingGameLoadingView;
public class StartingGameLoadingState extends BaseState{
private StartingGameLoadingView startingView;
private StartingGameLoadingController controller;
public StartingGameLoadingState(StateManager gsm){
super(gsm);
startingView = new StartingGameLoadingView(cam);
this.controller = new StartingGameLoadingController(this);
cam.setToOrtho(false, WordBattle.WIDTH, WordBattle.HEIGHT);
}
@Override
public void handleInput() {
// Handle input for connecting lobby state
}
@Override
public void update(float dt){
controller.update(dt);
cam.update();
}
@Override
public void render(SpriteBatch sb) {
controller.render(sb);
}
@Override
public void enter() {
// Initialize any necessary components when entering the state
}
@Override
public void exit() {
// Clean up any resources when exiting the state
}
public StateManager getStateManager(){
return gsm;
}
@Override
public void dispose() {
startingView.dispose();
}
}
......@@ -9,6 +9,7 @@ import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
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;
......@@ -24,9 +25,10 @@ public class LobbyView {
private String pin;
private ImageButton startGameButton;
private Stage stage;
private Texture startGameTexture; // Texture for the "START GAME" button background
private Rectangle startTheGameBounds;
public LobbyView(OrthographicCamera cam, String pin) {
this.cam = cam;
this.pin = pin;
......@@ -50,8 +52,12 @@ public class LobbyView {
startGameButton.setPosition(WordBattle.WIDTH / 2 - startGameButton.getWidth() / 2, 100);
stage.addActor(startGameButton);
startTheGameBounds = new Rectangle((WordBattle.WIDTH / 2) - (startGameTexture.getWidth() / 2), 100, startGameTexture.getWidth(), startGameTexture.getHeight());
// Load background texture
backgroundTexture = new Texture("bg2.png");
}
public void render(SpriteBatch spriteBatch) {
......@@ -106,4 +112,10 @@ public class LobbyView {
public ImageButton getStartGameButton() {
return startGameButton;
}
public Rectangle getCreateGameBounds() {
return startTheGameBounds;
}
}
package com.wordbattle.game.view;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.wordbattle.game.WordBattle;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class StartingGameLoadingView {
private Texture backgroundTexture; // Stores the background texture
private BitmapFont font; // Bitmap font used for rendering text.
private OrthographicCamera cam; // Orthographic camera for rendering
private Stage stage; // Stage for handling UI elements
public StartingGameLoadingView(OrthographicCamera cam) {
this.cam = cam;
stage = new Stage(new ScreenViewport(cam));
Gdx.input.setInputProcessor(stage);
// Load and set up font
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("Knewave-Regular.ttf"));
FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
parameter.size = 36;
font = generator.generateFont(parameter);
generator.dispose();
// Load background texture
backgroundTexture = new Texture("bg2.png");
}
public void render(SpriteBatch spriteBatch) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
font.setColor(Color.valueOf("E456CE")); // Set text color to E456CE
cam.update();
spriteBatch.setProjectionMatrix(cam.combined);
spriteBatch.begin();
spriteBatch.draw(backgroundTexture, 0, 0, WordBattle.WIDTH, WordBattle.HEIGHT);
// Calculate the width of the text
float playersToJoinWidth = "STARTING GAME...".length() * 14;
// Draw "PLAYERS TO JOIN..." in the middle of the screen
font.draw(spriteBatch, "STARTING GAME...", (WordBattle.WIDTH - playersToJoinWidth) / 2, 450);
spriteBatch.end();
}
public void dispose() {
stage.dispose();
font.dispose();
backgroundTexture.dispose();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment