@canvasengine/tiled 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/classes/Gid.d.ts +13 -0
- package/dist/classes/Layer.d.ts +26 -0
- package/dist/classes/Map.d.ts +232 -0
- package/dist/classes/Object.d.ts +8 -0
- package/dist/classes/Properties.d.ts +11 -0
- package/dist/classes/Tile.d.ts +19 -0
- package/dist/classes/Tileset.d.ts +13 -0
- package/dist/generate/tileset.d.ts +11 -0
- package/dist/generate/wangtile.d.ts +21 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +6205 -0
- package/dist/index.js.map +1 -0
- package/dist/parser/open-file.d.ts +24 -0
- package/dist/parser/parser.d.ts +27 -0
- package/dist/types/Layer.d.ts +116 -0
- package/dist/types/Map.d.ts +72 -0
- package/dist/types/Objects.d.ts +57 -0
- package/dist/types/Text.d.ts +47 -0
- package/dist/types/Tile.d.ts +17 -0
- package/dist/types/Tileset.d.ts +92 -0
- package/dist/types/Types.d.ts +147 -0
- package/dist/types/WorldMaps.d.ts +12 -0
- package/dist/utils.d.ts +10 -0
- package/package.json +31 -0
- package/readme.md +1 -0
- package/src/classes/Gid.ts +46 -0
- package/src/classes/Layer.ts +135 -0
- package/src/classes/Map.ts +443 -0
- package/src/classes/Object.ts +16 -0
- package/src/classes/Properties.ts +30 -0
- package/src/classes/Tile.ts +22 -0
- package/src/classes/Tileset.ts +34 -0
- package/src/generate/tileset.ts +35 -0
- package/src/generate/wangtile.ts +166 -0
- package/src/index.ts +16 -0
- package/src/parser/open-file.ts +155 -0
- package/src/parser/parser.ts +309 -0
- package/src/types/Layer.ts +127 -0
- package/src/types/Map.ts +83 -0
- package/src/types/Objects.ts +64 -0
- package/src/types/Text.ts +47 -0
- package/src/types/Tile.ts +19 -0
- package/src/types/Tileset.ts +99 -0
- package/src/types/Types.ts +157 -0
- package/src/types/WorldMaps.ts +13 -0
- package/src/utils.ts +22 -0
- package/tests/class.spec.ts +88 -0
- package/tests/data.ts +5440 -0
- package/tests/parser.spec.ts +112 -0
- package/tests/tile-properties.spec.ts +99 -0
- package/tests/tiledmap-multi-layers.spec.ts +99 -0
- package/tests/tiledmap.spec.ts +223 -0
- package/tsconfig.json +28 -0
- package/vite.config.ts +21 -0
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { TilesetTile } from "./Tile";
|
|
2
|
+
import { TiledGrid, TiledMapTerrain, TiledProperty, TiledTileOffset, TiledWangSet } from "./Types";
|
|
3
|
+
|
|
4
|
+
export interface TiledTileset {
|
|
5
|
+
type: 'tileset';
|
|
6
|
+
/**
|
|
7
|
+
* The JSON format version
|
|
8
|
+
*/
|
|
9
|
+
version: number;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* GID corresponding to the first tile in the set
|
|
13
|
+
*/
|
|
14
|
+
firstgid: number;
|
|
15
|
+
/**
|
|
16
|
+
* Image used for tiles in this set
|
|
17
|
+
*/
|
|
18
|
+
image: {
|
|
19
|
+
source: string
|
|
20
|
+
height: number
|
|
21
|
+
width: number
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Height of source image in pixels
|
|
25
|
+
*/
|
|
26
|
+
imageheight: number;
|
|
27
|
+
/**
|
|
28
|
+
* Width of source image in pixels
|
|
29
|
+
*/
|
|
30
|
+
imagewidth: number;
|
|
31
|
+
/**
|
|
32
|
+
* (optional)
|
|
33
|
+
*/
|
|
34
|
+
grid: TiledGrid;
|
|
35
|
+
/**
|
|
36
|
+
* Buffer between image edge and first tile (pixels)
|
|
37
|
+
*/
|
|
38
|
+
margin: number;
|
|
39
|
+
/**
|
|
40
|
+
* Alignment to use for tile objects (unspecified (default), topleft, top, topright, left, center, right, bottomleft, bottom or bottomright) (since 1.4)
|
|
41
|
+
*/
|
|
42
|
+
objectalignment: 'unspecified' | 'topleft' | 'top' | 'topright' | 'left' | 'center' | 'right' | 'bottomleft' | 'bottom' | 'bottomright';
|
|
43
|
+
/**
|
|
44
|
+
* Refers to external tileset file (should be JSON)
|
|
45
|
+
*/
|
|
46
|
+
source: string;
|
|
47
|
+
/**
|
|
48
|
+
* Spacing between adjacent tiles in image (pixels)
|
|
49
|
+
*/
|
|
50
|
+
spacing: number;
|
|
51
|
+
|
|
52
|
+
columns: number;
|
|
53
|
+
rows: number;
|
|
54
|
+
/**
|
|
55
|
+
* Maximum height of tiles in this set
|
|
56
|
+
*/
|
|
57
|
+
tileheight: number;
|
|
58
|
+
tilewidth: number;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Array of Tiles (optional)
|
|
62
|
+
*/
|
|
63
|
+
tiles: TilesetTile[];
|
|
64
|
+
|
|
65
|
+
name: string;
|
|
66
|
+
properties: {
|
|
67
|
+
[key: string]: any
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The number of tiles in this tileset
|
|
71
|
+
*/
|
|
72
|
+
tilecount: number;
|
|
73
|
+
/**
|
|
74
|
+
* Optional
|
|
75
|
+
*/
|
|
76
|
+
tileoffset: TiledTileOffset;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* The Tiled version used to save the file
|
|
80
|
+
*/
|
|
81
|
+
tiledversion: string;
|
|
82
|
+
/**
|
|
83
|
+
* Hex-formatted color (#RRGGBB or #AARRGGBB) (optional)
|
|
84
|
+
*/
|
|
85
|
+
backgroundcolor: string;
|
|
86
|
+
/**
|
|
87
|
+
* Hex-formatted color (#RRGGBB) (optional)
|
|
88
|
+
*/
|
|
89
|
+
transparentcolor: string;
|
|
90
|
+
/**
|
|
91
|
+
* Array of Terrains (optional)
|
|
92
|
+
*/
|
|
93
|
+
terrains: TiledMapTerrain[];
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Array of Wang sets (since 1.1.5)
|
|
97
|
+
*/
|
|
98
|
+
wangsets: TiledWangSet[];
|
|
99
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
export interface TiledProperty<T = unknown> {
|
|
2
|
+
/**
|
|
3
|
+
* Name of the property
|
|
4
|
+
*/
|
|
5
|
+
name: string;
|
|
6
|
+
/**
|
|
7
|
+
* Type of the property (string (default), int, float, bool, color or file (since 0.16, with color and file added in 0.17))
|
|
8
|
+
*/
|
|
9
|
+
type: 'string' | 'int' | 'float' | 'bool' | 'color' | 'file';
|
|
10
|
+
/**
|
|
11
|
+
* Value of the property
|
|
12
|
+
*/
|
|
13
|
+
value: T;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type TiledEncoding = 'csv' | 'base64';
|
|
17
|
+
export type TiledCompression = 'zlib' | 'gzip' | 'zstd';
|
|
18
|
+
|
|
19
|
+
export interface TiledChunk {
|
|
20
|
+
/**
|
|
21
|
+
* Array of unsigned int (GIDs) or base64-encoded data
|
|
22
|
+
*/
|
|
23
|
+
data: number[] | string;
|
|
24
|
+
/**
|
|
25
|
+
* Height in tiles
|
|
26
|
+
*/
|
|
27
|
+
height: number;
|
|
28
|
+
/**
|
|
29
|
+
* Width in tiles
|
|
30
|
+
*/
|
|
31
|
+
width: number;
|
|
32
|
+
/**
|
|
33
|
+
* X coordinate in tiles
|
|
34
|
+
*/
|
|
35
|
+
x: number;
|
|
36
|
+
/**
|
|
37
|
+
* Y coordinate in tiles
|
|
38
|
+
*/
|
|
39
|
+
y: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface TiledTileOffset {
|
|
43
|
+
/**
|
|
44
|
+
* Horizontal offset in pixels
|
|
45
|
+
*/
|
|
46
|
+
x: number;
|
|
47
|
+
/**
|
|
48
|
+
* Vertical offset in pixels (positive is down)
|
|
49
|
+
*/
|
|
50
|
+
y: number;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface TiledWangSet {
|
|
54
|
+
/**
|
|
55
|
+
* Array of Wang colors
|
|
56
|
+
*/
|
|
57
|
+
cornercolors: TiledWangColor[];
|
|
58
|
+
/**
|
|
59
|
+
* Array of Wang colors
|
|
60
|
+
*/
|
|
61
|
+
edgecolors: TiledWangColor[];
|
|
62
|
+
name: string;
|
|
63
|
+
properties: TiledProperty[];
|
|
64
|
+
/**
|
|
65
|
+
* Local ID of tile representing the Wang set
|
|
66
|
+
*/
|
|
67
|
+
tile: number;
|
|
68
|
+
wangtiles: TiledWangTile[];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface TiledWangTile {
|
|
72
|
+
/**
|
|
73
|
+
* Array of Wang color indexes (uchar[8]
|
|
74
|
+
*/
|
|
75
|
+
wangid: number[];
|
|
76
|
+
/**
|
|
77
|
+
* Tile is flipped diagonally (default: false)
|
|
78
|
+
*/
|
|
79
|
+
dflip: boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Tile is flipped horizontally (default: false)
|
|
82
|
+
*/
|
|
83
|
+
hflip: boolean;
|
|
84
|
+
/**
|
|
85
|
+
* Local ID of tile
|
|
86
|
+
*/
|
|
87
|
+
tileid: number;
|
|
88
|
+
/**
|
|
89
|
+
* Tile is flipped vertically (default: false)
|
|
90
|
+
*/
|
|
91
|
+
vflip: boolean;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface TiledWangColor {
|
|
95
|
+
/**
|
|
96
|
+
* Hex-formatted color (#RRGGBB or #AARRGGBB)
|
|
97
|
+
*/
|
|
98
|
+
color: string;
|
|
99
|
+
/**
|
|
100
|
+
* Name of the Wang color
|
|
101
|
+
*/
|
|
102
|
+
name: string;
|
|
103
|
+
/**
|
|
104
|
+
* Probability used when randomizing
|
|
105
|
+
*/
|
|
106
|
+
probability: number;
|
|
107
|
+
/**
|
|
108
|
+
* Local ID of tile representing the Wang color
|
|
109
|
+
*/
|
|
110
|
+
tile: number;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export interface TiledGrid {
|
|
114
|
+
/**
|
|
115
|
+
* orthogonal (default) or isometric
|
|
116
|
+
*/
|
|
117
|
+
orientation: 'orthogonal' | 'isometric';
|
|
118
|
+
/**
|
|
119
|
+
* Cell width of tile grid
|
|
120
|
+
*/
|
|
121
|
+
width: number;
|
|
122
|
+
/**
|
|
123
|
+
* Cell height of tile grid
|
|
124
|
+
*/
|
|
125
|
+
height: number;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface TiledFrame {
|
|
129
|
+
/**
|
|
130
|
+
* Frame duration in milliseconds
|
|
131
|
+
*/
|
|
132
|
+
duration: number;
|
|
133
|
+
/**
|
|
134
|
+
* Local tile ID representing this frame
|
|
135
|
+
*/
|
|
136
|
+
tileid: number;
|
|
137
|
+
}
|
|
138
|
+
export interface TiledMapTerrain {
|
|
139
|
+
name: string;
|
|
140
|
+
/**
|
|
141
|
+
* Local ID of tile representing terrain
|
|
142
|
+
*/
|
|
143
|
+
tile: number;
|
|
144
|
+
properties: TiledProperty[];
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface TiledPoint {
|
|
148
|
+
x: number;
|
|
149
|
+
y: number;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export interface TiledImage {
|
|
153
|
+
source: string,
|
|
154
|
+
width: number,
|
|
155
|
+
height: number
|
|
156
|
+
trans?: string
|
|
157
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function isTiledFormat(val: any): boolean {
|
|
2
|
+
return typeof val == 'object' && val.version && val.orientation
|
|
3
|
+
}
|
|
4
|
+
/**
|
|
5
|
+
* Join path segments with forward slashes
|
|
6
|
+
* @param {...string} segments - Path segments to join
|
|
7
|
+
* @returns {string} Joined path
|
|
8
|
+
* @example
|
|
9
|
+
* joinPath('base', 'static', 'file.json') // returns 'base/static/file.json'
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export function joinPath(...segments: string[]): string {
|
|
13
|
+
return segments
|
|
14
|
+
.filter(segment => segment && segment.length > 0)
|
|
15
|
+
.join('/')
|
|
16
|
+
.replace(/\/+/g, '/') // Replace multiple slashes with single slash
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
export function getBaseName(path: string): string {
|
|
21
|
+
return path.substring(0, path.lastIndexOf('/') + 1)
|
|
22
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { TiledParser } from "../src/parser/parser"
|
|
2
|
+
import { tileset, xmlObjectImage, xmlTile } from "./data"
|
|
3
|
+
import { Tileset } from '../src/classes/Tileset'
|
|
4
|
+
import { Layer, Tile, TiledLayerType, TiledObjectClass } from '../src'
|
|
5
|
+
import { test, expect, describe, beforeEach } from 'vitest'
|
|
6
|
+
|
|
7
|
+
let layer: Layer
|
|
8
|
+
|
|
9
|
+
function setLayer(xml) {
|
|
10
|
+
const parser = new TiledParser(xml)
|
|
11
|
+
const map = parser.parseMap()
|
|
12
|
+
const parserTileset = new TiledParser(tileset)
|
|
13
|
+
const tilesetData = parserTileset.parseTileset()
|
|
14
|
+
const tilesetInstance = new Tileset(tilesetData)
|
|
15
|
+
map.layers[0].properties = {}
|
|
16
|
+
map.layers[0].properties['cache-tiles'] = true
|
|
17
|
+
layer = new Layer(map.layers[0], [tilesetInstance])
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
describe('Tests classes', () => {
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
setLayer(xmlTile)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
test('Verify tiles array', () => {
|
|
27
|
+
expect(layer.tiles).toBeDefined()
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('tiles is not empty', () => {
|
|
31
|
+
expect(layer.tiles).toHaveLength(layer.width * layer.height)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
test('tile is Tile instance', () => {
|
|
35
|
+
expect(layer.tiles[0]).toBeInstanceOf(Tile)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
test('Get Gid', () => {
|
|
39
|
+
const tile = layer.tiles[0]
|
|
40
|
+
expect(tile?.gid).toBe(9)
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('Get Gid Flip', () => {
|
|
44
|
+
const tile = layer.tiles[1]
|
|
45
|
+
expect(tile?.horizontalFlip).toBe(true)
|
|
46
|
+
expect(tile?.verticalFlip).toBe(false)
|
|
47
|
+
expect(tile?.diagonalFlip).toBe(true)
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
describe('Tests classes for objectgroup', () => {
|
|
54
|
+
|
|
55
|
+
beforeEach(() => {
|
|
56
|
+
setLayer(xmlObjectImage)
|
|
57
|
+
})
|
|
58
|
+
|
|
59
|
+
test('Verify Type', () => {
|
|
60
|
+
expect(layer.type).toBe(TiledLayerType.ObjectGroup)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
test('has objects property', () => {
|
|
64
|
+
expect(layer).toHaveProperty('objects')
|
|
65
|
+
expect(layer.objects).toHaveLength(1)
|
|
66
|
+
})
|
|
67
|
+
|
|
68
|
+
test('TiledObjectClass instance', () => {
|
|
69
|
+
const object = layer.objects[0]
|
|
70
|
+
expect(object).toBeInstanceOf(TiledObjectClass)
|
|
71
|
+
expect(object).toHaveProperty('x')
|
|
72
|
+
expect(object).toHaveProperty('y')
|
|
73
|
+
expect(object).toHaveProperty('width')
|
|
74
|
+
expect(object).toHaveProperty('height')
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test('get gid', () => {
|
|
78
|
+
const object = layer.objects[0]
|
|
79
|
+
expect(object.gid).toBe(1)
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
test('test flip', () => {
|
|
83
|
+
const object = layer.objects[0]
|
|
84
|
+
expect(object?.horizontalFlip).toBe(true)
|
|
85
|
+
expect(object?.verticalFlip).toBe(false)
|
|
86
|
+
expect(object?.diagonalFlip).toBe(false)
|
|
87
|
+
})
|
|
88
|
+
})
|