Main menu transition screens implementation - UQdeco2800/2022-studio-2 GitHub Wiki

Introduction

The main menu transition screens are implemented based on the Team06's animation code logic. The main menu transtition screen is a seperate GdxGame.java screen that plays an appropriate animation between MainMenuDisplayProMax.java and GameAreaDisplay.java after the startbutton is clicked.

Code Explaination

The two main classes used to build the animation are MainMenuTransitionScreen.java and MainMenuTransitionAnimation.java.

MainMenuTransitionScreen.java

The first class is responsible for generating all the frames required for the game. Since our animation is consists of 84 frames, it would be difficult and redundant to just list all the 84 frames and process them together. Therefore, the below method is created to chuck all the frames file into a list.

private void loadFrames() {
        logger.debug("Loading assets");
        ResourceService resourceService = ServiceLocator.getResourceService();

        for (int i = 0; i < frameCount; i++) {
            transitionTextures[i] = animationPrefix + i + ".png";
        }
        resourceService.loadTextures(transitionTextures);
        ServiceLocator.getResourceService().loadAll();
    }

The above algorithm uses a for loop to automatically iterate over all 84 frames, grab them and put them into a list for later use. Then when the player click the start button, it will trigger the onLoad() method within MainMenuDisplay.java class, and the method will set the screen to the MENU_TRANSITION.

  private void onLoad() {
    Sound sound = Gdx.audio.newSound(Gdx.files.internal("sounds/ButtonSoundtrack.wav"));
    sound.play(1.0f);
    logger.info("Load game");
    game.setScreen(GdxGame.ScreenType.MENU_TRANSITION);
  }

MainMenuTransitionAnimation.java

In this class, it will grab the frame list generated by MainMenuTransitionScreen.java, loop through the list and generate each frame to form the animation, it is done by:

    private void addActors() {
        if (frame < MainMenuTransitionScreen.frameCount) {
            stage.clear();

            transitionFrames = new Image(ServiceLocator.getResourceService()
                    .getAsset(MainMenuTransitionScreen.transitionTextures[frame], Texture.class));

            transitionFrames.setWidth(Gdx.graphics.getWidth());
            transitionFrames.setHeight(Gdx.graphics.getHeight());
            frame++;
            logger.info("frame = " + frame);
            stage.addActor(transitionFrames);
            lastFrameTime = System.currentTimeMillis();
        }
    }

More imporatantly, as shown below, in order to adjust the animation frame rate (the speed of the animation), the update() method is modified to simulate the same functionality as what they did in the built in animation system.

    @Override
    public void update() {
        if (System.currentTimeMillis() - lastFrameTime > frameDuration) {
            addActors();
        }
    }

Key bind trigger

Similar to the pre-defined TouchPlayerInputComponent.java, a similar class is made to deal with when player use ENTER key to skip the transition animation. The idea is to first detect user's key press and then use entity events to trigger game start.

@Override
    public boolean keyDown(int keycode) {
        switch (keycode) {
            case Keys.ENTER:
                logger.info("Exiting transition screen");
                entity.getEvents().trigger("start");
                return true;
            default:
                return false;
        }
    }

Who to talk to

Isaac Graham, (@The Master Craig#7285, IsaacGraham128)

Yingzheng Cai, (@Rey_cyz#1464, Rey-666)