Working debug console, fix dialogue bugs

This commit is contained in:
3wc 2024-02-15 01:54:01 -03:00
parent 8921f231bd
commit 05fc9156b0
10 changed files with 195 additions and 49 deletions

View File

@ -8,9 +8,14 @@ import event.EventBus;
import event.ChangeSceneEvent;
import Const;
import DialogueBox;
import DialogueManager;
import DialogueManager.Option;
import dialogue.DialogueBox;
import dialogue.DialogueManager;
import dialogue.event.LineShown;
import dialogue.event.OptionsShown;
import dialogue.event.OptionSelected;
import dialogue.event.DialogueComplete;
import dialogue.event.NextLine;
@:uiComp("game-container")
class GameContainer extends h2d.Flow implements h2d.domkit.Object {
@ -29,13 +34,13 @@ class GameContainer extends h2d.Flow implements h2d.domkit.Object {
class Game extends hxd.App {
public var font:h2d.Font;
public var curDialogue:h2d.Object;
public var actions:Array<h2d.ScaleGrid> = [];
public var options:Array<h2d.ScaleGrid> = [];
public var dialogueManager:DialogueManager;
public var globalEventBus:EventBus;
public static var current:Game; // Current Game (singleton)
// var layer:Layers;
var layer:Layers;
public var root:GameContainer;
var console:Console;
var style = null;
@ -60,13 +65,20 @@ class Game extends hxd.App {
style.allowInspect = true;
style.addObject(root);
font = Res.font.minecraftia_regular_6.toFont();
layer = new Layers(s2d);
#if debug
console = new Console(font);
layer.addChildAt(console, 4);
#end
font.resizeTo(20);
globalEventBus = new EventBus(console);
globalEventBus.subscribe(ChangeSceneEvent, onChangeScene);
font = Res.font.minecraftia_regular_6.toFont();
font.resizeTo(24);
dialogueManager = new DialogueManager(dialogue, renderActions);
dialogueManager = new DialogueManager(globalEventBus);
var yarnText = [
hxd.Res.text.intro.entry.getText(),
@ -76,6 +88,11 @@ class Game extends hxd.App {
];
dialogueManager.load(yarnText, yarnFileNames);
globalEventBus.subscribe(LineShown, dialogue);
globalEventBus.subscribe(OptionsShown, renderOptions);
globalEventBus.subscribe(OptionSelected, onOptionSelected);
globalEventBus.subscribe(DialogueComplete, onDialogueComplete);
#if debug
setGameScene(new WorldMapScene(root));
#else
@ -97,19 +114,24 @@ class Game extends hxd.App {
style.sync();
}
function dialogue(manager:DialogueManager, t:String) {
var d = new DialogueBox(Const.W, 100, t);
function dialogue(event:LineShown) {
if (curDialogue != null) {
curDialogue.remove();
}
var d = new DialogueBox(Const.W, 100, event.line());
d.y = 0;
d.onClick = function() {
manager.resume();
globalEventBus.publishEvent(new NextLine());
};
curDialogue = d;
}
function renderActions(manager:DialogueManager, t:Array<Option>) {
for (action in t) {
addAction(action.text, action.callback);
function renderOptions(event:OptionsShown) {
for (option in event.options) {
addOption(option.text, function() {
globalEventBus.publishEvent(new OptionSelected(option.index));
});
}
}
@ -122,27 +144,33 @@ class Game extends hxd.App {
return g;
}
function addAction(a: String, callback: () -> Void) {
function addOption(o: String, callback: () -> Void) {
var spr = newPanel(Const.W, 60);
curDialogue.addChild(spr);
var tf = new h2d.Text(font, spr);
tf.text = a;
tf.text = o;
tf.x = 10;
tf.y = 10;
spr.x = 0;
spr.y = 100 + actions.length * 60;
spr.y = 100 + options.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);
options.push(spr);
return spr;
}
function onOptionSelected(event:OptionSelected) {
for (o in options) {
o.remove();
}
}
function onDialogueComplete(event:DialogueComplete) {
curDialogue.remove();
}
public function onChangeScene(event:ChangeSceneEvent) {
setGameScene(event.newScene);
@ -150,7 +178,7 @@ class Game extends hxd.App {
public function setGameScene(gs:GameScene) {
#if debug
// console.resetCommands();
console.resetCommands();
#end
if (root.scene != null) {

View File

@ -1,3 +1,5 @@
package dialogue;
class DialogueBox extends h2d.Object {
var game:Game;
var bg:h2d.ScaleGrid;

View File

@ -1,3 +1,5 @@
package dialogue;
import hxyarn.dialogue.Dialogue;
import hxyarn.dialogue.VariableStorage.MemoryVariableStore;
import hxyarn.dialogue.StringInfo;
@ -5,22 +7,30 @@ import hxyarn.dialogue.Line;
import hxyarn.dialogue.Command;
import hxyarn.dialogue.Option;
import hxyarn.dialogue.OptionSet;
import hxyarn.dialogue.markup.MarkupParseResult;
import hxyarn.compiler.Compiler;
import hxyarn.compiler.CompilationJob;
typedef Option = {
text:String,
callback:() -> Void
};
import event.EventBus;
import dialogue.event.LineShown;
import dialogue.event.OptionsShown;
import dialogue.event.OptionsShown.OptionChoice;
import dialogue.event.OptionSelected;
import dialogue.event.NextLine;
import dialogue.event.StartDialogueNode;
import dialogue.event.DialogueComplete;
class DialogueManager {
public var eventBus:EventBus;
var storage = new MemoryVariableStore();
var dialogue:Dialogue;
var stringTable:Map<String, StringInfo>;
var dialogueCallback:(DialogueManager, String) -> Void;
var optionCallback:(DialogueManager, Array<Option>) -> Void;
public function new(_dialogueCallback:(DialogueManager, String) -> Void, _optionCallback:(DialogueManager, Array<Option>) -> Void) {
public var waitingForOption:Bool = false;
public function new(eventBus:EventBus) {
this.eventBus = eventBus;
dialogue = new Dialogue(new MemoryVariableStore());
dialogue.logDebugMessage = this.logDebugMessage;
@ -32,8 +42,11 @@ class DialogueManager {
dialogue.nodeStartHandler = this.nodeStartHandler;
dialogue.dialogueCompleteHandler = this.dialogueCompleteHandler;
dialogueCallback = _dialogueCallback;
optionCallback = _optionCallback;
eventBus.subscribe(NextLine, this.nextLine);
eventBus.subscribe(OptionSelected, this.optionSelected);
eventBus.subscribe(StartDialogueNode, function(event) {
this.runNode(event.node);
});
}
public function load(text:Array<String>, fileNames:Array<String>) {
@ -63,39 +76,45 @@ class DialogueManager {
public function logErrorMessage(message:String):Void {
}
public function lineHandler(line:Line):HandlerExecutionType {
public function nextLine(event:NextLine) {
if ((dialogue.isActive()) && (!waitingForOption))
dialogue.resume();
}
public function lineHandler(line:Line):Void {
var text = getComposedTextForLine(line);
eventBus.publishEvent(new LineShown(text));
}
dialogueCallback(this, text);
return HandlerExecutionType.ContinueExecution;
public function optionSelected(event:OptionSelected) {
dialogue.setSelectedOption(event.index);
dialogue.resume();
waitingForOption = false;
}
public function optionsHandler(options:OptionSet) {
var optionCount = options.options.length;
var optionChoices = new Array<Option>();
var optionChoices = new Array<OptionChoice>();
for (i => option in options.options) {
var text = getComposedTextForLine(option.line);
var markup = getComposedTextForLine(option.line);
optionChoices.push({
text: text,
callback: function() {
dialogue.setSelectedOption(i);
dialogue.resume();
}
text: markup.text,
index: i,
enabled: option.enabled,
markup: markup
});
}
eventBus.publishEvent(new OptionsShown(optionChoices));
optionCallback(this, optionChoices);
waitingForOption = true;
}
public function getComposedTextForLine(line:Line):String {
public function getComposedTextForLine(line:Line):MarkupParseResult {
var substitutedText = Dialogue.expandSubstitutions(stringTable[line.id].text, line.substitutions);
var markup = dialogue.parseMarkup(substitutedText);
return markup.text;
return dialogue.parseMarkup(substitutedText);
}
public function commandHandler(command:Command) {
@ -106,6 +125,7 @@ class DialogueManager {
public function nodeStartHandler(nodeName:String) {}
public function dialogueCompleteHandler() {
eventBus.publishEvent(new DialogueComplete(""));
}
public function getNodeNames(?includesTag = "") {

View File

@ -0,0 +1,11 @@
package dialogue.event;
import event.IEvent;
class DialogueComplete implements IEvent {
public var nodeName:String;
public function new(nodeName:String) {
this.nodeName = nodeName;
}
}

View File

@ -0,0 +1,36 @@
package dialogue.event;
import hxyarn.dialogue.markup.MarkupAttribute;
import hxyarn.dialogue.markup.MarkupParseResult;
import event.IEvent;
class LineShown implements IEvent {
public var markUpResults:MarkupParseResult;
public function new(markupParseResults:MarkupParseResult) {
this.markUpResults = markupParseResults;
}
public function line():String {
var text = markUpResults.text;
var characterAttribute = characterNameAttribute();
if (characterAttribute != null)
text = text.substr(characterAttribute.position + characterAttribute.length);
return text;
}
public function characterName():String {
var attribute = characterNameAttribute();
if (attribute != null)
return attribute.properties[0].value.stringValue;
return "";
}
function characterNameAttribute():MarkupAttribute {
return markUpResults.tryGetAttributeWithName("character");
}
}

View File

@ -0,0 +1,7 @@
package dialogue.event;
import event.IEvent;
class NextLine implements IEvent {
public function new() {}
}

View File

@ -0,0 +1,11 @@
package dialogue.event;
import event.IEvent;
class OptionSelected implements IEvent {
public var index:Int;
public function new(index:Int) {
this.index = index;
}
}

View File

@ -0,0 +1,19 @@
package dialogue.event;
import hxyarn.dialogue.markup.MarkupParseResult;
import event.IEvent;
typedef OptionChoice = {
text:String,
index:Int,
enabled:Bool,
markup:MarkupParseResult
}
class OptionsShown implements IEvent {
public var options:Array<OptionChoice>;
public function new(options:Array<OptionChoice>) {
this.options = options;
}
}

View File

@ -0,0 +1,11 @@
package dialogue.event;
import event.IEvent;
class StartDialogueNode implements IEvent {
public var node:String;
public function new(node:String) {
this.node = node;
}
}

View File

@ -34,6 +34,7 @@ class EventBus {
public function publishEvent<T:IEvent>(event:T) {
var type = Type.getClassName(Type.getClass(event));
trace(type);
if (!listeners.exists(type)) {
#if debug
console.log('Publishing event with no listeners: $type');