Working dialogue actions handling
This commit is contained in:
parent
1d8222a395
commit
da1f1f1e2a
@ -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: 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: Why is nobody else awake?
|
||||||
You: What happened to the updates from mission control?
|
You: What happened to the updates from mission control?
|
||||||
-> Wow, some options!
|
-> Maybe I'm still asleep?
|
||||||
You got it, pal!
|
You: Surely the scriptwriting isn't *that* cliché...
|
||||||
-> Can I put text inside options?
|
-> Is this even still the same ship?
|
||||||
You sure can!
|
You: Best not get too philsophical just yet
|
||||||
|
You: Let's have another look around...
|
||||||
===
|
===
|
@ -8,13 +8,19 @@ import hxyarn.dialogue.OptionSet;
|
|||||||
import hxyarn.compiler.Compiler;
|
import hxyarn.compiler.Compiler;
|
||||||
import hxyarn.compiler.CompilationJob;
|
import hxyarn.compiler.CompilationJob;
|
||||||
|
|
||||||
|
typedef Option = {
|
||||||
|
text:String,
|
||||||
|
callback:() -> Void
|
||||||
|
};
|
||||||
|
|
||||||
class DialogueManager {
|
class DialogueManager {
|
||||||
var storage = new MemoryVariableStore();
|
var storage = new MemoryVariableStore();
|
||||||
var dialogue:Dialogue;
|
var dialogue:Dialogue;
|
||||||
var stringTable:Map<String, StringInfo>;
|
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 = new Dialogue(new MemoryVariableStore());
|
||||||
|
|
||||||
dialogue.logDebugMessage = this.logDebugMessage;
|
dialogue.logDebugMessage = this.logDebugMessage;
|
||||||
@ -26,7 +32,8 @@ class DialogueManager {
|
|||||||
dialogue.nodeStartHandler = this.nodeStartHandler;
|
dialogue.nodeStartHandler = this.nodeStartHandler;
|
||||||
dialogue.dialogueCompleteHandler = this.dialogueCompleteHandler;
|
dialogue.dialogueCompleteHandler = this.dialogueCompleteHandler;
|
||||||
|
|
||||||
callback = lineHandlerCallback;
|
dialogueCallback = _dialogueCallback;
|
||||||
|
optionCallback = _optionCallback;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function load(text:Array<String>, fileNames:Array<String>) {
|
public function load(text:Array<String>, fileNames:Array<String>) {
|
||||||
@ -59,21 +66,28 @@ class DialogueManager {
|
|||||||
public function lineHandler(line:Line):HandlerExecutionType {
|
public function lineHandler(line:Line):HandlerExecutionType {
|
||||||
var text = getComposedTextForLine(line);
|
var text = getComposedTextForLine(line);
|
||||||
|
|
||||||
callback(this, text);
|
dialogueCallback(this, text);
|
||||||
|
|
||||||
return HandlerExecutionType.ContinueExecution;
|
return HandlerExecutionType.ContinueExecution;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function optionsHandler(options:OptionSet) {
|
public function optionsHandler(options:OptionSet) {
|
||||||
var optionCount = options.options.length;
|
var optionCount = options.options.length;
|
||||||
var optionText = new Array<String>();
|
var optionChoices = new Array<Option>();
|
||||||
|
|
||||||
for (option in options.options) {
|
for (i => option in options.options) {
|
||||||
var text = getComposedTextForLine(option.line);
|
var text = getComposedTextForLine(option.line);
|
||||||
optionText.push(text);
|
optionChoices.push({
|
||||||
|
text: text,
|
||||||
|
callback: function() {
|
||||||
|
dialogue.setSelectedOption(i);
|
||||||
|
dialogue.resume();
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// show options
|
|
||||||
|
optionCallback(this, optionChoices);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getComposedTextForLine(line:Line):String {
|
public function getComposedTextForLine(line:Line):String {
|
||||||
|
50
src/Game.hx
50
src/Game.hx
@ -1,13 +1,15 @@
|
|||||||
import Const;
|
import Const;
|
||||||
import DialogueBox;
|
import DialogueBox;
|
||||||
import DialogueManager;
|
import DialogueManager;
|
||||||
|
import DialogueManager.Option;
|
||||||
|
|
||||||
@:publicFields
|
@:publicFields
|
||||||
class Game extends hxd.App {
|
class Game extends hxd.App {
|
||||||
public var scene:h2d.Scene;
|
public var scene:h2d.Scene;
|
||||||
public var font:h2d.Font;
|
public var font:h2d.Font;
|
||||||
public var world:World;
|
public var world:World;
|
||||||
public var curDialog:h2d.Object;
|
public var curDialogue:h2d.Object;
|
||||||
|
public var actions:Array<h2d.ScaleGrid> = [];
|
||||||
public var dialogueManager:DialogueManager;
|
public var dialogueManager:DialogueManager;
|
||||||
|
|
||||||
override function init() {
|
override function init() {
|
||||||
@ -19,13 +21,13 @@ class Game extends hxd.App {
|
|||||||
font = Res.font.minecraftia_regular_6.toFont();
|
font = Res.font.minecraftia_regular_6.toFont();
|
||||||
font.resizeTo(24);
|
font.resizeTo(24);
|
||||||
|
|
||||||
dialogueManager = new DialogueManager(dialogue);
|
dialogueManager = new DialogueManager(dialogue, renderActions);
|
||||||
|
|
||||||
var yarnText = [
|
var yarnText = [
|
||||||
hxd.Res.text.encounters.entry.getText(),
|
hxd.Res.text.intro.entry.getText(),
|
||||||
];
|
];
|
||||||
var yarnFileNames = [
|
var yarnFileNames = [
|
||||||
hxd.Res.text.encounters.entry.name,
|
hxd.Res.text.intro.entry.name,
|
||||||
];
|
];
|
||||||
dialogueManager.load(yarnText, yarnFileNames);
|
dialogueManager.load(yarnText, yarnFileNames);
|
||||||
dialogueManager.runNode("Intro");
|
dialogueManager.runNode("Intro");
|
||||||
@ -40,6 +42,44 @@ class Game extends hxd.App {
|
|||||||
manager.resume();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@ -47,9 +47,9 @@ class Main extends hxd.App {
|
|||||||
root.btnContinueGame.onClick = function() {
|
root.btnContinueGame.onClick = function() {
|
||||||
root.btnContinueGame.dom.addClass("highlight");
|
root.btnContinueGame.dom.addClass("highlight");
|
||||||
}
|
}
|
||||||
root.btnQuit.onClick = function() {
|
// root.btnQuit.onClick = function() {
|
||||||
Sys.exit(0);
|
// Sys.exit(0);
|
||||||
}
|
// }
|
||||||
|
|
||||||
style = new h2d.domkit.Style();
|
style = new h2d.domkit.Style();
|
||||||
style.load(hxd.Res.style);
|
style.load(hxd.Res.style);
|
||||||
|
Loading…
Reference in New Issue
Block a user