glasshouse/src/Game.hx

134 lines
3.1 KiB
Haxe

import h2d.Layers;
import h2d.Console;
import h3d.Vector4;
import scene.TitleScene;
import scene.WorldMapScene;
import scene.GameScene;
import event.EventBus;
import event.ChangeSceneEvent;
import Const;
import listeners.DialogueBoxController;
import dialogue.DialogueManager;
import dialogue.command.OpenRoomCommand;
@:uiComp("game-container")
class GameContainer extends h2d.Flow implements h2d.domkit.Object {
static var SRC = <game-container>
${scene}
</game-container>;
public var scene:GameScene;
public function new(?parent) {
super(parent);
initComponent();
this.maxWidth = Const.W;
this.minWidth = Const.W;
}
}
class Game extends hxd.App {
public static var loader = new tiled.Tiled();
public static var current:Game; // Current Game (singleton)
public var font:h2d.Font;
public var options:Array<h2d.ScaleGrid> = [];
public var dialogueManager:DialogueManager;
public var root:GameContainer;
public var console:Console;
public var globalEventBus:EventBus;
var dialogueBoxController:DialogueBoxController;
var layer:Layers;
var style = null;
override function init() {
s2d.scaleMode = Stretch(Const.W, Const.H);
layer = new Layers(s2d);
#if hl
hxd.res.Resource.LIVE_UPDATE = true;
hxd.Res.initLocal();
#else
hxd.Res.initEmbed();
#end
root = new GameContainer();
root.horizontalAlign = root.verticalAlign = Middle;
layer.addChildAt(root, 1);
onResize();
style = new h2d.domkit.Style();
style.load(hxd.Res.style);
style.allowInspect = true;
style.addObject(root);
font = Res.font.minecraftia_regular_6.toFont();
font.resizeTo(18);
#if debug
console = new Console(font);
layer.addChildAt(console, 4);
#end
globalEventBus = new EventBus(console);
globalEventBus.subscribe(ChangeSceneEvent, onChangeScene);
dialogueManager = new DialogueManager(globalEventBus);
var yarnText = [hxd.Res.text.intro.entry.getText(), hxd.Res.text.rooms.entry.getText(),];
var yarnFileNames = [hxd.Res.text.intro.entry.name, hxd.Res.text.rooms.entry.name,];
dialogueManager.load(yarnText, yarnFileNames);
dialogueManager.addCommandHandler(new OpenRoomCommand(globalEventBus));
var dialogueParent = new h2d.Flow();
dialogueParent.dom = domkit.Properties.create("flow", dialogueParent);
dialogueParent.dom.addClass('dialogue-container');
layer.addChildAt(dialogueParent, 2);
style.addObject(dialogueParent);
dialogueBoxController = new DialogueBoxController(globalEventBus, dialogueParent, style);
#if debug
setGameScene(new WorldMapScene(root));
#else
setGameScene(new TitleScene(root));
#end
}
public function new() {
super();
current = this;
}
override function onResize() {
root.minWidth = root.maxWidth = s2d.width;
root.minHeight = root.maxHeight = s2d.height;
}
override function update(dt:Float) {
style.sync();
}
public function onChangeScene(event:ChangeSceneEvent) {
setGameScene(event.newScene);
}
public function setGameScene(gs:GameScene) {
#if debug
console.resetCommands();
#end
if (root.scene != null) {
root.scene.getH2dObject().remove();
}
root.scene = gs;
style.addObject(gs.getH2dObject());
}
static function main() {
new Game();
}
}