Main Menu Button_code - UQdeco2800/2021-ext-studio-2 GitHub Wiki
Description
the making of a button on the main menu, its listeners, event triggers, initialization and everything that makes a main menu accessible.
code snipets
- listeners:
the listeners for events that trigger specific function which in turn cause a speicif case to occus and the appropriate screen to be set accordingly.
@Override
public void create() {
entity.getEvents().addListener("start", this::onStart);
entity.getEvents().addListener("load", this::onLoad);
entity.getEvents().addListener("exit", this::onExit);
entity.getEvents().addListener("settings", this::onSettings);
entity.getEvents().addListener("gameOver", this::onGameOver);
//Team History Score board
entity.getEvents().addListener("displayPropsShop", this::onDisplayPropsShop);
entity.getEvents().addListener("displayHistoryScores", this::onDisplayHistoryScores);
entity.getEvents().addListener("achievements", this::onAchievements);
entity.getEvents().addListener("unlockedAttires", this::onUnlockedAttires);
entity.getEvents().addListener("monsterMenu", this::onDisplayMonsterMenu);
entity.getEvents().addListener("buffMenu", this::onDisplayBuffManualMenu);
entity.getEvents().addListener("GameTutorial", this::onDisplayInstructions);
}
}
/**
* Swaps to the Main Game screen.
*/
private void onStart() {
logger.info("Start game");
game.setScreen(GdxGame.ScreenType.MAIN_GAME);
}
/**
* Intended for loading a saved game state.
* Load functionality is not actually implemented.
*/
private void onLoad() {
logger.info("Load game");
}
/**
* Exits the game.
*/
private void onExit() {
logger.info("Exit game");
game.exit();
}
/**
* Swaps to the Settings screen.
*/
private void onSettings() {
logger.info("Launching settings screen");
game.setScreen(GdxGame.ScreenType.SETTINGS);
}
/**
* Swaps to the GameOver screen.
*/
private void onGameOver() {
logger.info("Launching GameOver screen");
game.setScreen(GdxGame.ScreenType.GAME_OVER);
}
private void onDisplayPropsShop() {
logger.info("Open the propsShop");
game.setScreen(GdxGame.ScreenType.PROPS_SHOP);
}
private void onDisplayHistoryScores() {
logger.info("Open the history scores board");
game.setScreen(GdxGame.ScreenType.HISTORY_SCORES);
}
private void onAchievements(){
logger.info("Launching Achievements screen");
game.setScreen(GdxGame.ScreenType.ACHIEVEMENTS);
}
private void onUnlockedAttires(){
logger.info("Launching Unlocked Attires screen");
game.setScreen(GdxGame.ScreenType.UNLOCKED_ATTIRES);
}
private void onDisplayMonsterMenu() {
//logger.info("Open the history scores board");
game.setScreen(GdxGame.ScreenType.MONSTER_MENU);
}
private void onDisplayBuffManualMenu() {
game.setScreen(GdxGame.ScreenType.BUFF_MENU);
}
private void onDisplayInstructions() {
System.out.println("check 1");
game.setScreen(GdxGame.ScreenType.INSTRUCTIONS);
}
- initializing a button :
making the button, giving it texture, adding it to the table to put it on the main menu list or in a specific position depending on the button
/** build new start button */
Button.ButtonStyle start = new Button.ButtonStyle();
start.up= new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/start1.png"))));
start.over= new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/start2.png"))));
Button startBtn = new Button(start);
/** build select unlocked attire button */
Button.ButtonStyle attires = new Button.ButtonStyle();
attires.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/select1.png"))));
attires.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/select2.png"))));
Button attiresBtn = new Button(attires);
/** build new setting button */
Button.ButtonStyle setting = new Button.ButtonStyle();
setting.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/setting1.png"))));
setting.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/setting2.png"))));
Button settingBtn = new Button(setting);
/** build new exit button */
Button.ButtonStyle exit = new Button.ButtonStyle();
exit.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/exit1.png"))));
exit.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/exit2.png"))));
Button exitBtn = new Button(exit);
/** build new prop shop button */
Button.ButtonStyle propShop = new Button.ButtonStyle();
propShop.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/shop1.png"))));
propShop.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/shop2.png"))));
Button shopBtn = new Button(propShop);
shopBtn.setPosition(820,650);
/** build new score button */
Button.ButtonStyle score = new Button.ButtonStyle();
score.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/score1.png"))));
score.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/score2.png"))));
Button scoreBtn = new Button(score);
scoreBtn.setPosition(960,685);
/** build new achievement button */
Button.ButtonStyle achievement = new Button.ButtonStyle();
achievement.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/achievement1.png"))));
achievement.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/achievement2.png"))));
Button achievementBtn = new Button(achievement);
achievementBtn.setPosition(1100,682);
/** build new monster button */
Button.ButtonStyle monster = new Button.ButtonStyle();
monster.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/monster1.png"))));
monster.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/monster2.png"))));
Button monsterBtn = new Button(monster);
monsterBtn.setPosition(1120,40);
Button.ButtonStyle buff = new Button.ButtonStyle();
buff.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/buff1.png"))));
buff.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/buff2.png"))));
Button buffBtn = new Button(buff);
buffBtn.setPosition(1120,170);
Button.ButtonStyle tutorial = new Button.ButtonStyle();
tutorial.up = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/tutorial1.png"))));
tutorial.over = new TextureRegionDrawable(new TextureRegion(
new Texture(Gdx.files.internal("button/tutorial2.png"))));
Button tutorialBtn = new Button(tutorial);
- assigning screens to each case:
depending on what button has been clicked an what event has occurred there will be an associated screen that we're taken to depending on the case that event and hence that event was associated to
private void switchScreen(GdxGame game, ScreenType screenType) {
game.setScreen(screenType);
}
public enum ScreenType {
MAIN_MENU, MAIN_GAME, SETTINGS, GAME_OVER, PROPS_SHOP, HISTORY_SCORES,
ACHIEVEMENTS,MONSTER_MENU, UNLOCKED_ATTIRES,BUFF_MENU, INSTRUCTIONS
}
@Override
public void create() {
logger.info("Creating game");
loadSettings();
// Sets background to light yellow
Gdx.gl.glClearColor(248f / 255f, 249 / 255f, 178 / 255f, 1);
setScreen(ScreenType.MAIN_MENU);
}
/**
* Loads the game's settings.
*/
private void loadSettings() {
logger.debug("Loading game settings");
UserSettings.Settings settings = UserSettings.get();
UserSettings.applySettings(settings);
}
/**
* Sets the game's screen to a new screen of the provided type.
*
* @param screenType screen type
*/
public void setScreen(ScreenType screenType) {
logger.info("Setting game screen to {}", screenType);
Screen currentScreen = getScreen();
if (currentScreen != null) {
currentScreen.dispose();
}
setScreen(newScreen(screenType));
}
@Override
public void dispose() {
logger.debug("Disposing of current screen");
getScreen().dispose();
}
/**
* Create a new screen of the provided type.
*
* @param screenType screen type
* @return new screen
*/
private Screen newScreen(ScreenType screenType) {
switch (screenType) {
case MAIN_MENU:
return new MainMenuScreen(this);
case MAIN_GAME:
return new MainGameScreen(this);
case SETTINGS:
return new SettingsScreen(this);
case GAME_OVER:
return new GameOverScreen(this);
case PROPS_SHOP:
return new PropsShopScreen(this);
case HISTORY_SCORES:
return new HistoryScoreScreen(this);
case ACHIEVEMENTS:
return new AchievementsScreen(this);
case UNLOCKED_ATTIRES:
return new UnlockedAttiresScreen(this);
case MONSTER_MENU:
return new MonsterMenuScreen(this);
case BUFF_MENU:
return new BuffManualMenuScreen(this);
case INSTRUCTIONS:
return new InstructionsScreen(this);
default:
return null;
}
}
/**
* Exit the game.
*/
public void exit() {
app.exit();
}
}