diff --git a/source/core/assets/images/background/main_menu/main_menu_bg.png b/source/core/assets/images/background/main_menu/main_menu_bg.png index e60414ece..05281bdf5 100644 Binary files a/source/core/assets/images/background/main_menu/main_menu_bg.png and b/source/core/assets/images/background/main_menu/main_menu_bg.png differ diff --git a/source/core/src/main/com/csse3200/game/components/mainmenu/MainMenuDisplay.java b/source/core/src/main/com/csse3200/game/components/mainmenu/MainMenuDisplay.java index 559843d32..3bd2deea3 100644 --- a/source/core/src/main/com/csse3200/game/components/mainmenu/MainMenuDisplay.java +++ b/source/core/src/main/com/csse3200/game/components/mainmenu/MainMenuDisplay.java @@ -53,14 +53,13 @@ private void addActors() { cursorPixmap.dispose(); // Dispose of the Pixmap to release resources table = new Table(); - table1 = new Table(); table.setFillParent(true); - table1.setFillParent(true); + stage.addActor(table); - Image title = - new Image( - ServiceLocator.getResourceService() - .getAsset("images/background/main_menu/main_menu_bg.png", Texture.class)); + Texture backgroundTexture = ServiceLocator.getResourceService().getAsset("images/background/main_menu/main_menu_bg.png", Texture.class); + Image title = new Image(backgroundTexture); + title.setFillParent(true); + table.addActorAt(0, title); title.setWidth(Gdx.graphics.getWidth()); title.setHeight(Gdx.graphics.getHeight()); title.setPosition(0, 0); @@ -130,18 +129,13 @@ public void changed(ChangeEvent changeEvent, Actor actor) { float padTopOtherBtns = 15f / originalScreenHeight * Gdx.graphics.getHeight(); - table.add(title); - table1.row(); - table1.add(startBtn).padTop(padTopStartBtn); - table1.row(); - table1.add(helpBtn).padTop(padTopOtherBtns); - table1.row(); - table1.add(settingsBtn).padTop(padTopOtherBtns); - table1.row(); - table1.add(exitBtn).padTop(padTopOtherBtns); + table.center(); + table.add(startBtn).padTop(250f).center().row(); + table.add(helpBtn).padTop(15f).center().row(); + table.add(settingsBtn).padTop(15f).center().row(); + table.add(exitBtn).padTop(15f).center().row(); stage.addActor(table); - stage.addActor(table1); } @Override diff --git a/source/core/src/main/com/csse3200/game/screens/AnimationTexturePair.java b/source/core/src/main/com/csse3200/game/screens/AnimationTexturePair.java new file mode 100644 index 000000000..1aebf5c26 --- /dev/null +++ b/source/core/src/main/com/csse3200/game/screens/AnimationTexturePair.java @@ -0,0 +1,45 @@ +/** + * Represents a pair of animation and texture for rendering in a game screen. + * Provides easy access to the animation and its associated texture. + * + * @param The type of texture region used in the animation. + */ +package com.csse3200.game.screens; + +import com.badlogic.gdx.graphics.Texture; +import com.badlogic.gdx.graphics.g2d.Animation; +import com.badlogic.gdx.graphics.g2d.TextureRegion; + +public class AnimationTexturePair { + public final Animation animation; + public final Texture texture; + + /** + * Creates an {@code AnimationTexturePair} with the specified animation and texture. + * + * @param animation The animation to be associated with this pair. + * @param texture The texture to be associated with this pair. + */ + public AnimationTexturePair(Animation animation, Texture texture) { + this.animation = animation; + this.texture = texture; + } + + /** + * Gets the animation associated with this pair. + * + * @return The animation. + */ + public Animation getAnimation() { + return animation; + } + + /** + * Gets the texture associated with this pair. + * + * @return The texture. + */ + public Texture getTexture() { + return texture; + } +} diff --git a/source/core/src/main/com/csse3200/game/screens/MainMenuScreen.java b/source/core/src/main/com/csse3200/game/screens/MainMenuScreen.java index 37915c5d6..62ab29c96 100644 --- a/source/core/src/main/com/csse3200/game/screens/MainMenuScreen.java +++ b/source/core/src/main/com/csse3200/game/screens/MainMenuScreen.java @@ -19,6 +19,7 @@ import com.csse3200.game.services.ServiceLocator; import com.badlogic.gdx.graphics.g2d.Animation; import com.badlogic.gdx.graphics.g2d.TextureRegion; +import com.csse3200.game.screens.AnimationTexturePair; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.badlogic.gdx.graphics.Texture; @@ -38,18 +39,19 @@ public class MainMenuScreen extends ScreenAdapter { "images/ui/buttons/glass.atlas" }; private static final String[] titleMusic = {"sounds/background/title_screen/ScifiAmbient.ogg"}; + private static final String[] mainMenuTextures = {"images/background/main_menu/main_menu_bg.png"}; private Animation MM_Star1_animation; private Animation MM_Galaxy1_animation; private Animation MM_Planet1_animation; private Animation MM_Planet2_animation; private Animation MM_Planet3_animation; -// private Animation MM_MonitorFace1_animation; + private Animation MM_MonitorFace1_animation; private Texture MM_Star1_Texture; private Texture MM_Galaxy1_Texture; private Texture MM_Planet1_Texture; private Texture MM_Planet2_Texture; private Texture MM_Planet3_Texture; -// private Texture MM_MonitorFace1_Texture; + private Texture MM_MonitorFace1_Texture; private float elapsedTime = 0; private int MM_Star1_frameWidth; private int MM_Star1_frameHeight; @@ -61,10 +63,8 @@ public class MainMenuScreen extends ScreenAdapter { private int MM_Planet2_frameHeight; private int MM_Planet3_frameWidth; private int MM_Planet3_frameHeight; - -// private int MM_MonitorFace1_frameWidth; -// private int MM_MonitorFace1_frameHeight; - private static final String[] mainMenuTextures = {"images/background/main_menu/main_menu_bg.png"}; + private int MM_MonitorFace1_frameWidth; + private int MM_MonitorFace1_frameHeight; public MainMenuScreen(GdxGame game) { this.game = game; @@ -85,9 +85,9 @@ public MainMenuScreen(GdxGame game) { } /** - * Loads the assets required for the main menu screen, including textures and animations. - * This method initializes and loads sprite sheets for various celestial objects like stars, planets, and galaxies. - * Each sprite sheet is divided into individual frames for animation purposes. + * Renders the game screen with the given time delta. + * + * @param delta The time elapsed since the last frame in seconds. */ @Override public void render(float delta) { @@ -107,73 +107,13 @@ public void render(float delta) { batch.begin(); elapsedTime += delta; - // MM_Star1 - // Determine the proportional offset of the MM_Star1 sprite - float MM_Star1_proportionalOffsetX = 830f / backgroundTexture.getWidth(); - float MM_Star1_proportionalOffsetY = 650f / backgroundTexture.getHeight(); - // Calculate the scaling factor based on how the background is stretched to fit the screen - float scaleX = screenWidth / backgroundTexture.getWidth(); - float scaleY = screenHeight / backgroundTexture.getHeight(); - // Calculate the position of the MM_Star1 sprite on the screen - float MM_Star1_spriteX = MM_Star1_proportionalOffsetX * screenWidth; - float MM_Star1_spriteY = MM_Star1_proportionalOffsetY * screenHeight; - // Size adjustments - float MM_Star1_ScaleFactor = 0.3f; - float MM_Star1_Width = MM_Star1_frameWidth * MM_Star1_ScaleFactor; - float MM_Star1_Height = MM_Star1_frameHeight * MM_Star1_ScaleFactor; - batch.draw(MM_Star1_animation.getKeyFrame(elapsedTime, true), MM_Star1_spriteX, MM_Star1_spriteY, MM_Star1_Width, MM_Star1_Height); - - // MM_Galaxy1 - // Determine the proportional offset of the MM_Star1 sprite - float MM_Galaxy1_proportionalOffsetX = 2000f / backgroundTexture.getWidth(); - float MM_Galaxy1_proportionalOffsetY = 1075f / backgroundTexture.getHeight(); - // Calculate the position of the MM_Star1 sprite on the screen - float MM_Galaxy1_spriteX = MM_Galaxy1_proportionalOffsetX * screenWidth; - float MM_Galaxy1_spriteY = MM_Galaxy1_proportionalOffsetY * screenHeight; - // Size adjustments - float MM_Galaxy1_ScaleFactor = 0.95f; - float MM_Galaxy1_Width = MM_Galaxy1_frameWidth * MM_Galaxy1_ScaleFactor; - float MM_Galaxy1_Height = MM_Galaxy1_frameHeight * MM_Galaxy1_ScaleFactor; - batch.draw(MM_Galaxy1_animation.getKeyFrame(elapsedTime, true), MM_Galaxy1_spriteX, MM_Galaxy1_spriteY, MM_Galaxy1_Width, MM_Galaxy1_Height); - - // MM_Planet1 - // Determine the proportional offset of the MM_Star1 sprite - float MM_Planet1_proportionalOffsetX = 1630f / backgroundTexture.getWidth(); - float MM_Planet1_proportionalOffsetY = 800f / backgroundTexture.getHeight(); - // Calculate the position of the MM_Star1 sprite on the screen - float MM_Planet1_spriteX = MM_Planet1_proportionalOffsetX * screenWidth; - float MM_Planet1_spriteY = MM_Planet1_proportionalOffsetY * screenHeight; - // Size adjustments - float MM_Planet1_ScaleFactor = 0.5f; - float MM_Planet1_Width = MM_Planet1_frameWidth * MM_Planet1_ScaleFactor; - float MM_Planet1_Height = MM_Planet1_frameHeight * MM_Planet1_ScaleFactor; - batch.draw(MM_Planet1_animation.getKeyFrame(elapsedTime, true), MM_Planet1_spriteX, MM_Planet1_spriteY, MM_Planet1_Width, MM_Planet1_Height); - - // MM_Planet2 - // Determine the proportional offset of the MM_Star1 sprite - float MM_Planet2_proportionalOffsetX = 1430f / backgroundTexture.getWidth(); - float MM_Planet2_proportionalOffsetY = 1250f / backgroundTexture.getHeight(); - // Calculate the position of the MM_Star1 sprite on the screen - float MM_Planet2_spriteX = MM_Planet2_proportionalOffsetX * screenWidth; - float MM_Planet2_spriteY = MM_Planet2_proportionalOffsetY * screenHeight; - // Size adjustments - float MM_Planet2_ScaleFactor = 0.7f; - float MM_Planet2_Width = MM_Planet2_frameWidth * MM_Planet2_ScaleFactor; - float MM_Planet2_Height = MM_Planet2_frameHeight * MM_Planet2_ScaleFactor; - batch.draw(MM_Planet2_animation.getKeyFrame(elapsedTime, true), MM_Planet2_spriteX, MM_Planet2_spriteY, MM_Planet2_Width, MM_Planet2_Height); - - // MM_Planet3 - // Determine the proportional offset of the MM_Star1 sprite - float MM_Planet3_proportionalOffsetX = 420f / backgroundTexture.getWidth(); - float MM_Planet3_proportionalOffsetY = 990f / backgroundTexture.getHeight(); - // Calculate the position of the MM_Star1 sprite on the screen - float MM_Planet3_spriteX = MM_Planet3_proportionalOffsetX * screenWidth; - float MM_Planet3_spriteY = MM_Planet3_proportionalOffsetY * screenHeight; - // Size adjustments - float MM_Planet3_ScaleFactor = 0.65f; - float MM_Planet3_Width = MM_Planet3_frameWidth * MM_Planet3_ScaleFactor; - float MM_Planet3_Height = MM_Planet3_frameHeight * MM_Planet3_ScaleFactor; - batch.draw(MM_Planet3_animation.getKeyFrame(elapsedTime, true), MM_Planet3_spriteX, MM_Planet3_spriteY, MM_Planet3_Width, MM_Planet3_Height); + // Draw various animations at specific positions and scales + drawAnimation(MM_Star1_animation, 890f / backgroundTexture.getWidth(), 680f / backgroundTexture.getHeight(), 0.3f); + drawAnimation(MM_Galaxy1_animation, 2100f / backgroundTexture.getWidth(), 1140f / backgroundTexture.getHeight(), 0.95f); + drawAnimation(MM_Planet1_animation, 1630f / backgroundTexture.getWidth(), 800f / backgroundTexture.getHeight(), 0.5f); + drawAnimation(MM_Planet2_animation, 1430f / backgroundTexture.getWidth(), 1250f / backgroundTexture.getHeight(), 0.7f); + drawAnimation(MM_Planet3_animation, 420f / backgroundTexture.getWidth(), 990f / backgroundTexture.getHeight(), 0.65f); + drawAnimation(MM_MonitorFace1_animation, 1930f / backgroundTexture.getWidth(), 360f / backgroundTexture.getHeight(), 3.0f); batch.end(); } @@ -208,9 +148,10 @@ public void dispose() { } /** - * Loads the assets required for the main menu screen, including textures and animations. + * Loads the assets required for the main menu screen, including textures, texture atlases, music, and animations. * This method initializes and loads sprite sheets for various celestial objects such as stars, galaxies, and planets. * Each sprite sheet is divided into individual frames for animation purposes. + * It also loads and sets up animations and textures for specific game objects. */ private void loadAssets() { logger.debug("Loading assets"); @@ -220,95 +161,33 @@ private void loadAssets() { ServiceLocator.getResourceService().loadMusic(titleMusic); backgroundTexture = new Texture("images/background/main_menu/main_menu_bg.png"); - // MM_Star1 - MM_Star1_Texture = new Texture(Gdx.files.internal("images/background/main_menu/MM_Objects/MM_Star1.png")); - - int MM_Star1_totalColumns=60; - MM_Star1_frameWidth = MM_Star1_Texture.getWidth() / MM_Star1_totalColumns; // totalColumns = no. of columns in MM_Star1 sprite sheet - int MM_Star1_totalRows=1; - MM_Star1_frameHeight = MM_Star1_Texture.getHeight() / MM_Star1_totalRows; // totalRows = no. of rows in MM_Star1 sprite sheet - - TextureRegion[][] MM_Star1_Frames = TextureRegion.split(MM_Star1_Texture, MM_Star1_frameWidth, MM_Star1_frameHeight); - - TextureRegion[] MM_Star1_animationFrames = new TextureRegion[MM_Star1_totalColumns]; - - System.arraycopy(MM_Star1_Frames[0], 0, MM_Star1_animationFrames, 0, MM_Star1_totalColumns); - - MM_Star1_animation = new Animation<>(0.17f, MM_Star1_animationFrames); - - - // MM_Galaxy1 - MM_Galaxy1_Texture = new Texture(Gdx.files.internal("images/background/main_menu/MM_Objects/MM_Galaxy1.png")); - - int MM_Galaxy1_totalColumns=60; - MM_Galaxy1_frameWidth = MM_Galaxy1_Texture.getWidth() / MM_Galaxy1_totalColumns; // totalColumns = no. of columns in MM_Star1 sprite sheet - int MM_Galaxy1_totalRows=1; - MM_Galaxy1_frameHeight = MM_Galaxy1_Texture.getHeight() / MM_Galaxy1_totalRows; // totalRows = no. of rows in MM_Star1 sprite sheet - - TextureRegion[][] MM_Galaxy1_Frames = TextureRegion.split(MM_Galaxy1_Texture, MM_Galaxy1_frameWidth, MM_Galaxy1_frameHeight); - - TextureRegion[] MM_Galaxy1_animationFrames = new TextureRegion[MM_Galaxy1_totalColumns]; - - System.arraycopy(MM_Galaxy1_Frames[0], 0, MM_Galaxy1_animationFrames, 0, MM_Galaxy1_totalColumns); - - MM_Galaxy1_animation = new Animation<>(0.17f, MM_Galaxy1_animationFrames); - - - // MM_Planet1 - MM_Planet1_Texture = new Texture(Gdx.files.internal("images/background/main_menu/MM_Objects/MM_Planet1.png")); - - int MM_Planet1_totalColumns=60; - MM_Planet1_frameWidth = MM_Planet1_Texture.getWidth() / MM_Planet1_totalColumns; // totalColumns = no. of columns in MM_Star1 sprite sheet - int MM_Planet1_totalRows=1; - MM_Planet1_frameHeight = MM_Planet1_Texture.getHeight() / MM_Planet1_totalRows; // totalRows = no. of rows in MM_Star1 sprite sheet - - TextureRegion[][] MM_Planet1_Frames = TextureRegion.split(MM_Planet1_Texture, MM_Planet1_frameWidth, MM_Planet1_frameHeight); - - TextureRegion[] MM_Planet1_animationFrames = new TextureRegion[MM_Planet1_totalColumns]; - - System.arraycopy(MM_Planet1_Frames[0], 0, MM_Planet1_animationFrames, 0, MM_Planet1_totalColumns); - - MM_Planet1_animation = new Animation<>(0.17f, MM_Planet1_animationFrames); - - - // MM_Planet2 - MM_Planet2_Texture = new Texture(Gdx.files.internal("images/background/main_menu/MM_Objects/MM_Planet2.png")); - - int MM_Planet2_totalColumns=60; - MM_Planet2_frameWidth = MM_Planet2_Texture.getWidth() / MM_Planet2_totalColumns; // totalColumns = no. of columns in MM_Star1 sprite sheet - int MM_Planet2_totalRows=1; - MM_Planet2_frameHeight = MM_Planet2_Texture.getHeight() / MM_Planet2_totalRows; // totalRows = no. of rows in MM_Star1 sprite sheet - - TextureRegion[][] MM_Planet2_Frames = TextureRegion.split(MM_Planet2_Texture, MM_Planet2_frameWidth, MM_Planet2_frameHeight); - - TextureRegion[] MM_Planet2_animationFrames = new TextureRegion[MM_Planet2_totalColumns]; - - System.arraycopy(MM_Planet2_Frames[0], 0, MM_Planet2_animationFrames, 0, MM_Planet2_totalColumns); - - MM_Planet2_animation = new Animation<>(0.17f, MM_Planet2_animationFrames); - - - // MM_Planet3 - MM_Planet3_Texture = new Texture(Gdx.files.internal("images/background/main_menu/MM_Objects/MM_Planet3.png")); - - int MM_Planet3_totalColumns=54; - MM_Planet3_frameWidth = MM_Planet3_Texture.getWidth() / MM_Planet3_totalColumns; // totalColumns = no. of columns in MM_Star1 sprite sheet - int MM_Planet3_totalRows=1; - MM_Planet3_frameHeight = MM_Planet3_Texture.getHeight() / MM_Planet3_totalRows; // totalRows = no. of rows in MM_Star1 sprite sheet - - TextureRegion[][] MM_Planet3_Frames = TextureRegion.split(MM_Planet3_Texture, MM_Planet3_frameWidth, MM_Planet3_frameHeight); - - TextureRegion[] MM_Planet3_animationFrames = new TextureRegion[MM_Planet3_totalColumns]; - - System.arraycopy(MM_Planet3_Frames[0], 0, MM_Planet3_animationFrames, 0, MM_Planet3_totalColumns); - - MM_Planet3_animation = new Animation<>(0.17f, MM_Planet3_animationFrames); + AnimationTexturePair result; + + result = loadAnimation("images/background/main_menu/MM_Objects/MM_Star1.png", 60, 1, 0.15f); + MM_Star1_animation = result.getAnimation(); + MM_Star1_Texture = result.getTexture(); + result = loadAnimation("images/background/main_menu/MM_Objects/MM_Galaxy1.png", 60, 1, 0.15f); + MM_Galaxy1_animation = result.getAnimation(); + MM_Galaxy1_Texture = result.getTexture(); + result = loadAnimation("images/background/main_menu/MM_Objects/MM_Planet1.png", 60, 1, 0.15f); + MM_Planet1_animation = result.getAnimation(); + MM_Planet1_Texture = result.getTexture(); + result = loadAnimation("images/background/main_menu/MM_Objects/MM_Planet2.png", 60, 1, 0.15f); + MM_Planet2_animation = result.getAnimation(); + MM_Planet2_Texture = result.getTexture(); + result = loadAnimation("images/background/main_menu/MM_Objects/MM_Planet3.png", 54, 1, 0.15f); + MM_Planet3_animation = result.getAnimation(); + MM_Planet3_Texture = result.getTexture(); + result = loadAnimation("images/background/main_menu/MM_Objects/MM_MonitorFace1.png", 4, 1, 0.1f); + MM_MonitorFace1_animation = result.getAnimation(); + MM_MonitorFace1_Texture = result.getTexture(); ServiceLocator.getResourceService().loadAll(); } /** * Unloads the assets that were previously loaded for the main menu screen. + * This method is responsible for releasing resources to free up memory. * This method disposes of textures and sprite sheets used for celestial objects such as stars, galaxies, and planets. */ private void unloadAssets() { @@ -322,8 +201,10 @@ private void unloadAssets() { MM_Planet1_Texture.dispose(); MM_Planet2_Texture.dispose(); MM_Planet3_Texture.dispose(); + MM_MonitorFace1_Texture.dispose(); } + /** * Creates the main menu's ui including components for rendering ui elements to the screen and * capturing and handling ui input. @@ -341,4 +222,46 @@ private void createUI() { music.setVolume(0.3f); music.play(); } + + /** + * Loads an animation from a given texture file path by splitting it into frames + * based on the specified number of columns and rows and creating an AnimationTexturePair. + * + * @param texturePath The file path of the texture to create the animation from. + * @param totalColumns The total number of columns in the texture grid. + * @param totalRows The total number of rows in the texture grid. + * @param frameDuration The duration of each frame in seconds. + * @return An AnimationTexturePair containing the animation and its associated texture. + */ + private AnimationTexturePair loadAnimation(String texturePath, int totalColumns, int totalRows, float frameDuration) { + Texture texture = new Texture(Gdx.files.internal(texturePath)); + int frameWidth = texture.getWidth() / totalColumns; + int frameHeight = texture.getHeight() / totalRows; + TextureRegion[][] frames = TextureRegion.split(texture, frameWidth, frameHeight); + TextureRegion[] animationFrames = new TextureRegion[totalColumns]; + System.arraycopy(frames[0], 0, animationFrames, 0, totalColumns); + return new AnimationTexturePair(new Animation<>(frameDuration, animationFrames), texture); + } + + /** + * Draws an animation onto the screen at a specified position and scale. + * + * @param animation The animation to be drawn. + * @param proportionalOffsetX The proportional X-coordinate offset from the screen's left edge. + * @param proportionalOffsetY The proportional Y-coordinate offset from the screen's bottom edge. + * @param referenceScaleFactor The scaling factor relative to the screen width. + */ + private void drawAnimation(Animation animation, float proportionalOffsetX, float proportionalOffsetY, float referenceScaleFactor) { + float screenWidth = Gdx.graphics.getWidth(); + float screenHeight = Gdx.graphics.getHeight(); + float spriteX = proportionalOffsetX * screenWidth; + float spriteY = proportionalOffsetY * screenHeight; + + float scalingFactor = screenWidth / 1920f; + + float width = animation.getKeyFrames()[0].getRegionWidth() * referenceScaleFactor * scalingFactor; + float height = animation.getKeyFrames()[0].getRegionHeight() * referenceScaleFactor * scalingFactor; + + batch.draw(animation.getKeyFrame(elapsedTime, true), spriteX, spriteY, width, height); + } } \ No newline at end of file