@theatrejs/plugin-ldtk 1.3.0 → 2.0.0
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 +23 -141
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.0"
|
|
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,33 +8,10 @@ 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
|
-
/**
|
|
14
|
-
* @typedef {Object} TypeEntity A transformed LDTK JSON entity.
|
|
15
|
-
* @property {string} TypeEntity.$identifier The identifier.
|
|
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.
|
|
31
|
-
* @protected
|
|
32
|
-
*
|
|
33
|
-
* @memberof Ldtk
|
|
34
|
-
*/
|
|
35
|
-
|
|
36
15
|
/**
|
|
37
16
|
* @typedef {Object} TypeLdtkDefinitionLayerGridValue A LDTK JSON data layer grid value definition.
|
|
38
17
|
* @property {string} TypeLdtkDefinitionLayerGridValue.identifier The identifier.
|
|
@@ -141,61 +120,26 @@ class Ldtk {
|
|
|
141
120
|
}
|
|
142
121
|
|
|
143
122
|
/**
|
|
144
|
-
*
|
|
123
|
+
* Creates a grid of LDTK entities from the given level on the given layer.
|
|
145
124
|
* @param {Object} $parameters The given parameters.
|
|
146
125
|
* @param {string} $parameters.$layer The layer of the entities to get.
|
|
147
126
|
* @param {string} $parameters.$level The level of the entities to get.
|
|
148
|
-
* @returns {
|
|
127
|
+
* @returns {Grid<LdtkEntity>}
|
|
149
128
|
* @public
|
|
150
129
|
*/
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
const level = this.$data.levels
|
|
154
|
-
.find(($current) => ($current.identifier === $level));
|
|
155
|
-
|
|
156
|
-
if (typeof level === 'undefined') {
|
|
157
|
-
|
|
158
|
-
return [];
|
|
159
|
-
}
|
|
130
|
+
createGrid({$layer, $level}) {
|
|
160
131
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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}) {
|
|
132
|
+
/**
|
|
133
|
+
* @type {Grid<LdtkEntity>}
|
|
134
|
+
*/
|
|
135
|
+
const grid = new Grid();
|
|
192
136
|
|
|
193
137
|
const level = this.$data.levels
|
|
194
138
|
.find(($current) => ($current.identifier === $level));
|
|
195
139
|
|
|
196
140
|
if (typeof level === 'undefined') {
|
|
197
141
|
|
|
198
|
-
return
|
|
142
|
+
return grid;
|
|
199
143
|
}
|
|
200
144
|
|
|
201
145
|
const layer = level
|
|
@@ -203,86 +147,24 @@ class Ldtk {
|
|
|
203
147
|
|
|
204
148
|
if (typeof layer === 'undefined') {
|
|
205
149
|
|
|
206
|
-
return
|
|
150
|
+
return grid;
|
|
207
151
|
}
|
|
208
152
|
|
|
209
|
-
|
|
210
|
-
.entityInstances
|
|
211
|
-
.map(($entity) => (window.structuredClone($entity)));
|
|
212
|
-
}
|
|
153
|
+
layer.entityInstances.forEach(($entity) => {
|
|
213
154
|
|
|
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}) {
|
|
223
|
-
|
|
224
|
-
const level = this.$data.levels
|
|
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));
|
|
155
|
+
const [x, y] = $entity.__grid;
|
|
242
156
|
|
|
243
|
-
|
|
157
|
+
const entity = new LdtkEntity({
|
|
244
158
|
|
|
245
|
-
|
|
246
|
-
|
|
159
|
+
$data: window.structuredClone($entity.fieldInstances),
|
|
160
|
+
$identifier: $entity.iid,
|
|
161
|
+
$label: $entity.__identifier
|
|
162
|
+
});
|
|
247
163
|
|
|
248
|
-
|
|
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));
|
|
271
|
-
|
|
272
|
-
if (typeof level === 'undefined') {
|
|
273
|
-
|
|
274
|
-
return;
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
const layer = level
|
|
278
|
-
.layerInstances.find(($current) => ($current.__identifier === $layer));
|
|
279
|
-
|
|
280
|
-
if (typeof layer === 'undefined') {
|
|
281
|
-
|
|
282
|
-
return;
|
|
283
|
-
}
|
|
164
|
+
grid.set(new Vector2(x, y), entity);
|
|
165
|
+
});
|
|
284
166
|
|
|
285
|
-
return
|
|
167
|
+
return grid;
|
|
286
168
|
}
|
|
287
169
|
}
|
|
288
170
|
|