@theatrejs/plugin-ldtk 1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024-present Alexandre Blondeau (deformhead) deformhead@gmail.com
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,33 @@
1
+ [![Copyright](https://img.shields.io/badge/©-deformhead-white.svg)](https://github.com/deformhead) [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/theatrejs/plugin-ldtk/blob/master/LICENSE) [![Bundle Size (Gzipped)](https://img.shields.io/bundlejs/size/@theatrejs/plugin-ldtk@latest)](https://www.npmjs.com/package/@theatrejs/plugin-ldtk/v/latest) [![NPM Version](https://img.shields.io/npm/v/@theatrejs/plugin-ldtk/latest)](https://www.npmjs.com/package/@theatrejs/plugin-ldtk/v/latest)
2
+
3
+ # LDTK Plugin
4
+
5
+ > *🛠️ A Plugin for LDTK JDON data.*
6
+
7
+ ## Installation
8
+
9
+ ```shell
10
+ npm install @theatrejs/plugin-ldtk --save
11
+ ```
12
+
13
+ ## Quick Start
14
+
15
+ ```javascript
16
+ import * as PLUGINLDTK from '@theatrejs/plugin-ldtk';
17
+
18
+ import ldtkData from './ldtk.json';
19
+
20
+ const ldtk = new PLUGINLDTK.Ldtk(ldtkData);
21
+
22
+ const entities = ldtk.getEntities({
23
+ $level: 'Prototype',
24
+ $layer: 'actors'
25
+ });
26
+
27
+ const grid = ldtk.getGrid({
28
+ $level: 'Prototype',
29
+ $layer: 'grid'
30
+ });
31
+ ```
32
+
33
+ ## [API](https://theatrejs.github.io/plugin-ldtk/index.html)
package/package.json ADDED
@@ -0,0 +1,58 @@
1
+ {
2
+ "author": "Alexandre Blondeau",
3
+ "description": "🛠️ A Plugin for LDTK JDON data.",
4
+ "devDependencies": {
5
+
6
+ "docdash": "2.0.2",
7
+ "jsdoc": "4.0.3"
8
+ },
9
+ "engines": {
10
+
11
+ "node": "16.13.0",
12
+ "npm": "8.1.0"
13
+ },
14
+ "files": [
15
+
16
+ "sources/"
17
+ ],
18
+ "homepage": "https://github.com/theatrejs/plugin-ldtk",
19
+ "keywords": [
20
+
21
+ "2d",
22
+ "canvas",
23
+ "engine",
24
+ "game",
25
+ "game-engine",
26
+ "html",
27
+ "html5",
28
+ "javascript",
29
+ "ldtk",
30
+ "pixel-art",
31
+ "plugin",
32
+ "theatrejs",
33
+ "theatrejs-plugin",
34
+ "webgl",
35
+ "webgl2"
36
+ ],
37
+ "license": "MIT",
38
+ "main": "./sources/index.js",
39
+ "name": "@theatrejs/plugin-ldtk",
40
+ "peerDependencies": {
41
+
42
+ "@theatrejs/theatrejs": ">= 1.12.0"
43
+ },
44
+ "repository": {
45
+
46
+ "type": "git",
47
+ "url": "git+https://github.com/theatrejs/plugin-ldtk.git"
48
+ },
49
+ "scripts": {
50
+
51
+ "github-pages": "npm run github-pages:clean && npm run github-pages:generate ",
52
+ "github-pages:clean": "rm -rf ./docs/",
53
+ "github-pages:generate": "jsdoc ./tools/jsdoc/HOME.md ./sources/ --recurse --destination ./docs/ --template ./node_modules/docdash --configure ./tools/jsdoc/jsdoc.config.json",
54
+ "postversion": "node ./tools/custom/postversion.cjs"
55
+ },
56
+ "type": "module",
57
+ "version": "1.0.0"
58
+ }
@@ -0,0 +1 @@
1
+ export {Ldtk} from './ldtk.js';
@@ -0,0 +1,293 @@
1
+ import {Vector2} from '@theatrejs/theatrejs';
2
+
3
+ /**
4
+ * Creates LDTK module managers.
5
+ *
6
+ * @example
7
+ *
8
+ * const ldtk = new Ldtk(data);
9
+ * ldtk.getEntities({$level, $layer});
10
+ */
11
+ class Ldtk {
12
+
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
+ /**
37
+ * @typedef {Object} typeldtkdefinitionlayergridvalue A LDTK JSON data layer grid value definition.
38
+ * @property {string} typeldtkdefinitionlayergridvalue.identifier The identifier.
39
+ * @property {number} typeldtkdefinitionlayergridvalue.value The value.
40
+ * @protected
41
+ *
42
+ * @memberof Ldtk
43
+ */
44
+
45
+ /**
46
+ * @typedef {Object} typeldtkdefinitionlayer A LDTK JSON data layer definition.
47
+ * @property {string} typeldtkdefinitionlayer.identifier The identifier.
48
+ * @property {Array<typeldtkdefinitionlayergridvalue>} typeldtkdefinitionlayer.intGridValues The grid values.
49
+ * @protected
50
+ *
51
+ * @memberof Ldtk
52
+ */
53
+
54
+ /**
55
+ * @typedef {Object} typeldtkdefinitions A LDTK JSON data definition.
56
+ * @property {Array<typeldtkdefinitionlayer>} typeldtkdefinitionlayer.layers The layers' definitions.
57
+ * @protected
58
+ *
59
+ * @memberof Ldtk
60
+ */
61
+
62
+ /**
63
+ * @typedef {Object} typeldtkentityfield A LDTK JSON data custom field.
64
+ * @property {string} typeldtkentityfield.__identifier The identifier.
65
+ * @property {string} typeldtkentityfield.__type The type.
66
+ * @property {any} typeldtkentityfield.__value The value.
67
+ * @protected
68
+ *
69
+ * @memberof Ldtk
70
+ */
71
+
72
+ /**
73
+ * @typedef {Object} typeldtkentity A LDTK JSON data entity.
74
+ * @property {string} typeldtkentity.__identifier The identifier.
75
+ * @property {Array<typeldtkentityfield>} typeldtkentity.fieldInstances The custom fields.
76
+ * @property {string} typeldtkentity.iid The iid.
77
+ * @property {Array<number>} typeldtkentity.px The position.
78
+ * @protected
79
+ *
80
+ * @memberof Ldtk
81
+ */
82
+
83
+ /**
84
+ * @typedef {Object} typeldtklayer A LDTK JSON data layer.
85
+ * @property {number} typeldtklayer.__cHei The number of grid cells on the x-axis.
86
+ * @property {number} typeldtklayer.__cWid The number of grid cells on the y-axis.
87
+ * @property {number} typeldtklayer.__gridSize The size of each cell.
88
+ * @property {string} typeldtklayer.__identifier The identifier.
89
+ * @property {Array<typeldtkentity>} typeldtklayer.entityInstances The entities.
90
+ * @property {Array<number>} typeldtklayer.intGridCsv The grid values.
91
+ * @protected
92
+ *
93
+ * @memberof Ldtk
94
+ */
95
+
96
+ /**
97
+ * @typedef {Object} typeldtklevel A LDTK JSON data level.
98
+ * @property {string} typeldtklevel.identifier The identifier.
99
+ * @property {Array<typeldtklayer>} typeldtklevel.layerInstances The layers.
100
+ * @property {number} typeldtklevel.pxHei The height.
101
+ * @property {number} typeldtklevel.pxWid The width.
102
+ * @protected
103
+ *
104
+ * @memberof Ldtk
105
+ */
106
+
107
+ /**
108
+ * @typedef {Object} typeldtk A LDTK JSON data.
109
+ * @property {typeldtkdefinitions} typeldtk.defs The definitions.
110
+ * @property {Array<typeldtklevel>} typeldtk.levels The delevs.
111
+ * @protected
112
+ *
113
+ * @memberof Ldtk
114
+ */
115
+
116
+ /**
117
+ * Stores the LDTK JSON data.
118
+ * @type {typeldtk}
119
+ * @private
120
+ */
121
+ $data;
122
+
123
+ /**
124
+ * Gets the LDTK JSON data.
125
+ * @type {typeldtk}
126
+ * @public
127
+ */
128
+ get data() {
129
+
130
+ return window.structuredClone(this.$data);
131
+ }
132
+
133
+ /**
134
+ * Creates a new LDTK module manager.
135
+ * @param {typeldtk} $data The LDTK JSON data.
136
+ */
137
+ constructor($data) {
138
+
139
+ this.$data = window.structuredClone($data);
140
+ }
141
+
142
+ /**
143
+ * Gets the entities from the given level on the given layer.
144
+ * @param {Object} $parameters The given parameters.
145
+ * @param {string} $parameters.$layer The layer of the entities to get.
146
+ * @param {string} $parameters.$level The level of the entities to get.
147
+ * @returns {Array<typeentity>}
148
+ * @public
149
+ */
150
+ getEntities({$layer, $level}) {
151
+
152
+ const level = this.$data.levels
153
+ .find(($current) => ($current.identifier === $level));
154
+
155
+ if (typeof level === 'undefined') {
156
+
157
+ return [];
158
+ }
159
+
160
+ const layer = level
161
+ .layerInstances.find(($current) => ($current.__identifier === $layer));
162
+
163
+ if (typeof layer === 'undefined') {
164
+
165
+ return [];
166
+ }
167
+
168
+ return layer
169
+ .entityInstances
170
+ .map(($entity) => ({
171
+
172
+ $identifier: $entity.iid,
173
+ $type: $entity.__identifier,
174
+ $position: new Vector2(
175
+
176
+ $entity.px[0] - (level.pxWid / 2),
177
+ - ($entity.px[1] - (level.pxHei / 2))
178
+ )
179
+ }));
180
+ }
181
+
182
+ /**
183
+ * Gets the LDTK JSON data entities from the given level on the given layer.
184
+ * @param {Object} $parameters The given parameters.
185
+ * @param {string} $parameters.$layer The layer of the entities to get.
186
+ * @param {string} $parameters.$level The level of the entities to get.
187
+ * @returns {Array<typeldtkentity>}
188
+ * @public
189
+ */
190
+ getEntitiesData({$layer, $level}) {
191
+
192
+ const level = this.$data.levels
193
+ .find(($current) => ($current.identifier === $level));
194
+
195
+ if (typeof level === 'undefined') {
196
+
197
+ return [];
198
+ }
199
+
200
+ const layer = level
201
+ .layerInstances.find(($current) => ($current.__identifier === $layer));
202
+
203
+ if (typeof layer === 'undefined') {
204
+
205
+ return [];
206
+ }
207
+
208
+ return layer
209
+ .entityInstances
210
+ .map(($entity) => (window.structuredClone($entity)));
211
+ }
212
+
213
+ /**
214
+ * Gets the grid from the given level on the given layer.
215
+ * @param {Object} $parameters The given parameters.
216
+ * @param {string} $parameters.$layer The layer of the grid to get.
217
+ * @param {string} $parameters.$level The level of the grid to get.
218
+ * @returns {(typegrid | undefined)}
219
+ * @public
220
+ */
221
+ getGrid({$layer, $level}) {
222
+
223
+ const level = this.$data.levels
224
+ .find(($current) => ($current.identifier === $level));
225
+
226
+ if (typeof level === 'undefined') {
227
+
228
+ return;
229
+ }
230
+
231
+ const layer = level
232
+ .layerInstances.find(($current) => ($current.__identifier === $layer));
233
+
234
+ if (typeof layer === 'undefined') {
235
+
236
+ return;
237
+ }
238
+
239
+ const definition = this.$data.defs.layers
240
+ .find(($current) => ($current.identifier === $layer));
241
+
242
+ if (typeof definition === 'undefined') {
243
+
244
+ return;
245
+ }
246
+
247
+ return {
248
+
249
+ $data: [...layer.intGridCsv],
250
+ $definitions: new Map(definition.intGridValues.map(($definition) => ([$definition.value, $definition.identifier]))),
251
+ $cell: new Vector2(layer.__gridSize, layer.__gridSize),
252
+ $width: layer.__cWid,
253
+ $height: layer.__cHei,
254
+ $position: new Vector2(level.pxWid / 2, level.pxHei / 2)
255
+ };
256
+ }
257
+
258
+ /**
259
+ * Gets the LDTK JSON data layer grid from the given level on the given layer.
260
+ * @param {Object} $parameters The given parameters.
261
+ * @param {string} $parameters.$layer The layer of the grid to get.
262
+ * @param {string} $parameters.$level The level of the grid to get.
263
+ * @returns {typeldtklayer}
264
+ * @public
265
+ */
266
+ getGridData({$layer, $level}) {
267
+
268
+ const level = this.$data.levels
269
+ .find(($current) => ($current.identifier === $level));
270
+
271
+ if (typeof level === 'undefined') {
272
+
273
+ return;
274
+ }
275
+
276
+ const layer = level
277
+ .layerInstances.find(($current) => ($current.__identifier === $layer));
278
+
279
+ if (typeof layer === 'undefined') {
280
+
281
+ return;
282
+ }
283
+
284
+ return window.structuredClone(layer);
285
+ }
286
+ }
287
+
288
+ export {
289
+
290
+ Ldtk
291
+ };
292
+
293
+ export default Ldtk;