Byttet pakkenavn.

This commit is contained in:
Steinar Aalstad Lillesund
2020-04-14 15:54:09 +02:00
parent 9f716cc9a3
commit 382960d623
49 changed files with 94 additions and 93 deletions

View File

@@ -0,0 +1,30 @@
package inf112.fiasko.roborally.gamewrapper;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import inf112.fiasko.roborally.networking.RoboRallyClient;
import inf112.fiasko.roborally.networking.RoboRallyServer;
import inf112.fiasko.roborally.objects.IDrawableGame;
public class RoboRallyWrapper extends Game {
public SpriteBatch batch;
public BitmapFont font;
public ScreenManager screenManager;
public IDrawableGame roboRallyGame;
public RoboRallyServer server;
public RoboRallyClient client;
@Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("assets/Montserrat-Regular.fnt"));
this.screenManager = new ScreenManager();
this.setScreen(screenManager.getStartMenuScreen(this));
}
public void dispose() {
batch.dispose();
}
}

View File

@@ -0,0 +1,105 @@
package inf112.fiasko.roborally.gamewrapper;
import inf112.fiasko.roborally.gamewrapper.screens.BoardActiveScreen;
import inf112.fiasko.roborally.gamewrapper.screens.LoadingScreen;
import inf112.fiasko.roborally.gamewrapper.screens.PowerDownScreen;
import inf112.fiasko.roborally.gamewrapper.screens.UsernameScreen;
import inf112.fiasko.roborally.gamewrapper.screens.IPAddressScreen;
import inf112.fiasko.roborally.gamewrapper.screens.LobbyScreen;
import inf112.fiasko.roborally.gamewrapper.screens.StartMenuScreen;
/**
* Keeps track of screen instances
*/
public class ScreenManager {
private BoardActiveScreen boardActiveScreen;
private PowerDownScreen powerDownScreen;
private LoadingScreen loadingScreen;
private UsernameScreen usernameScreen;
private IPAddressScreen ipAddressScreen;
private LobbyScreen lobbyScreen;
/**
* Gets an instance of the power down screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A power down screen instance
*/
public synchronized PowerDownScreen getPowerDownScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.powerDownScreen == null) {
this.powerDownScreen = new PowerDownScreen(roboRallyWrapper);
}
return powerDownScreen;
}
/**
* Gets an instance of the lobby screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A lobby screen instance
*/
public synchronized LobbyScreen getLobbyScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.lobbyScreen == null) {
this.lobbyScreen = new LobbyScreen(roboRallyWrapper);
}
return lobbyScreen;
}
/**
* Gets an instance of the ip address screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return An ip address screen instance
*/
public synchronized IPAddressScreen getIPAddressScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.ipAddressScreen == null) {
this.ipAddressScreen = new IPAddressScreen(roboRallyWrapper);
}
return ipAddressScreen;
}
/**
* Gets an instance of the username screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A username screen instance
*/
public synchronized UsernameScreen getUsernameScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.usernameScreen == null) {
this.usernameScreen = new UsernameScreen(roboRallyWrapper);
}
return usernameScreen;
}
/**
* Gets an instance of the start menu screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A start menu screen instance
*/
public synchronized StartMenuScreen getStartMenuScreen(RoboRallyWrapper roboRallyWrapper) {
return new StartMenuScreen(roboRallyWrapper);
}
/**
* Gets an instance of the loading screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A loading screen instance
*/
public synchronized LoadingScreen getLoadingScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.loadingScreen == null) {
this.loadingScreen = new LoadingScreen(roboRallyWrapper);
}
return loadingScreen;
}
/**
* Gets an instance of the board active screen
* @param roboRallyWrapper The Robo Rally launcher instance to use
* @return A board active screen instance
*/
public synchronized BoardActiveScreen getBoardActiveScreen(RoboRallyWrapper roboRallyWrapper) {
if (this.boardActiveScreen == null) {
this.boardActiveScreen = new BoardActiveScreen(roboRallyWrapper);
}
return boardActiveScreen;
}
}

View File

@@ -0,0 +1,37 @@
package inf112.fiasko.roborally.gamewrapper;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
/**
* This class generates a simple text button using a default skin
*/
public class SimpleButton {
private final TextButton button;
/**
* Instantiates a new simple button
* @param text The text to display on the button
* @param font The font to use to draw button text
*/
public SimpleButton(String text, BitmapFont font) {
TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("uiskin.atlas"));
Skin skin = new Skin(buttonAtlas);
TextButton.TextButtonStyle confirmCardsStyle = new TextButton.TextButtonStyle();
confirmCardsStyle.font = font;
confirmCardsStyle.up = skin.getDrawable("default-round");
confirmCardsStyle.down = skin.getDrawable("default-round-down");
this.button = new TextButton(text, confirmCardsStyle);
}
/**
* Gets the button generated
* @return A button
*/
public TextButton getButton() {
return this.button;
}
}

View File

@@ -0,0 +1,36 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Screen;
/**
* This class overrides methods of screens which are often unused
*/
public abstract class AbstractScreen implements Screen {
protected final int applicationWidth = 600;
protected final int applicationHeight = 800;
@Override
public void show() {
//Nothing to do
}
@Override
public void pause() {
//Nothing to do
}
@Override
public void resume() {
//Nothing to do
}
@Override
public void hide() {
//Nothing to do
}
@Override
public void dispose() {
//Nothing to do
}
}

View File

@@ -0,0 +1,217 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.objects.IDrawableGame;
import inf112.fiasko.roborally.objects.IDrawableObject;
import inf112.fiasko.roborally.utility.IOUtil;
import inf112.fiasko.roborally.utility.TextureConverterUtil;
import java.util.List;
/**
* This screen shows the game board in real time
*/
public class BoardActiveScreen extends AbstractScreen implements InputProcessor {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private IDrawableGame debugGame;
private final int tileDimensions = 64;
private float cameraZoom = 1;
private int cameraX = 0;
private int cameraY = 0;
private Vector2 lastTouch;
private final int viewPortWidth = 12 * tileDimensions;
private final int viewPortHeight = 12 * tileDimensions;
private final Viewport viewport;
/**
* Instantiates a new board active screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public BoardActiveScreen(final RoboRallyWrapper roboRallyWrapper) {
this.roboRallyWrapper = roboRallyWrapper;
camera = new OrthographicCamera();
camera.setToOrtho(false, viewPortWidth, viewPortHeight);
camera.position.set(viewPortWidth / 2f, viewPortHeight / 2f, 0);
viewport = new ExtendViewport(viewPortWidth, viewPortHeight, camera);
lastTouch = new Vector2();
}
@Override
public void show() {
Gdx.input.setInputProcessor(this);
resetCamera();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT |
(Gdx.graphics.getBufferFormat().coverageSampling?GL20.GL_COVERAGE_BUFFER_BIT_NV:0));
updateCamera();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
drawBoard(roboRallyWrapper.batch);
roboRallyWrapper.batch.end();
}
@Override
public void dispose() {
for (Disposable disposable : TextureConverterUtil.getDisposableElements()) {
disposable.dispose();
}
}
@Override
public boolean keyDown(int keyCode) {
return false;
}
@Override
public boolean keyUp(int keyCode) {
if (keyCode == Input.Keys.HOME) {
IDrawableGame temp = roboRallyWrapper.roboRallyGame;
roboRallyWrapper.roboRallyGame = debugGame;
this.debugGame = temp;
return true;
}
return false;
}
@Override
public boolean keyTyped(char character) {
if (character == 'r') {
//camera.rotate(-90);
camera.rotateAround(
new Vector3(viewPortWidth / 2f, viewPortHeight / 2f, 0),
new Vector3(0, 0, 1), 90);
return true;
} else if (character == 'q') {
resetCamera();
}
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
lastTouch = new Vector2(screenX, screenY);
return true;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
lastTouch = new Vector2(screenX, screenY);
return true;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
Vector2 newTouch = new Vector2(screenX, screenY);
Vector2 diff = newTouch.cpy().sub(lastTouch);
lastTouch = newTouch;
int[] positionChange = translateCoordinateAccountingForCameraRotation(diff.x, diff.y);
cameraX = (int)(positionChange[0] * cameraZoom);
cameraY = (int)(positionChange[1] * cameraZoom);
return true;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean scrolled(int amount) {
if (amount < 0 && cameraZoom > 0.3 || amount > 0 && cameraZoom < 3) {
cameraZoom += 0.2 * amount;
}
return true;
}
/**
* Resets the camera to its initial position
*/
private void resetCamera() {
camera.up.x = 0;
camera.up.y = 1;
cameraZoom = 1;
camera.position.set(viewPortWidth / 2f, viewPortHeight / 2f, 0);
}
/**
* Renders all drawable objects on the board
* @param batch The sprite batch to use for drawing
*/
private void drawBoard(SpriteBatch batch) {
List<IDrawableObject> elementsToDraw =
IOUtil.getDrawableObjectsFromGame(roboRallyWrapper.roboRallyGame, tileDimensions, tileDimensions);
for (IDrawableObject object : elementsToDraw) {
TextureRegion objectTextureRegion = object.getTexture();
batch.draw(objectTextureRegion.getTexture(), object.getXPosition(), object.getYPosition(),
(float)object.getWidth() / 2, (float)object.getHeight() / 2,
object.getWidth(), object.getHeight(), 1, 1, object.getRotation(),
objectTextureRegion.getRegionX(), objectTextureRegion.getRegionY(),
objectTextureRegion.getRegionWidth(), objectTextureRegion.getRegionHeight(),
object.flipX(), object.flipY());
}
}
/**
* Updates the camera according to any user input
*/
private void updateCamera() {
camera.translate(cameraX, cameraY);
cameraX = 0;
cameraY = 0;
camera.zoom = cameraZoom;
camera.update();
}
/**
* Translates x and y coordinates according to the camera's direction
* @param x The x coordinate to translate
* @param y The y coordinate to translate
* @return A list containing the translated coordinates of x and y
*/
private int[] translateCoordinateAccountingForCameraRotation(float x, float y) {
int outX = 0;
int outY = 0;
int cameraUpX = Math.round(camera.up.x);
int cameraUpY = Math.round(camera.up.y);
if (cameraUpX == 0 && Math.round(camera.up.y) == 1) {
outX = (int)-x;
outY = (int)y;
} else if (cameraUpX == 0 && cameraUpY == -1) {
outX = (int)x;
outY = (int)-y;
} else if (cameraUpX == -1 && cameraUpY == 0) {
outX = (int)-y;
outY = (int)-x;
} else if (cameraUpX == 1 && cameraUpY == 0) {
outX = (int)y;
outY = (int)x;
}
return new int[]{outX, outY};
}
}

View File

@@ -0,0 +1,98 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.networking.RoboRallyClient;
import javax.swing.*;
import java.io.IOException;
/**
* This screen allows the user to enter the ip address to connect to
*/
public class IPAddressScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private final Viewport viewport;
private final Stage stage;
private TextField txtinput;
/**
* Instantiates a new ip address screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public IPAddressScreen(final RoboRallyWrapper roboRallyWrapper) {
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
TextButton joinButton = new TextButton("Join", skin);
joinButton.setSize(300,60);
joinButton.setPosition(applicationWidth/2f -joinButton.getWidth()/2f,300);
joinButton.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
return true;
}
@Override
public void touchUp(InputEvent e, float x, float y, int point, int button) {
try {
roboRallyWrapper.client = new RoboRallyClient(txtinput.getText(),roboRallyWrapper);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException ex) {
ex.printStackTrace();
JOptionPane.showMessageDialog(null, "Could not connect to the server."
+ " Please make sure the ip address you typed is correct, and that the server is online.");
}
}
});
txtinput = new TextField("", skin);
txtinput.setPosition(applicationWidth/2f -txtinput.getWidth()/2f,250);
txtinput.setSize(150,40);
stage.addActor(txtinput);
stage.addActor(joinButton);
camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
stage.setViewport(viewport);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Enter IP address and click the button to join a server",
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
}

View File

@@ -0,0 +1,93 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton;
import inf112.fiasko.roborally.networking.containers.GameStartInfo;
import inf112.fiasko.roborally.objects.Player;
import inf112.fiasko.roborally.utility.IOUtil;
import com.esotericsoftware.kryonet.Connection;
import java.util.List;
import java.util.Map;
/**
* This screen allows the host to wait for players to join
*/
public class LobbyScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private final Viewport viewport;
private final Stage stage;
/**
* Instantiates a new lobby screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public LobbyScreen(final RoboRallyWrapper roboRallyWrapper) {
camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera);
stage = new Stage();
TextButton startGameButton= new SimpleButton("Start", roboRallyWrapper.font).getButton();
stage.addActor(startGameButton);
startGameButton.setY(applicationHeight / 2f-50 );
startGameButton.setX(applicationWidth / 2f - startGameButton.getWidth() / 2f);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
startGameButton.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Map<Connection,String> playernames = roboRallyWrapper.server.getPlayerNames();
List<Player> playerlist = IOUtil.playerGenerator(playernames,
roboRallyWrapper.server.getRobotID());
for (Connection connection:playernames.keySet()) {
roboRallyWrapper.server.sendToClient(connection,new GameStartInfo("Checkmate.txt"
,playerlist,playernames.get(connection)));
}
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
return true;
}
});
stage.setViewport(viewport);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Joined players: " +
roboRallyWrapper.server.getPlayerNames().values().toString(),
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 350,380, 1,
true);
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Click the button to start game",
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
}

View File

@@ -0,0 +1,86 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton;
/**
* This screen is used for asking players whether they want to power down
*/
public class PowerDownScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private final Viewport viewport;
private final Stage stage;
private long startTime;
/**
* Instantiates a new power down screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public PowerDownScreen(final RoboRallyWrapper roboRallyWrapper) {
camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera);
stage = new Stage();
stage.setViewport(viewport);
TextButton powerDownButton = new SimpleButton("PowerDown", roboRallyWrapper.font).getButton();
stage.addActor(powerDownButton);
powerDownButton.setY(applicationHeight / 2f-50);
powerDownButton.setX(applicationWidth / 2f - powerDownButton.getWidth() / 2f);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
startTime = System.currentTimeMillis();
powerDownButton.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
return true;//her we do stuff
}
});
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.2f, 1f, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
int elapsedTime = (int)Math.floor((System.currentTimeMillis() - startTime) / 1000f);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Click the button to enter Power Down next round",
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.font.draw(roboRallyWrapper.batch, String.valueOf(10 - elapsedTime),
applicationWidth / 2f - 40 / 2f,applicationHeight / 2f - 100,40, 1,
true);
roboRallyWrapper.batch.end();
stage.draw();
if (elapsedTime > 10) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(this.roboRallyWrapper));
}
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
startTime = System.currentTimeMillis();
}
}

View File

@@ -0,0 +1,114 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
import inf112.fiasko.roborally.gamewrapper.SimpleButton;
import inf112.fiasko.roborally.networking.RoboRallyClient;
import inf112.fiasko.roborally.networking.RoboRallyServer;
import java.io.IOException;
/**
* This screen is the first screen shown to a player
*/
public class StartMenuScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private final Viewport viewport;
private final Stage stage;
/**
* Instantiates a new start menu screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public StartMenuScreen(final RoboRallyWrapper roboRallyWrapper) {
camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera);
stage = new Stage();
stage.setViewport(viewport);
TextButton serverButton = new SimpleButton("Create", roboRallyWrapper.font).getButton();
stage.addActor(serverButton);
serverButton.setY(applicationHeight / 2f);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
serverButton.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
try {
roboRallyWrapper.server = new RoboRallyServer();
roboRallyWrapper.client = new RoboRallyClient("127.0.0.1", roboRallyWrapper);
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getUsernameScreen(roboRallyWrapper));
} catch (IOException e) {
e.printStackTrace();
//Hard fail
Gdx.app.error("Server error", "Server could not be started");
Gdx.app.exit();
}
return true;
}
});
TextButton clientButton = new SimpleButton("Join", roboRallyWrapper.font).getButton();
stage.addActor(clientButton);
clientButton.setY(applicationHeight / 2f);
camera.setToOrtho(false, applicationWidth, applicationHeight);
Gdx.input.setInputProcessor(stage);
clientButton.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getIPAddressScreen(roboRallyWrapper));
return true;
}
});
TextButton quitButton = new SimpleButton("Quit", roboRallyWrapper.font).getButton();
stage.addActor(quitButton);
quitButton.setY(applicationHeight / 2f);
camera.setToOrtho(false, applicationWidth, applicationHeight);
quitButton.addListener(new InputListener() {
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.exit();
return true;
}
});
serverButton.setX(applicationWidth / 2f-serverButton.getWidth()-clientButton.getWidth()/2 - 10);
clientButton.setX(applicationWidth / 2f - clientButton.getWidth()/2);
quitButton.setX(applicationWidth / 2f + clientButton.getWidth()/2+10);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "RoboRally",
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
}

View File

@@ -0,0 +1,107 @@
package inf112.fiasko.roborally.gamewrapper.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import inf112.fiasko.roborally.gamewrapper.RoboRallyWrapper;
/**
* This screen allows a user to choose their player name
*/
public class UsernameScreen extends AbstractScreen {
private final RoboRallyWrapper roboRallyWrapper;
private final OrthographicCamera camera;
private final Viewport viewport;
private final Stage stage;
private TextField textInput;
/**
* Instantiates a new username screen
* @param roboRallyWrapper The Robo Rally wrapper which is parent of this screen
*/
public UsernameScreen(final RoboRallyWrapper roboRallyWrapper) {
stage = new Stage();
Skin skin = new Skin(Gdx.files.internal("uiskin.json"));
TextButton confirm = new TextButton("Confirm", skin);
confirm.setSize(300,60);
confirm.setPosition(applicationWidth/2f - confirm.getWidth()/2,300);
confirm.addListener(new ClickListener() {
@Override
public boolean touchDown(InputEvent e, float x, float y, int point, int button) {
return true;
}
@Override
public void touchUp(InputEvent e, float x, float y, int point, int button) {
String userName = textInput.getText();
if (nameInvalid(userName)) {
return;
}
roboRallyWrapper.client.sendElement(userName);
if (roboRallyWrapper.server == null) {
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLoadingScreen(roboRallyWrapper));
} else{
roboRallyWrapper.setScreen(roboRallyWrapper.screenManager.getLobbyScreen(roboRallyWrapper));
}
}
});
textInput = new TextField("",skin);
textInput.setPosition(applicationWidth/2f - textInput.getWidth()/2,250);
textInput.setSize(150,40);
stage.addActor(textInput);
stage.addActor(confirm);
camera = new OrthographicCamera();
viewport = new FitViewport(applicationWidth, applicationHeight, camera);
this.roboRallyWrapper = roboRallyWrapper;
camera.setToOrtho(false, applicationWidth, applicationHeight);
stage.setViewport(viewport);
}
/**
* Checks whether the username is invalid
* @param userName The username the user wants
* @return False if the username can be used
*/
private boolean nameInvalid(String userName) {
//TODO: Find a way to ask the server if the name is taken
return "".equals(userName);
}
@Override
public void show() {
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 0.5f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
roboRallyWrapper.batch.setProjectionMatrix(camera.combined);
roboRallyWrapper.batch.begin();
roboRallyWrapper.font.draw(roboRallyWrapper.batch, "Click the button to enter username",
applicationWidth / 2f - 380 / 2f,applicationHeight / 2f + 100,380, 1,
true);
roboRallyWrapper.batch.end();
stage.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
}
}