@rpgjs/tiledmap 5.0.0-alpha.9 → 5.0.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +19 -0
- package/dist/client/index.js +3 -2
- package/dist/client/index2.js +7 -1
- package/dist/client/index3.js +586 -17
- package/dist/client/index4.js +4 -1
- package/dist/client/index5.js +50 -0
- package/dist/physics.d.ts +7 -0
- package/dist/server/index.js +1 -1
- package/dist/server/index2.js +6 -84
- package/dist/server/index3.js +7 -296
- package/dist/server/index4.js +72 -0
- package/dist/server.d.ts +0 -53
- package/package.json +11 -11
- package/src/client.ts +9 -3
- package/src/index.ts +5 -1
- package/src/physics.spec.ts +90 -0
- package/src/physics.ts +98 -0
- package/src/server.ts +6 -152
- package/src/tiled.ce +5 -1
package/dist/client/index3.js
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
var TiledLayerType = /* @__PURE__ */ ((TiledLayerType2) => {
|
|
2
|
+
TiledLayerType2["Tile"] = "tilelayer";
|
|
3
|
+
TiledLayerType2["ObjectGroup"] = "objectgroup";
|
|
4
|
+
TiledLayerType2["Image"] = "imagelayer";
|
|
5
|
+
TiledLayerType2["Group"] = "group";
|
|
6
|
+
return TiledLayerType2;
|
|
7
|
+
})(TiledLayerType || {});
|
|
1
8
|
function joinPath(...segments) {
|
|
2
9
|
return segments.filter((segment) => segment && segment.length > 0).join("/").replace(/\/+/g, "/");
|
|
3
10
|
}
|
|
@@ -135,7 +142,6 @@ function requireBase64Js() {
|
|
|
135
142
|
return base64Js;
|
|
136
143
|
}
|
|
137
144
|
var ieee754 = {};
|
|
138
|
-
/*! ieee754. BSD-3-Clause License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
139
145
|
var hasRequiredIeee754;
|
|
140
146
|
function requireIeee754() {
|
|
141
147
|
if (hasRequiredIeee754) return ieee754;
|
|
@@ -219,12 +225,6 @@ function requireIeee754() {
|
|
|
219
225
|
};
|
|
220
226
|
return ieee754;
|
|
221
227
|
}
|
|
222
|
-
/*!
|
|
223
|
-
* The buffer module from node.js, for the browser.
|
|
224
|
-
*
|
|
225
|
-
* @author Feross Aboukhadijeh <https://feross.org>
|
|
226
|
-
* @license MIT
|
|
227
|
-
*/
|
|
228
228
|
var hasRequiredBuffer;
|
|
229
229
|
function requireBuffer() {
|
|
230
230
|
if (hasRequiredBuffer) return buffer;
|
|
@@ -1796,7 +1796,7 @@ function requireBuffer() {
|
|
|
1796
1796
|
function numberIsNaN(obj) {
|
|
1797
1797
|
return obj !== obj;
|
|
1798
1798
|
}
|
|
1799
|
-
const hexSliceLookupTable = function() {
|
|
1799
|
+
const hexSliceLookupTable = (function() {
|
|
1800
1800
|
const alphabet = "0123456789abcdef";
|
|
1801
1801
|
const table = new Array(256);
|
|
1802
1802
|
for (let i = 0; i < 16; ++i) {
|
|
@@ -1806,7 +1806,7 @@ function requireBuffer() {
|
|
|
1806
1806
|
}
|
|
1807
1807
|
}
|
|
1808
1808
|
return table;
|
|
1809
|
-
}();
|
|
1809
|
+
})();
|
|
1810
1810
|
function defineBigIntMethod(fn) {
|
|
1811
1811
|
return typeof BigInt === "undefined" ? BufferBigIntNotDefined : fn;
|
|
1812
1812
|
}
|
|
@@ -1816,7 +1816,6 @@ function requireBuffer() {
|
|
|
1816
1816
|
})(buffer);
|
|
1817
1817
|
return buffer;
|
|
1818
1818
|
}
|
|
1819
|
-
/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */
|
|
1820
1819
|
var hasRequiredSafeBuffer;
|
|
1821
1820
|
function requireSafeBuffer() {
|
|
1822
1821
|
if (hasRequiredSafeBuffer) return safeBuffer.exports;
|
|
@@ -3524,7 +3523,6 @@ function requireSax() {
|
|
|
3524
3523
|
}
|
|
3525
3524
|
return parser;
|
|
3526
3525
|
}
|
|
3527
|
-
/*! http://mths.be/fromcodepoint v0.1.0 by @mathias */
|
|
3528
3526
|
if (!String.fromCodePoint) {
|
|
3529
3527
|
(function() {
|
|
3530
3528
|
var stringFromCharCode = String.fromCharCode;
|
|
@@ -4378,7 +4376,6 @@ const _TiledParser = class _TiledParser {
|
|
|
4378
4376
|
this.basePath = basePath;
|
|
4379
4377
|
this.layers = /* @__PURE__ */ new Map();
|
|
4380
4378
|
this.transform = (obj) => {
|
|
4381
|
-
var _a, _b;
|
|
4382
4379
|
if (!obj) return;
|
|
4383
4380
|
const attr = obj.attributes || obj._attributes;
|
|
4384
4381
|
if (!attr) return obj;
|
|
@@ -4452,7 +4449,7 @@ const _TiledParser = class _TiledParser {
|
|
|
4452
4449
|
break;
|
|
4453
4450
|
case "class":
|
|
4454
4451
|
val = {
|
|
4455
|
-
...
|
|
4452
|
+
...this.transform(prop)?.properties ?? {},
|
|
4456
4453
|
_classname: attr2.propertytype
|
|
4457
4454
|
};
|
|
4458
4455
|
break;
|
|
@@ -4496,7 +4493,7 @@ const _TiledParser = class _TiledParser {
|
|
|
4496
4493
|
newObj.source = this.getImagePath(newObj.source);
|
|
4497
4494
|
}
|
|
4498
4495
|
}
|
|
4499
|
-
const objectgroup = newObj.object ||
|
|
4496
|
+
const objectgroup = newObj.object || newObj.objectgroup?.object;
|
|
4500
4497
|
if (objectgroup) {
|
|
4501
4498
|
newObj.objects = _TiledParser.toArray(objectgroup).map((object) => {
|
|
4502
4499
|
return this.transform(object);
|
|
@@ -4573,12 +4570,11 @@ const _TiledParser = class _TiledParser {
|
|
|
4573
4570
|
};
|
|
4574
4571
|
recursiveObjectGroup(json.map);
|
|
4575
4572
|
const recursiveLayer = (elements, array = []) => {
|
|
4576
|
-
var _a;
|
|
4577
4573
|
if (!elements) return array;
|
|
4578
4574
|
for (let element of elements) {
|
|
4579
4575
|
const { name } = element;
|
|
4580
4576
|
if (!["layer", "group", "imagelayer", "objectgroup"].includes(name)) continue;
|
|
4581
|
-
const data =
|
|
4577
|
+
const data = element.elements?.find((el) => el.name == "data");
|
|
4582
4578
|
element.layer = this.layers.get(+element.attributes.id);
|
|
4583
4579
|
const obj = {
|
|
4584
4580
|
...this.transform(data) ?? {},
|
|
@@ -4647,6 +4643,579 @@ _TiledParser.propToBool = (obj, props) => {
|
|
|
4647
4643
|
return obj;
|
|
4648
4644
|
};
|
|
4649
4645
|
let TiledParser = _TiledParser;
|
|
4646
|
+
class TiledProperties {
|
|
4647
|
+
constructor(data) {
|
|
4648
|
+
this.properties = {};
|
|
4649
|
+
this.properties = data?.properties ?? {};
|
|
4650
|
+
}
|
|
4651
|
+
getProperty(name, defaultValue) {
|
|
4652
|
+
const val = this.properties[name];
|
|
4653
|
+
if (val === void 0) {
|
|
4654
|
+
return defaultValue;
|
|
4655
|
+
}
|
|
4656
|
+
return val;
|
|
4657
|
+
}
|
|
4658
|
+
hasProperty(name) {
|
|
4659
|
+
return !!this.properties[name];
|
|
4660
|
+
}
|
|
4661
|
+
setProperty(name, value) {
|
|
4662
|
+
this.properties[name] = value;
|
|
4663
|
+
}
|
|
4664
|
+
getType() {
|
|
4665
|
+
return this.class || this["type"];
|
|
4666
|
+
}
|
|
4667
|
+
}
|
|
4668
|
+
const FLIPPED_HORIZONTALLY_FLAG = 2147483648;
|
|
4669
|
+
const FLIPPED_VERTICALLY_FLAG = 1073741824;
|
|
4670
|
+
const FLIPPED_DIAGONALLY_FLAG = 536870912;
|
|
4671
|
+
const ROTATED_HEXAGONAL_120_FLAG = 268435456;
|
|
4672
|
+
class TileGid extends TiledProperties {
|
|
4673
|
+
constructor(obj) {
|
|
4674
|
+
super(obj);
|
|
4675
|
+
this.obj = obj;
|
|
4676
|
+
this._gid = obj?.gid;
|
|
4677
|
+
}
|
|
4678
|
+
static getRealGid(gid) {
|
|
4679
|
+
return gid & 268435455;
|
|
4680
|
+
}
|
|
4681
|
+
get horizontalFlip() {
|
|
4682
|
+
return !!(this._gid & FLIPPED_HORIZONTALLY_FLAG);
|
|
4683
|
+
}
|
|
4684
|
+
get verticalFlip() {
|
|
4685
|
+
return !!(this._gid & FLIPPED_VERTICALLY_FLAG);
|
|
4686
|
+
}
|
|
4687
|
+
get diagonalFlip() {
|
|
4688
|
+
return !!(this._gid & FLIPPED_DIAGONALLY_FLAG);
|
|
4689
|
+
}
|
|
4690
|
+
get rotatedHex120() {
|
|
4691
|
+
return !!(this._gid & ROTATED_HEXAGONAL_120_FLAG);
|
|
4692
|
+
}
|
|
4693
|
+
get gid() {
|
|
4694
|
+
return TileGid.getRealGid(this._gid);
|
|
4695
|
+
}
|
|
4696
|
+
set gid(val) {
|
|
4697
|
+
this._gid = val;
|
|
4698
|
+
}
|
|
4699
|
+
}
|
|
4700
|
+
class Tile extends TileGid {
|
|
4701
|
+
constructor(tile) {
|
|
4702
|
+
super(tile);
|
|
4703
|
+
this.tile = tile;
|
|
4704
|
+
const preservedProperties = this.properties;
|
|
4705
|
+
Reflect.deleteProperty(tile, "gid");
|
|
4706
|
+
Object.assign(this, tile);
|
|
4707
|
+
if (preservedProperties && Object.keys(preservedProperties).length > 0) {
|
|
4708
|
+
this.properties = { ...preservedProperties, ...this.properties };
|
|
4709
|
+
}
|
|
4710
|
+
}
|
|
4711
|
+
}
|
|
4712
|
+
class TiledObjectClass extends TileGid {
|
|
4713
|
+
constructor(object) {
|
|
4714
|
+
super(object);
|
|
4715
|
+
this.layerName = "";
|
|
4716
|
+
Object.assign(this, object);
|
|
4717
|
+
if (object?.gid) {
|
|
4718
|
+
this.y -= this.height;
|
|
4719
|
+
}
|
|
4720
|
+
}
|
|
4721
|
+
}
|
|
4722
|
+
class Layer extends TiledProperties {
|
|
4723
|
+
constructor(layer, tilesets, parent) {
|
|
4724
|
+
super(layer);
|
|
4725
|
+
this.tilesets = tilesets;
|
|
4726
|
+
this.parent = parent;
|
|
4727
|
+
this.cacheTiles = false;
|
|
4728
|
+
this.tiles = [];
|
|
4729
|
+
Object.assign(this, layer);
|
|
4730
|
+
this.mapObjects();
|
|
4731
|
+
this.mergePropertiesWithParent();
|
|
4732
|
+
this.cacheTiles = this.getProperty("cache-tiles", false);
|
|
4733
|
+
if (this.cacheTiles) this.propertiesTiles();
|
|
4734
|
+
}
|
|
4735
|
+
get size() {
|
|
4736
|
+
return this.data.length;
|
|
4737
|
+
}
|
|
4738
|
+
createTile(gid, tileIndex, layerIndex) {
|
|
4739
|
+
if (gid == 0) {
|
|
4740
|
+
return;
|
|
4741
|
+
}
|
|
4742
|
+
const realGid = TileGid.getRealGid(gid);
|
|
4743
|
+
const tileset = Layer.findTileSet(realGid, this.tilesets);
|
|
4744
|
+
if (!tileset) {
|
|
4745
|
+
return void 0;
|
|
4746
|
+
}
|
|
4747
|
+
const tile = tileset.getTile(realGid - tileset.firstgid);
|
|
4748
|
+
if (tile) {
|
|
4749
|
+
return new Tile({
|
|
4750
|
+
...tile.tile,
|
|
4751
|
+
gid,
|
|
4752
|
+
index: tileIndex,
|
|
4753
|
+
layerIndex
|
|
4754
|
+
});
|
|
4755
|
+
}
|
|
4756
|
+
return new Tile({
|
|
4757
|
+
gid,
|
|
4758
|
+
index: tileIndex,
|
|
4759
|
+
layerIndex
|
|
4760
|
+
});
|
|
4761
|
+
}
|
|
4762
|
+
mergePropertiesWithParent() {
|
|
4763
|
+
const parent = this.getLayerParent();
|
|
4764
|
+
if (!this.properties) this.properties = {};
|
|
4765
|
+
if (!parent) return;
|
|
4766
|
+
for (let key in parent.properties) {
|
|
4767
|
+
const val = parent.properties[key];
|
|
4768
|
+
const valChild = this.properties[key];
|
|
4769
|
+
if (valChild === void 0) {
|
|
4770
|
+
this.properties[key] = val;
|
|
4771
|
+
} else {
|
|
4772
|
+
if (key == "z") {
|
|
4773
|
+
this.properties[key] += val;
|
|
4774
|
+
} else {
|
|
4775
|
+
continue;
|
|
4776
|
+
}
|
|
4777
|
+
}
|
|
4778
|
+
}
|
|
4779
|
+
this.opacity = Math.round((parent.opacity ?? 1) * (this.opacity ?? 1) * 100) / 100;
|
|
4780
|
+
this.offsetx = (parent.offsetx ?? 0) + (this.offsetx ?? 0);
|
|
4781
|
+
this.offsety = (parent.offsety ?? 0) + (this.offsety ?? 0);
|
|
4782
|
+
this.locked = parent.locked ?? false;
|
|
4783
|
+
}
|
|
4784
|
+
propertiesTiles() {
|
|
4785
|
+
if (!this.data) return;
|
|
4786
|
+
const data = this.data;
|
|
4787
|
+
for (let i = 0; i < data.length; i++) {
|
|
4788
|
+
const id = data[i];
|
|
4789
|
+
this.tiles.push(this.createTile(id, i));
|
|
4790
|
+
}
|
|
4791
|
+
}
|
|
4792
|
+
mapObjects() {
|
|
4793
|
+
if (this.objects) {
|
|
4794
|
+
this.objects = this.objects.map((object) => {
|
|
4795
|
+
const obj = new TiledObjectClass(object);
|
|
4796
|
+
obj.layerName = this.name;
|
|
4797
|
+
return obj;
|
|
4798
|
+
});
|
|
4799
|
+
}
|
|
4800
|
+
}
|
|
4801
|
+
getTileByIndex(tileIndex) {
|
|
4802
|
+
if (this.cacheTiles) {
|
|
4803
|
+
return this.tiles[tileIndex];
|
|
4804
|
+
}
|
|
4805
|
+
return this.createTile(this.data[tileIndex], tileIndex);
|
|
4806
|
+
}
|
|
4807
|
+
static findTileSet(gid, tileSets) {
|
|
4808
|
+
let tileset;
|
|
4809
|
+
for (let i = tileSets.length - 1; i >= 0; i--) {
|
|
4810
|
+
tileset = tileSets[i];
|
|
4811
|
+
if (tileset.firstgid && tileset.firstgid <= gid) {
|
|
4812
|
+
break;
|
|
4813
|
+
}
|
|
4814
|
+
}
|
|
4815
|
+
return tileset;
|
|
4816
|
+
}
|
|
4817
|
+
getLayerParent() {
|
|
4818
|
+
return this.parent;
|
|
4819
|
+
}
|
|
4820
|
+
tilesForEach(cb) {
|
|
4821
|
+
for (let i = 0; i < this.data.length; i++) {
|
|
4822
|
+
if (this.cacheTiles) {
|
|
4823
|
+
cb(this.tiles[i], i);
|
|
4824
|
+
continue;
|
|
4825
|
+
}
|
|
4826
|
+
cb(this.createTile(this.data[i], i), i);
|
|
4827
|
+
}
|
|
4828
|
+
}
|
|
4829
|
+
setData(tileIndex, gid) {
|
|
4830
|
+
this.data[tileIndex] = gid;
|
|
4831
|
+
}
|
|
4832
|
+
}
|
|
4833
|
+
class Tileset extends TiledProperties {
|
|
4834
|
+
constructor(tileset) {
|
|
4835
|
+
super(tileset);
|
|
4836
|
+
this.tileset = tileset;
|
|
4837
|
+
this.cacheTileId = /* @__PURE__ */ new Map();
|
|
4838
|
+
Object.assign(this, tileset);
|
|
4839
|
+
this.margin = this.margin ?? 0;
|
|
4840
|
+
this.spacing = this.spacing ?? 0;
|
|
4841
|
+
const tilesArray = tileset.tiles || tileset.tile || [];
|
|
4842
|
+
for (let tile of tilesArray) {
|
|
4843
|
+
this.addTile(tile);
|
|
4844
|
+
}
|
|
4845
|
+
Reflect.deleteProperty(this, "tiles");
|
|
4846
|
+
Reflect.deleteProperty(this, "tile");
|
|
4847
|
+
}
|
|
4848
|
+
addTile(tileObj) {
|
|
4849
|
+
const tile = new Tile(tileObj);
|
|
4850
|
+
this.cacheTileId.set(tile.id, tile);
|
|
4851
|
+
return tile;
|
|
4852
|
+
}
|
|
4853
|
+
getTile(id) {
|
|
4854
|
+
return this.cacheTileId.get(+id);
|
|
4855
|
+
}
|
|
4856
|
+
}
|
|
4857
|
+
let bufferTilesets = {};
|
|
4858
|
+
class MapClass extends TiledProperties {
|
|
4859
|
+
constructor(map) {
|
|
4860
|
+
super(map ?? {});
|
|
4861
|
+
this.tilesets = [];
|
|
4862
|
+
this.layers = [];
|
|
4863
|
+
this.tmpLayers = [];
|
|
4864
|
+
this.tilesIndex = {};
|
|
4865
|
+
this.allocateMemory = 0;
|
|
4866
|
+
this.lowMemory = false;
|
|
4867
|
+
if (map) this.load(map);
|
|
4868
|
+
}
|
|
4869
|
+
load(map) {
|
|
4870
|
+
Object.assign(this, map);
|
|
4871
|
+
if (this.hasProperty("low-memory")) {
|
|
4872
|
+
this.lowMemory = this.getProperty("low-memory", false);
|
|
4873
|
+
}
|
|
4874
|
+
this.tmpLayers = [];
|
|
4875
|
+
this.mapTilesets();
|
|
4876
|
+
this.mapLayers(this.layers);
|
|
4877
|
+
this.layers = [...this.tmpLayers];
|
|
4878
|
+
Reflect.deleteProperty(this, "tmpLayers");
|
|
4879
|
+
this.setTilesIndex();
|
|
4880
|
+
this.data = map;
|
|
4881
|
+
}
|
|
4882
|
+
/**
|
|
4883
|
+
* @title Width of the map in pixels
|
|
4884
|
+
* @prop {number} [widthPx]
|
|
4885
|
+
* @readonly
|
|
4886
|
+
* @memberof Map
|
|
4887
|
+
* @memberof RpgSceneMap
|
|
4888
|
+
* */
|
|
4889
|
+
get widthPx() {
|
|
4890
|
+
return this.width * this.tilewidth;
|
|
4891
|
+
}
|
|
4892
|
+
/**
|
|
4893
|
+
* @title Height of the map in pixels
|
|
4894
|
+
* @prop {number} [heightPx]
|
|
4895
|
+
* @readonly
|
|
4896
|
+
* @memberof Map
|
|
4897
|
+
* @memberof RpgSceneMap
|
|
4898
|
+
* */
|
|
4899
|
+
get heightPx() {
|
|
4900
|
+
return this.height * this.tileheight;
|
|
4901
|
+
}
|
|
4902
|
+
/**
|
|
4903
|
+
* @title The depth of the map in pixels (this is the height of a tile ;))
|
|
4904
|
+
* @prop {number} map.zTileHeight
|
|
4905
|
+
* @readonly
|
|
4906
|
+
* @memberof Map
|
|
4907
|
+
* @memberof RpgSceneMap
|
|
4908
|
+
* */
|
|
4909
|
+
get zTileHeight() {
|
|
4910
|
+
return this.tileheight;
|
|
4911
|
+
}
|
|
4912
|
+
/**
|
|
4913
|
+
* Find a layer by name. Returns `undefined` is the layer is not found
|
|
4914
|
+
|
|
4915
|
+
* @title Get Layer by name
|
|
4916
|
+
* @method map.getLayerByName(name)
|
|
4917
|
+
* @param {string} name layer name
|
|
4918
|
+
* @returns {LayerInfo | undefined}
|
|
4919
|
+
* @example
|
|
4920
|
+
* ```ts
|
|
4921
|
+
* const tiles = map.getLayerByName(0, 0)
|
|
4922
|
+
* ```
|
|
4923
|
+
* @memberof Map
|
|
4924
|
+
* @memberof RpgSceneMap
|
|
4925
|
+
*/
|
|
4926
|
+
getLayerByName(name) {
|
|
4927
|
+
return this.layers.find((layer) => layer.name == name);
|
|
4928
|
+
}
|
|
4929
|
+
/**
|
|
4930
|
+
* Get the tile index on the tileset
|
|
4931
|
+
*
|
|
4932
|
+
* @title Get index of tile
|
|
4933
|
+
* @method map.getTileIndex(x,y)
|
|
4934
|
+
* @param {number} x Position X
|
|
4935
|
+
* @param {number} x Position Y
|
|
4936
|
+
* @returns {number}
|
|
4937
|
+
* @memberof Map
|
|
4938
|
+
* @memberof RpgSceneMap
|
|
4939
|
+
*/
|
|
4940
|
+
getTileIndex(x, y, [z] = [0]) {
|
|
4941
|
+
return this.width * Math.floor((y - z) / this.tileheight) + Math.floor(x / this.tilewidth);
|
|
4942
|
+
}
|
|
4943
|
+
getTilePosition(index) {
|
|
4944
|
+
return {
|
|
4945
|
+
y: Math.floor(index / this.width) * this.tileheight,
|
|
4946
|
+
x: index % this.width * this.tilewidth
|
|
4947
|
+
};
|
|
4948
|
+
}
|
|
4949
|
+
/**
|
|
4950
|
+
* Find the point of origin (top left) of a tile. Of course, its position depends on the size of the tile
|
|
4951
|
+
|
|
4952
|
+
* @title Get origin position of tile
|
|
4953
|
+
* @method map.getTileOriginPosition(x,y)
|
|
4954
|
+
* @param {number} x Position X
|
|
4955
|
+
* @param {number} x Position Y
|
|
4956
|
+
* @returns { {x: number, y: number }}
|
|
4957
|
+
* @example
|
|
4958
|
+
* ```ts
|
|
4959
|
+
* // If the size of a tile is 32x32px
|
|
4960
|
+
* const position = map.getTileOriginPosition(35, 12)
|
|
4961
|
+
* console.log(position) // { x: 32, y: 0 }
|
|
4962
|
+
* ```
|
|
4963
|
+
* @memberof Map
|
|
4964
|
+
* @memberof RpgSceneMap
|
|
4965
|
+
*/
|
|
4966
|
+
getTileOriginPosition(x, y) {
|
|
4967
|
+
return {
|
|
4968
|
+
x: Math.floor(x / this.tilewidth) * this.tilewidth,
|
|
4969
|
+
y: Math.floor(y / this.tileheight) * this.tileheight
|
|
4970
|
+
};
|
|
4971
|
+
}
|
|
4972
|
+
/**
|
|
4973
|
+
* Recover tiles according to a position
|
|
4974
|
+
|
|
4975
|
+
* @title Get tile by position
|
|
4976
|
+
* @method map.getTileByPosition(x,y)
|
|
4977
|
+
* @param {number} x Position X
|
|
4978
|
+
* @param {number} x Position Y
|
|
4979
|
+
* @returns {TileInfo}
|
|
4980
|
+
* @example
|
|
4981
|
+
* ```ts
|
|
4982
|
+
* const tiles = map.getTileByPosition(0, 0)
|
|
4983
|
+
* ```
|
|
4984
|
+
* @memberof Map
|
|
4985
|
+
* @memberof RpgSceneMap
|
|
4986
|
+
*/
|
|
4987
|
+
getTileByPosition(x, y, z = [0, 0], options = {}) {
|
|
4988
|
+
const tileIndex = this.getTileIndex(x, y, [z[0]]);
|
|
4989
|
+
return this.getTileByIndex(tileIndex, z, options);
|
|
4990
|
+
}
|
|
4991
|
+
/**
|
|
4992
|
+
* Retrieves tiles according to its index
|
|
4993
|
+
|
|
4994
|
+
* @title Get tile by index
|
|
4995
|
+
* @method map.getTileByIndex(tileIndex)
|
|
4996
|
+
* @param {number} tileIndex tile index
|
|
4997
|
+
* @returns {TileInfo}
|
|
4998
|
+
* @example
|
|
4999
|
+
* ```ts
|
|
5000
|
+
* const index = map.getTileIndex(0, 0)
|
|
5001
|
+
* const tiles = map.getTileByIndex(index)
|
|
5002
|
+
* ```
|
|
5003
|
+
* @memberof Map
|
|
5004
|
+
* @memberof RpgSceneMap
|
|
5005
|
+
*/
|
|
5006
|
+
getTileByIndex(tileIndex, zPlayer = [0, 0], options = {
|
|
5007
|
+
populateTiles: true
|
|
5008
|
+
}) {
|
|
5009
|
+
const zA = Math.floor(zPlayer[0] / this.zTileHeight);
|
|
5010
|
+
Math.floor(zPlayer[1] / this.zTileHeight);
|
|
5011
|
+
const level = this.tilesIndex[zA];
|
|
5012
|
+
const obj = {
|
|
5013
|
+
tiles: [],
|
|
5014
|
+
hasCollision: false,
|
|
5015
|
+
isOverlay: false,
|
|
5016
|
+
objectGroups: [],
|
|
5017
|
+
tileIndex
|
|
5018
|
+
};
|
|
5019
|
+
if (!level) {
|
|
5020
|
+
return obj;
|
|
5021
|
+
}
|
|
5022
|
+
const [layer] = this.layers;
|
|
5023
|
+
const getTileByPointer = (pointer = 0) => {
|
|
5024
|
+
const pos = tileIndex * this.realAllocateMemory + pointer;
|
|
5025
|
+
const gid = level[pos];
|
|
5026
|
+
if (gid === 0) {
|
|
5027
|
+
return obj;
|
|
5028
|
+
}
|
|
5029
|
+
const tile2 = layer.createTile(gid, tileIndex, level[pos + 1]);
|
|
5030
|
+
if (tile2) obj.tiles.push(tile2);
|
|
5031
|
+
};
|
|
5032
|
+
if (options.populateTiles) {
|
|
5033
|
+
for (let i = 0; i < this.realAllocateMemory; i += 2) {
|
|
5034
|
+
getTileByPointer(i);
|
|
5035
|
+
}
|
|
5036
|
+
} else {
|
|
5037
|
+
getTileByPointer();
|
|
5038
|
+
}
|
|
5039
|
+
const [tile] = obj.tiles;
|
|
5040
|
+
if (tile) {
|
|
5041
|
+
obj.hasCollision = tile.getProperty("collision", false);
|
|
5042
|
+
obj.objectGroups = tile.objects ?? [];
|
|
5043
|
+
}
|
|
5044
|
+
return obj;
|
|
5045
|
+
}
|
|
5046
|
+
getAllObjects() {
|
|
5047
|
+
return this.layers.reduce((prev, current) => {
|
|
5048
|
+
if (!current.objects) return prev;
|
|
5049
|
+
return prev.concat(...current.objects);
|
|
5050
|
+
}, []);
|
|
5051
|
+
}
|
|
5052
|
+
getData() {
|
|
5053
|
+
return {
|
|
5054
|
+
...this.data,
|
|
5055
|
+
layers: this.layers
|
|
5056
|
+
};
|
|
5057
|
+
}
|
|
5058
|
+
setTile(x, y, layerFilter, tileInfo) {
|
|
5059
|
+
if (this.lowMemory) {
|
|
5060
|
+
throw "Impossible to change a tile with the lowMemory option";
|
|
5061
|
+
}
|
|
5062
|
+
const tileIndex = this.getTileIndex(x, y);
|
|
5063
|
+
let fnFilter;
|
|
5064
|
+
let tilesEdited = {};
|
|
5065
|
+
if (typeof layerFilter == "string") {
|
|
5066
|
+
fnFilter = (layer) => layer.name == layerFilter;
|
|
5067
|
+
} else {
|
|
5068
|
+
fnFilter = layerFilter;
|
|
5069
|
+
}
|
|
5070
|
+
for (let i = 0; i < this.layers.length; i++) {
|
|
5071
|
+
const layer = this.layers[i];
|
|
5072
|
+
if (!fnFilter(layer)) continue;
|
|
5073
|
+
let tile;
|
|
5074
|
+
const oldTile = this.getTileByIndex(tileIndex);
|
|
5075
|
+
if (tileInfo.gid) {
|
|
5076
|
+
tile = layer.createTile(tileInfo.gid, tileIndex);
|
|
5077
|
+
}
|
|
5078
|
+
if (!tile) continue;
|
|
5079
|
+
for (let key in tileInfo) {
|
|
5080
|
+
if (key == "gid") continue;
|
|
5081
|
+
tile[key] = tileInfo[key];
|
|
5082
|
+
}
|
|
5083
|
+
tilesEdited[layer.name] = {
|
|
5084
|
+
gid: tile.gid,
|
|
5085
|
+
properties: tile.properties
|
|
5086
|
+
};
|
|
5087
|
+
this.setTileIndex(layer, oldTile.tiles[0], tile, tileIndex, i);
|
|
5088
|
+
layer.setData(tileIndex, tile.gid);
|
|
5089
|
+
}
|
|
5090
|
+
return {
|
|
5091
|
+
x,
|
|
5092
|
+
y,
|
|
5093
|
+
tiles: tilesEdited
|
|
5094
|
+
};
|
|
5095
|
+
}
|
|
5096
|
+
removeCacheTileset(name) {
|
|
5097
|
+
delete bufferTilesets[name];
|
|
5098
|
+
}
|
|
5099
|
+
clearCacheTilesets() {
|
|
5100
|
+
bufferTilesets = {};
|
|
5101
|
+
}
|
|
5102
|
+
mapTilesets() {
|
|
5103
|
+
this.tilesets = this.tilesets.map((tileset) => {
|
|
5104
|
+
if (bufferTilesets[tileset.name]) {
|
|
5105
|
+
const instance = bufferTilesets[tileset.name];
|
|
5106
|
+
instance.firstgid = tileset.firstgid;
|
|
5107
|
+
return instance;
|
|
5108
|
+
}
|
|
5109
|
+
const _tileset = new Tileset(tileset);
|
|
5110
|
+
bufferTilesets[_tileset.name] = _tileset;
|
|
5111
|
+
return _tileset;
|
|
5112
|
+
});
|
|
5113
|
+
}
|
|
5114
|
+
mapLayers(layers = [], parent) {
|
|
5115
|
+
for (let layer of layers) {
|
|
5116
|
+
const layerInstance = new Layer(layer, this.tilesets, parent);
|
|
5117
|
+
this.tmpLayers.push(layerInstance);
|
|
5118
|
+
if (layer.layers) {
|
|
5119
|
+
this.mapLayers(layer.layers, layerInstance);
|
|
5120
|
+
}
|
|
5121
|
+
}
|
|
5122
|
+
if (this.lowMemory) this.allocateMemory = 1;
|
|
5123
|
+
if (!this.allocateMemory) this.allocateMemory = this.layers.length;
|
|
5124
|
+
}
|
|
5125
|
+
setTileIndex(layer, oldTile, newTile, tileIndex, layerIndex) {
|
|
5126
|
+
const startPos = tileIndex * this.realAllocateMemory;
|
|
5127
|
+
let pointer = startPos + this.realAllocateMemory - 2;
|
|
5128
|
+
const zLayer = layer.getProperty("z", 0);
|
|
5129
|
+
const zTile = oldTile.getProperty("z", 0);
|
|
5130
|
+
let z = zLayer + zTile;
|
|
5131
|
+
while (pointer >= startPos) {
|
|
5132
|
+
const zlayer = this.tilesIndex[z];
|
|
5133
|
+
if (zlayer[pointer] === oldTile.gid && zlayer[pointer + 1] === layerIndex) {
|
|
5134
|
+
this.tilesIndex[z][pointer] = newTile.gid;
|
|
5135
|
+
}
|
|
5136
|
+
pointer -= 2;
|
|
5137
|
+
}
|
|
5138
|
+
}
|
|
5139
|
+
/**
|
|
5140
|
+
* We multiply by 2 because 2 entries are stored for a tile: its GID and the Layer Index
|
|
5141
|
+
*
|
|
5142
|
+
* Example If I have 3 layers, The array will have the following form
|
|
5143
|
+
*
|
|
5144
|
+
* [
|
|
5145
|
+
* GID of Layer 3,
|
|
5146
|
+
* Layer Index of Layer 3,
|
|
5147
|
+
* GID of Layer 2,
|
|
5148
|
+
* Layer Index of Layer 2,
|
|
5149
|
+
* GID of Layer 1,
|
|
5150
|
+
* Layer Index of Layer 1,
|
|
5151
|
+
* ... others tiles
|
|
5152
|
+
* ]
|
|
5153
|
+
*
|
|
5154
|
+
* The size in memory of the map is therefore:
|
|
5155
|
+
*
|
|
5156
|
+
* `(map width * map height * number of layers * 4) bytes`
|
|
5157
|
+
*
|
|
5158
|
+
* > We multiply by 4, because an element takes 2 bytes and has 2 elements for a tile is 4 bytes in all
|
|
5159
|
+
*
|
|
5160
|
+
* Example (a 100x100 map with 5 layers)
|
|
5161
|
+
*
|
|
5162
|
+
* `100 * 100 * 5 * 4 = 200000 bytes = ~195 Kb`
|
|
5163
|
+
*
|
|
5164
|
+
* If we define on lowMemory then the calculation is the following
|
|
5165
|
+
*
|
|
5166
|
+
* `(map width * map height * 4) bytes`
|
|
5167
|
+
*
|
|
5168
|
+
* Example
|
|
5169
|
+
*
|
|
5170
|
+
* `100 * 100 * 4 = 40000 bytes = ~39 Kb`
|
|
5171
|
+
*/
|
|
5172
|
+
get realAllocateMemory() {
|
|
5173
|
+
return this.allocateMemory * 2;
|
|
5174
|
+
}
|
|
5175
|
+
/**
|
|
5176
|
+
* We keep each tile in memory classified by z value. The values are ordered from the end to the beginning so that the first element of the array (when retrieved with getTileByIndex() is the tile on the highest layer. This way, the tile search is very fast for collisions
|
|
5177
|
+
*
|
|
5178
|
+
*/
|
|
5179
|
+
addTileIndex(layer, tile, tileIndex, layerIndex) {
|
|
5180
|
+
if (!tile || tile && tile.gid == 0) {
|
|
5181
|
+
return;
|
|
5182
|
+
}
|
|
5183
|
+
const zLayer = layer.getProperty("z", 0);
|
|
5184
|
+
const zTile = tile.getProperty("z", 0);
|
|
5185
|
+
let z = zLayer + zTile;
|
|
5186
|
+
if (!this.tilesIndex[z]) {
|
|
5187
|
+
const buffer2 = new ArrayBuffer(layer.size * this.realAllocateMemory * 2);
|
|
5188
|
+
this.tilesIndex[z] = new Uint16Array(buffer2);
|
|
5189
|
+
}
|
|
5190
|
+
const startPos = tileIndex * this.realAllocateMemory;
|
|
5191
|
+
let pointer = startPos + this.realAllocateMemory - 2;
|
|
5192
|
+
while (this.tilesIndex[z][pointer] !== 0 && pointer > startPos) {
|
|
5193
|
+
pointer -= 2;
|
|
5194
|
+
}
|
|
5195
|
+
this.tilesIndex[z][pointer] = tile.gid;
|
|
5196
|
+
this.tilesIndex[z][pointer + 1] = layerIndex;
|
|
5197
|
+
this.tilesIndex[z][startPos] = tile.gid;
|
|
5198
|
+
this.tilesIndex[z][startPos + 1] = layerIndex;
|
|
5199
|
+
}
|
|
5200
|
+
setTilesIndex() {
|
|
5201
|
+
for (let i = 0; i < this.layers.length; i++) {
|
|
5202
|
+
const layer = this.layers[i];
|
|
5203
|
+
if (layer.type != TiledLayerType.Tile) {
|
|
5204
|
+
continue;
|
|
5205
|
+
}
|
|
5206
|
+
layer.tilesForEach((tile, index) => {
|
|
5207
|
+
this.addTileIndex(layer, tile, index, i);
|
|
5208
|
+
});
|
|
5209
|
+
}
|
|
5210
|
+
}
|
|
5211
|
+
}
|
|
4650
5212
|
export {
|
|
4651
|
-
|
|
5213
|
+
Layer,
|
|
5214
|
+
MapClass,
|
|
5215
|
+
Tile,
|
|
5216
|
+
TiledLayerType,
|
|
5217
|
+
TiledObjectClass,
|
|
5218
|
+
TiledParser,
|
|
5219
|
+
TiledProperties,
|
|
5220
|
+
Tileset
|
|
4652
5221
|
};
|
package/dist/client/index4.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useProps, useDefineProps, signal, h, Container } from "canvasengine";
|
|
1
|
+
import { useProps, useDefineProps, signal, effect, h, Container } from "canvasengine";
|
|
2
2
|
import { EventLayerComponent } from "@rpgjs/client";
|
|
3
3
|
import { TiledMap } from "@canvasengine/presets";
|
|
4
4
|
function component($$props) {
|
|
@@ -7,6 +7,9 @@ function component($$props) {
|
|
|
7
7
|
var _a = defineProps(), data = _a.data, params = _a.params;
|
|
8
8
|
var map = signal(data());
|
|
9
9
|
var basePath = signal(params().basePath);
|
|
10
|
+
effect(function() {
|
|
11
|
+
map.set(data());
|
|
12
|
+
});
|
|
10
13
|
let $this = h(Container, null, h(TiledMap, { map, basePath, createLayersPerTilesZ: true, objectLayer: () => h(EventLayerComponent) }));
|
|
11
14
|
return $this;
|
|
12
15
|
}
|