Compare commits

...

3 Commits

Author SHA1 Message Date
3wc 021aac9914 Make dialogue system work (hackily) with domkit 2024-02-14 12:13:29 -03:00
3wc 477b57e2a7 Build in debug mode 2024-02-14 00:21:23 -03:00
3wc 87302a3713 Add EventBus and GameScene from ecs 2024-02-14 00:21:02 -03:00
12 changed files with 244 additions and 107 deletions

View File

@ -10,8 +10,11 @@ bin/rewild.hl: $(SOURCES)
bin/rewild.js: $(SOURCES)
haxe compile-js.hxml
debug: $(SOURCES)
haxe --debug compile-hl.hxml
clean:
rm -f bin/rewild.hl bin/rewild.js
watch:
@inotifywait --recursive --monitor --event modify,move,create,delete --include '.*.hx$$' ./ | while read changed; do echo $$changed; make; done
@inotifywait --recursive --monitor --event modify,move,create,delete --include '.*.hx$$' ./ | while read changed; do echo $$changed; make debug; done

View File

@ -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;
}

View File

@ -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.scene.getH2dObject().getScene().add(this, 1);
bg = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5, this);
bg.colorKey = 0xFF00FF;
tf = new h2d.Text(game.font, this);

View File

@ -1,22 +1,73 @@
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;
@:publicFields
@: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 scene:h2d.Scene;
public var font:h2d.Font;
public var world:World;
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() {
scene = s2d;
s2d.scaleMode = Stretch(Const.W, Const.H + 12);
world = new World(Res.map, Res.tiles);
s2d.add(world.root, 0);
#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);
@ -33,7 +84,19 @@ class Game extends hxd.App {
dialogueManager.runNode("Intro");
}
public static var inst:Game;
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);
@ -56,7 +119,7 @@ class Game extends hxd.App {
g.width = w;
g.height = h;
g.colorKey = 0xFF00FF;
scene.add(g, 1);
root.scene.getH2dObject().getScene().add(g, 1);
return g;
}
@ -82,4 +145,20 @@ class Game extends hxd.App {
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());
}
}

View File

@ -1,51 +1,5 @@
import screens.TitleScreen;
class Main extends hxd.App {
var center:h2d.Flow;
var style = null;
override function init() {
center = new h2d.Flow(s2d);
center.horizontalAlign = center.verticalAlign = Middle;
onResize();
var root = new TitleScreen(Right, center);
root.btnNewGame.onClick = function() {
Game.inst = new Game();
}
root.btnContinueGame.onClick = function() {
root.btnContinueGame.dom.addClass("highlight");
}
#if hl
root.btnQuit.onClick = function() {
Sys.exit(0);
}
#else
root.btnQuit.remove();
#end
style = new h2d.domkit.Style();
style.load(hxd.Res.style);
style.allowInspect = true;
style.addObject(root);
}
override function onResize() {
center.minWidth = center.maxWidth = s2d.width;
center.minHeight = center.maxHeight = s2d.height;
}
override function update(dt:Float) {
style.sync();
}
class Main {
static function main() {
#if hl
hxd.res.Resource.LIVE_UPDATE = true;
hxd.Res.initLocal();
#else
hxd.Res.initEmbed();
#end
new Main();
new Game();
}
}

View File

@ -0,0 +1,11 @@
package event;
import scene.GameScene;
class ChangeSceneEvent implements IEvent {
public var newScene:GameScene;
public function new(gs:GameScene) {
this.newScene = gs;
}
}

47
src/event/EventBus.hx Normal file
View File

@ -0,0 +1,47 @@
package event;
import h2d.Console;
class EventBus {
var listeners = new Map<String, Array<Dynamic>>();
var console:Console;
public function new(console:Console) {
this.console = console;
}
public function subscribe<T:IEvent>(event:Class<T>, callback:(T) -> Void) {
var type = Type.getClassName(event);
if (!listeners.exists(type))
listeners.set(type, new Array<T>());
listeners.get(type).push(callback);
}
public function unsubscribe<T:IEvent>(event:Class<T>, callback:(T) -> Void) {
var type = Type.getClassName(event);
if (!listeners.exists(type))
return;
var callbacks = listeners.get(type);
for (c in callbacks) {
if (Reflect.compareMethods(c, callback)) {
callbacks.remove(c);
return;
}
}
}
public function publishEvent<T:IEvent>(event:T) {
var type = Type.getClassName(Type.getClass(event));
if (!listeners.exists(type)) {
#if debug
console.log('Publishing event with no listeners: $type');
#end
return;
}
for (func in listeners.get(type))
func(event);
}
}

3
src/event/IEvent.hx Normal file
View File

@ -0,0 +1,3 @@
package event;
interface IEvent {}

5
src/scene/GameScene.hx Normal file
View File

@ -0,0 +1,5 @@
package scene;
interface GameScene {
function getH2dObject():h2d.Object;
}

55
src/scene/TitleScene.hx Normal file
View File

@ -0,0 +1,55 @@
package scene;
import scene.WorldMapScene;
import event.ChangeSceneEvent;
import components.MenubuttonComponent;
@:uiComp("heading")
class HeadingComp extends h2d.Flow implements h2d.domkit.Object {
static var SRC = <heading class="mybox" min-width="200">
<text text={"Glass House"}/>
for( i in icons )
<bitmap src={i} id="icons[]"/>
</heading>;
public function new(icons:Array<h2d.Tile>, ?parent) {
super(parent);
initComponent();
}
}
@:uiComp("title-scene")
class TitleScene extends h2d.Flow implements GameScene implements h2d.domkit.Object {
static var SRC = <title-scene>
<heading([]) id="view"/>
<menubutton("New game") public id="btnNewGame"/>
<menubutton("Continue game") public id="btnContinueGame"/>
<menubutton("Credits") public id="btnCredits"/>
<menubutton("Quit") public id="btnQuit"/>
</title-scene>;
public function new(?parent) {
super(parent);
initComponent();
btnNewGame.onClick = function() {
Game.current.globalEventBus.publishEvent(new ChangeSceneEvent(new WorldMapScene(Game.current.root)));
}
btnContinueGame.onClick = function() {
btnContinueGame.dom.addClass("highlight");
}
#if hl
btnQuit.onClick = function() {
Sys.exit(0);
}
#else
btnQuit.remove();
#end
}
public function getH2dObject() {
return this;
}
}

View File

@ -1,8 +1,8 @@
class World {
var game:Game;
var map:hxd.res.TiledMap.TiledMapData;
package scene;
public var root:h2d.Object;
@:uiComp("world-map-scene")
class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.Object {
var map:hxd.res.TiledMap.TiledMapData;
var layers:Map<String, {
name:String,
@ -15,16 +15,17 @@ class World {
public var width:Int;
public var height:Int;
public function new(r:hxd.res.TiledMap, tiles:hxd.res.Image) {
game = Game.inst;
root = new h2d.Object();
map = r.toMap();
root = new h2d.Object();
public function new(?parent) {
super(parent);
initComponent();
var root = new h2d.Object(this);
map = Res.map.toMap();
width = map.width;
height = map.height;
var t = tiles.toTile();
var t = Res.tiles.toTile();
layers = new Map();
var font:h2d.Font = hxd.res.DefaultFont.get();
this.tiles = [
for (y in 0...Std.int(t.height) >> 5) for (x in 0...Std.int(t.width) >> 5) t.sub(x * 32, y * 32, 32, 32)
];
@ -37,13 +38,16 @@ class World {
g: new h2d.TileGroup(t, root),
alpha: ld.opacity,
}
// l.g.colorKey = 0x1D8700;
l.g.alpha = ld.opacity;
layers.set(ld.name, l);
rebuildLayer(ld.name);
}
}
public function getH2dObject() {
return this;
}
function rebuildLayer(name:String) {
var l = layers.get(name);
if (l == null)
@ -51,8 +55,9 @@ class World {
var pos = 0;
var g = l.g;
g.clear();
while (g.numChildren > 0)
while (g.numChildren > 0) {
g.getChildAt(0).remove();
}
for (y in 0...height)
for (x in 0...width) {

View File

@ -1,33 +0,0 @@
package screens;
import components.MenubuttonComponent;
@:uiComp("heading")
class HeadingComp extends h2d.Flow implements h2d.domkit.Object {
static var SRC = <heading class="mybox" min-width="200" content-halign={align}>
<text text={"Rewild"}/>
for( i in icons )
<bitmap src={i} id="icons[]"/>
</heading>;
public function new(align:h2d.Flow.FlowAlign, icons:Array<h2d.Tile>, ?parent) {
super(parent);
initComponent();
}
}
@:uiComp("title-screen")
class TitleScreen extends h2d.Flow implements h2d.domkit.Object {
static var SRC = <title-screen>
<heading(align,[]) id="view"/>
<menubutton("New game") public id="btnNewGame"/>
<menubutton("Continue game") public id="btnContinueGame"/>
<menubutton("Credits") public id="btnCredits"/>
<menubutton("Quit") public id="btnQuit"/>
</title-screen>;
public function new(align:h2d.Flow.FlowAlign, ?parent) {
super(parent);
initComponent();
}
}