From c1b102860ebfd883b7ae31c2e0d256193c05d3a8 Mon Sep 17 00:00:00 2001
From: 3wc <3wc@doesthisthing.work>
Date: Fri, 16 Feb 2024 01:53:38 -0300
Subject: [PATCH] Clickable rooms with per-room dialogue
---
compile-common.hxml | 1 +
res/map.tmx | 2 +-
res/text/intro.yarn | 4 +
src/Game.hx | 4 +-
src/TilemapLayer.hx | 197 +++++++++++++++++++++++++++++++++++++
src/scene/WorldMapScene.hx | 83 ++++++----------
6 files changed, 238 insertions(+), 53 deletions(-)
create mode 100644 src/TilemapLayer.hx
diff --git a/compile-common.hxml b/compile-common.hxml
index a2b14e0..027a9bf 100644
--- a/compile-common.hxml
+++ b/compile-common.hxml
@@ -1,6 +1,7 @@
-cp src
--macro Init.setup()
-lib heaps:git:https://github.com/HeapsIO/heaps.git
+-lib tiledhx
-lib hxyarn
-main Main
-lib domkit
diff --git a/res/map.tmx b/res/map.tmx
index 54719f0..3fb0b86 100644
--- a/res/map.tmx
+++ b/res/map.tmx
@@ -21,6 +21,6 @@
-
+
diff --git a/res/text/intro.yarn b/res/text/intro.yarn
index 8d58e4b..817712b 100644
--- a/res/text/intro.yarn
+++ b/res/text/intro.yarn
@@ -9,3 +9,7 @@ You: What happened to the updates from mission control?
You: Best not get too philsophical just yet
You: Let's have another look around...
===
+title: Room:quarters
+---
+It's the crew quarters
+===
diff --git a/src/Game.hx b/src/Game.hx
index 6b5d244..f9ab794 100644
--- a/src/Game.hx
+++ b/src/Game.hx
@@ -42,9 +42,11 @@ class Game extends hxd.App {
var layer:Layers;
public var root:GameContainer;
- var console:Console;
+ 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);
diff --git a/src/TilemapLayer.hx b/src/TilemapLayer.hx
new file mode 100644
index 0000000..fe0b3b8
--- /dev/null
+++ b/src/TilemapLayer.hx
@@ -0,0 +1,197 @@
+import h2d.col.Point;
+import h2d.Object;
+import h2d.SpriteBatch;
+
+import tiled.types.TmxLayer;
+import tiled.types.TmxMap;
+using tiled.TiledTools;
+
+class TilemapLayer extends h2d.TileGroup {
+
+ public var map:TmxMap;
+ public var layer:TmxLayer;
+
+ public function new (map:TmxMap, layer:TmxLayer, ?parent:Object) {
+ this.map = map;
+ this.layer = layer;
+ super(parent);
+ for (c in layer.tileChunks) {
+ addChunk(c);
+ }
+ // TODO: Animations
+ }
+
+ // public function gidAt(x:Int, y:Int):Int {
+ // if (x < 0 || x >= layer.width || y < 0 || y >= layer.height) return 0;
+ // return layer.data.tiles[y * layer.width + x].gid;
+ // }
+ public function addChunk(chunk:TileChunk) {
+ var tw = map.tileWidth;
+ var th = map.tileHeight;
+ var ix = 0, iy = 0;
+ var tiles = chunk.tiles, i = 0;
+ var tsets = map.tilesets;
+ switch (map.orientation) {
+ case Orthogonal:
+ var cx = chunk.x * tw + layer.offsetX;
+ var cy = chunk.y * th + layer.offsetY;
+ while (i < tiles.length) {
+ var gid = tiles[i++];
+ if (gid != 0) {
+ var t = tsets.getImage(gid);
+ var x = cx + ix * tw;
+ var y = cy + iy * th + th - t.height;
+ if (gid.flippedHorizontally) addTransform(x + t.width, y, -1, 1, 0, t);
+ else if (gid.flippedVertically) addTransform(x, y + t.height, 1, -1, 0, t);
+ else if (gid.flippedDiagonally) addTransform(x + t.width, y + t.height, -1, -1, 0, t);
+ else add(x, y, t);
+ }
+ if (++ix == chunk.width) { iy++; ix = 0; }
+ }
+ case Isometric:
+ var isoW = tw >> 1;
+ var isoH = th >> 1;
+
+ var cx = (chunk.x - chunk.y) * isoW + layer.offsetX;
+ var cy = (chunk.x + chunk.y) * isoH + layer.offsetY;
+ while (i < tiles.length) {
+ var gid = tiles[i++];
+ if (gid != 0) {
+ var t = tsets.getImage(gid);
+ var x = cx + (ix - iy) * isoW;
+ var y = cy + (ix + iy) * isoH + th - t.height;
+ if (gid.flippedHorizontally) addTransform(x + t.width, y, -1, 1, 0, t);
+ else if (gid.flippedVertically) addTransform(x, y + t.height, 1, -1, 0, t);
+ else if (gid.flippedDiagonally) addTransform(x + t.width, y + t.height, -1, -1, 0, t);
+ else add(x, y, t);
+ }
+ if (++ix == chunk.width) { iy++; ix = 0; }
+ }
+ case Staggered(staggerYAxis, staggerIndexOdd):
+ var staggerX = 0, staggerY = 0, stepX = tw >> 1, stepY = th >> 1;
+ var cx = chunk.x * stepX + layer.offsetX;
+ var cy = chunk.y * stepY + layer.offsetY;
+ if (staggerYAxis) {
+ if (staggerIndexOdd) {
+ staggerX = tw >> 1;
+ } else {
+ staggerX = -(tw >> 1);
+ cx -= staggerX;
+ }
+ } else {
+ if (staggerIndexOdd) {
+ staggerY = th >> 1;
+ } else {
+ staggerY = -(tw >> 1);
+ cy -= staggerY;
+ }
+ }
+ cx += (chunk.x % 2) * staggerX;
+ cy += (chunk.y % 2) * staggerY;
+ while (i < tiles.length) {
+ var gid = tiles[i++];
+ if (gid != 0) {
+ var t = tsets.getImage(gid);
+ var x = cx + ix * stepX + staggerX * (ix % 1);
+ var y = cy + iy * stepY + th - t.height;
+ if (gid.flippedHorizontally) addTransform(x + t.width, y, -1, 1, 0, t);
+ else if (gid.flippedVertically) addTransform(x, y + t.height, 1, -1, 0, t);
+ else if (gid.flippedDiagonally) addTransform(x + t.width, y + t.height, -1, -1, 0, t);
+ else add(x, y, t);
+ }
+ if (++ix == chunk.width) { iy++; ix = 0; }
+ }
+ case Hexagonal(sideLength, staggerYAxis, staggerIndexOdd):
+ // TODO: Fix
+ var staggerX = 0, staggerY = 0, stepX, stepY;
+ var cx, cy;
+ if (staggerYAxis) {
+ stepX = tw;
+ stepY = (th + sideLength) >> 1;
+ cx = chunk.x * stepX + layer.offsetX;
+ cy = chunk.y * stepY + layer.offsetY;
+ if (staggerIndexOdd) {
+ staggerX = tw >> 1;
+ } else {
+ staggerX = -(tw >> 1);
+ cx -= staggerX;
+ }
+ } else {
+ stepX = (tw + sideLength) >> 1;
+ stepY = th;
+ cx = chunk.x * stepX + layer.offsetX;
+ cy = chunk.y * stepY + layer.offsetY;
+ if (staggerIndexOdd) {
+ staggerY = th >> 1;
+ } else {
+ staggerY = -(tw >> 1);
+ cy -= staggerY;
+ }
+ }
+ while (i < tiles.length) {
+ var gid = tiles[i++];
+ if (gid != 0) {
+ var t = tsets.getImage(gid);
+ var x = cx + ix * stepX;
+ var y = cy + iy * stepY + th - t.height;
+ if (gid.flippedHorizontally) addTransform(x + t.width, y, -1, 1, 0, t);
+ else if (gid.flippedVertically) addTransform(x, y + t.height, 1, -1, 0, t);
+ else if (gid.flippedDiagonally) addTransform(x + t.width, y + t.height, -1, -1, 0, t);
+ else add(x, y, t);
+ }
+ if (++ix == chunk.width) { iy++; ix = 0; }
+ }
+ }
+ }
+
+
+ public function addTile(tx:Int, ty:Int, ox:Float, oy:Float, gid:TmxTileIndex) {
+ if (gid == 0) return;
+ var t = map.tilesets.getImage(gid);
+ if (t != null) {
+ var x, y;
+ switch (map.orientation) {
+ case Orthogonal:
+ x = tx * map.tileWidth + ox;
+ y = ty * map.tileHeight + oy;
+ case Isometric:
+ x = (tx - ty) * (map.tileWidth >> 1) + ox;
+ y = (tx + ty) * (map.tileHeight >> 1) + oy;
+ case Staggered(staggerYAxis, staggerIndexOdd):
+ if (staggerYAxis) {
+ y = ty * (map.tileHeight >> 1);
+ if (staggerIndexOdd)
+ x = tx * map.tileWidth + (tx % 2) * (map.tileWidth >> 1);
+ else
+ x = tx * map.tileWidth + ((tx + 1) % 2) * (map.tileWidth >> 1);
+ } else {
+ x = tx * (map.tileWidth >> 1);
+ if (staggerIndexOdd)
+ y = ty * map.tileHeight + (tx % 2) * (map.tileHeight >> 1);
+ else
+ y = ty * map.tileHeight + ((tx + 1) % 2) * (map.tileHeight >> 1);
+ }
+ case Hexagonal(sideLength, staggerYAxis, staggerIndexOdd):
+ if (staggerYAxis) {
+ y = ty * ((map.tileHeight + sideLength) >> 1);
+ if (staggerIndexOdd)
+ x = tx * map.tileWidth + (tx % 2) * (map.tileWidth >> 1);
+ else
+ x = tx * map.tileWidth + ((tx + 1) % 2) * (map.tileWidth >> 1);
+ } else {
+ x = tx * ((map.tileWidth + sideLength) >> 1);
+ if (staggerIndexOdd)
+ y = ty * map.tileHeight + (tx % 2) * (map.tileHeight >> 1);
+ else
+ y = ty * map.tileHeight + ((tx + 1) % 2) * (map.tileHeight >> 1);
+ }
+ }
+ if (gid.flippedHorizontally) addTransform(x + t.width, y, -1, 1, 0, t);
+ else if (gid.flippedVertically) addTransform(x, y + t.height, 1, -1, 0, t);
+ else if (gid.flippedDiagonally) addTransform(x + t.width, y + t.height, -1, -1, 0, t);
+ else add(x, y, t);
+ }
+ }
+
+
+}
diff --git a/src/scene/WorldMapScene.hx b/src/scene/WorldMapScene.hx
index 8ccbed3..64c5e12 100644
--- a/src/scene/WorldMapScene.hx
+++ b/src/scene/WorldMapScene.hx
@@ -1,17 +1,10 @@
package scene;
+import dialogue.event.StartDialogueNode;
+import TilemapLayer;
+
@:uiComp("world-map-scene")
class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.Object {
- var map:hxd.res.TiledMap.TiledMapData;
-
- var layers:Map,
- g:h2d.TileGroup,
- alpha:Float
- }>;
- var tiles:Array;
-
public var width:Int;
public var height:Int;
@@ -21,52 +14,40 @@ class WorldMapScene extends h2d.Flow implements GameScene implements h2d.domkit.
var root = new h2d.Object(this);
- map = Res.map.toMap();
- width = map.width;
- height = map.height;
- var t = Res.tiles.toTile();
- layers = new Map();
- 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)
- ];
- for (ld in map.layers) {
- if (ld.name == "hotspots")
- continue;
- var l = {
- name: ld.name,
- data: ld.data,
- g: new h2d.TileGroup(t, root),
- alpha: ld.opacity,
+ var map = Game.loader.loadTMX("map.tmx");
+
+ for (layer in (map.layers)) {
+ switch(layer.kind) {
+ case TTileLayer:
+ var l = new TilemapLayer(map, layer, root);
+ case TObjectGroup:
+ for (obj in layer.objects) {
+ var rect = new h2d.Graphics(root);
+ rect.beginFill(0x333333);
+ rect.alpha = 0.8;
+ rect.drawRect(obj.x, obj.y, obj.width, obj.height);
+ rect.endFill();
+ var area = new h2d.Interactive(obj.width, obj.height, root);
+ area.x = obj.x;
+ area.y = obj.y;
+ area.onClick = function(_) {
+ trace("Room:" + obj.name);
+ Game.current.globalEventBus.publishEvent(new StartDialogueNode("Room:" + obj.name));
+ };
+ }
+ case TImageLayer:
+ case TGroup:
}
- l.g.alpha = ld.opacity;
- layers.set(ld.name, l);
- rebuildLayer(ld.name);
}
- Game.current.dialogueManager.runNode("Intro");
+ #if debug
+ trace('skipping intro dialogue');
+ #else
+ Game.current.globalEventBus.publishEvent(new StartDialogueNode("Intro"));
+ #end
}
-
+
public function getH2dObject() {
return this;
}
-
- function rebuildLayer(name:String) {
- var l = layers.get(name);
- if (l == null)
- return;
- var pos = 0;
- var g = l.g;
- g.clear();
- while (g.numChildren > 0) {
- g.getChildAt(0).remove();
- }
-
- for (y in 0...height)
- for (x in 0...width) {
- var t = l.data[pos++] - 1;
- if (t < 0)
- continue;
- g.add(x * 32, y * 32, tiles[t]);
- }
- }
}