diff --git a/example/basic.d b/example/basic.d index ec6294d..efee46b 100644 --- a/example/basic.d +++ b/example/basic.d @@ -9,13 +9,13 @@ module popka.example.basic; public import popka.example.camera; public import popka.example.coins; public import popka.example.hello; -public import popka.example.story; +public import popka.example.dialogue; void runEveryExample() { runHelloExample(); runCoinsExample(); runCameraExample(); - runStoryExample(); + runDialogueExample(); } unittest {} diff --git a/example/dialogue.d b/example/dialogue.d new file mode 100644 index 0000000..679f519 --- /dev/null +++ b/example/dialogue.d @@ -0,0 +1,50 @@ +// Copyright 2024 Alexandros F. G. Kapretsos +// SPDX-License-Identifier: MIT + +/// An example that shows how to use the dialogue system of Popka. +// TODO: This example needs some work. + +module popka.example.dialogue; + +import popka.basic; +import popka.game.dialogue; + +void runDialogueExample() { + openWindow(640, 480); + lockResolution(320, 180); + + // The game variables. + auto dialogue = Dialogue(); + auto script = " + # This is a comment. + + * Point1 + > Bob + | Hi. + | My name is Bob. + > Mia + | Hello! + | Nice to meet you! + @ Point1 + "; + + // Parse the dialogue script of the game. + // The first update makes the dialogue go to the first available line. + dialogue.parse(script); + dialogue.update(); + + while (isWindowOpen) { + if (dialogue.canUpdate) { + if (Keyboard.space.isPressed) { + dialogue.update(); + } + drawDebugText("{}: {}".fmt(dialogue.actor, dialogue.text)); + } else { + drawDebugText("End of dialogue."); + } + } + + // Free all the game resources. + dialogue.free(); + freeWindow(); +} diff --git a/example/story.d b/example/story.d deleted file mode 100644 index 470a508..0000000 --- a/example/story.d +++ /dev/null @@ -1,55 +0,0 @@ -// Copyright 2024 Alexandros F. G. Kapretsos -// SPDX-License-Identifier: MIT - -/// An example that shows how to use the dialogue system of Popka. -// TODO: This example needs some work. - -module popka.example.story; - -import popka.basic; -import popka.game.dialogue; - -void runStoryExample() { - openWindow(640, 480); - lockResolution(320, 180); - - // The game variables. - auto file = " - # This is a comment. - - > Alex - | Hi. - | My name is Alex. - > Maria - | Hello! - | Nice to meet you! - - - - # I don't know why this does not work... - # * Point - # > Actor3 - # | This is a loop. - # @ Point - "; - auto dialogue = Dialogue(); - - // Parse the file that is the story of the game. - dialogue.parse(file); - - while (isWindowOpen) { - // The first update makes the dialogue system go to the first available line. - if (!dialogue.canUpdate) { - dialogue.reset(); - dialogue.update(); - } - // Update the story. - if (Keyboard.space.isPressed) { - dialogue.update(); - } - // Draw the game. - drawDebugText("{}: {}".fmt(dialogue.actor, dialogue.content)); - } - // Free all the game resources. - dialogue.free(); - freeWindow(); -} diff --git a/game/dialogue.d b/game/dialogue.d index 07dc760..a7fbb6a 100644 --- a/game/dialogue.d +++ b/game/dialogue.d @@ -44,7 +44,7 @@ struct Dialogue { List!DialogueUnit units; size_t unitIndex; const(char)[] actor; - const(char)[] content; + const(char)[] text; this(const(char)[] path) { load(path); @@ -62,7 +62,7 @@ struct Dialogue { if (units.length != 0 && unitIndex < units.length - 1) { unitIndex += 1; auto unit = units[unitIndex]; - content = unit.content.items; + text = unit.content.items; if (unit.isOneOf(DialogueUnit.comment, DialogueUnit.point)) { update(); } @@ -93,11 +93,11 @@ struct Dialogue { units.free(); } - void parse(const(char)[] text) { + void parse(const(char)[] script) { free(); units.append(DialogueUnit(List!char(), DialogueUnit.pause)); auto isFirstLine = true; - auto view = text; + auto view = script; while (view.length != 0) { auto line = trim(skipLine(view)); if (line.length == 0) { @@ -105,9 +105,11 @@ struct Dialogue { } auto content = trimStart(line[1 .. $]); auto kind = line[0]; - if (isFirstLine && kind == DialogueUnit.pause) { + if (isFirstLine) { isFirstLine = false; - continue; + if (kind == DialogueUnit.pause) { + continue; + } } auto unit = DialogueUnit(List!char(), kind); if (unit.isValid) { diff --git a/game/engine.d b/game/engine.d index 7bbd228..fb56229 100644 --- a/game/engine.d +++ b/game/engine.d @@ -495,7 +495,9 @@ void randomize() { } void openWindow(float width, float height, const(char)[] title = "Popka", Color color = defaultBackgroundColor) { - popkaState = PopkaState(); + if (popkaState.isWindowOpen) { + return; + } ray.SetConfigFlags(ray.FLAG_VSYNC_HINT | ray.FLAG_WINDOW_RESIZABLE); ray.SetTraceLogLevel(ray.LOG_ERROR); ray.InitWindow(cast(int) width, cast(int) height, toStrz(title)); @@ -515,8 +517,12 @@ void closeWindow() { } void freeWindow() { + if (!popkaState.isWindowOpen) { + return; + } popkaState.view.free(); ray.CloseWindow(); + popkaState = PopkaState(); } bool isWindowOpen() {