Clickable rooms with per-room dialogue

This commit is contained in:
3wc 2024-02-16 01:53:38 -03:00
parent bb9db51560
commit c1b102860e
6 changed files with 238 additions and 53 deletions

View File

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

View File

@ -21,6 +21,6 @@
<object id="7" name="medical" x="96" y="96" width="224" height="128"/>
<object id="8" name="generator" x="96" y="416" width="192" height="128"/>
<object id="9" name="quarters" x="320" y="416" width="288" height="128"/>
<object id="10" name="hangar" x="640" y="128" width="224" height="416"/>
<object id="10" name="hangar" x="640" y="96" width="224" height="448"/>
</objectgroup>
</map>

View File

@ -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
===

View File

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

197
src/TilemapLayer.hx Normal file
View File

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

View File

@ -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<String, {
name:String,
data:Array<Int>,
g:h2d.TileGroup,
alpha:Float
}>;
var tiles:Array<h2d.Tile>;
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]);
}
}
}