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

View File

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

View File

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