Game settings menu and settings structure. Have to fix camera rotation problem

master
EmaMaker 2020-03-29 22:18:23 +02:00
parent 8bcfbf9d3f
commit a63a7fc835
12 changed files with 353 additions and 125 deletions

View File

@ -23,7 +23,6 @@ public class AMazeIng extends Game {
public GameManager gameManager;
public InputMultiplexer multiplexer = new InputMultiplexer();
@Override
public void create() {
//Bullet init for physics

View File

@ -8,7 +8,7 @@ import com.badlogic.gdx.Game;
import com.badlogic.gdx.math.Vector3;
import com.emamaker.amazeing.AMazeIng;
import com.emamaker.amazeing.maze.MazeGenerator;
import com.emamaker.amazeing.maze.MazeSettings;
import com.emamaker.amazeing.maze.settings.MazeSettings;
import com.emamaker.amazeing.player.MazePlayer;
import com.emamaker.voxelengine.block.CellId;
import com.emamaker.voxelengine.player.Player;
@ -35,8 +35,8 @@ public class GameManager {
public void update() {
if (gameStarted) {
main.world.cam.position.set(mazeGen.w / 2, VoxelSettings.chunkSize * 1.25f, mazeGen.h / 2);
main.world.cam.lookAt(mazeGen.w / 2, 0, mazeGen.h / 2);
main.world.cam.position.set(mazeGen.w / 2, (MazeSettings.MAZEX+MazeSettings.MAZEZ) * 0.45f, mazeGen.h / 2);
main.world.cam.lookAt(MazeSettings.MAZEX / 2, 0, MazeSettings.MAZEX / 2);
main.world.cam.update();
main.world.render();
@ -61,6 +61,7 @@ public class GameManager {
ArrayList<MazePlayer> toDelete = new ArrayList<MazePlayer>();
public void generateMaze(Set<MazePlayer> pl) {
main.setScreen(null);
anyoneWon = false;
// Only add new players and dispose the old ones
@ -96,13 +97,14 @@ public class GameManager {
// Set camera
main.world.cam.lookAt(mazeGen.w / 2, 0, mazeGen.h / 2);
main.world.cam.rotate(Vector3.X, 135);
main.world.cam.rotate(Vector3.X, 15);
main.world.cam.update();
// Init the player with relative movement keys
// players.add(generateNewPlayer(Keys.W, Keys.S, Keys.A, Keys.D));
// players.add(generateNewPlayer(Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT));
spreadPlayers();
mazeGen.setupEndPoint();
gameStarted = true;
}

View File

@ -112,7 +112,7 @@ public class MazeGenerator {
}
//Setup end point in a random location. At a distance of EP_DIST from every player
void setupEndPoint(){
public void setupEndPoint(){
//Randomly spawns all the players
//For a spawn location to be valid, it has to be free from both walls and players
int x = 0, y = 0;
@ -125,6 +125,7 @@ public class MazeGenerator {
WINX = x;
WINZ = y;
todraw[x][y] = 2;
main.world.worldManager.setCell(WINX, 0, WINZ, CellId.ID_WOOD);
}
public void prepareShow() {
@ -159,14 +160,14 @@ public class MazeGenerator {
}
}
setupEndPoint();
//setupEndPoint();
for (int j = 0; j < h; j++) {
for (int i = 0; i < w; i++) {
main.world.worldManager.setCell(i, 0, j, CellId.ID_GRASS);
if (todraw[i][j] == 1)
main.world.worldManager.setCell(i, 1, j, CellId.ID_LEAVES);
else if (todraw[i][j] == 2)
main.world.worldManager.setCell(i, 0, j, CellId.ID_WOOD);
// if (todraw[i][j] == 2)
// main.world.worldManager.setCell(i, 0, j, CellId.ID_WOOD);
}
}
}

View File

@ -1,12 +0,0 @@
package com.emamaker.amazeing.maze;
public class MazeSettings {
public static int MAZEX = 20;
public static int MAZEZ = 20;
public void setMazeDimensions(int x, int z) {
MAZEX = x;
MAZEZ = z;
}
}

View File

@ -0,0 +1,84 @@
package com.emamaker.amazeing.maze.settings;
import java.util.Arrays;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.emamaker.amazeing.ui.UIManager;
public class MazeSetting {
/*This object holds whatever is needed for a single setting to be changed, this includes:
* A set of possible options
* Two buttons to go back and forth between the options
* A label with the current option
* A label with the name of the option
* Methods to set the correct variables, this should be overwritten by every single class
*/
protected int currentOption = 0;
protected String[] options;
protected String name;
protected Table table;
private UIManager uiManager;
private Label currentOptLabel;
public MazeSetting(String name_, String[] options_, UIManager uiManager_) {
this.currentOption = 0;
this.name = name_;
this.options = Arrays.copyOf(options_, options_.length);
this.uiManager = uiManager_;
//Build the Table which will be later used to add this to the screen
table = new Table();
Label nameLabel = new Label(this.name, uiManager.skin);
currentOptLabel = new Label(this.options[currentOption], uiManager.skin);
TextButton backBtn = new TextButton("<", uiManager.skin);
TextButton forthBtn = new TextButton(">", uiManager.skin);
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
currentOption--;
update();
return true;
}
});
forthBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
currentOption++;
update();
return true;
}
});
table.row().expandX().fillX();
table.add(nameLabel).fillX();
table.add(backBtn).fillX();
table.add(currentOptLabel).fillX();
table.add(forthBtn).fillX();
}
public Table getTable() {
return table;
}
public void update() {
preUpdate();
customUpdate();
}
public void preUpdate() {
this.currentOption = (this.currentOption+this.options.length)%this.options.length;
this.currentOptLabel.setText(this.options[currentOption]);
}
//This is the method to overwrite to update the MazeSettings variables
public void customUpdate() {}
}

View File

@ -0,0 +1,23 @@
package com.emamaker.amazeing.maze.settings;
import com.emamaker.amazeing.ui.UIManager;
public class MazeSettingDimension extends MazeSetting{
/* Maze dimension settings
* Provide Options in the x*z format
*/
public MazeSettingDimension(String name_, String[] options_, UIManager uiManager_) {
super(name_, options_, uiManager_);
}
@Override
public void customUpdate(){
String opt = options[currentOption];
String[] split = opt.split("x");
MazeSettings.MAZEX = Integer.valueOf(split[0]);
MazeSettings.MAZEZ = Integer.valueOf(split[1]);
}
}

View File

@ -0,0 +1,18 @@
package com.emamaker.amazeing.maze.settings;
import com.emamaker.amazeing.ui.UIManager;
public class MazeSettingMaxPlayers extends MazeSetting{
/* Game max. number of players settings*/
public MazeSettingMaxPlayers(String name_, String[] options_, UIManager uiManager_) {
super(name_, options_, uiManager_);
}
@Override
public void customUpdate(){
MazeSettings.MAXPLAYERS = Integer.valueOf(options[currentOption]);
}
}

View File

@ -0,0 +1,9 @@
package com.emamaker.amazeing.maze.settings;
public class MazeSettings {
//This must only hold public static variables to eventually their getters and setters
public static int MAZEX = 20;
public static int MAZEZ = 20;
public static int MAXPLAYERS = 8;
}

View File

@ -5,6 +5,7 @@ import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.emamaker.amazeing.AMazeIng;
import com.emamaker.amazeing.ui.screens.PlayerChooseScreen;
import com.emamaker.amazeing.ui.screens.SettingsScreen;
import com.emamaker.amazeing.ui.screens.TitleScreen;
public class UIManager {
@ -14,6 +15,7 @@ public class UIManager {
float delta;
public TitleScreen titleScreen;
public PlayerChooseScreen playersScreen;
public SettingsScreen setScreen;
public UIManager(Game main_) {
main = (AMazeIng)main_;
@ -23,6 +25,7 @@ public class UIManager {
//Load all the screens after loading the skin
titleScreen = new TitleScreen(this);
playersScreen = new PlayerChooseScreen(this);
setScreen = new SettingsScreen(this);
main.setScreen(titleScreen);
}

View File

@ -18,6 +18,7 @@ import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.emamaker.amazeing.maze.settings.MazeSettings;
import com.emamaker.amazeing.player.MazePlayer;
import com.emamaker.amazeing.ui.UIManager;
@ -26,13 +27,16 @@ public class PlayerChooseScreen implements Screen {
UIManager uiManager;
Stage stage;
Table table;
Label[] labels = new Label[8];
Label[] labels;
int currentLabel = 0;
HashMap<MazePlayer, Label> players = new HashMap<MazePlayer, Label>();
Screen thisScreen;
public PlayerChooseScreen(UIManager uiManager_) {
uiManager = uiManager_;
show();
//
// stage = new Stage(new ScreenViewport());
//
@ -88,27 +92,44 @@ public class PlayerChooseScreen implements Screen {
// tableContainer.setActor(table);
// stage.addActor(tableContainer);
float sw = Gdx.graphics.getWidth();
float sh = Gdx.graphics.getHeight();
// float sw = Gdx.graphics.getWidth();
// float sh = Gdx.graphics.getHeight();
// Table buttonTable = new Table();
//
// table.add(buttonTable).colspan(3);
// buttonTable.pad(16);
// buttonTable.add(backBtn).width(80);
// buttonTable.add(playBtn).width(80);
//
// tableContainer.setActor(table);
// stage.addActor(tableContainer);
}
@Override
public void show() {
thisScreen = this;
labels = new Label[MazeSettings.MAXPLAYERS];
stage = new Stage(new ScreenViewport());
Container<Table> tableContainer = new Container<Table>();
Table table = new Table();
float cw = stage.getWidth();
float ch = stage.getHeight();
tableContainer.setSize(cw, ch);
tableContainer.setPosition(0, 0);
Label instLab = new Label("Use WASD, ARROWS, or button on controller to join the match", uiManager.skin);
TextButton backBtn = new TextButton("Back", uiManager.skin);
TextButton setBtn = new TextButton("Settings", uiManager.skin);
TextButton helpBtn = new TextButton("?", uiManager.skin);
TextButton playBtn = new TextButton("Play", uiManager.skin);
//Labels to know if players joined
for(int i = 0; i < labels.length; i++) {
// Labels to know if players joined
for (int i = 0; i < labels.length; i++) {
labels[i] = new Label("Not joined yet", uiManager.skin);
}
@ -125,7 +146,7 @@ public class PlayerChooseScreen implements Screen {
playBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
if(!players.isEmpty()) {
if (!players.isEmpty()) {
hide();
uiManager.main.gameManager.generateMaze(players.keySet());
}
@ -136,7 +157,8 @@ public class PlayerChooseScreen implements Screen {
setBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Make this appear settings screen (TODO)");
uiManager.setScreen.setPrevScreen(thisScreen);
uiManager.main.setScreen(uiManager.setScreen);
return true;
}
});
@ -148,92 +170,82 @@ public class PlayerChooseScreen implements Screen {
return true;
}
});
Table firstRowTable = new Table();
firstRowTable.add(backBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw*0.25f);
firstRowTable.add(setBtn).height(50).fillX().expandX().space(cw*0.005f);
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
firstRowTable.add(backBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw * 0.25f);
firstRowTable.add(setBtn).height(50).fillX().expandX().space(cw * 0.005f);
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw * 0.005f);
firstRowTable.setOrigin(Align.center | Align.top);
table.row().colspan(4);
table.add(firstRowTable);
for(int i = 0; i < labels.length; i++) {
if(i % 4 == 0) table.row().expandY().fillY();
table.add( labels[i] ).space(1);
for (int i = 0; i < labels.length; i++) {
if (i % 4 == 0)
table.row().expandY().fillY();
table.add(labels[i]).space(1);
}
table.row().colspan(4);
table.add(playBtn).fillX().width(cw*0.06f);
table.add(playBtn).fillX().width(cw * 0.06f);
tableContainer.setActor(table);
stage.addActor(tableContainer);
// Table buttonTable = new Table();
//
// table.add(buttonTable).colspan(3);
// buttonTable.pad(16);
// buttonTable.add(backBtn).width(80);
// buttonTable.add(playBtn).width(80);
//
// tableContainer.setActor(table);
// stage.addActor(tableContainer);
}
@Override
public void show() {
uiManager.main.multiplexer.addProcessor(stage);
}
@Override
public void hide() {
uiManager.main.multiplexer.removeProcessor(stage);
uiManager.main.multiplexer.removeProcessor(stage);
}
MazePlayer p;
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
if(!uiManager.main.gameManager.gameStarted) {
//Consantly search for new players to be added
//First search for keyboard players (WASD and ARROWS)
if(Gdx.input.isKeyJustPressed(Keys.W) || Gdx.input.isKeyJustPressed(Keys.A) || Gdx.input.isKeyJustPressed(Keys.S) || Gdx.input.isKeyJustPressed(Keys.D)) {
p = getPlayerWithKeys(Keys.W, Keys.S, Keys.A, Keys.D);
if(p == null) p = new MazePlayer(uiManager.main, Keys.W, Keys.S, Keys.A, Keys.D);
// Consantly search for new players to be added
// First search for keyboard players (WASD and ARROWS)
if (Gdx.input.isKeyJustPressed(Keys.W) || Gdx.input.isKeyJustPressed(Keys.A)
|| Gdx.input.isKeyJustPressed(Keys.S) || Gdx.input.isKeyJustPressed(Keys.D)) {
p = getPlayerWithKeys(Keys.W, Keys.S, Keys.A, Keys.D);
if (p == null)
p = new MazePlayer(uiManager.main, Keys.W, Keys.S, Keys.A, Keys.D);
togglePlayer(p);
}
if (Gdx.input.isKeyJustPressed(Keys.UP) || Gdx.input.isKeyJustPressed(Keys.LEFT)
|| Gdx.input.isKeyJustPressed(Keys.DOWN) || Gdx.input.isKeyJustPressed(Keys.RIGHT)) {
p = getPlayerWithKeys(Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT);
if (p == null)
p = new MazePlayer(uiManager.main, Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT);
togglePlayer(p);
}
for (Controller c : Controllers.getControllers()) {
if (c.getButton(Xbox.Y)) {
p = getPlayerWithCtrl(c);
if (p == null)
p = new MazePlayer(uiManager.main, c);
togglePlayer(p);
}
if(Gdx.input.isKeyJustPressed(Keys.UP) || Gdx.input.isKeyJustPressed(Keys.LEFT) || Gdx.input.isKeyJustPressed(Keys.DOWN) || Gdx.input.isKeyJustPressed(Keys.RIGHT)) {
p = getPlayerWithKeys(Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT);
if(p == null) p = new MazePlayer(uiManager.main, Keys.UP, Keys.DOWN, Keys.LEFT, Keys.RIGHT);
togglePlayer(p);
}
for(Controller c : Controllers.getControllers()) {
if(c.getButton(Xbox.Y)){
p = getPlayerWithCtrl(c);
if(p == null) p = new MazePlayer(uiManager.main, c);
togglePlayer(p);
}
}
}
}
public void togglePlayer(MazePlayer p) {
try {
if(alreadyAddedPlayer(p)) {
if (alreadyAddedPlayer(p)) {
players.get(p).setText("Not Joined Yet");
players.remove(p);
}else {
} else {
players.put(p, labels[players.size()]);
players.get(p).setText("Player Read");
}
}catch(Exception e) {
} catch (Exception e) {
System.out.println("All players already joined");
}
}
@ -241,15 +253,16 @@ public class PlayerChooseScreen implements Screen {
public boolean alreadyAddedPlayerWithKeys(int... keys) {
return getPlayerWithKeys(keys) != null;
}
public boolean alreadyAddedPlayer(MazePlayer p) {
return players.containsKey(p);
}
public MazePlayer getPlayerWithKeys(int... keys) {
for(MazePlayer p : players.keySet()) {
for(int k : keys) {
if(p.kup == k || p.kdown == k || p.ksx == k || p.kdx == k) return p;
for (MazePlayer p : players.keySet()) {
for (int k : keys) {
if (p.kup == k || p.kdown == k || p.ksx == k || p.kdx == k)
return p;
}
}
return null;
@ -258,10 +271,11 @@ public class PlayerChooseScreen implements Screen {
public boolean alreadyAddedPlayerWithCtrl(Controller ctrl) {
return getPlayerWithCtrl(ctrl) != null;
}
public MazePlayer getPlayerWithCtrl(Controller ctrl) {
for(MazePlayer p : players.keySet()) {
if(p.ctrl == ctrl) return p;
for (MazePlayer p : players.keySet()) {
if (p.ctrl == ctrl)
return p;
}
return null;
}
@ -285,8 +299,7 @@ public class PlayerChooseScreen implements Screen {
@Override
public void dispose() {
// TODO Auto-generated method stub
stage.dispose();
}
}

View File

@ -1,49 +1,137 @@
package com.emamaker.amazeing.ui.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
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.Container;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.VerticalGroup;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.emamaker.amazeing.maze.settings.MazeSetting;
import com.emamaker.amazeing.maze.settings.MazeSettingDimension;
import com.emamaker.amazeing.maze.settings.MazeSettingMaxPlayers;
import com.emamaker.amazeing.ui.UIManager;
public class SettingsScreen implements Screen{
UIManager uiManager;
Stage stage;
Screen prevScreen;
public SettingsScreen(UIManager uiManager_) {
this.uiManager = uiManager_;
// float sw = Gdx.graphics.getWidth();
// float sh = Gdx.graphics.getHeight();
stage = new Stage(new ScreenViewport());
Container<Table> tableContainer = new Container<Table>();
Table table = new Table();
float cw = stage.getWidth();
float ch = stage.getHeight();
tableContainer.setSize(cw, ch);
tableContainer.setPosition(0, 0);
Label instLab = new Label("Here you can customize game settings!", uiManager.skin);
TextButton backBtn = new TextButton("Back", uiManager.skin);
TextButton helpBtn = new TextButton("?", uiManager.skin);
// Add actions to the buttons
backBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
hide();
uiManager.main.setScreen(prevScreen == null ? uiManager.titleScreen : prevScreen);
return true;
}
});
// Add actions to the buttons
helpBtn.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Make this appear help dialog (TODO)");
return true;
}
});
Table firstRowTable = new Table();
firstRowTable.add(backBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
firstRowTable.add(instLab).height(50).fillX().expandX().space(cw*0.25f);
firstRowTable.add(helpBtn).width(50).height(50).fillX().expandX().space(cw*0.005f);
firstRowTable.setOrigin(Align.center | Align.top);
table.row().colspan(4);
table.add(firstRowTable);
table.row().colspan(4);
table.add(setSettings());
tableContainer.setActor(table);
stage.addActor(tableContainer);
}
VerticalGroup setSettings() {
VerticalGroup group = new VerticalGroup().space(5).pad(5).fill();
//Add various settings here
MazeSetting setDim = new MazeSettingDimension("MAZE DIMENSIONS: ", new String[] {
"10x10", "20x20", "30x30"
}, this.uiManager);
MazeSetting setPlayers = new MazeSettingMaxPlayers("MAX NUMBER OF PLAYERS: ", new String[] {
"2", "4", "6", "8", "10", "15", "20"
}, this.uiManager);
group.addActor(setDim.getTable());
group.addActor(setPlayers.getTable());
return group;
}
public void setPrevScreen(Screen s) {
this.prevScreen = s;
}
@Override
public void show() {
// TODO Auto-generated method stub
}
@Override
public void render(float delta) {
// TODO Auto-generated method stub
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
uiManager.main.multiplexer.addProcessor(stage);
}
@Override
public void hide() {
// TODO Auto-generated method stub
uiManager.main.multiplexer.removeProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0,0,0,0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height, true);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
}

View File

@ -25,7 +25,7 @@ public class TitleScreen implements Screen {
Table table = new Table();
VerticalGroup mainScreenGroup = new VerticalGroup().space(5).pad(5).fill();
TextButton setBut = new TextButton("Customize Game Settings (TODO)", uiManager.skin);
TextButton setBut = new TextButton("Customize Game Settings", uiManager.skin);
mainScreenGroup.addActor(setBut);
TextButton servBut = new TextButton("Start Server and play online with friends (TODO)", uiManager.skin);
mainScreenGroup.addActor(servBut);
@ -62,10 +62,10 @@ public class TitleScreen implements Screen {
return true;
}
});
servBut.addListener(new InputListener() {
setBut.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("Make this appear settings screen (TODO)");
uiManager.main.setScreen(uiManager.setScreen);
return true;
}
});