most basic of menu (element collection + render/update)

This commit is contained in:
Armin Benz 2017-11-22 19:37:25 +01:00
parent 6078009368
commit fb2de07f9a
13 changed files with 1776 additions and 457 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

View File

@ -6,7 +6,7 @@ import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.audio.Music;
import de.samdev.colorrunner.screens.menu.SplashScreen;
import de.samdev.colorrunner.screens.menu.impl.SplashScreen;
public class CRGame extends Game {
public final static boolean DEBUG = true;

View File

@ -17,8 +17,6 @@ import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEnt
import de.samdev.colorrunner.game.world.map.provider.MapProvider;
import de.samdev.colorrunner.input.GameInputListener;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
import de.samdev.colorrunner.screens.menu.EndlessGameMenu;
import de.samdev.colorrunner.screens.menu.MainMenu;
public class CRGameWorld implements GameInputListener {
private Preferences prefs = Gdx.app.getPreferences("settings");
@ -61,10 +59,11 @@ public class CRGameWorld implements GameInputListener {
Gdx.input.setCatchBackKey(true);
if(Gdx.input.isKeyPressed(Input.Keys.BACK) || Gdx.input.isKeyPressed(Input.Keys.BACKSPACE)) {
Gdx.input.setCatchBackKey(false);
if(mapprovider.isNextEndlessMenu())
((Game) Gdx.app.getApplicationListener()).setScreen(new EndlessGameMenu(music));
else
((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
//TODO ::
//if(mapprovider.isNextEndlessMenu())
// ((Game) Gdx.app.getApplicationListener()).setScreen(new EndlessGameMenu(music));
//else
// ((Game) Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
for (int i = entities.size()-1; i >= 0; i--) {

View File

@ -21,7 +21,6 @@ import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;
import de.samdev.colorrunner.game.world.map.provider.TriggerType;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
import de.samdev.colorrunner.screens.menu.MainMenu;
public class PlayerEntity extends MovingEntity {
public final static float PLAYER_WIDTH = 31.9f;
@ -81,10 +80,11 @@ public class PlayerEntity extends MovingEntity {
ingameprefs.flush();
int maximumlvl = 4;
if(currantlvl > maximumlvl)
((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
else
CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.map_map.get(currantlvl))));
//TODO ::
//if(currantlvl > maximumlvl)
// ((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
//else
// CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.map_map.get(currantlvl))));
world.music.stop();
break;

View File

@ -0,0 +1,92 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import java.util.ArrayList;
import java.util.List;
public abstract class BaseMenu implements Screen {
private final int VIEWPORT_WIDTH = 100;
private final int VIEWPORT_HEIGHT = 100;
private List<BaseMenuElement> elements = new ArrayList<BaseMenuElement>();
private OrthographicCamera cam;
private SpriteBatch sbatch = new SpriteBatch();
public BaseMenu() {
super();
cam = new OrthographicCamera();
cam.position.x += VIEWPORT_WIDTH/2;
cam.position.y += VIEWPORT_HEIGHT/2;
cam.viewportWidth += VIEWPORT_WIDTH;
cam.viewportHeight += VIEWPORT_HEIGHT;
cam.update();
sbatch.setProjectionMatrix(cam.combined);
init();
}
protected abstract void init();
protected void add(BaseMenuElement e) {
elements.add(e);
}
@Override
public void show() {
// NOP
}
@Override
public void render(float delta) {
// ####### UPDATE #########
for (BaseMenuElement e : elements) {
e.update(delta);
}
// ####### RENDER #########
Gdx.gl.glClearColor(0.22f, 0.22f, 0.22f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sbatch.begin();
for (BaseMenuElement e : elements) {
e.render(sbatch);
}
sbatch.end();
}
@Override
public void resize(int width, int height) {
// NOP
}
@Override
public void pause() {
// NOP
}
@Override
public void resume() {
// NOP
}
@Override
public void hide() {
// NOP
}
@Override
public void dispose() {
}
}

View File

@ -0,0 +1,10 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public abstract class BaseMenuElement {
public abstract void render(SpriteBatch batch);
public abstract void update(float delta);
}

View File

@ -1,132 +0,0 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
/**
* Created by benza on 16.04.2017.
*/
public class EndlessGameMenu implements Screen {
private OrthographicCamera cam = new OrthographicCamera();
private Texture background = new Texture("images/background2.jpg");
private Music music;
private Preferences endlessGameInfos = Gdx.app.getPreferences("endlessGameInfos");
private Stage stage = new Stage();
private Table table = new Table();
private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));
private int highScore = endlessGameInfos.getInteger("Highscore", 0);
private int lastBest = endlessGameInfos.getInteger("lastBest", 0);
private int tries = endlessGameInfos.getInteger("Tries", 0);
private TextButton buttonPlay = new TextButton("Play", skin);
private TextButton buttonBack = new TextButton("Back", skin);
private Label labelTitle = new Label("Endless Game", skin);
private Label labelHighscore = new Label("Highscore: " + highScore, skin);
private Label labelLastBest = new Label("Last Best: " + lastBest, skin);
private Label labelTries = new Label("Tries: " + tries, skin);
public EndlessGameMenu(Music music) {
this.music = music;
}
@Override
public void show() {
buttonPlay.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
CRGame.Inst().setScreen(new GameScreen(new EndlessMapProvider(System.currentTimeMillis())));
music.stop();
}
});
buttonBack.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
CRGame.Inst().setScreen(new MainMenu());
}
});
table.add(labelTitle).padBottom(40).row();
table.add(labelHighscore).padBottom(20).row();
table.add(labelLastBest).padBottom(20).row();
table.add(labelTries).padBottom(20).row();
table.add(buttonPlay).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
table.add(buttonBack).size((int)((double)Gdx.graphics.getWidth() / 2.0D),80).padBottom(20).row();
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.setToOrtho(false, (float)Gdx.graphics.getWidth() / 2.0F, (float)Gdx.graphics.getHeight() / 2.0F);
SpriteBatch sb = new SpriteBatch();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, 0, 0);
sb.end();
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}

View File

@ -1,178 +0,0 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.map.CRMapStorage;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;
import de.samdev.colorrunner.screens.gameScreen.GameScreen;
public class MainMenu implements Screen {
private Preferences prefs = Gdx.app.getPreferences("settings");
private Preferences ingameprefs = Gdx.app.getPreferences("ingamepreferences");
private Stage stage = new Stage();
private Table table = new Table();
private OrthographicCamera cam = new OrthographicCamera();
private Texture background = new Texture("images/background2.jpg");
private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));
private TextButton buttonPlay1 = new TextButton("Play Levels", skin);
private TextButton buttonPlay2 = new TextButton("Play Endless", skin);
private TextButton buttonOption = new TextButton("Option", skin);
private TextButton buttonExit = new TextButton("Exit", skin);
// folgendes mal als Beispiel :)
/*
button3.setSize(col_width*4,(float)(row_height*2));
button3.getStyle().imageUp = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_off.png"))));
button3.getStyle().imageDown = new TextureRegionDrawable(new TextureRegion(new Texture(Gdx.files.internal("switch_on.png"))));
button3.setPosition(col_width,Gdx.graphics.getHeight()-row_height*6);
button3.addListener(new InputListener(){
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
outputLabel.setText("Press a Button");
}
@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
outputLabel.setText("Pressed Image Button");
return true;
}
});
*/
private Label title = new Label("Color Runner" , skin);
private Label highestscore = new Label("Highest Level: " + ingameprefs.getInteger("highestlevel", 1), skin);
public Music music;
public MainMenu() {
music = CRGame.manager.get("sound/menusound.mp3", Music.class);
music.setLooping(true);
if(prefs.getBoolean("sound", true))
music.play();
else
music.stop();
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.setToOrtho(false, (float)Gdx.graphics.getWidth() / 2.0F, (float)Gdx.graphics.getHeight() / 2.0F);
SpriteBatch sb = new SpriteBatch();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, 0, 0);
sb.end();
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
// NOP
}
@Override
public void show() {
buttonPlay1.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
//int level = ingameprefs.getInteger("highestlevel", 1);
ingameprefs.putInteger("currantlvl", 1);
int currantlvl = ingameprefs.getInteger("currantlvl", 1);
CRGame.Inst().setScreen(new GameScreen(new StaticMapProvider(CRMapStorage.map_map.get(currantlvl))));
music.stop();
}
});
buttonPlay2.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
//CRGame.Inst().setScreen(new GameScreen(new EndlessMapProvider(System.currentTimeMillis())));
CRGame.Inst().setScreen(new EndlessGameMenu(music));
}
});
buttonOption.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
CRGame.Inst().setScreen(new OptionMenuScreen(music));
}
});
buttonExit.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.exit();
}
});
table.add(buttonPlay1).size((int)((double)Gdx.graphics.getWidth() / 2.0D),250);
table.add(buttonPlay2).size((int)((double)Gdx.graphics.getWidth() / 2.0D),250).row();
table.add(title).size((int)((double)Gdx.graphics.getWidth() / 2.0D), 62);
table.add(highestscore).size((int)((double)Gdx.graphics.getWidth() / 2.0D),62).row();
table.add(buttonOption).size((int)((double)Gdx.graphics.getWidth() / 2.0D),250);
table.add(buttonExit).size((int)((double)Gdx.graphics.getWidth() / 2.0D),250);
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
// NOP
}
@Override
public void resume() {
// NOP
}
@Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}

View File

@ -1,133 +0,0 @@
package de.samdev.colorrunner.screens.menu;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
public class OptionMenuScreen implements Screen{
private Preferences prefs = Gdx.app.getPreferences("settings");
private Stage stage = new Stage();
private Table table = new Table();
private Texture background = new Texture("images/background2.jpg");
private OrthographicCamera cam = new OrthographicCamera();
private Skin skin = new Skin(Gdx.files.internal("skins/menuSkin.json"),
new TextureAtlas(Gdx.files.internal("skins/menuSkin.pack")));
private Music music;
public OptionMenuScreen(Music music) {
this.music = music;
}
private Label title = new Label("Option", skin);
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.setToOrtho(false, (float)Gdx.graphics.getWidth() / 2.0F, (float)Gdx.graphics.getHeight() / 2.0F);
SpriteBatch sb = new SpriteBatch();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(background, 0, 0);
sb.end();
stage.act();
stage.draw();
}
@Override
public void resize(int width, int height) {
// NOP
}
@Override
public void show() {
TextButton buttonSound;
if(prefs.getBoolean("sound", true))
buttonSound = new TextButton("Sound On", skin);
else
buttonSound = new TextButton("Sound Off", skin);
TextButton buttonBack = new TextButton("Back", skin);
buttonSound.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y){
if(prefs.getBoolean("sound", true)) {
prefs.putBoolean("sound", false);
music.stop();
}
else {
prefs.putBoolean("sound", true);
music.play();
}
prefs.flush();
((Game)Gdx.app.getApplicationListener()).setScreen(new OptionMenuScreen(music));
}
});
buttonBack.addListener(new ClickListener(){
@Override
public void clicked(InputEvent event, float x, float y) {
((Game)Gdx.app.getApplicationListener()).setScreen(new MainMenu());
}
});
table.add(title).padBottom(40).row();
table.add(buttonSound).size((int)((double)Gdx.graphics.getWidth() / 2.0D),70).padBottom(20).row();
table.add(buttonBack).size((int)((double)Gdx.graphics.getWidth() / 2.0D), 70).padBottom(20).row();
table.setFillParent(true);
stage.addActor(table);
Gdx.input.setInputProcessor(stage);
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
// NOP
}
@Override
public void resume() {
// NOP
}
@Override
public void dispose() {
stage.dispose();
skin.dispose();
}
}

View File

@ -0,0 +1,15 @@
package de.samdev.colorrunner.screens.menu.impl;
import com.badlogic.gdx.graphics.Color;
import de.samdev.colorrunner.screens.menu.BaseMenu;
import de.samdev.colorrunner.screens.menu.impl.elements.CRMenuRectangle;
public class LevelSelectMenu extends BaseMenu {
@Override
protected void init() {
add(new CRMenuRectangle(1, 1, 30, 30, Color.RED));
}
}

View File

@ -1,4 +1,4 @@
package de.samdev.colorrunner.screens.menu;
package de.samdev.colorrunner.screens.menu.impl;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
@ -34,7 +34,7 @@ public class SplashScreen implements Screen {
loadTime += delta;
if (loadTime > 0.9)
CRGame.Inst().setScreen(new MainMenu());
CRGame.Inst().setScreen(new LevelSelectMenu());
}
@Override

View File

@ -0,0 +1,33 @@
package de.samdev.colorrunner.screens.menu.impl.elements;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import de.samdev.colorrunner.game.renderer.CRAssets;
import de.samdev.colorrunner.screens.menu.BaseMenuElement;
public class CRMenuRectangle extends BaseMenuElement {
public final Rectangle Bounds;
public final Color Color;
public CRMenuRectangle(float x, float y, float w, float h, Color c) {
Bounds = new Rectangle(x, y, w, h);
Color = c;
}
@Override
public void render(SpriteBatch batch) {
batch.draw(CRAssets.TEX_PLAYER_UP, Bounds.x, Bounds.y, Bounds.width, Bounds.height);
}
@Override
public void update(float delta) {
Gdx.app.log("crmenurect", "> " + delta);
}
}