@theatrejs/plugin-ldtk 1.3.1 → 2.0.1
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/README.md +2 -9
- package/package.json +2 -2
- package/sources/index.js +1 -0
- package/sources/ldtk-entity.js +91 -0
- package/sources/ldtk.js +27 -136
package/README.md
CHANGED
|
@@ -15,19 +15,12 @@ npm install @theatrejs/plugin-ldtk --save
|
|
|
15
15
|
```javascript
|
|
16
16
|
import * as PLUGIN_LDTK from '@theatrejs/plugin-ldtk';
|
|
17
17
|
|
|
18
|
-
import
|
|
18
|
+
import ldtk from './ldtk.json';
|
|
19
19
|
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
const entities = ldtk.getEntities({
|
|
20
|
+
const grid = ldtk.createGrid({
|
|
23
21
|
$level: 'Prototype',
|
|
24
22
|
$layer: 'actors'
|
|
25
23
|
});
|
|
26
|
-
|
|
27
|
-
const grid = ldtk.getGrid({
|
|
28
|
-
$level: 'Prototype',
|
|
29
|
-
$layer: 'grid'
|
|
30
|
-
});
|
|
31
24
|
```
|
|
32
25
|
|
|
33
26
|
## [API](https://theatrejs.github.io/plugin-ldtk/index.html)
|
package/package.json
CHANGED
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"name": "@theatrejs/plugin-ldtk",
|
|
40
40
|
"peerDependencies": {
|
|
41
41
|
|
|
42
|
-
"@theatrejs/theatrejs": ">= 1.
|
|
42
|
+
"@theatrejs/theatrejs": ">= 1.25.2"
|
|
43
43
|
},
|
|
44
44
|
"repository": {
|
|
45
45
|
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"postversion": "node ./tools/custom/postversion.cjs"
|
|
55
55
|
},
|
|
56
56
|
"type": "module",
|
|
57
|
-
"version": "
|
|
57
|
+
"version": "2.0.1"
|
|
58
58
|
}
|
package/sources/index.js
CHANGED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates LDTK entities.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
*
|
|
6
|
+
* const entity = new LdtkEntity({$data, $identifier, $label});
|
|
7
|
+
*/
|
|
8
|
+
class LdtkEntity {
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @typedef {Object} TypeLdtkEntityField A LDTK JSON data custom field.
|
|
12
|
+
* @property {string} TypeLdtkEntityField.__identifier The identifier.
|
|
13
|
+
* @property {string} TypeLdtkEntityField.__type The type.
|
|
14
|
+
* @property {any} TypeLdtkEntityField.__value The value.
|
|
15
|
+
* @protected
|
|
16
|
+
*
|
|
17
|
+
* @memberof LdtkEntity
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Stores the LDTK JSON data custom fields.
|
|
22
|
+
* @type {Array<TypeLdtkEntityField>}
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
$data;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Stores the identifier.
|
|
29
|
+
* @type {string}
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
$identifier;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Stores the label.
|
|
36
|
+
* @type {string}
|
|
37
|
+
* @private
|
|
38
|
+
*/
|
|
39
|
+
$label;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Gets the LDTK JSON data custom fields.
|
|
43
|
+
* @type {Array<TypeLdtkEntityField>}
|
|
44
|
+
* @public
|
|
45
|
+
*/
|
|
46
|
+
get data() {
|
|
47
|
+
|
|
48
|
+
return this.$data;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Gets the identifier.
|
|
53
|
+
* @type {string}
|
|
54
|
+
* @public
|
|
55
|
+
*/
|
|
56
|
+
get identifier() {
|
|
57
|
+
|
|
58
|
+
return this.$identifier;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Gets the label.
|
|
63
|
+
* @type {string}
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
66
|
+
get label() {
|
|
67
|
+
|
|
68
|
+
return this.$label;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a new LDTK entity.
|
|
73
|
+
* @param {Object} $parameters The given parameters.
|
|
74
|
+
* @param {Array<TypeLdtkEntityField>} $parameters.$data The LDTK JSON data custom fields.
|
|
75
|
+
* @param {string} $parameters.$identifier The identifier of the entity to get.
|
|
76
|
+
* @param {string} $parameters.$label The label of the entity to get.
|
|
77
|
+
*/
|
|
78
|
+
constructor({$data, $identifier, $label}) {
|
|
79
|
+
|
|
80
|
+
this.$data = $data;
|
|
81
|
+
this.$identifier = $identifier;
|
|
82
|
+
this.$label = $label;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export {
|
|
87
|
+
|
|
88
|
+
LdtkEntity
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export default LdtkEntity;
|
package/sources/ldtk.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {Vector2} from '@theatrejs/theatrejs';
|
|
1
|
+
import {Grid, Vector2} from '@theatrejs/theatrejs';
|
|
2
|
+
|
|
3
|
+
import {LdtkEntity} from './index.js';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Creates LDTK module managers.
|
|
@@ -6,28 +8,13 @@ import {Vector2} from '@theatrejs/theatrejs';
|
|
|
6
8
|
* @example
|
|
7
9
|
*
|
|
8
10
|
* const ldtk = new Ldtk(data);
|
|
9
|
-
* ldtk.
|
|
11
|
+
* ldtk.createGrid({$level, $layer});
|
|
10
12
|
*/
|
|
11
13
|
class Ldtk {
|
|
12
14
|
|
|
13
15
|
/**
|
|
14
|
-
* @typedef {Object}
|
|
15
|
-
* @property {string}
|
|
16
|
-
* @property {Vector2} TypeEntity.$position The position.
|
|
17
|
-
* @property {string} TypeEntity.$type The type.
|
|
18
|
-
* @protected
|
|
19
|
-
*
|
|
20
|
-
* @memberof Ldtk
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @typedef {Object} TypeGrid A transformed LDTK JSON grid.
|
|
25
|
-
* @property {Vector2} TypeGrid.$cell The size of each cell.
|
|
26
|
-
* @property {Array<number>} TypeGrid.$data The flat data (one-dimensional).
|
|
27
|
-
* @property {Map<number, string>} TypeGrid.$definitions The data definitions.
|
|
28
|
-
* @property {number} TypeGrid.$height The number of cells on the y-axis.
|
|
29
|
-
* @property {Vector2} TypeGrid.$position The position.
|
|
30
|
-
* @property {number} TypeGrid.$width The number of cells on the x-axis.
|
|
16
|
+
* @typedef {Object} TypeLdtkDefinitionEntity A LDTK JSON data entity definition.
|
|
17
|
+
* @property {string} TypeLdtkDefinitionEntity.identifier The identifier.
|
|
31
18
|
* @protected
|
|
32
19
|
*
|
|
33
20
|
* @memberof Ldtk
|
|
@@ -53,7 +40,8 @@ class Ldtk {
|
|
|
53
40
|
|
|
54
41
|
/**
|
|
55
42
|
* @typedef {Object} TypeLdtkDefinitions A LDTK JSON data definition.
|
|
56
|
-
* @property {Array<
|
|
43
|
+
* @property {Array<TypeLdtkDefinitionEntity>} TypeLdtkDefinitions.entities The entities' definitions.
|
|
44
|
+
* @property {Array<TypeLdtkDefinitionLayer>} TypeLdtkDefinitions.layers The layers' definitions.
|
|
57
45
|
* @protected
|
|
58
46
|
*
|
|
59
47
|
* @memberof Ldtk
|
|
@@ -141,61 +129,26 @@ class Ldtk {
|
|
|
141
129
|
}
|
|
142
130
|
|
|
143
131
|
/**
|
|
144
|
-
*
|
|
132
|
+
* Creates a grid of LDTK entities from the given level on the given layer.
|
|
145
133
|
* @param {Object} $parameters The given parameters.
|
|
146
134
|
* @param {string} $parameters.$layer The layer of the entities to get.
|
|
147
135
|
* @param {string} $parameters.$level The level of the entities to get.
|
|
148
|
-
* @returns {
|
|
136
|
+
* @returns {Grid<LdtkEntity>}
|
|
149
137
|
* @public
|
|
150
138
|
*/
|
|
151
|
-
|
|
139
|
+
createGrid({$layer, $level}) {
|
|
152
140
|
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
return [];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const layer = level
|
|
162
|
-
.layerInstances.find(($current) => ($current.__identifier === $layer));
|
|
163
|
-
|
|
164
|
-
if (typeof layer === 'undefined') {
|
|
165
|
-
|
|
166
|
-
return [];
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return layer
|
|
170
|
-
.entityInstances
|
|
171
|
-
.map(($entity) => ({
|
|
172
|
-
|
|
173
|
-
$identifier: $entity.iid,
|
|
174
|
-
$type: $entity.__identifier,
|
|
175
|
-
$position: new Vector2(
|
|
176
|
-
|
|
177
|
-
$entity.px[0] - (level.pxWid / 2),
|
|
178
|
-
- ($entity.px[1] - (level.pxHei / 2))
|
|
179
|
-
)
|
|
180
|
-
}));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* Gets the LDTK JSON data entities from the given level on the given layer.
|
|
185
|
-
* @param {Object} $parameters The given parameters.
|
|
186
|
-
* @param {string} $parameters.$layer The layer of the entities to get.
|
|
187
|
-
* @param {string} $parameters.$level The level of the entities to get.
|
|
188
|
-
* @returns {Array<TypeLdtkEntity>}
|
|
189
|
-
* @public
|
|
190
|
-
*/
|
|
191
|
-
getEntitiesData({$layer, $level}) {
|
|
141
|
+
/**
|
|
142
|
+
* @type {Grid<LdtkEntity>}
|
|
143
|
+
*/
|
|
144
|
+
const grid = new Grid();
|
|
192
145
|
|
|
193
146
|
const level = this.$data.levels
|
|
194
147
|
.find(($current) => ($current.identifier === $level));
|
|
195
148
|
|
|
196
149
|
if (typeof level === 'undefined') {
|
|
197
150
|
|
|
198
|
-
return
|
|
151
|
+
return grid;
|
|
199
152
|
}
|
|
200
153
|
|
|
201
154
|
const layer = level
|
|
@@ -203,86 +156,24 @@ class Ldtk {
|
|
|
203
156
|
|
|
204
157
|
if (typeof layer === 'undefined') {
|
|
205
158
|
|
|
206
|
-
return
|
|
159
|
+
return grid;
|
|
207
160
|
}
|
|
208
161
|
|
|
209
|
-
|
|
210
|
-
.entityInstances
|
|
211
|
-
.map(($entity) => (window.structuredClone($entity)));
|
|
212
|
-
}
|
|
162
|
+
layer.entityInstances.forEach(($entity) => {
|
|
213
163
|
|
|
214
|
-
|
|
215
|
-
* Gets the grid from the given level on the given layer.
|
|
216
|
-
* @param {Object} $parameters The given parameters.
|
|
217
|
-
* @param {string} $parameters.$layer The layer of the grid to get.
|
|
218
|
-
* @param {string} $parameters.$level The level of the grid to get.
|
|
219
|
-
* @returns {(TypeGrid | undefined)}
|
|
220
|
-
* @public
|
|
221
|
-
*/
|
|
222
|
-
getGrid({$layer, $level}) {
|
|
164
|
+
const [x, y] = $entity.__grid;
|
|
223
165
|
|
|
224
|
-
|
|
225
|
-
.find(($current) => ($current.identifier === $level));
|
|
226
|
-
|
|
227
|
-
if (typeof level === 'undefined') {
|
|
228
|
-
|
|
229
|
-
return;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const layer = level
|
|
233
|
-
.layerInstances.find(($current) => ($current.__identifier === $layer));
|
|
234
|
-
|
|
235
|
-
if (typeof layer === 'undefined') {
|
|
236
|
-
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
const definition = this.$data.defs.layers
|
|
241
|
-
.find(($current) => ($current.identifier === $layer));
|
|
242
|
-
|
|
243
|
-
if (typeof definition === 'undefined') {
|
|
244
|
-
|
|
245
|
-
return;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
return {
|
|
249
|
-
|
|
250
|
-
$data: [...layer.intGridCsv],
|
|
251
|
-
$definitions: new Map(definition.intGridValues.map(($definition) => ([$definition.value, $definition.identifier]))),
|
|
252
|
-
$cell: new Vector2(layer.__gridSize, layer.__gridSize),
|
|
253
|
-
$width: layer.__cWid,
|
|
254
|
-
$height: layer.__cHei,
|
|
255
|
-
$position: new Vector2(level.pxWid / 2, level.pxHei / 2)
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Gets the LDTK JSON data layer grid from the given level on the given layer.
|
|
261
|
-
* @param {Object} $parameters The given parameters.
|
|
262
|
-
* @param {string} $parameters.$layer The layer of the grid to get.
|
|
263
|
-
* @param {string} $parameters.$level The level of the grid to get.
|
|
264
|
-
* @returns {TypeLdtkLayer}
|
|
265
|
-
* @public
|
|
266
|
-
*/
|
|
267
|
-
getGridData({$layer, $level}) {
|
|
268
|
-
|
|
269
|
-
const level = this.$data.levels
|
|
270
|
-
.find(($current) => ($current.identifier === $level));
|
|
166
|
+
const entity = new LdtkEntity({
|
|
271
167
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
168
|
+
$data: window.structuredClone($entity.fieldInstances),
|
|
169
|
+
$identifier: $entity.iid,
|
|
170
|
+
$label: $entity.__identifier
|
|
171
|
+
});
|
|
276
172
|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (typeof layer === 'undefined') {
|
|
281
|
-
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
173
|
+
grid.set(new Vector2(x, y), entity);
|
|
174
|
+
});
|
|
284
175
|
|
|
285
|
-
return
|
|
176
|
+
return grid;
|
|
286
177
|
}
|
|
287
178
|
}
|
|
288
179
|
|