mirror of
https://github.com/Kapendev/parin.git
synced 2025-04-26 21:19:56 +03:00
Fixed dialogue bug that skiped pause lines.
This commit is contained in:
parent
67b9070a15
commit
57903d7954
5 changed files with 67 additions and 64 deletions
|
@ -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 {}
|
||||
|
|
50
example/dialogue.d
Normal file
50
example/dialogue.d
Normal file
|
@ -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();
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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) {
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue