@canvasengine/presets 2.0.0-beta.23 → 2.0.0-beta.25
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/dist/Bar.d.ts +14 -0
- package/dist/Bar.d.ts.map +1 -0
- package/dist/Button.d.ts +1 -0
- package/dist/Button.d.ts.map +1 -0
- package/dist/Joystick.d.ts +32 -0
- package/dist/Joystick.d.ts.map +1 -0
- package/dist/NightAmbiant.d.ts +3 -0
- package/dist/NightAmbiant.d.ts.map +1 -0
- package/dist/Particle.d.ts +2 -0
- package/dist/Particle.d.ts.map +1 -0
- package/dist/Tilemap/Tile.d.ts +23 -0
- package/dist/Tilemap/Tile.d.ts.map +1 -0
- package/dist/Tilemap/TileGroup.d.ts +62 -0
- package/dist/Tilemap/TileGroup.d.ts.map +1 -0
- package/dist/Tilemap/TileLayer.d.ts +87 -0
- package/dist/Tilemap/TileLayer.d.ts.map +1 -0
- package/dist/Tilemap/TileSet.d.ts +11 -0
- package/dist/Tilemap/TileSet.d.ts.map +1 -0
- package/dist/Tilemap/index.d.ts +2 -0
- package/dist/Tilemap/index.d.ts.map +1 -0
- package/dist/Weathers/index.d.ts +57 -0
- package/dist/Weathers/index.d.ts.map +1 -0
- package/dist/index.d.ts +7 -112
- package/dist/index.d.ts.map +1 -0
- package/dist/index.global.js +222 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +5777 -755
- package/dist/index.js.map +1 -1
- package/package.json +31 -9
- package/src/Bar.ts +0 -90
- package/src/Button.ts +0 -0
- package/src/Joystick.ts +0 -284
- package/src/NightAmbiant.ts +0 -116
- package/src/Particle.ts +0 -50
- package/src/Tilemap/Tile.ts +0 -83
- package/src/Tilemap/TileGroup.ts +0 -207
- package/src/Tilemap/TileLayer.ts +0 -146
- package/src/Tilemap/TileSet.ts +0 -41
- package/src/Tilemap/index.ts +0 -222
- package/src/Weathers/index.ts +0 -224
- package/src/index.ts +0 -6
package/src/Tilemap/TileGroup.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
interface TileOptions {
|
|
2
|
-
tilesetIndex?: number
|
|
3
|
-
tileId: number
|
|
4
|
-
x: number
|
|
5
|
-
y: number
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
interface TilesGroupOptions {
|
|
9
|
-
ignore?: boolean
|
|
10
|
-
probability?: number
|
|
11
|
-
baseHeight?: number
|
|
12
|
-
baseWidth?: number
|
|
13
|
-
rectMargin?: number
|
|
14
|
-
baseOffsetX?: number
|
|
15
|
-
baseOffsetY?: number
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class TileInfo {
|
|
19
|
-
tilesetIndex?: number
|
|
20
|
-
tileId: number
|
|
21
|
-
flags: Map<string, any> = new Map()
|
|
22
|
-
id: number = Math.random()
|
|
23
|
-
|
|
24
|
-
constructor(obj: TileOptions) {
|
|
25
|
-
this.tilesetIndex = obj.tilesetIndex ?? 0
|
|
26
|
-
this.tileId = obj.tileId
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
addFlag(key: string, value: any) {
|
|
30
|
-
this.flags.set(key, value)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export class TilesGroup {
|
|
35
|
-
tiles: (TileInfo | null)[][] = []
|
|
36
|
-
width: number
|
|
37
|
-
height: number
|
|
38
|
-
ignore: boolean = false
|
|
39
|
-
probability: number = 1
|
|
40
|
-
baseHeight: number = 1
|
|
41
|
-
baseWidth?: number
|
|
42
|
-
baseOffsetX: number = 0
|
|
43
|
-
baseOffsetY: number = 0
|
|
44
|
-
rectMargin: number = 0
|
|
45
|
-
|
|
46
|
-
constructor(tiles: TileOptions[], public tilesetIndex: number = 0, options: TilesGroupOptions = {}) {
|
|
47
|
-
const pointsX = tiles.map(tile => tile.x)
|
|
48
|
-
const pointsY = tiles.map(tile => tile.y)
|
|
49
|
-
const offsetX = Math.min(...pointsX)
|
|
50
|
-
const offsetY = Math.min(...pointsY)
|
|
51
|
-
this.width = Math.max(...pointsX) - offsetX + 1
|
|
52
|
-
this.height = Math.max(...pointsY) - offsetY + 1
|
|
53
|
-
this.ignore = !!options.ignore
|
|
54
|
-
this.probability = options.probability ?? 1
|
|
55
|
-
this.baseHeight = options.baseHeight ?? 1
|
|
56
|
-
this.baseWidth = options.baseWidth
|
|
57
|
-
this.rectMargin = options.rectMargin ?? 0
|
|
58
|
-
this.baseOffsetX = options.baseOffsetX ?? 0
|
|
59
|
-
this.baseOffsetY = options.baseOffsetY ?? 0
|
|
60
|
-
this.fillTiles()
|
|
61
|
-
for (let tile of tiles) {
|
|
62
|
-
this.addTile(tile.x - offsetX, tile.y - offsetY, tile)
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
getRect(x: number, y: number): { minX: number, minY: number, maxX: number, maxY: number } {
|
|
67
|
-
const margin = this.rectMargin
|
|
68
|
-
return {
|
|
69
|
-
minX: x - margin + this.baseOffsetX,
|
|
70
|
-
minY: y - this.tilesBaseHeight - margin - this.baseOffsetY,
|
|
71
|
-
maxX: x + this.tilesBaseWidth + margin,
|
|
72
|
-
maxY: y + margin
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
get tilesBase() {
|
|
77
|
-
return this.tiles[this.tiles.length - 1]
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get tilesBaseWidth(): number {
|
|
81
|
-
return this.baseWidth ?? this.tilesBase.length
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
get tilesBaseHeight(): number {
|
|
85
|
-
return this.baseHeight
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
forEach(cb: (tileInfo: TileInfo | null, x: number, y: number) => void) {
|
|
89
|
-
for (let i = 0; i < this.tiles.length; i++) {
|
|
90
|
-
for (let j = 0; j < this.tiles[i].length; j++) {
|
|
91
|
-
cb(this.tiles[i][j], j, i)
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
find(cb: (tileInfo: TileInfo | null, x: number, y: number) => boolean): TileInfo | null {
|
|
97
|
-
let found: TileInfo | null = null
|
|
98
|
-
this.forEach((tileInfo, x, y) => {
|
|
99
|
-
const bool = cb(tileInfo, x, y)
|
|
100
|
-
if (bool) found = tileInfo
|
|
101
|
-
})
|
|
102
|
-
return found
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
getOffsetY(): number {
|
|
106
|
-
const tilesBase = this.tilesBase
|
|
107
|
-
let offset = 0
|
|
108
|
-
this.forEach((tile, x, y) => {
|
|
109
|
-
if (tile?.tileId == (tilesBase?.[0]?.tileId)) {
|
|
110
|
-
offset = y
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
return offset
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
fillTiles() {
|
|
117
|
-
for (let i = 0; i < this.height; i++) {
|
|
118
|
-
this.tiles[i] = []
|
|
119
|
-
for (let j = 0; j < this.width; j++) {
|
|
120
|
-
this.tiles[i][j] = null
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
shiftToTopLeft(): void {
|
|
126
|
-
const matrix = this.tiles
|
|
127
|
-
|
|
128
|
-
// Find the first non-undefined element and its position
|
|
129
|
-
const foundFirst = () => {
|
|
130
|
-
let firstElementRow = -1;
|
|
131
|
-
let firstElementColumn = -1;
|
|
132
|
-
|
|
133
|
-
for (let col = 0; col < matrix.length; col++) {
|
|
134
|
-
if (!matrix[col]) matrix[col] = []
|
|
135
|
-
for (let row = 0; row < matrix[col].length; row++) {
|
|
136
|
-
if (matrix[col][row] !== undefined) {
|
|
137
|
-
firstElementRow = row;
|
|
138
|
-
firstElementColumn = col;
|
|
139
|
-
return {
|
|
140
|
-
firstElementRow,
|
|
141
|
-
firstElementColumn
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
firstElementRow,
|
|
149
|
-
firstElementColumn
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
const { firstElementRow, firstElementColumn } = foundFirst()
|
|
154
|
-
|
|
155
|
-
// If no non-undefined element is found, return the original matrix
|
|
156
|
-
if (firstElementRow === -1) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// Shift the matrix elements
|
|
161
|
-
const newMatrix: (TileInfo | null)[][] = [];
|
|
162
|
-
for (let col = firstElementColumn; col < matrix.length; col++) {
|
|
163
|
-
const newRow: (TileInfo | null)[] = [];
|
|
164
|
-
for (let row = firstElementRow; row < matrix[col].length; row++) {
|
|
165
|
-
newRow.push(matrix[col][row]);
|
|
166
|
-
}
|
|
167
|
-
newMatrix.push(newRow);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
this.tiles = newMatrix;
|
|
171
|
-
|
|
172
|
-
this.width = this.tiles[0].length
|
|
173
|
-
this.height = this.tiles.length
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
addTile(x: number, y: number, tileOptions: TileOptions) {
|
|
177
|
-
if (!this.tiles[y]) this.tiles[y] = []
|
|
178
|
-
this.tiles[y][x] = new TileInfo(tileOptions)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
addTileFlag(x: number, y: number, key: string, value: any) {
|
|
182
|
-
this.getTile(x, y)?.addFlag(key, value)
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
getTile(x: number, y: number): TileInfo | null {
|
|
186
|
-
return this.tiles[y]?.[x]
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
getTilesByFlag(key: string, value: any): { tileInfo: TileInfo, x: number, y: number }[] {
|
|
190
|
-
const array: any = []
|
|
191
|
-
this.forEach((tileInfo, x, y) => {
|
|
192
|
-
const flag = tileInfo?.flags.get(key)
|
|
193
|
-
if (flag && flag == value) {
|
|
194
|
-
array.push({
|
|
195
|
-
tileInfo,
|
|
196
|
-
x,
|
|
197
|
-
y
|
|
198
|
-
})
|
|
199
|
-
}
|
|
200
|
-
})
|
|
201
|
-
return array
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
isTileBase(tileInfo: TileInfo): boolean {
|
|
205
|
-
return !!this.tilesBase.find(tile => tile?.id == tileInfo.id)
|
|
206
|
-
}
|
|
207
|
-
}
|
package/src/Tilemap/TileLayer.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
CompositeTilemap,
|
|
3
|
-
POINT_STRUCT_SIZE,
|
|
4
|
-
Tilemap,
|
|
5
|
-
settings,
|
|
6
|
-
} from "@canvasengine/tilemap";
|
|
7
|
-
import { Layer, Tile as TileClass } from "@rpgjs/tiled";
|
|
8
|
-
import {
|
|
9
|
-
createComponent,
|
|
10
|
-
registerComponent,
|
|
11
|
-
DisplayObject,
|
|
12
|
-
Signal,
|
|
13
|
-
} from "canvasengine";
|
|
14
|
-
import { Tile } from "./Tile";
|
|
15
|
-
import { TileSet } from "./TileSet";
|
|
16
|
-
import { Subscription } from "rxjs";
|
|
17
|
-
|
|
18
|
-
settings.use32bitIndex = true;
|
|
19
|
-
|
|
20
|
-
export class CanvasTileLayer extends DisplayObject(CompositeTilemap) {
|
|
21
|
-
private _tiles: any = {};
|
|
22
|
-
tiles: (TileClass | null)[];
|
|
23
|
-
private _layer: any; // TODO: fix this, remove any. replace with Layer
|
|
24
|
-
private frameTile: number = 0;
|
|
25
|
-
private frameRateAnimation: number = 10;
|
|
26
|
-
private subscriptionTick: Subscription;
|
|
27
|
-
|
|
28
|
-
static findTileSet(gid: number, tileSets: TileSet[]) {
|
|
29
|
-
let tileset: TileSet | undefined;
|
|
30
|
-
for (let i = tileSets.length - 1; i >= 0; i--) {
|
|
31
|
-
tileset = tileSets[i];
|
|
32
|
-
if (tileset.firstgid && tileset.firstgid <= gid) {
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return tileset;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** @internal */
|
|
40
|
-
createTile(x: number, y: number, options: any = {}): Tile | undefined {
|
|
41
|
-
const { real, filter } = options;
|
|
42
|
-
const { tilewidth, tileheight, width } = this._layer;
|
|
43
|
-
if (real) {
|
|
44
|
-
x = Math.floor(x / tilewidth);
|
|
45
|
-
y = Math.floor(y / tileheight);
|
|
46
|
-
}
|
|
47
|
-
const i = x + y * width;
|
|
48
|
-
const tiledTile = this._layer.getTileByIndex(i);
|
|
49
|
-
|
|
50
|
-
if (!tiledTile || (tiledTile && tiledTile.gid == 0)) return;
|
|
51
|
-
|
|
52
|
-
const tileset = CanvasTileLayer.findTileSet(tiledTile.gid, this.tileSets);
|
|
53
|
-
|
|
54
|
-
if (!tileset) return;
|
|
55
|
-
|
|
56
|
-
const tile = new Tile(tiledTile, tileset);
|
|
57
|
-
|
|
58
|
-
tile.x = x * tilewidth;
|
|
59
|
-
tile.y = y * tileheight + (tileheight - tile.texture.height);
|
|
60
|
-
|
|
61
|
-
tile._x = x;
|
|
62
|
-
tile._y = y;
|
|
63
|
-
|
|
64
|
-
if (tileset.tileoffset) {
|
|
65
|
-
tile.x += tileset.tileoffset.x ?? 0;
|
|
66
|
-
tile.y += tileset.tileoffset.y ?? 0;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (filter) {
|
|
70
|
-
const ret = filter(tile);
|
|
71
|
-
if (!ret) return;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return tile;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
private _addFrame(tile: Tile, x: number, y: number) {
|
|
78
|
-
const frame = this.tile(tile.texture, tile.x, tile.y, {
|
|
79
|
-
rotate: tile.texture.rotate,
|
|
80
|
-
});
|
|
81
|
-
// const pb = this.pointsBuf
|
|
82
|
-
// if (!pb) return null
|
|
83
|
-
// tile.pointsBufIndex = pb.length - POINT_STRUCT_SIZE
|
|
84
|
-
tile.setAnimation(frame);
|
|
85
|
-
this._tiles[x + ";" + y] = tile;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async onMount(args) {
|
|
89
|
-
const { props } = args;
|
|
90
|
-
|
|
91
|
-
this.tileSets = props.tilesets;
|
|
92
|
-
this._layer = new Layer(
|
|
93
|
-
{
|
|
94
|
-
...props,
|
|
95
|
-
},
|
|
96
|
-
this.tileSets
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
const tick: Signal = props.context.tick;
|
|
100
|
-
|
|
101
|
-
this.subscriptionTick = tick.observable.subscribe(({ value }) => {
|
|
102
|
-
if (value.frame % this.frameRateAnimation == 0) {
|
|
103
|
-
this.tileAnim = [this.frameTile, this.frameTile];
|
|
104
|
-
this.frameTile++
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
super.onMount(args);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
onUpdate(props) {
|
|
112
|
-
super.onUpdate(props);
|
|
113
|
-
if (!this.isMounted) return;
|
|
114
|
-
if (props.tileheight) this._layer.tileheight = props.tileheight;
|
|
115
|
-
if (props.tilewidth) this._layer.tilewidth = props.tilewidth;
|
|
116
|
-
if (props.width) this._layer.width = props.width;
|
|
117
|
-
if (props.height) this._layer.height = props.height;
|
|
118
|
-
if (props.parallaxX) this._layer.parallaxX = props.parallaxx;
|
|
119
|
-
if (props.parallaxY) this._layer.parallaxY = props.parallaxy;
|
|
120
|
-
|
|
121
|
-
this.removeChildren();
|
|
122
|
-
|
|
123
|
-
for (let y = 0; y < this._layer.height; y++) {
|
|
124
|
-
for (let x = 0; x < this._layer.width; x++) {
|
|
125
|
-
const tile = this.createTile(x, y);
|
|
126
|
-
if (tile) {
|
|
127
|
-
this._addFrame(tile, x, y);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
async onDestroy(parent: any) {
|
|
134
|
-
this.subscriptionTick.unsubscribe();
|
|
135
|
-
await super.onDestroy(parent);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// @ts-ignore
|
|
140
|
-
export interface CanvasTileLayer extends CompositeTilemap {}
|
|
141
|
-
|
|
142
|
-
registerComponent("CompositeTileLayer", CanvasTileLayer);
|
|
143
|
-
|
|
144
|
-
export function CompositeTileLayer(props) {
|
|
145
|
-
return createComponent("CompositeTileLayer", props);
|
|
146
|
-
}
|
package/src/Tilemap/TileSet.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { TiledTileset, Tileset as TiledTilesetClass } from "@rpgjs/tiled";
|
|
2
|
-
import { Assets, Rectangle, Texture } from "pixi.js";
|
|
3
|
-
|
|
4
|
-
export class TileSet extends TiledTilesetClass {
|
|
5
|
-
public textures: Texture[] = [];
|
|
6
|
-
private tileGroups = {};
|
|
7
|
-
|
|
8
|
-
constructor(tileSet: TiledTileset) {
|
|
9
|
-
super(tileSet);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
loadGroup() {
|
|
13
|
-
// for (let tile of this.tileset.tiles) {
|
|
14
|
-
// }
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** @internal */
|
|
18
|
-
async load(image: string) {
|
|
19
|
-
const texture = await Assets.load(image);
|
|
20
|
-
for (
|
|
21
|
-
let y = this.margin;
|
|
22
|
-
y < this.image.height;
|
|
23
|
-
y += this.tileheight + this.spacing
|
|
24
|
-
) {
|
|
25
|
-
for (
|
|
26
|
-
let x = this.margin;
|
|
27
|
-
x < this.image.width;
|
|
28
|
-
x += this.tilewidth + this.spacing
|
|
29
|
-
) {
|
|
30
|
-
this.textures.push(
|
|
31
|
-
new Texture({
|
|
32
|
-
source: texture.source,
|
|
33
|
-
frame: new Rectangle(+x, +y, +this.tilewidth, +this.tileheight),
|
|
34
|
-
})
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
this.loadGroup();
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
}
|
package/src/Tilemap/index.ts
DELETED
|
@@ -1,222 +0,0 @@
|
|
|
1
|
-
import { TiledLayer, TiledLayerType, TiledMap, TiledParserFile, TiledTileset } from "@rpgjs/tiled"
|
|
2
|
-
import { loop, h, Container, TilingSprite, useProps, effect, signal } from "canvasengine"
|
|
3
|
-
import { CompositeTileLayer } from "./TileLayer"
|
|
4
|
-
import { TileSet } from "./TileSet"
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Reorganizes tile layers based on the z property of tiles
|
|
8
|
-
*
|
|
9
|
-
* This function analyzes each tile in the layer data and groups them by their z property.
|
|
10
|
-
* If a tile has a z property different from 0, it creates new layers for each z value.
|
|
11
|
-
*
|
|
12
|
-
* @param {TiledLayer[]} originalLayers - The original layers from the tilemap
|
|
13
|
-
* @param {TileSet[]} tilesets - Array of loaded tilesets
|
|
14
|
-
* @param {TiledMap} mapData - The complete map data
|
|
15
|
-
* @returns {TiledLayer[]} - Reorganized layers with tiles grouped by z property
|
|
16
|
-
*
|
|
17
|
-
* @example
|
|
18
|
-
* ```typescript
|
|
19
|
-
* const reorganizedLayers = reorganizeLayersByTileZ(mapData.layers, tilesets, mapData);
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
function reorganizeLayersByTileZ(originalLayers: TiledLayer[], tilesets: TileSet[], mapData: TiledMap): TiledLayer[] {
|
|
23
|
-
const reorganizedLayers: TiledLayer[] = [];
|
|
24
|
-
|
|
25
|
-
for (const layer of originalLayers) {
|
|
26
|
-
if (layer.type !== TiledLayerType.Tile) {
|
|
27
|
-
// Keep non-tile layers as they are
|
|
28
|
-
reorganizedLayers.push(layer);
|
|
29
|
-
continue;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
// Group tiles by their z property
|
|
33
|
-
const layersByZ = new Map<number, number[]>();
|
|
34
|
-
|
|
35
|
-
// Initialize empty arrays for all z values we'll find
|
|
36
|
-
// Don't pre-populate with original data anymore
|
|
37
|
-
|
|
38
|
-
// Ensure layer data is number array
|
|
39
|
-
let layerData: number[];
|
|
40
|
-
if (Array.isArray(layer.data)) {
|
|
41
|
-
layerData = layer.data.map(gid => {
|
|
42
|
-
if (typeof gid === 'number') {
|
|
43
|
-
return gid;
|
|
44
|
-
} else {
|
|
45
|
-
return parseInt(String(gid), 10);
|
|
46
|
-
}
|
|
47
|
-
});
|
|
48
|
-
} else {
|
|
49
|
-
// If data is a string, it might be compressed - for now, skip this layer
|
|
50
|
-
reorganizedLayers.push(layer);
|
|
51
|
-
continue;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
let tilesProcessed = 0;
|
|
55
|
-
let tilesWithZ = 0;
|
|
56
|
-
|
|
57
|
-
// Analyze each tile in the layer
|
|
58
|
-
for (let i = 0; i < layerData.length; i++) {
|
|
59
|
-
const gid = layerData[i];
|
|
60
|
-
|
|
61
|
-
if (gid === 0) continue; // Empty tile
|
|
62
|
-
|
|
63
|
-
tilesProcessed++;
|
|
64
|
-
|
|
65
|
-
// Find the corresponding tileset
|
|
66
|
-
let tileset: TileSet | undefined;
|
|
67
|
-
for (let j = tilesets.length - 1; j >= 0; j--) {
|
|
68
|
-
if (tilesets[j].firstgid && tilesets[j].firstgid <= gid) {
|
|
69
|
-
tileset = tilesets[j];
|
|
70
|
-
break;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
if (!tileset) {
|
|
75
|
-
// If no tileset found, put tile in z=0 layer
|
|
76
|
-
if (!layersByZ.has(0)) {
|
|
77
|
-
layersByZ.set(0, new Array(layerData.length).fill(0));
|
|
78
|
-
}
|
|
79
|
-
layersByZ.get(0)![i] = gid;
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
// Get tile properties from tileset
|
|
84
|
-
const localTileId = gid - tileset.firstgid;
|
|
85
|
-
// @ts-ignore
|
|
86
|
-
const tileProperties = tileset.tileset.tiles?.[localTileId]?.properties;
|
|
87
|
-
const zValue = tileProperties?.z ?? 0;
|
|
88
|
-
|
|
89
|
-
// Count tiles with explicit z property
|
|
90
|
-
if (tileProperties?.z !== undefined) {
|
|
91
|
-
tilesWithZ++;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
// Create or get the layer for this z value
|
|
95
|
-
if (!layersByZ.has(zValue)) {
|
|
96
|
-
layersByZ.set(zValue, new Array(layerData.length).fill(0));
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
// Place tile in the appropriate z layer
|
|
100
|
-
layersByZ.get(zValue)![i] = gid;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// Create layers for each z value, ensuring z=0 comes first
|
|
104
|
-
const sortedZValues = Array.from(layersByZ.keys()).sort((a, b) => a - b);
|
|
105
|
-
|
|
106
|
-
for (const zValue of sortedZValues) {
|
|
107
|
-
const layerDataForZ = layersByZ.get(zValue)!;
|
|
108
|
-
|
|
109
|
-
// Only create layer if it has tiles
|
|
110
|
-
if (layerDataForZ.some(gid => gid !== 0)) {
|
|
111
|
-
const newLayer = {
|
|
112
|
-
...layer,
|
|
113
|
-
name: `${layer.name}_z${zValue}`, // Always add _z suffix
|
|
114
|
-
data: layerDataForZ,
|
|
115
|
-
properties: {
|
|
116
|
-
...layer.properties,
|
|
117
|
-
z: zValue
|
|
118
|
-
}
|
|
119
|
-
};
|
|
120
|
-
|
|
121
|
-
reorganizedLayers.push(newLayer);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Sort final layers to ensure z=0 layers come first, then by z value
|
|
127
|
-
reorganizedLayers.sort((a, b) => {
|
|
128
|
-
const zA = a.properties?.z ?? 0.5;
|
|
129
|
-
const zB = b.properties?.z ?? 0.5;
|
|
130
|
-
return zA - zB;
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
return reorganizedLayers;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export function TiledMap(props) {
|
|
137
|
-
const { map, basePath, createLayersPerTilesZ } = useProps(props, {
|
|
138
|
-
createLayersPerTilesZ: false,
|
|
139
|
-
basePath: ''
|
|
140
|
-
})
|
|
141
|
-
const layers = signal<TiledLayer[]>([])
|
|
142
|
-
const objectLayer = props.objectLayer
|
|
143
|
-
let tilesets: TileSet[] = []
|
|
144
|
-
let mapData: TiledMap = {} as TiledMap
|
|
145
|
-
|
|
146
|
-
const parseTmx = async (file: string, relativePath: string = '') => {
|
|
147
|
-
if (typeof file !== 'string') {
|
|
148
|
-
return file
|
|
149
|
-
}
|
|
150
|
-
// @ts-ignore
|
|
151
|
-
const parser = new TiledParserFile(
|
|
152
|
-
file,
|
|
153
|
-
{
|
|
154
|
-
basePath: '',
|
|
155
|
-
staticDir: '',
|
|
156
|
-
relativePath
|
|
157
|
-
}
|
|
158
|
-
)
|
|
159
|
-
const data = await parser.parseFilePromise({
|
|
160
|
-
getOnlyBasename: false
|
|
161
|
-
})
|
|
162
|
-
|
|
163
|
-
return data
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
effect(async () => {
|
|
167
|
-
const _map = map()
|
|
168
|
-
if (_map) {
|
|
169
|
-
mapData = await parseTmx(_map, basePath())
|
|
170
|
-
tilesets = [] // Reset tilesets array
|
|
171
|
-
for (let tileSet of mapData.tilesets) {
|
|
172
|
-
// @ts-ignore
|
|
173
|
-
if (tileSet.tile) tileSet.tiles = tileSet.tile
|
|
174
|
-
if (!tileSet.tiles) tileSet.tiles = []
|
|
175
|
-
tilesets.push(await new TileSet(tileSet).load(tileSet.image.source))
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
// Reorganize layers by tile z property if enabled
|
|
179
|
-
let finalLayers = mapData.layers;
|
|
180
|
-
if (createLayersPerTilesZ()) {
|
|
181
|
-
finalLayers = reorganizeLayersByTileZ(mapData.layers, tilesets, mapData);
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
layers.set(finalLayers)
|
|
185
|
-
}
|
|
186
|
-
})
|
|
187
|
-
|
|
188
|
-
const createLayer = (layers, props = {}) => {
|
|
189
|
-
return h(Container, props, loop<any>(layers, (layer) => {
|
|
190
|
-
switch (layer.type) {
|
|
191
|
-
case TiledLayerType.Tile:
|
|
192
|
-
return h(CompositeTileLayer, {
|
|
193
|
-
tilewidth: mapData.tilewidth,
|
|
194
|
-
tileheight: mapData.tileheight,
|
|
195
|
-
// @ts-ignore
|
|
196
|
-
width: mapData.width,
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
height: mapData.height,
|
|
199
|
-
...layer,
|
|
200
|
-
tilesets
|
|
201
|
-
})
|
|
202
|
-
case TiledLayerType.Image:
|
|
203
|
-
const { width, height, source } = layer.image
|
|
204
|
-
return h(TilingSprite, {
|
|
205
|
-
image: source,
|
|
206
|
-
...layer,
|
|
207
|
-
width: layer.repeatx ? layer.width * layer.tilewidth : width,
|
|
208
|
-
height: layer.repeaty ? layer.height * layer.tileheight : height
|
|
209
|
-
})
|
|
210
|
-
case TiledLayerType.Group:
|
|
211
|
-
return createLayer(signal(layer.layers), layer)
|
|
212
|
-
case TiledLayerType.ObjectGroup:
|
|
213
|
-
const child = objectLayer?.(layer)
|
|
214
|
-
return h(Container, layer, child)
|
|
215
|
-
default:
|
|
216
|
-
return h(Container)
|
|
217
|
-
}
|
|
218
|
-
}))
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return h(Container, props, createLayer(layers))
|
|
222
|
-
}
|