glasshouse/src/Game.hx

204 lines
4.6 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 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;
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();
}
}
class Game extends hxd.App {
public var font:h2d.Font;
public var curDialogue:h2d.Object;
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;
public var root:GameContainer;
public var console:Console;
var style = null;
public static var loader = new tiled.Tiled();
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;
// s2d.add(root);
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();
#if debug
console = new Console(font);
layer.addChildAt(console, 4);
#end
font.resizeTo(20);
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);
globalEventBus.subscribe(LineShown, dialogue);
globalEventBus.subscribe(OptionsShown, renderOptions);
globalEventBus.subscribe(OptionSelected, onOptionSelected);
globalEventBus.subscribe(DialogueComplete, onDialogueComplete);
dialogueManager.addCommandHandler(new OpenRoomCommand(globalEventBus));
#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();
}
function dialogue(event:LineShown) {
if (curDialogue != null) {
curDialogue.remove();
}
var d = new DialogueBox(Const.W, 100, event.line());
d.y = 0;
d.onClick = function() {
globalEventBus.publishEvent(new NextLine());
};
curDialogue = d;
}
function renderOptions(event:OptionsShown) {
for (option in event.options) {
addOption(option.text, option.enabled, function() {
if (option.enabled) {
globalEventBus.publishEvent(new OptionSelected(option.index));
}
});
}
}
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 addOption(o:String, enabled:Bool, callback:() -> Void) {
var spr = newPanel(Const.W, 60);
curDialogue.addChild(spr);
var tf = new h2d.Text(font, spr);
if (!enabled) {
tf.color = new Vector4(0.5, 0.5, 0.5, 1);
}
tf.text = o;
tf.x = 10;
tf.y = 10;
spr.x = 0;
spr.y = 100 + options.length * 60;
var int = new h2d.Interactive(spr.width, spr.height, spr);
int.onClick = function(_) {
callback();
}
int.cursor = Button;
options.push(spr);
return spr;
}
function onOptionSelected(event:OptionSelected) {
for (o in options) {
o.remove();
}
options.splice(0, options.length);
}
function onDialogueComplete(event:DialogueComplete) {
curDialogue.remove();
}
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();
}
}