Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.cometmebro.game; import com.badlogic.gdx.Game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Preferences; import com.badlogic.gdx.Screen; import com.badlogic.gdx.graphics.Color; import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.Sprite; import com.badlogic.gdx.graphics.g2d.TextureAtlas; import com.badlogic.gdx.math.Vector2; import com.badlogic.gdx.scenes.scene2d.Actor; import com.badlogic.gdx.scenes.scene2d.Stage; import com.badlogic.gdx.scenes.scene2d.actions.Actions; import com.badlogic.gdx.scenes.scene2d.ui.*; import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable; import com.badlogic.gdx.utils.Align; import com.badlogic.gdx.utils.viewport.StretchViewport; import java.util.Random; /** * * @author Dan */ class PlanetMenu implements Screen { private CMB cmb; private Stage stage = new Stage(new StretchViewport(CMB.WIDTH, CMB.HEIGHT)); private Table table = new Table(); float deltatime = 0; Button sand, ice, candy, spring, winter, lava, ocean, grass, city; Button planet1, planet2, planet3, play_button; Texture space, buttonUp, buttonDown, backButtonTex, settings, planetHudTex; int STYLE = 3; int PLAY_CLICKED = 0; double rotation = 0, rotation2 = 50, rotation3 = 80; Table table1 = new Table(); Table table2 = new Table(); Table table3 = new Table(); Table subtable = new Table(); Table crystaltable = new Table(); TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("planetselections.txt")); TextureAtlas atlas2 = new TextureAtlas(Gdx.files.internal("crystals.txt")); Skin skin = new Skin(Gdx.files.internal("planetmenu.json"), atlas); float currentDangerCount, currentDurationCount, currentResourcesCount; String planetName, blank, one, two, three, currentDanger, currentDuration, currentResources; String sandPlanet, springPlanet, simplePlanet, holidayPlanet, waterPlanet, icePlanet, lavaPlanet, candyPlanet, cityPlanet; Label planet_name, dangerDots, durationDots, resourcesDots, pickPlanetLbl, crystalCountlbl; Preferences prefs = Gdx.app.getPreferences("CMBSettings"); public PlanetMenu(CMB game) { this.cmb = game; } @Override public void show() { skin.addRegions(atlas2); blank = " "; one = "o "; two = "oo "; three = "ooo"; currentDangerCount = 1; currentDurationCount = 1; currentResourcesCount = 1; currentDanger = blank; currentDuration = blank; currentResources = blank; sandPlanet = "Sand Planet"; springPlanet = "Spring Planet"; simplePlanet = "Grass Planet"; holidayPlanet = "Winter Wonderland"; waterPlanet = "Ocean Planet"; icePlanet = "Ice Planet"; lavaPlanet = "Lava Planet"; candyPlanet = "Candy Planet"; cityPlanet = "Suburban Planet"; backButtonTex = new Texture(Gdx.files.internal("backbutton.png")); Button backButton = new Button(new SpriteDrawable(new Sprite(backButtonTex))); backButton.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { stage.addAction( Actions.sequence(Actions.fadeOut(.25f), Actions.delay(.25f), Actions.run(new Runnable() { @Override public void run() { MainMenu menu = new MainMenu(cmb); cmb.setScreen(menu); } }))); } }); settings = new Texture(Gdx.files.internal("settingsbutton.png")); Button settingsButton = new Button(new SpriteDrawable(new Sprite(settings))); planetHudTex = new Texture(Gdx.files.internal("planethud.png")); SpriteDrawable planethud = new SpriteDrawable(new Sprite(planetHudTex)); settingsButton.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { stage.addAction( Actions.sequence(Actions.fadeOut(.25f), Actions.delay(.25f), Actions.run(new Runnable() { @Override public void run() { SettingsMenu menu = new SettingsMenu(cmb); cmb.setScreen(menu); } }))); } }); buttonUp = new Texture(Gdx.files.internal("roundplaybuttonalt.png")); buttonDown = new Texture(Gdx.files.internal("roundplaybuttonaltdown.png")); Button.ButtonStyle buttonStyle2 = new Button.ButtonStyle(); buttonStyle2.up = new SpriteDrawable(new Sprite(buttonUp)).tint(Color.GREEN); buttonStyle2.down = new SpriteDrawable(new Sprite(buttonDown)).tint(Color.GREEN); play_button = new Button(buttonStyle2); play_button.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { PLAY_CLICKED++; if (!prefs.getString("type", "").equals("")) { stage.addAction(Actions.sequence(Actions.fadeOut(.25f), Actions.delay(.25f), Actions.run(new Runnable() { @Override public void run() { CometMeBro menu = new CometMeBro(cmb, currentDurationCount * 20, 3 / currentDangerCount, currentResourcesCount * 10, prefs.getString("type", "sand")); cmb.setScreen(menu); } }))); } } }); Button.ButtonStyle style1 = new Button.ButtonStyle(); style1.up = skin.getDrawable("desert"); style1.down = skin.getDrawable("desert"); sand = new Button(style1); Button.ButtonStyle style2 = new Button.ButtonStyle(); style2.up = skin.getDrawable("city"); style2.down = skin.getDrawable("city"); city = new Button(style2); Button.ButtonStyle style3 = new Button.ButtonStyle(); style3.up = skin.getDrawable("ice"); style3.down = skin.getDrawable("ice"); ice = new Button(style3); Button.ButtonStyle style4 = new Button.ButtonStyle(); style4.up = skin.getDrawable("grassland"); style4.down = skin.getDrawable("grassland"); grass = new Button(style4); Button.ButtonStyle style5 = new Button.ButtonStyle(); style5.up = skin.getDrawable("ocean"); style5.down = skin.getDrawable("ocean"); ocean = new Button(style5); Button.ButtonStyle style6 = new Button.ButtonStyle(); style6.up = skin.getDrawable("lava"); style6.down = skin.getDrawable("lava"); lava = new Button(style6); Button.ButtonStyle style7 = new Button.ButtonStyle(); style7.up = skin.getDrawable("candy"); style7.down = skin.getDrawable("candy"); candy = new Button(style7); Button.ButtonStyle style8 = new Button.ButtonStyle(); style8.up = skin.getDrawable("easter"); style8.down = skin.getDrawable("easter"); spring = new Button(style8); Button.ButtonStyle style9 = new Button.ButtonStyle(); style9.up = skin.getDrawable("winter"); style9.down = skin.getDrawable("winter"); winter = new Button(style9); sand.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "sand"); prefs.putString("title", sandPlanet); prefs.flush(); currentDanger = one; currentDuration = two; currentResources = one; currentDangerCount = 1; currentDurationCount = 2; currentResourcesCount = 1; } }); ice.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "ice"); prefs.putString("title", icePlanet); prefs.flush(); currentDanger = two; currentDuration = two; currentResources = two; currentDangerCount = 2; currentDurationCount = 2; currentResourcesCount = 2; } }); candy.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "candy"); prefs.putString("title", candyPlanet); prefs.flush(); currentDanger = one; currentDuration = one; currentResources = one; currentDangerCount = 1; currentDurationCount = 1; currentResourcesCount = 1; } }); spring.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "spring"); prefs.putString("title", springPlanet); prefs.flush(); currentDanger = one; currentDuration = two; currentResources = two; currentDangerCount = 1; currentDurationCount = 2; currentResourcesCount = 2; } }); winter.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "christmas"); prefs.putString("title", holidayPlanet); prefs.flush(); currentDanger = two; currentDuration = one; currentResources = three; currentDangerCount = 2; currentDurationCount = 1; currentResourcesCount = 3; } }); lava.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "lava"); prefs.putString("title", lavaPlanet); prefs.flush(); currentDanger = three; currentDuration = two; currentResources = three; currentDangerCount = 3; currentDurationCount = 2; currentResourcesCount = 3; } }); ocean.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "water"); prefs.putString("title", waterPlanet); prefs.flush(); currentDanger = two; currentDuration = three; currentResources = two; currentDangerCount = 2; currentDurationCount = 3; currentResourcesCount = 2; } }); grass.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "simple"); prefs.putString("title", simplePlanet); prefs.flush(); currentDanger = one; currentDuration = two; currentResources = one; currentDangerCount = 1; currentDurationCount = 2; currentResourcesCount = 1; } }); city.addListener(new ChangeListener() { @Override public void changed(ChangeEvent event, Actor actor) { prefs.putString("type", "city"); prefs.putString("title", cityPlanet); prefs.flush(); currentDanger = three; currentDuration = three; currentResources = two; currentDangerCount = 3; currentDurationCount = 3; currentResourcesCount = 2; } }); Random rand = new Random(); int planetRandom = rand.nextInt(8); switch (planetRandom) { case 0: planet1 = sand; planet2 = ice; planet3 = candy; break; case 1: planet1 = ocean; planet2 = ice; planet3 = spring; break; case 2: planet1 = lava; planet2 = city; planet3 = sand; break; case 3: planet1 = grass; planet2 = ocean; planet3 = spring; break; case 4: planet1 = ice; planet2 = lava; planet3 = candy; break; case 5: planet1 = sand; planet2 = spring; planet3 = ocean; break; case 6: planet1 = ice; planet2 = grass; planet3 = spring; break; case 7: planet1 = candy; planet2 = spring; planet3 = sand; break; } if (STYLE == 3) { table1.setTransform(true); table1.add(planet1).width(Value.percentHeight(.25f, table)).height(Value.percentHeight(.25f, table)) .center().pad(10); table1.setX((stage.getWidth() / 4) * 3 - table1.getHeight() / 2); table1.setY(stage.getHeight() / 2 - table1.getHeight() / 2); table.addActor(table1); table2 = new Table(); table2.setTransform(true); table2.add(planet2).width(Value.percentHeight(.15f, table)).height(Value.percentHeight(.15f, table)) .center().pad(10); table2.setX((stage.getWidth() / 4) * 2 - table2.getWidth() / 2); table2.setY(stage.getHeight() / 2 - table2.getHeight() / 2); table.addActor(table2); table3 = new Table(); table3.setTransform(true); table3.add(planet3).width(Value.percentHeight(.2f, table)).height(Value.percentHeight(.2f, table)) .center().pad(10); table3.setX(stage.getWidth() / 4 - table3.getWidth() / 2); table3.setY(stage.getHeight() / 2 - table3.getHeight() / 2); table.addActor(table3); } space = new Texture("space16x9fourth.png"); SpriteDrawable spaceDrawable = new SpriteDrawable(new Sprite(space)); table.setBackground(spaceDrawable); table.setFillParent(true); Label resourceslbl = new Label("Resources: ", skin.get("label", Label.LabelStyle.class)); resourcesDots = new Label(three, skin.get("label", Label.LabelStyle.class)); Label dangerlbl = new Label("Danger: ", skin.get("label", Label.LabelStyle.class)); dangerDots = new Label(two, skin.get("label", Label.LabelStyle.class)); Label durationlbl = new Label("Duration: ", skin.get("label", Label.LabelStyle.class)); durationDots = new Label(three, skin.get("label", Label.LabelStyle.class)); pickPlanetLbl = new Label("", skin.get("label", Label.LabelStyle.class)); crystalCountlbl = new Label(String.valueOf(prefs.getInteger("crystalCount", 0)), skin.get("labelblue", Label.LabelStyle.class)); resourceslbl.setFontScale(.5f); dangerlbl.setFontScale(.5f); durationlbl.setFontScale(.5f); resourcesDots.setFontScale(.5f); dangerDots.setFontScale(.5f); durationDots.setFontScale(.5f); subtable.setBackground(planethud); crystaltable.setBackground(skin.getDrawable("bluecrystalhud")); subtable.row().colspan(2); subtable.add(dangerlbl).left().colspan(1).padBottom(-15).align(Align.right); //.padRight(Value.percentWidth(3.75f)) subtable.add(dangerDots).left().colspan(1).padBottom(-15).align(Align.left) .padRight(subtable.getWidth() / 2); subtable.row(); subtable.add(durationlbl).left().colspan(1).align(Align.right); //.padRight(Value.percentWidth(2.91f)); subtable.add(durationDots).left().colspan(1).align(Align.left).padRight(subtable.getWidth() / 2); subtable.add(play_button).right().width(Value.percentHeight(.1f, table)) .height(Value.percentHeight(.1f, table)); subtable.row(); subtable.add(resourceslbl).left().colspan(1).padTop(-15).align(Align.right); //.padRight(Value.percentWidth(2.6f)); subtable.add(resourcesDots).left().colspan(1).padTop(-15).align(Align.left) .padRight(subtable.getWidth() / 2); subtable.setX(0); subtable.setY(0); subtable.setWidth(CMB.WIDTH); subtable.setHeight(CMB.HEIGHT / 4.5f); prefs.putString("type", ""); prefs.putString("title", ""); prefs.flush(); planetName = prefs.getString("title", ""); planet_name = new Label(planetName, skin.get("label", Label.LabelStyle.class)); table.row(); table.add(backButton).left().top().width(Value.percentWidth(1f)).height(Value.percentHeight(1f)) .padBottom(20).padTop(70).padRight(1050); table.add(settingsButton).right().top().width(Value.percentWidth(1f)).height(Value.percentHeight(1f)) .padTop(70); table.row(); table.add(pickPlanetLbl).center().top().padBottom(240); table.row(); table.add(crystaltable).left().bottom().width(Value.percentHeight(.24f, table)) .height(Value.percentHeight(.187f, table)).padBottom(200).padLeft(-20); table.add(planet_name).center().bottom().padLeft(Value.percentWidth(-.9f, table)).padBottom(200); table.row(); table.addActor(subtable); crystalCountlbl.setFontScale(.4f); crystaltable.row().colspan(1); crystaltable.add(crystalCountlbl).colspan(1).bottom().center().align(Align.center).padTop(70).padRight(10); stage.addActor(table); stage.addAction(Actions.sequence(Actions.fadeIn(1f))); Gdx.input.setInputProcessor(stage); } @Override public void render(float delta) { planet_name.setText(prefs.getString("title", "")); if (PLAY_CLICKED >= 3 && prefs.getString("title").isEmpty()) { pickPlanetLbl.setText("Pick a planet, bro."); } else if (PLAY_CLICKED >= 3 && !prefs.getString("title").isEmpty()) { pickPlanetLbl.setText("Almost there, bro."); } else { pickPlanetLbl.setText(""); } crystalCountlbl.setText(String.valueOf(prefs.getInteger("crystalCount", 0))); dangerDots.setText(currentDanger); durationDots.setText(currentDuration); resourcesDots.setText(currentResources); deltatime += delta; Gdx.gl.glClearColor(0, 0, 0, 1); Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); stage.act(); if (STYLE == 1) { rotation += delta * 30; rotation2 += delta * 15; } else if (STYLE == 2) { rotation += delta * 15; rotation2 += delta * 15; } else if (STYLE == 3) { rotation += delta * 10; rotation2 -= delta * 38; rotation3 += delta * 18; } table1.setRotation(((float) rotation)); if (deltatime % 5 >= 4.75) { planet1.setWidth(planet1.getWidth() * .995f); planet1.setHeight(planet1.getHeight() * .995f); planet1.setOrigin(planet1.getWidth() / 2, planet1.getHeight() / 2); planet1.setX(table1.getWidth() / 2 - planet1.getOriginX()); planet1.setY(table1.getHeight() / 2 - planet1.getOriginY()); table1.setOrigin(table1.getWidth() / 2, table1.getHeight() / 2); table1.setX((stage.getWidth() / 4 * 3) - table1.getOriginX()); table1.setY(stage.getHeight() / 2 - table1.getOriginY()); } else if (deltatime % 5 >= 4.5) { planet1.setWidth(planet1.getWidth() * 1.005f); planet1.setHeight(planet1.getHeight() * 1.005f); planet1.setOrigin(planet1.getWidth() / 2, planet1.getHeight() / 2); planet1.setX(table1.getWidth() / 2 - planet1.getOriginX()); planet1.setY(table1.getHeight() / 2 - planet1.getOriginY()); table1.setOrigin(table1.getWidth() / 2, table1.getHeight() / 2); table1.setX((stage.getWidth() / 4 * 3) - table1.getOriginX()); table1.setY(stage.getHeight() / 2 - table1.getOriginY()); } table2.setRotation(((float) rotation2)); if (deltatime % 5 >= 3.75 && deltatime % 5 < 4) { planet2.setWidth(planet2.getWidth() * .995f); planet2.setHeight(planet2.getHeight() * .995f); planet2.setOrigin(planet2.getWidth() / 2, planet2.getHeight() / 2); planet2.setX(table2.getWidth() / 2 - planet2.getOriginX()); planet2.setY(table2.getHeight() / 2 - planet2.getOriginY()); table2.setOrigin(table2.getWidth() / 2, table2.getHeight() / 2); table2.setX((stage.getWidth() / 4 * 2) - table2.getOriginX()); table2.setY(stage.getHeight() / 2 - table2.getOriginY()); } else if (deltatime % 5 >= 3.5 && deltatime % 5 < 3.75) { planet2.setWidth(planet2.getWidth() * 1.005f); planet2.setHeight(planet2.getHeight() * 1.005f); planet2.setOrigin(planet2.getWidth() / 2, planet2.getHeight() / 2); planet2.setX(table2.getWidth() / 2 - planet2.getOriginX()); planet2.setY(table2.getHeight() / 2 - planet2.getOriginY()); table2.setOrigin(table2.getWidth() / 2, table2.getHeight() / 2); table2.setX((stage.getWidth() / 4 * 2) - table2.getOriginX()); table2.setY(stage.getHeight() / 2 - table2.getOriginY()); } table3.setRotation(((float) rotation3)); if (deltatime % 5 >= 2.75 && deltatime % 5 < 3) { planet3.setWidth(planet3.getWidth() * .995f); planet3.setHeight(planet3.getHeight() * .995f); planet3.setOrigin(planet3.getWidth() / 2, planet3.getHeight() / 2); planet3.setX(table3.getWidth() / 2 - planet3.getOriginX()); planet3.setY(table3.getHeight() / 2 - planet3.getOriginY()); table3.setOrigin(table3.getWidth() / 2, table3.getHeight() / 2); table3.setX(stage.getWidth() / 4 - table3.getOriginX()); table3.setY(stage.getHeight() / 2 - table3.getOriginY()); } else if (deltatime % 5 >= 2.5 && deltatime % 5 < 2.75) { planet3.setWidth(planet3.getWidth() * 1.005f); planet3.setHeight(planet3.getHeight() * 1.005f); planet3.setOrigin(planet3.getWidth() / 2, planet3.getHeight() / 2); planet3.setX(table3.getWidth() / 2 - planet3.getOriginX()); planet3.setY(table3.getHeight() / 2 - planet3.getOriginY()); table3.setOrigin(table3.getWidth() / 2, table3.getHeight() / 2); table3.setX(stage.getWidth() / 4 - table3.getOriginX()); table3.setY(stage.getHeight() / 2 - table3.getOriginY()); } 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 hide() { dispose(); } @Override public void dispose() { atlas.dispose(); space.dispose(); skin.dispose(); stage.dispose(); space.dispose(); buttonUp.dispose(); buttonDown.dispose(); backButtonTex.dispose(); settings.dispose(); planetHudTex.dispose(); } }