omg working yarnspinner dialogue 😍
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
class Dialogue extends h2d.Object {
|
||||
class DialogueBox extends h2d.Object {
|
||||
var game:Game;
|
||||
var bg:h2d.ScaleGrid;
|
||||
var tf:h2d.Text;
|
||||
@ -12,11 +12,8 @@ class Dialogue extends h2d.Object {
|
||||
var textPos:Int = 0;
|
||||
var chan:hxd.snd.Channel;
|
||||
|
||||
// public function new(width:Int, height:Int, text : String,sfx:hxd.res.Sound) {
|
||||
public function new(width:Int, height:Int, text:String) {
|
||||
super();
|
||||
// if( sfx != null )
|
||||
// chan = sfx.play(true);
|
||||
if (text == null)
|
||||
text = "NULL";
|
||||
game = Game.inst;
|
||||
@ -24,8 +21,6 @@ class Dialogue extends h2d.Object {
|
||||
bg = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5, this);
|
||||
bg.colorKey = 0xFF00FF;
|
||||
tf = new h2d.Text(game.font, this);
|
||||
tf.scaleX = 2;
|
||||
tf.scaleY = 2;
|
||||
tf.y = 5;
|
||||
tf.x = 7;
|
||||
tf.dropShadow = {
|
112
src/DialogueManager.hx
Normal file
112
src/DialogueManager.hx
Normal file
@ -0,0 +1,112 @@
|
||||
import hxyarn.dialogue.Dialogue;
|
||||
import hxyarn.dialogue.VariableStorage.MemoryVariableStore;
|
||||
import hxyarn.dialogue.StringInfo;
|
||||
import hxyarn.dialogue.Line;
|
||||
import hxyarn.dialogue.Command;
|
||||
import hxyarn.dialogue.Option;
|
||||
import hxyarn.dialogue.OptionSet;
|
||||
import hxyarn.compiler.Compiler;
|
||||
import hxyarn.compiler.CompilationJob;
|
||||
|
||||
class DialogueManager {
|
||||
var storage = new MemoryVariableStore();
|
||||
var dialogue:Dialogue;
|
||||
var stringTable:Map<String, StringInfo>;
|
||||
var callback:(DialogueManager, String) -> Void;
|
||||
|
||||
public function new(lineHandlerCallback:(DialogueManager, String) -> Void) {
|
||||
dialogue = new Dialogue(new MemoryVariableStore());
|
||||
|
||||
dialogue.logDebugMessage = this.logDebugMessage;
|
||||
dialogue.logErrorMessage = this.logErrorMessage;
|
||||
dialogue.lineHandler = this.lineHandler;
|
||||
dialogue.optionsHandler = this.optionsHandler;
|
||||
dialogue.commandHandler = this.commandHandler;
|
||||
dialogue.nodeCompleteHandler = this.nodeCompleteHandler;
|
||||
dialogue.nodeStartHandler = this.nodeStartHandler;
|
||||
dialogue.dialogueCompleteHandler = this.dialogueCompleteHandler;
|
||||
|
||||
callback = lineHandlerCallback;
|
||||
}
|
||||
|
||||
public function load(text:Array<String>, fileNames:Array<String>) {
|
||||
var job = CompilationJob.createFromStrings(text, fileNames, dialogue.library);
|
||||
var compiler = Compiler.compile(job);
|
||||
stringTable = compiler.stringTable;
|
||||
|
||||
dialogue.addProgram(compiler.program);
|
||||
}
|
||||
|
||||
public function runNode(nodeName:String) {
|
||||
dialogue.setNode(nodeName);
|
||||
dialogue.resume();
|
||||
}
|
||||
|
||||
public function unload() {
|
||||
dialogue.unloadAll();
|
||||
}
|
||||
|
||||
public function resume() {
|
||||
dialogue.resume();
|
||||
}
|
||||
|
||||
public function logDebugMessage(message:String):Void {
|
||||
}
|
||||
|
||||
public function logErrorMessage(message:String):Void {
|
||||
}
|
||||
|
||||
public function lineHandler(line:Line):HandlerExecutionType {
|
||||
var text = getComposedTextForLine(line);
|
||||
|
||||
callback(this, text);
|
||||
|
||||
return HandlerExecutionType.ContinueExecution;
|
||||
}
|
||||
|
||||
public function optionsHandler(options:OptionSet) {
|
||||
var optionCount = options.options.length;
|
||||
var optionText = new Array<String>();
|
||||
|
||||
for (option in options.options) {
|
||||
var text = getComposedTextForLine(option.line);
|
||||
optionText.push(text);
|
||||
}
|
||||
|
||||
// show options
|
||||
}
|
||||
|
||||
public function getComposedTextForLine(line:Line):String {
|
||||
var substitutedText = Dialogue.expandSubstitutions(stringTable[line.id].text, line.substitutions);
|
||||
|
||||
var markup = dialogue.parseMarkup(substitutedText);
|
||||
|
||||
return markup.text;
|
||||
}
|
||||
|
||||
public function commandHandler(command:Command) {
|
||||
}
|
||||
|
||||
public function nodeCompleteHandler(nodeName:String) {}
|
||||
|
||||
public function nodeStartHandler(nodeName:String) {}
|
||||
|
||||
public function dialogueCompleteHandler() {
|
||||
}
|
||||
|
||||
public function getNodeNames(?includesTag = "") {
|
||||
var nodeNames = [];
|
||||
for (nodeName in dialogue.allNodes) {
|
||||
if (includesTag == "") {
|
||||
nodeNames.push(nodeName);
|
||||
continue;
|
||||
}
|
||||
|
||||
var tags = dialogue.getTagsForNode(nodeName);
|
||||
if (tags.contains(includesTag)) {
|
||||
nodeNames.push(nodeName);
|
||||
}
|
||||
}
|
||||
return nodeNames;
|
||||
}
|
||||
}
|
36
src/Game.hx
36
src/Game.hx
@ -1,4 +1,6 @@
|
||||
import Const;
|
||||
import DialogueBox;
|
||||
import DialogueManager;
|
||||
|
||||
@:publicFields
|
||||
class Game extends hxd.App {
|
||||
@ -6,32 +8,38 @@ class Game extends hxd.App {
|
||||
public var font:h2d.Font;
|
||||
public var world:World;
|
||||
public var curDialog:h2d.Object;
|
||||
public var dialogueManager:DialogueManager;
|
||||
|
||||
override function init() {
|
||||
scene = s2d;
|
||||
s2d.setFixedSize(Const.W, Const.H + 12);
|
||||
s2d.scaleMode = Stretch(Const.W, Const.H + 12);
|
||||
world = new World(Res.map, Res.tiles);
|
||||
s2d.add(world.root, 0);
|
||||
font = Res.minecraftia_regular_6.toFont();
|
||||
|
||||
dialog(["(you slowly wake up)", "...", "where am I?", "(the cold vastness of spaaaaace)"]);
|
||||
font = Res.font.minecraftia_regular_6.toFont();
|
||||
font.resizeTo(24);
|
||||
|
||||
dialogueManager = new DialogueManager(dialogue);
|
||||
|
||||
var yarnText = [
|
||||
hxd.Res.text.encounters.entry.getText(),
|
||||
];
|
||||
var yarnFileNames = [
|
||||
hxd.Res.text.encounters.entry.name,
|
||||
];
|
||||
dialogueManager.load(yarnText, yarnFileNames);
|
||||
dialogueManager.runNode("Intro");
|
||||
}
|
||||
|
||||
public static var inst:Game;
|
||||
|
||||
function dialog(t:Array<String>) {
|
||||
if (t.length == 0) {
|
||||
return;
|
||||
}
|
||||
var d = new Dialogue(Const.W, 100, t[0]);
|
||||
d.y = Const.H - d.height;
|
||||
function dialogue(manager:DialogueManager, t:String) {
|
||||
var d = new DialogueBox(Const.W, 100, t);
|
||||
d.y = 0;
|
||||
d.onClick = function() {
|
||||
d.remove();
|
||||
curDialog = null;
|
||||
var t2 = t.copy();
|
||||
t2.shift();
|
||||
dialog(t2);
|
||||
manager.resume();
|
||||
};
|
||||
|
||||
curDialog = d;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user