glasshouse/src/Game.hx

165 lines
3.4 KiB
Haxe

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 = <game-container>
${scene}
</game-container>;
public var scene:GameScene;
public function new(?parent) {
super(parent);
initComponent();
}
}
class Game extends hxd.App {
public var font:h2d.Font;
public var curDialogue:h2d.Object;
public var actions:Array<h2d.ScaleGrid> = [];
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() {
s2d.scaleMode = Stretch(Const.W, Const.H + 12);
#if hl
hxd.res.Resource.LIVE_UPDATE = true;
hxd.Res.initLocal();
#else
hxd.Res.initEmbed();
#end
root = new GameContainer();
root.horizontalAlign = root.verticalAlign = Middle;
s2d.add(root);
onResize();
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 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();
}
function dialogue(manager:DialogueManager, t:String) {
var d = new DialogueBox(Const.W, 100, t);
d.y = 0;
d.onClick = function() {
manager.resume();
};
curDialogue = d;
}
function renderActions(manager:DialogueManager, t:Array<Option>) {
for (action in t) {
addAction(action.text, action.callback);
}
}
function newPanel(w, h) {
var g = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5);
g.width = w;
g.height = h;
g.colorKey = 0xFF00FF;
root.scene.getH2dObject().getScene().add(g, 1);
return g;
}
function addAction(a: String, callback: () -> Void) {
var spr = newPanel(Const.W, 60);
curDialogue.addChild(spr);
var tf = new h2d.Text(font, spr);
tf.text = a;
tf.x = 10;
tf.y = 10;
spr.x = 0;
spr.y = 100 + actions.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);
return spr;
}
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());
}
}