Worked on dialogue system and added example for it.

This commit is contained in:
Kapendev 2024-03-10 17:58:07 +02:00
parent b520185cf2
commit 67b9070a15
5 changed files with 70 additions and 36 deletions

View file

@ -12,7 +12,6 @@ import popka.basic;
void main() {
openWindow(640, 480);
lockResolution(320, 180);
while (isWindowOpen) {
drawDebugText("Hello world!");
}
@ -28,6 +27,7 @@ Installing raylib is easy, and the official raylib instructions will guide you t
## Installation
This guide outlines the steps to install Popka using Git and the Dub package manager.
It is not recommended to use Dub for now.
1. **Clone the Popka repository:**

View file

@ -9,11 +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;
void runEveryExample() {
runHelloExample();
runCoinsExample();
runCameraExample();
runStoryExample();
}
unittest {}

View file

@ -10,7 +10,6 @@ import popka.basic;
void runHelloExample() {
openWindow(640, 480);
lockResolution(320, 180);
while (isWindowOpen) {
drawDebugText("Hello world!");
}

55
example/story.d Normal file
View file

@ -0,0 +1,55 @@
// 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();
}

View file

@ -43,6 +43,8 @@ struct DialogueUnit {
struct Dialogue {
List!DialogueUnit units;
size_t unitIndex;
const(char)[] actor;
const(char)[] content;
this(const(char)[] path) {
load(path);
@ -52,13 +54,22 @@ struct Dialogue {
return units[unitIndex];
}
void reset() {
unitIndex = 0;
}
void update() {
if (units.length != 0 && unitIndex < units.length - 1) {
unitIndex += 1;
auto unit = units[unitIndex];
content = unit.content.items;
if (unit.isOneOf(DialogueUnit.comment, DialogueUnit.point)) {
update();
}
if (unit.kind == DialogueUnit.actor) {
actor = unit.content.items;
update();
}
if (unit.kind == DialogueUnit.target) {
foreach (i, item; units.items) {
if (item.kind == DialogueUnit.point && item.content.items == unit.content.items) {
@ -120,37 +131,4 @@ struct Dialogue {
}
}
unittest {
auto text = "
# This is a comment.
> Actor1
| First line.
| Second line.
> Actor2
| First line.
-
* Point
> Actor3
| This is a loop.
@ Point
";
import popka.basic;
auto dialogue = Dialogue();
dialogue.parse(text);
dialogue.update();
openWindow(500, 500);
lockResolution(200, 200);
foreach (i, u; dialogue.units) {
println(i, ": ", u.kind, "|", u.content.items);
}
println("--- gane");
while (isWindowOpen) {
if (Keyboard.space.isPressed && dialogue.canUpdate && dialogue.now.kind != DialogueUnit.pause) {
auto unit = dialogue.now;
println(unit.kind, " | ", unit.content.items);
dialogue.update();
}
}
}
unittest {}