Skip to content

NPC Dialog Code Structure

zihao-xia edited this page Oct 4, 2022 · 2 revisions

NPC dialog interaction is mainly implemented in NpcInteractionDisplay.java and DialogWithSelection.java. In NpcInteractionDisplay.java, a dialog box is set, and dialog texts will be placed in the dialog box according to different chapter scripts.

// set dialog box
dialogBox = new Image(ServiceLocator.getResourceService().getAsset(
        "images/npc_interaction/dialog_box.png", Texture.class));
dialogBox.setPosition((float) (stage.getWidth() * 0.05), 0);
dialogBox.setSize((float) (stage.getWidth() * 0.9), (float) (stage.getHeight() * 0.2));
stage.addActor(dialogBox);

setDialog() will read dialogs in different chapters according to the chapterNum.

private void setDialog() {
        step = 0;
        dialog = new Label("Chapter " + chapterNum, skin);
        dialog.setPosition((float) (stage.getWidth() * 0.1), (float) (stage.getHeight() * 0.1));
        dialog.setWrap(true);
        dialog.setWidth((float) (stage.getWidth() * 0.8));
        stage.addActor(dialog);

        try {
            if (chapterNum == 1 || chapterNum == 4) {// chapter without selections
                ArrayList<String> texts = NpcInteraction.readNpcFiles(chapterNum);
                Iterator<String> it = texts.iterator();
                switch (chapterNum) {
                    case 1 -> clickListener = new ClickListener() {
                        @Override
                        public void clicked(InputEvent event, float x, float y) {
                            super.clicked(event, x, y);
                            chapter1Listener(it);
                        }
                    };
                    case 4 -> clickListener = new ClickListener() {
                        @Override
                        public void clicked(InputEvent event, float x, float y) {
                            super.clicked(event, x, y);
                            chapter4Listener(it);
                        }
                    };
                }

            } else {
                switch (chapterNum) {
                    case 2 -> {
                        root = DialogWithSelection.getChapter2Dialog();
                        clickListener = new ClickListener() {
                            @Override
                            public void clicked(InputEvent event, float x, float y) {
                                super.clicked(event, x, y);
                                chapter2Listener();
                            }
                        };
                    }
                    case 3 -> {
                        root = DialogWithSelection.getChapter3Dialog();
                        clickListener = new ClickListener() {
                            @Override
                            public void clicked(InputEvent event, float x, float y) {
                                super.clicked(event, x, y);
                                chapter3Listener();
                            }
                        };
                    }
                    case 5 -> {
                        root = DialogWithSelection.getChapter5Dialog();
                        clickListener = new ClickListener() {
                            @Override
                            public void clicked(InputEvent event, float x, float y) {
                                super.clicked(event, x, y);
                                chapter5Listener();
                            }
                        };
                    }
                }
            }
            dialogBox.addListener(clickListener);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

Chapter 1 and 4 do not have selection points, so their dialogs are implemented in an array list, and the dialogs are advanced through the iterator.

The other chapters have selection points, so their dialogs are implemented through a tree data structure (DialogWithSelection) in DialogWithSelection.java.

public class DialogWithSelection {
    private String dialog;
    private DialogWithSelection next;
    private DialogWithSelection option1;
    private DialogWithSelection option2;

A DialogWithSelection has four properties:

  • dialog: One line sentence in the dialog script
  • next: The next dialog.
  • option1: The first option of a selection point.
  • option2: The second option of a selection point.

If a dialog is a selection point, it will be set as the selection point through setSelectionPoint function, which will set a DialogWithSelection without dialog as its next DialogWithSelection, and set options on the new DialogWithSelection, and then set follow-up dialogs for the two options.

public void setSelectionPoint(String option1Text, String option2Text,
                                  int chapterNum, int selectionNum) {
        // set selection point for root
        DialogWithSelection selectionPoint = new DialogWithSelection(null);
        DialogWithSelection option1 = new DialogWithSelection(option1Text);
        DialogWithSelection option2 = new DialogWithSelection(option2Text);
        this.setNext(selectionPoint);
        selectionPoint.setOption1(option1);
        selectionPoint.setOption2(option2);

        // set follow-up dialogs for the two options
        try {
            for (String dialog : readOptionDialogs(chapterNum, selectionNum, 1)) {
                option1.setNext(new DialogWithSelection(dialog));
                option1 = option1.getNext();
            }
            for (String dialog : readOptionDialogs(chapterNum, selectionNum, 2)) {
                option2.setNext(new DialogWithSelection(dialog));
                option2 = option2.getNext();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

After setting all dialogs of chapter X, the root (the first sentence of a chapter) DialogWithSelection will be returned through the getChapterXDialog function, and the dialog can be advanced through getNext, getOption1, getOption2 functions.

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