From 87302a37135dd75f753577fd2fa9720c83fa0e1a Mon Sep 17 00:00:00 2001 From: 3wc <3wc@doesthisthing.work> Date: Wed, 14 Feb 2024 00:21:02 -0300 Subject: [PATCH] Add EventBus and GameScene from ecs --- res/style.css | 12 +- src/DialogueBox.hx | 4 +- src/Game.hx | 209 ++++++++++++++++------- src/Main.hx | 50 +----- src/event/ChangeSceneEvent.hx | 11 ++ src/event/EventBus.hx | 47 +++++ src/event/IEvent.hx | 3 + src/scene/GameScene.hx | 5 + src/scene/TitleScene.hx | 55 ++++++ src/{World.hx => scene/WorldMapScene.hx} | 31 ++-- src/screens/TitleScreen.hx | 33 ---- 11 files changed, 297 insertions(+), 163 deletions(-) create mode 100644 src/event/ChangeSceneEvent.hx create mode 100644 src/event/EventBus.hx create mode 100644 src/event/IEvent.hx create mode 100644 src/scene/GameScene.hx create mode 100644 src/scene/TitleScene.hx rename src/{World.hx => scene/WorldMapScene.hx} (72%) delete mode 100644 src/screens/TitleScreen.hx diff --git a/res/style.css b/res/style.css index 5dac9ab..df632ff 100644 --- a/res/style.css +++ b/res/style.css @@ -1,13 +1,21 @@ +text { + font : url("res/font/minecraftia_regular_6.fnt") +} + +game-container { + background: blue; +} + .mybox { background: #080; padding: -10; } -view.mybox { +heading.mybox { padding: 10; } -title-screen { +title-scene { max-width: 300; layout: vertical; } diff --git a/src/DialogueBox.hx b/src/DialogueBox.hx index dc40ac8..e00901c 100644 --- a/src/DialogueBox.hx +++ b/src/DialogueBox.hx @@ -16,8 +16,8 @@ class DialogueBox extends h2d.Object { super(); if (text == null) text = "NULL"; - game = Game.inst; - game.scene.add(this, 1); + game = Game.current; + game.root.add(this, 1); bg = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5, this); bg.colorKey = 0xFF00FF; tf = new h2d.Text(game.font, this); diff --git a/src/Game.hx b/src/Game.hx index 2b9eaaa..6ad44ca 100644 --- a/src/Game.hx +++ b/src/Game.hx @@ -1,85 +1,164 @@ -import Const; -import DialogueBox; -import DialogueManager; -import DialogueManager.Option; +import h2d.Layers; +import h2d.Console; + +import scene.TitleScene; +import scene.WorldMapScene; +import scene.GameScene; +import event.EventBus; +import event.ChangeSceneEvent; + +import Const; +// import DialogueBox; +// import DialogueManager; +// import DialogueManager.Option; + +@:uiComp("game-container") +class GameContainer extends h2d.Flow implements h2d.domkit.Object { + static var SRC = + ${scene} + ; + + public var scene:GameScene; + + public function new(?parent) { + super(parent); + initComponent(); + } +} -@:publicFields class Game extends hxd.App { - public var scene:h2d.Scene; public var font:h2d.Font; - public var world:World; public var curDialogue:h2d.Object; public var actions:Array = []; public var dialogueManager:DialogueManager; + public var globalEventBus:EventBus; + public static var current:Game; // Current Game (singleton) + + // var layer:Layers; + public var root:GameContainer; + var console:Console; + var style = null; + override function init() { - scene = s2d; s2d.scaleMode = Stretch(Const.W, Const.H + 12); - world = new World(Res.map, Res.tiles); - s2d.add(world.root, 0); - font = Res.font.minecraftia_regular_6.toFont(); - font.resizeTo(24); + #if hl + hxd.res.Resource.LIVE_UPDATE = true; + hxd.Res.initLocal(); + #else + hxd.Res.initEmbed(); + #end - dialogueManager = new DialogueManager(dialogue, renderActions); + root = new GameContainer(); + root.horizontalAlign = root.verticalAlign = Middle; + s2d.add(root); + onResize(); - var yarnText = [ - hxd.Res.text.intro.entry.getText(), - ]; - var yarnFileNames = [ - hxd.Res.text.intro.entry.name, - ]; - dialogueManager.load(yarnText, yarnFileNames); - dialogueManager.runNode("Intro"); + style = new h2d.domkit.Style(); + style.load(hxd.Res.style); + style.allowInspect = true; + style.addObject(root); + + globalEventBus = new EventBus(console); + globalEventBus.subscribe(ChangeSceneEvent, onChangeScene); + + #if debug + setGameScene(new WorldMapScene(root)); + #else + setGameScene(new TitleScene(root)); + #end + + // font = Res.font.minecraftia_regular_6.toFont(); + // font.resizeTo(24); + // + // dialogueManager = new DialogueManager(dialogue, renderActions); + // + // var yarnText = [ + // hxd.Res.text.intro.entry.getText(), + // ]; + // var yarnFileNames = [ + // hxd.Res.text.intro.entry.name, + // ]; + // dialogueManager.load(yarnText, yarnFileNames); + // dialogueManager.runNode("Intro"); } - public static var inst:Game; - - function dialogue(manager:DialogueManager, t:String) { - var d = new DialogueBox(Const.W, 100, t); - d.y = 0; - d.onClick = function() { - manager.resume(); - }; - - curDialogue = d; + public function new() { + super(); + current = this; } - function renderActions(manager:DialogueManager, t:Array