diff --git a/source/core/src/main/com/csse3200/game/GdxGame.java b/source/core/src/main/com/csse3200/game/GdxGame.java index 01966f2b7..4764a5aa0 100644 --- a/source/core/src/main/com/csse3200/game/GdxGame.java +++ b/source/core/src/main/com/csse3200/game/GdxGame.java @@ -80,13 +80,14 @@ private Screen newScreen(ScreenType screenType) { case HELP_BOSS_SCREEN -> new BossDescriptionHelpScreen(this); case HOW_TO_PLAY -> new HowToPlay(this); case LOAD_SCREEN -> new LoadingScreen(this); - default -> null; + case TUTORIAL_SCREEN-> new Tutorial(this); + default-> null; }; } public enum ScreenType { MAIN_MENU, MAIN_GAME, SETTINGS, STORY_SCREEN, LEVEL_SELECT, TURRET_SELECTION, LOSING_SCREEN, HELP_SCREEN, LOAD_SCREEN, - HELP_MOBS_SCREEN, HELP_TOWER_SCREEN, HELP_BOSS_SCREEN, Win_Screen, Next_Screen, HOW_TO_PLAY + HELP_MOBS_SCREEN, HELP_TOWER_SCREEN, HELP_BOSS_SCREEN, Win_Screen, Next_Screen, HOW_TO_PLAY, TUTORIAL_SCREEN } /** diff --git a/source/core/src/main/com/csse3200/game/screens/HelpScreen/GameDescriptionHelpScreen.java b/source/core/src/main/com/csse3200/game/screens/HelpScreen/GameDescriptionHelpScreen.java index a06ef6322..28047dd76 100644 --- a/source/core/src/main/com/csse3200/game/screens/HelpScreen/GameDescriptionHelpScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/HelpScreen/GameDescriptionHelpScreen.java @@ -130,6 +130,17 @@ public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, f } }); + TextButton TutorialButton = new TextButton("Tutorial", skin); + TutorialButton.addListener(new ClickListener() { + @Override + public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, float y) { + game.setScreen(GdxGame.ScreenType.TUTORIAL_SCREEN); + + } + }); + + + Table buttonTable = new Table(); buttonTable.add(BackButton).padRight(10); Table table1 = new Table(); @@ -157,6 +168,15 @@ public void clicked(com.badlogic.gdx.scenes.scene2d.InputEvent event, float x, f table3.add(buttonTable2).row(); // Add button table and move to the next row stage.addActor(table3); + Table buttonTable3 = new Table(); + buttonTable3.add(TutorialButton).padRight(10); + Table table4 = new Table(); + table4.setFillParent(true); + table4.center().top(); // Align to the middle-right corner + table4.pad(20); // Add padding to the middle-right corner + table4.add(buttonTable3).row(); // Add button table and move to the next row + stage.addActor(table4); + } @Override public void show() { diff --git a/source/core/src/main/com/csse3200/game/screens/HelpScreen/Tutorial.java b/source/core/src/main/com/csse3200/game/screens/HelpScreen/Tutorial.java new file mode 100644 index 000000000..ca5ab319e --- /dev/null +++ b/source/core/src/main/com/csse3200/game/screens/HelpScreen/Tutorial.java @@ -0,0 +1,328 @@ +package com.csse3200.game.screens.HelpScreen; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.ScreenAdapter; +import com.badlogic.gdx.audio.Music; +import com.badlogic.gdx.audio.Sound; +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.math.Vector2; +import com.badlogic.gdx.scenes.scene2d.Stage; +import com.badlogic.gdx.utils.Array; +import com.badlogic.gdx.utils.viewport.ScreenViewport; +import com.csse3200.game.GdxGame; +import com.csse3200.game.screens.HelpScreen.TutorialForestGameArea; +import com.csse3200.game.components.gamearea.PerformanceDisplay; +import com.csse3200.game.components.maingame.MainGameActions; +import com.csse3200.game.components.maingame.MainGameLoseDisplay; +import com.csse3200.game.components.maingame.MainGamePauseDisplay; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.EntityService; +import com.csse3200.game.entities.factories.RenderFactory; +import com.csse3200.game.input.*; +import com.csse3200.game.physics.PhysicsEngine; +import com.csse3200.game.physics.PhysicsService; +import com.csse3200.game.rendering.RenderService; +import com.csse3200.game.rendering.Renderer; +import com.csse3200.game.screens.GameLevelData; +import com.csse3200.game.services.*; +import com.csse3200.game.ui.terminal.Terminal; +import com.csse3200.game.ui.terminal.TerminalDisplay; +import com.csse3200.game.components.maingame.MainGameExitDisplay; +import com.csse3200.game.screens.HelpScreen.TutorialOkButton; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The game screen containing the tutorial game. + * + *

Details on libGDX screens: https://happycoding.io/tutorials/libgdx/game-screens + */ +public class Tutorial extends ScreenAdapter { + private static final Logger logger = LoggerFactory.getLogger(Tutorial.class); + private static final String[] mainGameTextures = { + "images/heart.png", + "images/ice_bg.png", + "images/lava_bg.png", + "images/desert_bg.png" + }; + + private static final String ICE_BACKDROP = mainGameTextures[1]; + private static final String LAVA_BACKDROP = mainGameTextures[2]; + private static final String DESERT_BACKDROP = mainGameTextures[3]; + private static final String[] backgroundMusic = { + "sounds/background/ice/ice_bgm.ogg", + "sounds/background/lava/lava_bgm.ogg", + "sounds/background/desert/desert_bgm.ogg" + }; + private static final String[] uiSounds = { + "sounds/ui/Click/NA_SFUI_Vol1_Click_01.ogg", + "sounds/ui/Hover/NA_SFUI_Vol1_hover_01.ogg", + "sounds/ui/Open_Close/NA_SFUI_Vol1_Close_01.ogg", + "sounds/ui/Open_Close/NA_SFUI_Vol1_Open_01.ogg", + "sounds/ui/Switch/NA_SFUI_Vol1_switch_01.ogg" + }; + private static final String[] desertSounds = { + "sounds/background/desert/Elements.ogg", + "sounds/background/desert/Rocks1.ogg", + "sounds/background/desert/Rocks2.ogg" + }; + private static final String[] iceSounds = { + "sounds/background/ice/Sequences1.ogg", + "sounds/background/ice/Sequences2.ogg", + "sounds/background/ice/Sequences3.ogg" + }; + private static final String[] lavaSounds = { + "sounds/background/lava/Burst.ogg", + "sounds/background/lava/Glitch_ripples.ogg", + "sounds/background/lava/Sizzling.ogg", + "sounds/background/lava/Swoosh.ogg" + }; + private static final String ICE_BGM = backgroundMusic[0]; + private static final String LAVA_BGM = backgroundMusic[1]; + private static final String DESERT_BGM = backgroundMusic[2]; + private static final Vector2 CAMERA_POSITION = new Vector2(10f, 5.64f); + + private final GdxGame game; + private final Renderer renderer; + private final PhysicsEngine physicsEngine; + + private InputComponent upgradedInputHandler; + private final Stage stage; + static int screenWidth = Gdx.graphics.getWidth(); + static int screenHeight = Gdx.graphics.getHeight(); + private Entity ui; + private int random = 0; + public static int viewportWidth = screenWidth; + public static int viewportHeight= screenHeight; + int selectedLevel = GameLevelData.getSelectedLevel(); + + private OrthographicCamera camera; + private SpriteBatch batch; + + private Texture backgroundTexture; + private Music music; + private Array ambientSounds = new Array<>(false, 5, String.class); + + public Tutorial(GdxGame game) { + this.game = game; + camera = new OrthographicCamera(); + camera.setToOrtho(false, viewportWidth, viewportHeight); + camera.position.set((float) (viewportWidth) / 2, (float) (viewportHeight) / 2, 0); + + batch = new SpriteBatch(); + + stage = new Stage(new ScreenViewport()); + + + logger.debug("Initialising tutorial game screen services"); + ServiceLocator.registerTimeSource(new GameTime()); + + PhysicsService physicsService = new PhysicsService(); + ServiceLocator.registerPhysicsService(physicsService); + physicsEngine = physicsService.getPhysics(); + + ServiceLocator.registerInputService(new InputService()); + ServiceLocator.registerResourceService(new ResourceService()); + + ServiceLocator.registerCurrencyService(new CurrencyService()); + + ServiceLocator.registerEntityService(new EntityService()); + ServiceLocator.registerRenderService(new RenderService()); + ServiceLocator.registerGameEndService(new GameEndService()); + ServiceLocator.registerWaveService(new WaveService()); + + renderer = RenderFactory.createRenderer(); + renderer.getCamera().getEntity().setPosition(CAMERA_POSITION); + renderer.getDebug().renderPhysicsWorld(physicsEngine.getWorld()); + InputComponent inputHandler = new DropInputComponent(renderer.getCamera().getCamera()); + InputComponent buildHandler = new BuildInputComponent(renderer.getCamera().getCamera()); + upgradedInputHandler = new UpgradeUIComponent(renderer.getCamera().getCamera(), renderer.getStage()); + InputComponent engineerInputHandler = new EngineerInputComponent(game, renderer.getCamera().getCamera()); + ServiceLocator.getInputService().register(inputHandler); + ServiceLocator.getInputService().register(buildHandler); + ServiceLocator.getInputService().register(engineerInputHandler); + ServiceLocator.getInputService().register(upgradedInputHandler); + ServiceLocator.getCurrencyService().getDisplay().setCamera(renderer.getCamera().getCamera()); + + loadAssets(); + createUI(); + ServiceLocator.registerMapService(new MapService(renderer.getCamera())); + logger.debug("Initialising tutorial game screen entities"); + TutorialForestGameArea forestGameArea = new TutorialForestGameArea(); + forestGameArea.create(); + } + + /** + * Retrieves the background texture based on the currently selected game level. + * + *

The method returns different textures for each game level: + *

+ * + * @return The background {@link Texture} corresponding to the selected level. + */ + public Texture getBackgroundTexture() { + Texture background; + switch (selectedLevel) { + // Desert + case 1: // Ice + background = ServiceLocator.getResourceService().getAsset(ICE_BACKDROP, Texture.class); + music = ServiceLocator.getResourceService().getAsset(ICE_BGM, Music.class); + ambientSounds.addAll(iceSounds); + break; + case 2: // Lava + background = ServiceLocator.getResourceService().getAsset(LAVA_BACKDROP, Texture.class); + music = ServiceLocator.getResourceService().getAsset(LAVA_BGM, Music.class); + ambientSounds.addAll(lavaSounds); + break; + default: + // Use a default background for other levels or planets + background = ServiceLocator.getResourceService().getAsset(DESERT_BACKDROP, Texture.class); + music = ServiceLocator.getResourceService().getAsset(DESERT_BGM, Music.class); + ambientSounds.addAll(desertSounds); + break; + } + return background; + } + + @Override + public void render(float delta) { + // Clear the screen + Gdx.gl.glClearColor(0, 0, 0, 1); + Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); + + // Update the camera and set the batch's projection matrix + camera.update(); + batch.setProjectionMatrix(camera.combined); + + // Begin the batch + batch.begin(); + + // Draw the background texture. + batch.draw(backgroundTexture, 0, 0, viewportWidth, viewportHeight); + + // End the batch + batch.end(); + + // Continue with other rendering logic + physicsEngine.update(); + ServiceLocator.getEntityService().update(); + + // Checks if tower selected is dead + this.getUpgradedInputHandler().checkForDispose(); + + // Check if the game has ended + if (ServiceLocator.getGameEndService().hasGameEnded()) { + ui.getEvents().trigger("lose"); + } + + ServiceLocator.getWaveService().getDisplay().updateTimerButton(); + ServiceLocator.getWaveService().getDisplay().updateMobCount(); + renderer.render(); + } + + @Override + public void resize(int width, int height) { + renderer.resize(width, height); + logger.trace("Resized renderer: ({} x {})", width, height); + } + + @Override + public void pause() { + logger.info("Game paused"); + } + + @Override + public void resume() { + logger.info("Game resumed"); + } + + @Override + public void dispose() { + logger.debug("Disposing tutorial game screen"); + + renderer.dispose(); + unloadAssets(); + + ServiceLocator.getEntityService().dispose(); + ServiceLocator.getRenderService().dispose(); + ServiceLocator.getResourceService().dispose(); + + ServiceLocator.clear(); + } + + private void loadAssets() { + logger.debug("Loading assets"); + ResourceService resourceService = ServiceLocator.getResourceService(); + resourceService.loadTextures(mainGameTextures); + ServiceLocator.getResourceService().loadMusic(backgroundMusic); + ServiceLocator.getResourceService().loadSounds(iceSounds); + ServiceLocator.getResourceService().loadSounds(desertSounds); + ServiceLocator.getResourceService().loadSounds(lavaSounds); + ServiceLocator.getResourceService().loadSounds(uiSounds); + ServiceLocator.getResourceService().loadAll(); + backgroundTexture = getBackgroundTexture(); // Load the background image + } + + private void unloadAssets() { + logger.debug("Unloading assets"); + ResourceService resourceService = ServiceLocator.getResourceService(); + resourceService.unloadAssets(mainGameTextures); + ServiceLocator.getResourceService().unloadAssets(backgroundMusic); + ServiceLocator.getResourceService().unloadAssets(iceSounds); + ServiceLocator.getResourceService().unloadAssets(desertSounds); + ServiceLocator.getResourceService().unloadAssets(lavaSounds); + ServiceLocator.getResourceService().unloadAssets(uiSounds); + } + + /** + * Creates the main game's ui including components for rendering ui elements to the screen and + * capturing and handling ui input. + */ + private void createUI() { + logger.debug("Creating ui"); + Stage stage = ServiceLocator.getRenderService().getStage(); + InputComponent inputComponent = + ServiceLocator.getInputService().getInputFactory().createForTerminal(); + + ui = new Entity(); + ui.addComponent(new InputDecorator(stage, 10)) + + .addComponent(new PerformanceDisplay()) + .addComponent(new MainGameActions(this.game)) + .addComponent(ServiceLocator.getWaveService().getDisplay()) + .addComponent(new MainGameExitDisplay()) + .addComponent(new MainGameLoseDisplay()) + .addComponent(new MainGamePauseDisplay(this.game)) + .addComponent(new TutorialOkButton(this.game)) + .addComponent(new Terminal()) + .addComponent(inputComponent) + .addComponent(new TerminalDisplay()); + + + ServiceLocator.getEntityService().register(ui); + + music.setLooping(true); + music.setVolume(0.3f); + music.play(); + playAmbientSound(); + } + + /** + * Plays one of the ambient sounds for the level at random + */ + private void playAmbientSound() { + + ServiceLocator.getResourceService().getAsset(ambientSounds.random(), Sound.class).play(0.2f); + } + + private UpgradeUIComponent getUpgradedInputHandler() { + return (UpgradeUIComponent) upgradedInputHandler; + } +} \ No newline at end of file diff --git a/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialForestGameArea.java b/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialForestGameArea.java new file mode 100644 index 000000000..e52c593a8 --- /dev/null +++ b/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialForestGameArea.java @@ -0,0 +1,270 @@ +package com.csse3200.game.screens.HelpScreen; + +import com.badlogic.gdx.Gdx; +import com.badlogic.gdx.math.GridPoint2; +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.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.ChangeListener; +import com.csse3200.game.GdxGame; +import com.csse3200.game.areas.GameArea; +import com.csse3200.game.areas.terrain.TerrainFactory; +import com.csse3200.game.areas.terrain.TerrainFactory.TerrainType; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.factories.*; +import com.csse3200.game.physics.PhysicsLayer; +import com.badlogic.gdx.audio.Music; +import com.badlogic.gdx.math.GridPoint2; +import com.badlogic.gdx.math.Vector2; + +import com.csse3200.game.areas.terrain.TerrainComponent; +import com.csse3200.game.components.ProjectileEffects; +import com.csse3200.game.areas.terrain.TerrainFactory; +import com.csse3200.game.areas.terrain.TerrainFactory.TerrainType; +import com.csse3200.game.components.ProjectileEffects; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.factories.*; + +import com.csse3200.game.physics.PhysicsLayer; +import com.csse3200.game.screens.AssetLoader; + +import com.csse3200.game.utils.math.RandomUtils; +import com.csse3200.game.services.ResourceService; +import com.csse3200.game.services.ServiceLocator; +import com.csse3200.game.components.gamearea.GameAreaDisplay; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import java.security.SecureRandom; +import java.util.Random; +import java.util.Timer; + +import static com.csse3200.game.entities.factories.NPCFactory.createGhost; +import static com.csse3200.game.screens.AssetLoader.loadAllAssets; +import static com.csse3200.game.screens.AssetLoader.unloadAllAssets; + +import java.util.ArrayList; + +import java.util.TimerTask; + +/** Forest area for the demo game with trees, a player, and some enemies. */ +public class TutorialForestGameArea extends GameArea { + private static final Logger logger = LoggerFactory.getLogger(com.csse3200.game.screens.HelpScreen.TutorialForestGameArea.class); + private Timer waveTimer; + + private static final int NUM_WEAPON_TOWERS = 3; + private static final GridPoint2 PLAYER_SPAWN = new GridPoint2(2, 4); + // Temporary spawn point for testing + private static final float WALL_WIDTH = 0.1f; + + // Required to load assets before using them + private static final String backgroundMusic = "sounds/background/Sci-Fi1.ogg"; + private GdxGame game; + private Entity waves; + private Table table; + private Stage stage; + Skin skin = new Skin(Gdx.files.internal("images/ui/buttons/glass.json")); + + public TutorialForestGameArea() { + super(); + } + + + /** + * Add this method to start the wave spawning timer when the game starts. + */ +// private void startWaveTimer() { +// waveTimer = new Timer(); +// waveTimer.scheduleAtFixedRate(new TimerTask() { +// @Override +// public void run() { +// spawnWave(); +// } +// }, 0, 10000); // 10000 milliseconds = 10 seconds +// } + + /** + * Add this method to stop the wave timer when the game ends or as needed. + */ + private void stopWaveTimer() { + if (waveTimer != null) { + waveTimer.cancel(); + waveTimer = null; + } + } + + /** + * Cases to spawn a wave + */ +// private void spawnWave() { +// wave++; +// switch (wave) { +// case 1: +// case 2: +// spawnFireWorm(); +// spawnDragonKnight(); +// +// break; +// case 3: +// spawnSkeleton(); +// spawnWizard(); +// // mobBoss2 = spawnMobBoss2(); +// break; +// case 4: +// spawnWaterQueen(); +// spawnWaterSlime(); +// // mobBoss2 = spawnMobBoss2(); +// +// break; +// case 5: +// spawnDemonBoss(); +// default: +// // Handle other wave scenarios if needed +// break; +// } +// } + + /** + * Create the game area, including terrain, static entities (trees), dynamic entities (player) + */ + @Override + public void create() { + // Load game assets + loadAllAssets(); + loadAssets(); + logger.info("selected towers in main game are " + ServiceLocator.getTowerTypes()); + displayUI(); + spawnTerrain(); + + // Set up infrastructure for end game tracking +// player = spawnPlayer(); + + waves = WaveFactory.createWaves(); + spawnEntity(waves); + waves.getEvents().addListener("spawnWave", this::spawnMob); + + spawnScrap(); + spawnGapScanners(); + +// spawnTNTTower(); +// spawnWeaponTower(); +// spawnGapScanners(); +// spawnDroidTower(); +// spawnFireWorksTower(); // Commented these out until they are needed for Demonstration +// spawnPierceTower(); +// spawnRicochetTower(); +// spawnBombship(); + + } + + private void displayUI() { + Entity ui = new Entity(); + ui.addComponent(new GameAreaDisplay("Tutorial")); + ui.addComponent(ServiceLocator.getGameEndService().getDisplay()); + ui.addComponent(ServiceLocator.getCurrencyService().getDisplay()); + spawnEntity(ui); + } + + private void spawnTerrain() { + + terrain = ServiceLocator.getMapService().getComponent(); + spawnEntity(ServiceLocator.getMapService().getEntity()); + + // Terrain walls + float tileSize = terrain.getTileSize(); + GridPoint2 tileBounds = terrain.getMapBounds(0); + Vector2 worldBounds = new Vector2(tileBounds.x * tileSize, tileBounds.y * tileSize); + + // Left + spawnEntityAt( + ObstacleFactory.createWall(WALL_WIDTH, worldBounds.y), + new GridPoint2(0, 0), + false, + false); + // Right + spawnEntityAt( + ObstacleFactory.createWall(WALL_WIDTH, worldBounds.y), + new GridPoint2(tileBounds.x, 0), + false, + false); + // Top + spawnEntityAt( + ObstacleFactory.createWall(worldBounds.x, WALL_WIDTH * 0), + new GridPoint2(0, tileBounds.y), + false, + false); + // Bottom + Entity wall = ObstacleFactory.createWall(worldBounds.x, WALL_WIDTH * 0); + wall.setPosition(0,-0.1f); + ServiceLocator.getEntityService().register(wall); + + } + + /** + * Spawn an entity on the map. Is called during a wave. Add cases here for each mob type + * @param entity mob to be spawned + * @param randomPos position to be spawned at + * @param health health of the mob + */ + public void spawnMob(String entity, GridPoint2 randomPos, int health) { + Entity mob; + mob = NPCFactory.createWaterQueen(health); + mob.setScale(1.5f, 1.5f); + spawnEntityAt(mob, randomPos, true, false); + } + + + private void loadAssets() { + logger.debug("Loading assets"); + ResourceService resourceService = ServiceLocator.getResourceService(); + while (!resourceService.loadForMillis(10)) { + // This could be upgraded to a loading screen + logger.info("Loading... {}%", resourceService.getProgress()); + } + } + + private void unloadAssets() { + logger.debug("Unloading assets"); + unloadAllAssets(); + + } + + @Override + public void dispose() { + super.dispose(); + this.unloadAssets(); + stopWaveTimer(); + } + + private void spawnScrap() { + GridPoint2 minPos = new GridPoint2(0, 0); + GridPoint2 maxPos = terrain.getMapBounds(0).sub(2, 2); + + for (int i = 0; i < 5; i++) { + GridPoint2 randomPos = RandomUtils.random(minPos, maxPos); + Entity scrap = DropFactory.createScrapDrop(); + spawnEntityAt(scrap, randomPos, true, false); + } + + for (int i = 0; i < 5; i++) { + GridPoint2 randomPos = RandomUtils.random(minPos, maxPos); + Entity crystal = DropFactory.createCrystalDrop(); + spawnEntityAt(crystal, randomPos, true, false); + } + } + + /** + * Creates the scanners (one per lane) that detect absence of towers and presence of mobs, + * and trigger engineer spawning + */ + private void spawnGapScanners() { + for (int i = 0; i < terrain.getMapBounds(0).y; i++) { + Entity scanner = GapScannerFactory.createScanner(); + spawnEntityAt(scanner, new GridPoint2(0, i), true, true); + } + } + + +} diff --git a/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialOkButton.java b/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialOkButton.java new file mode 100644 index 000000000..918d977d5 --- /dev/null +++ b/source/core/src/main/com/csse3200/game/screens/HelpScreen/TutorialOkButton.java @@ -0,0 +1,88 @@ +package com.csse3200.game.screens.HelpScreen; + +import com.badlogic.gdx.audio.Sound; +import com.badlogic.gdx.graphics.g2d.SpriteBatch; +import com.badlogic.gdx.scenes.scene2d.Actor; +import com.badlogic.gdx.scenes.scene2d.ui.Table; +import com.badlogic.gdx.scenes.scene2d.ui.TextButton; +import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; +import com.csse3200.game.GdxGame; +import com.csse3200.game.entities.Entity; +import com.csse3200.game.entities.factories.PauseMenuFactory; +import com.csse3200.game.services.ServiceLocator; +import com.csse3200.game.ui.UIComponent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Displays a button to pause the game and bring up a pause menu. + */ +public class TutorialOkButton extends UIComponent { + private static final Logger logger = LoggerFactory.getLogger(TutorialOkButton.class); + private static final float Z_INDEX = 2f; + private Table table; + private GdxGame game; + private final String[] sounds = { + "sounds/ui/Open_Close/NA_SFUI_Vol1_Open_01.ogg" + }; + private Sound openSound; + + + public TutorialOkButton(GdxGame screenSwitchHandle) { + game = screenSwitchHandle; + } + + @Override + public void create() { + super.create(); + addActors(); + loadSounds(); + } + + private void addActors() { + table = new Table(); + table.top().right(); + table.setFillParent(true); + + TextButton mainMenuBtn = new TextButton("ok", skin); + + // Spawns a pause menu when the button is pressed. + mainMenuBtn.addListener( + new ChangeListener() { + @Override + public void changed(ChangeEvent changeEvent, Actor actor) { + logger.debug("ok button clicked"); + openSound.play(0.4f); +// PauseMenuFactory.createPauseMenu(game, false); + PauseMenuFactory.createPauseMenu(game); + + } + }); + + table.add(mainMenuBtn).padLeft(100f); + + stage.addActor(table); + } + + @Override + public void draw(SpriteBatch batch) { + // draw is handled by the stage + } + + @Override + public float getZIndex() { + return Z_INDEX; + } + + @Override + public void dispose() { + table.clear(); + super.dispose(); + } + + public void loadSounds() { + ServiceLocator.getResourceService().loadSounds(sounds); + ServiceLocator.getResourceService().loadAll(); + openSound = ServiceLocator.getResourceService().getAsset(sounds[0], Sound.class); + } +}