Working dialogue actions handling

This commit is contained in:
3wc 2024-02-13 03:27:07 -03:00
parent 1d8222a395
commit da1f1f1e2a
4 changed files with 76 additions and 21 deletions

View File

@ -3,8 +3,9 @@ title: Intro
You: It's been 3 earth-days since I woke up from cryostasis, and I still don't know what's going on.
You: Why is nobody else awake?
You: What happened to the updates from mission control?
-> Wow, some options!
You got it, pal!
-> Can I put text inside options?
You sure can!
-> Maybe I'm still asleep?
You: Surely the scriptwriting isn't *that* cliché...
-> Is this even still the same ship?
You: Best not get too philsophical just yet
You: Let's have another look around...
===

View File

@ -8,13 +8,19 @@ import hxyarn.dialogue.OptionSet;
import hxyarn.compiler.Compiler;
import hxyarn.compiler.CompilationJob;
typedef Option = {
text:String,
callback:() -> Void
};
class DialogueManager {
var storage = new MemoryVariableStore();
var dialogue:Dialogue;
var stringTable:Map<String, StringInfo>;
var callback:(DialogueManager, String) -> Void;
var dialogueCallback:(DialogueManager, String) -> Void;
var optionCallback:(DialogueManager, Array<Option>) -> Void;
public function new(lineHandlerCallback:(DialogueManager, String) -> Void) {
public function new(_dialogueCallback:(DialogueManager, String) -> Void, _optionCallback:(DialogueManager, Array<Option>) -> Void) {
dialogue = new Dialogue(new MemoryVariableStore());
dialogue.logDebugMessage = this.logDebugMessage;
@ -26,7 +32,8 @@ class DialogueManager {
dialogue.nodeStartHandler = this.nodeStartHandler;
dialogue.dialogueCompleteHandler = this.dialogueCompleteHandler;
callback = lineHandlerCallback;
dialogueCallback = _dialogueCallback;
optionCallback = _optionCallback;
}
public function load(text:Array<String>, fileNames:Array<String>) {
@ -59,21 +66,28 @@ class DialogueManager {
public function lineHandler(line:Line):HandlerExecutionType {
var text = getComposedTextForLine(line);
callback(this, text);
dialogueCallback(this, text);
return HandlerExecutionType.ContinueExecution;
}
public function optionsHandler(options:OptionSet) {
var optionCount = options.options.length;
var optionText = new Array<String>();
var optionChoices = new Array<Option>();
for (option in options.options) {
var text = getComposedTextForLine(option.line);
optionText.push(text);
for (i => option in options.options) {
var text = getComposedTextForLine(option.line);
optionChoices.push({
text: text,
callback: function() {
dialogue.setSelectedOption(i);
dialogue.resume();
}
});
}
// show options
optionCallback(this, optionChoices);
}
public function getComposedTextForLine(line:Line):String {

View File

@ -1,13 +1,15 @@
import Const;
import DialogueBox;
import DialogueManager;
import DialogueManager.Option;
@:publicFields
class Game extends hxd.App {
public var scene:h2d.Scene;
public var font:h2d.Font;
public var world:World;
public var curDialog:h2d.Object;
public var curDialogue:h2d.Object;
public var actions:Array<h2d.ScaleGrid> = [];
public var dialogueManager:DialogueManager;
override function init() {
@ -19,13 +21,13 @@ class Game extends hxd.App {
font = Res.font.minecraftia_regular_6.toFont();
font.resizeTo(24);
dialogueManager = new DialogueManager(dialogue);
dialogueManager = new DialogueManager(dialogue, renderActions);
var yarnText = [
hxd.Res.text.encounters.entry.getText(),
hxd.Res.text.intro.entry.getText(),
];
var yarnFileNames = [
hxd.Res.text.encounters.entry.name,
hxd.Res.text.intro.entry.name,
];
dialogueManager.load(yarnText, yarnFileNames);
dialogueManager.runNode("Intro");
@ -40,6 +42,44 @@ class Game extends hxd.App {
manager.resume();
};
curDialog = d;
curDialogue = d;
}
function renderActions(manager:DialogueManager, t:Array<Option>) {
for (action in t) {
addAction(action.text, action.callback);
}
}
function newPanel(w, h) {
var g = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5);
g.width = w;
g.height = h;
g.colorKey = 0xFF00FF;
scene.add(g, 1);
return g;
}
function addAction(a: String, callback: () -> Void) {
var spr = newPanel(Const.W, 60);
curDialogue.addChild(spr);
var tf = new h2d.Text(font, spr);
tf.text = a;
tf.x = 10;
tf.y = 10;
spr.x = 0;
spr.y = 100 + actions.length * 60;
var int = new h2d.Interactive(spr.width, spr.height, spr);
int.onClick = function(_) {
for (action in actions) {
action.remove();
}
callback();
// removeDialog();
}
int.cursor = Button;
actions.push(spr);
return spr;
}
}

View File

@ -47,9 +47,9 @@ class Main extends hxd.App {
root.btnContinueGame.onClick = function() {
root.btnContinueGame.dom.addClass("highlight");
}
root.btnQuit.onClick = function() {
Sys.exit(0);
}
// root.btnQuit.onClick = function() {
// Sys.exit(0);
// }
style = new h2d.domkit.Style();
style.load(hxd.Res.style);