Skip to content

Win lose condition based on eviction: Code Guideline Sprint3

QiansenJhin edited this page Oct 18, 2022 · 4 revisions

The Design Guideline:Sprint3: Win/lose Condition

Contents

  1. Display the Win/Lose prompt window after select the traitor.
  2. Dispaly a result dialog and traitor message
  3. Handles the game logic of choosing a traitor.

UI/Game Logic design implementation

In sprint3, in order to better implement the game logic and display a series of related popup windows, we created a new class NpcEvictionMenuDisplayNew.java. The above contents are all implemented in this class.

1. Result dialog and traitor message on the stage.

In createResultDialog, The wrong box will display to to inform the player that they picked the wrong NPC and how many chances they have left. if player run out of their chance and lose, it will jump to ending screen, no need to create dialog.

If the player selects the correct traitor and clicks on the Select button corresponding to the correct traitor, the stage will display a dialog. After click the ok button, the stage will display another dialog to show the traitor message, and the meassage handled by the function helper.createTraitorMessageForSaveAtlantis, which related to the class NpcEvictionMenuDisplayHelper.java. And all scales of dialog are calculated according to the prototype from team7.

private void createResultDialog(String button_name, NpcResultDialogType type) {
        logger.debug("create Result dialog from name: " + button_name + " type:" + type);
 
        if (type == NpcResultDialogType.LOSE) {
            EndingMenuDisplay.setLose();
            entity.getEvents().trigger("ending");
            return;}
        if (type == NpcResultDialogType.WIN) {handleWin();return;}

        // set the style of dialog include font color of title; background; size; position
        float dialog_size_x,dialog_size_y;
        String backgroundPath, buttonPathDefault, buttonPathHover;

        if (type == NpcResultDialogType.RIGHT_BOX) {
            backgroundPath = IMAGES_PATH + "rightBox.png";
            buttonPathDefault = IMAGES_PATH + "rightBtn.png";
            buttonPathHover = IMAGES_PATH + "rightBtn_H.png";
        } else if (type == NpcResultDialogType.WRONG_BOX1) {...} else {....}
        TextureRegionDrawable styleImage = new TextureRegionDrawable(
                resourceService.getAsset(backgroundPath, Texture.class));

        Window.WindowStyle windowStyle = new Window.WindowStyle(new BitmapFont(), Color.BLACK, styleImage);
        Window dialog = new Window("", windowStyle);
        dialog.setModal(true);    // The dialog is always at the front
        Button okButton = createButton(buttonPathDefault, buttonPathHover);

        if (type == NpcResultDialogType.RIGHT_BOX) {
            dialog_size_x = (float) (bgWidth * (683.67 / 1600));
            dialog_size_y = (float) (bgHeight * (416.24 / 900));
            dialog.setSize(dialog_size_x, dialog_size_y);
            dialog.setPosition((float) (bgWidth * (433.33 / 1600)), (float) (bgHeight * (1 - 663.33 / 900)));

            okButton.setSize((float) (dialog_size_x * (116.67/683.67)), (float) (dialog_size_y * (53.33/416.24)));
            okButton.setPosition((float) (dialog.getWidth() * ((764.04-433.33)/683.67)),
                    (float) (dialog.getHeight() * ((663.33 - 635.65) / 416.24)));
            Label message_interjection =new Label(helper.createTraitorMessageInterjection(type),skin);
            message_interjection.setFontScale(dialog_size_y/400);
            message_interjection.setAlignment(Align.center);
            Label message = new Label(helper.createTraitorMessageForSaveAtlantis(button_name,type), skin);
            message.setFontScale(dialog_size_y/400);
            message.setWrap(true);
            message.setAlignment(Align.left);
            Table table = new Table();
            table.add(message_interjection).width(dialog_size_x * 3 / 5);
            table.row();
            table.add(message).width(dialog_size_x * 3 / 5);
            table.padLeft(dialog_size_x/6).padTop(dialog_size_y/6);
            dialog.add(table);
        } else {...}
        okButton.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent changeEvent, Actor actor) {
                logger.info("yes_button from " + button_name + " clicked");
                 //when you select ok button
                dialog.remove();
                if (type == NpcResultDialogType.RIGHT_BOX)
                    handleWin();
            }
        });
        dialog.addActor(okButton);
        stage.addActor(dialog);
    }
    private void exitMenu() {
        stage.remove();
    }
    public void setFindKey(Boolean findKey) {
        this.findKey = findKey;
    }

2. The Win/Lose window after select the traitor

In the handlewin, when player select the correct traitor, after click OK button on right_box, an win Information page will display and the key will be spawn on the game area.

private void handleWin() {
        logger.debug("Function handleWin is called");
        // set the style of dialog include font color of title; background; size; position
        TextureRegionDrawable styleImage = new TextureRegionDrawable(
                resourceService.getAsset(IMAGES_PATH + "saveMessage.png", Texture.class));
        Window.WindowStyle windowStyle = new Window.WindowStyle(new BitmapFont(), Color.BLACK, styleImage);
        Window dialog = new Window("", windowStyle);
        dialog.setModal(true);    // The dialog is always at the front
        dialog.setFillParent(true);
        dialog.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                logger.debug("win game page clicked");
                dialog.remove();
                exitMenu();
                // here we need call findKey function from team 5.
                if (!findKey){
                    gameArea.spawnKey(game);
                    findKey = true;
                }
                return true;
            }
        });
        showWinContext(dialog);
        stage.addActor(dialog);
    }

In addition, the showWinContext will create a table to include the lable of win context about the message of the traitor. The content of the message handle by the if (findKey)(if player chose the right traitor and find the key), the message will display in the scales of Team7 prototype.

private void showWinContext(Window dialog){
        // create frame for both title and context
        Table title =  new Table();
        title.setSize(stage.getWidth()*(1106-636)/1600,
                stage.getHeight()*(220-160)/900);
        title.setPosition(...);

        Table context =  new Table();
        context.setSize(...); context.setPosition(...);

        // set title/context srting
        String titleContent;
        String contextContent;
        titleContent = "Message from Ares";
        if (findKey) {
            contextContent = """
                    
                    You have found the traitor and got the information to save Atlantis.
                      
                    Go and find the mysterious key!""";

        } else {
            contextContent = """
                    \s
                    I love my country and people very much, but I have to do it for the throne. However, I regretted it after Atlantis sank, but maybe you can save it:
                    \s\s
                    Find the mysterious key, it can help you save Atlantis and help you out of here!""";
        }

        // create skin of label same as default skin in main menu
        Skin skin = new Skin(Gdx.files.internal("flat-earth/skin/flat-earth-ui.json"));

        // create and set the label with content
        Label titleLabel = new Label(titleContent, skin, "title",Color.NAVY);
        titleLabel.setAlignment(Align.center);
        titleLabel.setFontScale(stage.getWidth()/1920,stage.getHeight()/980);

        Label contextLabel = new Label(contextContent, skin, "font_large",Color.DARK_GRAY);
        contextLabel.setSize(context.getWidth(),context.getHeight());
        contextLabel.setAlignment(Align.top);
        contextLabel.setFontScale(stage.getWidth()/1920,stage.getHeight()/980);
        contextLabel.setWrap(true);

        // add to goal position
        title.add(titleLabel).center();
        context.addActor(contextLabel);
        dialog.addActor(title);
        dialog.addActor(context);
    }

3. The game logic of choosing a traitor

In handleLogic, Players have three chances to select the traitor, each wrong choice will reduce the player's HP and countdown at the same time. These series of functions are used for display the different result of the each selection.

  • NpcResultDialogType.RIGHT_BOX: if player correctly select the traitor
  • NpcResultDialogType.WRONG_BOX1: if player fails for the first time
  • NpcResultDialogType.WRONG_BOX2: if player fails for the second time
  • NpcResultDialogType.LOSE: if player select 3 times and all fail
  • NpcResultDialogType.WIN: if player have correctly select in the past

Otherwise, functions entity.getComponent(CombatStatsComponent.class).getHealth() and entity.getComponent(countdownDisplay.class).getRemainingTime() are used to determin the change of the player's HP and remaining chances.

protected NpcResultDialogType handleLogic (String name) {
        if (findKey) { // player have selected correct in the past
            return NpcResultDialogType.WIN;
        }
        if (Objects.equals(name, "Ares")){ // select npc correctly
            errorNum = 0;
            return NpcResultDialogType.RIGHT_BOX;
        } else {
            if (errorNum == 0){
                //decrease blood 10%
                int health = entity.getComponent(CombatStatsComponent.class).getHealth();
                entity.getComponent(CombatStatsComponent.class).setHealth((int) (health*0.9));

                //at the same time decrease remaing time 10%
                float remainingTime =entity.getComponent(countdownDisplay.class).getRemainingTime();
                entity.getComponent(countdownDisplay.class).setTimeRemaining((float) remainingTime*0.9f);

                errorNum++;
                return NpcResultDialogType.WRONG_BOX1;
            } else if (errorNum == 1) {
                //decrease blood 20%
                int health = entity.getComponent(CombatStatsComponent.class).getHealth();
                entity.getComponent(CombatStatsComponent.class).setHealth((int) (health*0.8));

                //at the same time decrease remaing time 20%
                float remainingTime =entity.getComponent(countdownDisplay.class).getRemainingTime();
                entity.getComponent(countdownDisplay.class).setTimeRemaining((float) remainingTime*0.8f);
                errorNum++;
                return NpcResultDialogType.WRONG_BOX2;
            } else if (errorNum == 2){
                // game over
                errorNum = 0;
                return NpcResultDialogType.LOSE;
            }
        }
        return null; // if return null, it means some error in code, please check
    }
}

Relevent Files

NpcEvictionMenuDisplayNew.java

NpcEvictionMenuDisplayHelper.java

UML Diagram

20221002163741

Table of Contents

Home

Game Design

User survey

Sprint 4

Eviction Menu and Win/lose Logic: Polishing tasks (Team 7)

Button Sounds and Ending Menu improve (Team 3)

Sound effect and Fixing the clue bug (Team 6)

Improvement of Enemy and Attack (Team 1)

Add Features When The Player Get Attacked and Overall UI Improvement (Team 8)

Sprint 1

Achievement System (Team 2)

Player Eviction Menu (Team 7)

Countdown Clock (Team 4)

Music (Team3)

Map (Team6)

Sprint 2

Player Eviction Menu (Team 7)

Character Design & Animation (Team 1)

Music (Team 3)

Inventory System and Consumables Items (Team 8)

Scenario design

Achievement System(team 2)

Storyline (Team 5)

Countdown Clock (Team 4)

Sprint 3

Ending Menu (Team 3)

NPC interaction (Team 2)

Win/lose Condition (Based on Eviction Menu) (Team 7)

Player Profile (Team 4)

Game Logo (Team 8)

Clue storage (Team 6)

Enemy Design and Attack (Team 1)

Scenario design for village(Team5)

Game design
Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally