@eva/plugin-renderer-tilemap 2.1.0-beta.2 → 2.1.0-beta.3
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/package.json +3 -3
- package/dist/EVA.plugin.renderer.tilemap.js +0 -350
- package/dist/EVA.plugin.renderer.tilemap.min.js +0 -1
- package/dist/plugin-renderer-tilemap.cjs.js +0 -301
- package/dist/plugin-renderer-tilemap.cjs.prod.js +0 -1
- package/dist/plugin-renderer-tilemap.d.ts +0 -95
- package/dist/plugin-renderer-tilemap.esm.js +0 -296
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eva/plugin-renderer-tilemap",
|
|
3
|
-
"version": "2.1.0-beta.
|
|
3
|
+
"version": "2.1.0-beta.3",
|
|
4
4
|
"description": "@eva/plugin-renderer-tilemap",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "dist/plugin-renderer-tilemap.esm.js",
|
|
@@ -20,8 +20,8 @@
|
|
|
20
20
|
"homepage": "https://eva.js.org",
|
|
21
21
|
"dependencies": {
|
|
22
22
|
"@eva/inspector-decorator": "^2.0.0-beta.0",
|
|
23
|
-
"@eva/eva.js": "2.1.0-beta.
|
|
24
|
-
"@eva/plugin-renderer": "2.1.0-beta.
|
|
23
|
+
"@eva/eva.js": "2.1.0-beta.3",
|
|
24
|
+
"@eva/plugin-renderer": "2.1.0-beta.3",
|
|
25
25
|
"pixi.js": "^8.6.3"
|
|
26
26
|
}
|
|
27
27
|
}
|
|
@@ -1,350 +0,0 @@
|
|
|
1
|
-
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
|
|
2
|
-
globalThis.EVA = globalThis.EVA || {};
|
|
3
|
-
globalThis.EVA.plugin = globalThis.EVA.plugin || {};
|
|
4
|
-
globalThis.EVA.plugin.renderer = globalThis.EVA.plugin.renderer || {};
|
|
5
|
-
var _EVA_IIFE_tilemap = function (exports, eva_js, pluginRenderer, pixi_js) {
|
|
6
|
-
'use strict';
|
|
7
|
-
function __decorate(decorators, target, key, desc) {
|
|
8
|
-
var c = arguments.length,
|
|
9
|
-
r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc,
|
|
10
|
-
d;
|
|
11
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
12
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
13
|
-
}
|
|
14
|
-
function __metadata(metadataKey, metadataValue) {
|
|
15
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
16
|
-
}
|
|
17
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
18
|
-
function adopt(value) {
|
|
19
|
-
return value instanceof P ? value : new P(function (resolve) {
|
|
20
|
-
resolve(value);
|
|
21
|
-
});
|
|
22
|
-
}
|
|
23
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
-
function fulfilled(value) {
|
|
25
|
-
try {
|
|
26
|
-
step(generator.next(value));
|
|
27
|
-
} catch (e) {
|
|
28
|
-
reject(e);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function rejected(value) {
|
|
32
|
-
try {
|
|
33
|
-
step(generator["throw"](value));
|
|
34
|
-
} catch (e) {
|
|
35
|
-
reject(e);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
function step(result) {
|
|
39
|
-
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
|
|
40
|
-
}
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
function __rest(s, e) {
|
|
45
|
-
var t = {};
|
|
46
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p];
|
|
47
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
48
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]];
|
|
49
|
-
}
|
|
50
|
-
return t;
|
|
51
|
-
}
|
|
52
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
53
|
-
var e = new Error(message);
|
|
54
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
55
|
-
};
|
|
56
|
-
class SymbolKeysNotSupportedError extends Error {
|
|
57
|
-
constructor() {
|
|
58
|
-
super('Symbol keys are not supported yet!');
|
|
59
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
const IDE_PROPERTY_METADATA = 'IDE_PROPERTY_METADATA';
|
|
63
|
-
function transformBasicType(type) {
|
|
64
|
-
if (type === String) {
|
|
65
|
-
return 'string';
|
|
66
|
-
}
|
|
67
|
-
if (type === Number) {
|
|
68
|
-
return 'number';
|
|
69
|
-
}
|
|
70
|
-
if (type === Boolean) {
|
|
71
|
-
return 'boolean';
|
|
72
|
-
}
|
|
73
|
-
return 'unknown';
|
|
74
|
-
}
|
|
75
|
-
function defineLegacyIDEProp(target, propertyKey, patch) {
|
|
76
|
-
const constructor = target.constructor;
|
|
77
|
-
const current = constructor.IDEProps || {};
|
|
78
|
-
current[propertyKey] = _extends(_extends({
|
|
79
|
-
key: propertyKey
|
|
80
|
-
}, current[propertyKey]), patch);
|
|
81
|
-
constructor.IDEProps = current;
|
|
82
|
-
}
|
|
83
|
-
function defineTypes(target, key, options, returnTypeFunction) {
|
|
84
|
-
let type = Reflect.getMetadata('design:type', target, key);
|
|
85
|
-
let isArray = type === Array;
|
|
86
|
-
const str = transformBasicType(type);
|
|
87
|
-
if (str !== 'unknown') {
|
|
88
|
-
type = str;
|
|
89
|
-
}
|
|
90
|
-
if (returnTypeFunction) {
|
|
91
|
-
const returnType = returnTypeFunction();
|
|
92
|
-
if (Array.isArray(returnType)) {
|
|
93
|
-
isArray = true;
|
|
94
|
-
type = returnType[0];
|
|
95
|
-
} else {
|
|
96
|
-
type = returnType;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
const properties = Reflect.getMetadata(IDE_PROPERTY_METADATA, target.constructor) || {};
|
|
100
|
-
const current = properties[key] || {};
|
|
101
|
-
const property = _extends(_extends(_extends({}, current), {
|
|
102
|
-
type,
|
|
103
|
-
isArray: isArray
|
|
104
|
-
}), options);
|
|
105
|
-
properties[key] = property;
|
|
106
|
-
Reflect.defineMetadata(IDE_PROPERTY_METADATA, properties, target.constructor);
|
|
107
|
-
const legacyProperty = __rest(property, ["isArray"]);
|
|
108
|
-
defineLegacyIDEProp(target, key, legacyProperty);
|
|
109
|
-
}
|
|
110
|
-
function type(type) {
|
|
111
|
-
return Field({
|
|
112
|
-
type
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
function getTypeDecoratorParams(returnTypeFuncOrOptions, maybeOptions) {
|
|
116
|
-
if (typeof returnTypeFuncOrOptions === 'function') {
|
|
117
|
-
return {
|
|
118
|
-
returnTypeFunc: returnTypeFuncOrOptions,
|
|
119
|
-
options: maybeOptions || {}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
return {
|
|
123
|
-
options: returnTypeFuncOrOptions || {}
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
function Field(returnTypeFunction, maybeOptions) {
|
|
127
|
-
return (target, propertyKey) => {
|
|
128
|
-
if (typeof propertyKey === 'symbol') {
|
|
129
|
-
throw new SymbolKeysNotSupportedError();
|
|
130
|
-
}
|
|
131
|
-
const {
|
|
132
|
-
options,
|
|
133
|
-
returnTypeFunc
|
|
134
|
-
} = getTypeDecoratorParams(returnTypeFunction, maybeOptions);
|
|
135
|
-
defineTypes(target, propertyKey, options, returnTypeFunc);
|
|
136
|
-
};
|
|
137
|
-
}
|
|
138
|
-
var ExecuteMode;
|
|
139
|
-
(function (ExecuteMode) {
|
|
140
|
-
ExecuteMode[ExecuteMode["Edit"] = 2] = "Edit";
|
|
141
|
-
ExecuteMode[ExecuteMode["Game"] = 4] = "Game";
|
|
142
|
-
ExecuteMode[ExecuteMode["All"] = 6] = "All";
|
|
143
|
-
})(ExecuteMode || (ExecuteMode = {}));
|
|
144
|
-
class Tilemap extends eva_js.Component {
|
|
145
|
-
constructor() {
|
|
146
|
-
super(...arguments);
|
|
147
|
-
this.tileset = '';
|
|
148
|
-
this.tileWidth = 32;
|
|
149
|
-
this.tileHeight = 32;
|
|
150
|
-
this.tilesetSpacing = 0;
|
|
151
|
-
this.tilesetMargin = 0;
|
|
152
|
-
this.layers = [];
|
|
153
|
-
}
|
|
154
|
-
init(obj) {
|
|
155
|
-
if (obj) _extends(this, obj);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
Tilemap.componentName = 'Tilemap';
|
|
159
|
-
__decorate([type('string'), __metadata("design:type", String)], Tilemap.prototype, "tileset", void 0);
|
|
160
|
-
__decorate([type('number'), __metadata("design:type", Number)], Tilemap.prototype, "tileWidth", void 0);
|
|
161
|
-
__decorate([type('number'), __metadata("design:type", Number)], Tilemap.prototype, "tileHeight", void 0);
|
|
162
|
-
__decorate([type('number'), __metadata("design:type", Number)], Tilemap.prototype, "tilesetSpacing", void 0);
|
|
163
|
-
__decorate([type('number'), __metadata("design:type", Number)], Tilemap.prototype, "tilesetMargin", void 0);
|
|
164
|
-
let TilemapSystem = class TilemapSystem extends pluginRenderer.Renderer {
|
|
165
|
-
constructor() {
|
|
166
|
-
super(...arguments);
|
|
167
|
-
this.name = 'Tilemap';
|
|
168
|
-
this.records = {};
|
|
169
|
-
}
|
|
170
|
-
init() {
|
|
171
|
-
this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
|
|
172
|
-
this.renderSystem.rendererManager.register(this);
|
|
173
|
-
}
|
|
174
|
-
rendererUpdate(_gameObject) {}
|
|
175
|
-
componentChanged(changed) {
|
|
176
|
-
var _a, _b, _c;
|
|
177
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
178
|
-
if (changed.componentName !== 'Tilemap') return;
|
|
179
|
-
const component = changed.component;
|
|
180
|
-
const gameObjectId = changed.gameObject.id;
|
|
181
|
-
if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
|
|
182
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
183
|
-
let texture = null;
|
|
184
|
-
if (component.tileset) {
|
|
185
|
-
const {
|
|
186
|
-
instance
|
|
187
|
-
} = yield eva_js.resource.getResource(component.tileset);
|
|
188
|
-
if (!this.validateAsyncId(gameObjectId, asyncId)) return;
|
|
189
|
-
if (!instance) {
|
|
190
|
-
console.error(`GameObject:${changed.gameObject.name}'s Tilemap tileset load error`);
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
texture = instance;
|
|
194
|
-
}
|
|
195
|
-
if (!texture) return;
|
|
196
|
-
const root = new pixi_js.Container();
|
|
197
|
-
const container = this.containerManager.getContainer(gameObjectId);
|
|
198
|
-
if (container) container.addChildAt(root, 0);
|
|
199
|
-
const record = {
|
|
200
|
-
root,
|
|
201
|
-
layerContainers: [],
|
|
202
|
-
frameTextures: [],
|
|
203
|
-
baseTexture: texture
|
|
204
|
-
};
|
|
205
|
-
this.records[gameObjectId] = record;
|
|
206
|
-
this.buildLayers(record, component);
|
|
207
|
-
} else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
|
|
208
|
-
const record = this.records[gameObjectId];
|
|
209
|
-
if (!record) return;
|
|
210
|
-
if (((_b = (_a = changed.prop) === null || _a === void 0 ? void 0 : _a.prop) === null || _b === void 0 ? void 0 : _b[0]) === 'tileset') {
|
|
211
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
212
|
-
const {
|
|
213
|
-
instance
|
|
214
|
-
} = yield eva_js.resource.getResource(component.tileset);
|
|
215
|
-
if (!this.validateAsyncId(gameObjectId, asyncId)) return;
|
|
216
|
-
if (!instance) return;
|
|
217
|
-
this.tearDownChildren(record);
|
|
218
|
-
record.baseTexture = instance;
|
|
219
|
-
this.buildLayers(record, component);
|
|
220
|
-
}
|
|
221
|
-
} else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
|
|
222
|
-
this.increaseAsyncId(gameObjectId);
|
|
223
|
-
const record = this.records[gameObjectId];
|
|
224
|
-
if (!record) return;
|
|
225
|
-
this.tearDownChildren(record);
|
|
226
|
-
const container = (_c = this.containerManager) === null || _c === void 0 ? void 0 : _c.getContainer(gameObjectId);
|
|
227
|
-
if (container) container.removeChild(record.root);
|
|
228
|
-
record.root.destroy({
|
|
229
|
-
children: true
|
|
230
|
-
});
|
|
231
|
-
delete this.records[gameObjectId];
|
|
232
|
-
}
|
|
233
|
-
});
|
|
234
|
-
}
|
|
235
|
-
tearDownChildren(record) {
|
|
236
|
-
for (const layer of record.layerContainers) {
|
|
237
|
-
record.root.removeChild(layer);
|
|
238
|
-
layer.destroy({
|
|
239
|
-
children: true
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
record.layerContainers = [];
|
|
243
|
-
for (const tex of record.frameTextures) {
|
|
244
|
-
try {
|
|
245
|
-
tex.destroy(false);
|
|
246
|
-
} catch (_a) {}
|
|
247
|
-
}
|
|
248
|
-
record.frameTextures = [];
|
|
249
|
-
}
|
|
250
|
-
buildLayers(record, component) {
|
|
251
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
252
|
-
const base = record.baseTexture;
|
|
253
|
-
if (!base) return;
|
|
254
|
-
const tileW = component.tileWidth || 32;
|
|
255
|
-
const tileH = component.tileHeight || 32;
|
|
256
|
-
const renderW = (_a = component.renderTileWidth) !== null && _a !== void 0 ? _a : tileW;
|
|
257
|
-
const renderH = (_b = component.renderTileHeight) !== null && _b !== void 0 ? _b : tileH;
|
|
258
|
-
const margin = component.tilesetMargin || 0;
|
|
259
|
-
const spacing = component.tilesetSpacing || 0;
|
|
260
|
-
const sourceWidth = (_j = (_g = (_f = (_d = (_c = base.orig) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = base.source) === null || _e === void 0 ? void 0 : _e.width) !== null && _f !== void 0 ? _f : base.width) !== null && _g !== void 0 ? _g : (_h = base.frame) === null || _h === void 0 ? void 0 : _h.width) !== null && _j !== void 0 ? _j : 0;
|
|
261
|
-
const inferredCols = sourceWidth > 0 ? Math.max(1, Math.floor((sourceWidth - margin + spacing) / (tileW + spacing))) : 1;
|
|
262
|
-
const cols = component.tilesetColumns && component.tilesetColumns > 0 ? component.tilesetColumns : inferredCols;
|
|
263
|
-
const tilesetCache = new Map();
|
|
264
|
-
const getFrameTexture = tileId => {
|
|
265
|
-
var _a, _b;
|
|
266
|
-
if (tileId <= 0) return null;
|
|
267
|
-
const idx = tileId - 1;
|
|
268
|
-
const cached = tilesetCache.get(idx);
|
|
269
|
-
if (cached) return cached;
|
|
270
|
-
const col = idx % cols;
|
|
271
|
-
const row = Math.floor(idx / cols);
|
|
272
|
-
const x = margin + col * (tileW + spacing);
|
|
273
|
-
const y = margin + row * (tileH + spacing);
|
|
274
|
-
try {
|
|
275
|
-
const source = (_b = (_a = base.source) !== null && _a !== void 0 ? _a : base.baseTexture) !== null && _b !== void 0 ? _b : base;
|
|
276
|
-
const sub = new pixi_js.Texture({
|
|
277
|
-
source,
|
|
278
|
-
frame: new pixi_js.Rectangle(x, y, tileW, tileH)
|
|
279
|
-
});
|
|
280
|
-
tilesetCache.set(idx, sub);
|
|
281
|
-
record.frameTextures.push(sub);
|
|
282
|
-
return sub;
|
|
283
|
-
} catch (e) {
|
|
284
|
-
if (typeof console !== 'undefined') {
|
|
285
|
-
console.warn('[Tilemap] Texture slice failed, falling back to base', e);
|
|
286
|
-
}
|
|
287
|
-
return base;
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
for (const layerSpec of component.layers || []) {
|
|
291
|
-
const layerContainer = new pixi_js.Container();
|
|
292
|
-
layerContainer.label = (_k = layerSpec.name) !== null && _k !== void 0 ? _k : 'tilemap-layer';
|
|
293
|
-
const offX = layerSpec.offsetX || 0;
|
|
294
|
-
const offY = layerSpec.offsetY || 0;
|
|
295
|
-
if (layerSpec.alpha != null) layerContainer.alpha = layerSpec.alpha;
|
|
296
|
-
if (layerSpec.visible === false) layerContainer.visible = false;
|
|
297
|
-
const data = layerSpec.data || [];
|
|
298
|
-
const tint = layerSpec.tint;
|
|
299
|
-
for (let r = 0; r < data.length; r++) {
|
|
300
|
-
const row = data[r];
|
|
301
|
-
if (!row) continue;
|
|
302
|
-
for (let c = 0; c < row.length; c++) {
|
|
303
|
-
const id = row[c];
|
|
304
|
-
if (!id || id <= 0) continue;
|
|
305
|
-
const tex = getFrameTexture(id);
|
|
306
|
-
if (!tex) continue;
|
|
307
|
-
const sp = new pixi_js.Sprite(tex);
|
|
308
|
-
sp.x = offX + c * tileW;
|
|
309
|
-
sp.y = offY + r * tileH;
|
|
310
|
-
sp.width = renderW;
|
|
311
|
-
sp.height = renderH;
|
|
312
|
-
if (tint != null) sp.tint = tint;
|
|
313
|
-
layerContainer.addChild(sp);
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
record.root.addChild(layerContainer);
|
|
317
|
-
record.layerContainers.push(layerContainer);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
destroy() {
|
|
321
|
-
var _a;
|
|
322
|
-
for (const key in this.records) {
|
|
323
|
-
const id = parseInt(key);
|
|
324
|
-
const record = this.records[id];
|
|
325
|
-
this.tearDownChildren(record);
|
|
326
|
-
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(id);
|
|
327
|
-
if (container) container.removeChild(record.root);
|
|
328
|
-
record.root.destroy({
|
|
329
|
-
children: true
|
|
330
|
-
});
|
|
331
|
-
delete this.records[id];
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
};
|
|
335
|
-
TilemapSystem.systemName = 'Tilemap';
|
|
336
|
-
TilemapSystem = __decorate([eva_js.decorators.componentObserver({
|
|
337
|
-
Tilemap: [{
|
|
338
|
-
prop: ['tileset'],
|
|
339
|
-
deep: false
|
|
340
|
-
}]
|
|
341
|
-
})], TilemapSystem);
|
|
342
|
-
var TilemapSystem$1 = TilemapSystem;
|
|
343
|
-
exports.Tilemap = Tilemap;
|
|
344
|
-
exports.TilemapSystem = TilemapSystem$1;
|
|
345
|
-
Object.defineProperty(exports, '__esModule', {
|
|
346
|
-
value: true
|
|
347
|
-
});
|
|
348
|
-
return exports;
|
|
349
|
-
}({}, EVA, EVA.plugin.renderer, PIXI);
|
|
350
|
-
globalThis.EVA.plugin.renderer.tilemap = globalThis.EVA.plugin.renderer.tilemap || _EVA_IIFE_tilemap;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
function _extends(){return _extends=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)({}).hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e},_extends.apply(null,arguments)}globalThis.EVA=globalThis.EVA||{},globalThis.EVA.plugin=globalThis.EVA.plugin||{},globalThis.EVA.plugin.renderer=globalThis.EVA.plugin.renderer||{};var _EVA_IIFE_tilemap=function(e,t,n,r){"use strict";function o(e,t,n,r){var o,i=arguments.length,s=i<3?t:null===r?r=Object.getOwnPropertyDescriptor(t,n):r;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,n,r);else for(var l=e.length-1;l>=0;l--)(o=e[l])&&(s=(i<3?o(s):i>3?o(t,n,s):o(t,n))||s);return i>3&&s&&Object.defineProperty(t,n,s),s}function i(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,n,r){return new(n||(n=Promise))(function(o,i){function s(e){try{a(r.next(e))}catch(e){i(e)}}function l(e){try{a(r.throw(e))}catch(e){i(e)}}function a(e){var t;e.done?o(e.value):(t=e.value,t instanceof n?t:new n(function(e){e(t)})).then(s,l)}a((r=r.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class l extends Error{constructor(){super("Symbol keys are not supported yet!"),Object.setPrototypeOf(this,new.target.prototype)}}const a="IDE_PROPERTY_METADATA";function c(e,t,n,r){let o=Reflect.getMetadata("design:type",e,t),i=o===Array;const s=function(e){return e===String?"string":e===Number?"number":e===Boolean?"boolean":"unknown"}(o);if("unknown"!==s&&(o=s),r){const e=r();Array.isArray(e)?(i=!0,o=e[0]):o=e}const l=Reflect.getMetadata(a,e.constructor)||{},c=_extends(_extends(_extends({},l[t]||{}),{type:o,isArray:i}),n);l[t]=c,Reflect.defineMetadata(a,l,e.constructor);!function(e,t,n){const r=e.constructor,o=r.IDEProps||{};o[t]=_extends(_extends({key:t},o[t]),n),r.IDEProps=o}(e,t,function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&Object.prototype.propertyIsEnumerable.call(e,r[o])&&(n[r[o]]=e[r[o]])}return n}(c,["isArray"]))}function d(e){return t={type:e},(e,r)=>{if("symbol"==typeof r)throw new l;const{options:o,returnTypeFunc:i}=function(e,t){return"function"==typeof e?{returnTypeFunc:e,options:t||{}}:{options:e||{}}}(t,n);c(e,r,o,i)};var t,n}var u;!function(e){e[e.Edit=2]="Edit",e[e.Game=4]="Game",e[e.All=6]="All"}(u||(u={}));class p extends t.Component{constructor(){super(...arguments),this.tileset="",this.tileWidth=32,this.tileHeight=32,this.tilesetSpacing=0,this.tilesetMargin=0,this.layers=[]}init(e){e&&_extends(this,e)}}p.componentName="Tilemap",o([d("string"),i("design:type",String)],p.prototype,"tileset",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tileWidth",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tileHeight",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tilesetSpacing",void 0),o([d("number"),i("design:type",Number)],p.prototype,"tilesetMargin",void 0);let f=class extends n.Renderer{constructor(){super(...arguments),this.name="Tilemap",this.records={}}init(){this.renderSystem=this.game.getSystem(n.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){}componentChanged(e){var n,o,i;return s(this,void 0,void 0,function*(){if("Tilemap"!==e.componentName)return;const s=e.component,l=e.gameObject.id;if(e.type===t.OBSERVER_TYPE.ADD){const n=this.increaseAsyncId(l);let o=null;if(s.tileset){const{instance:r}=yield t.resource.getResource(s.tileset);if(!this.validateAsyncId(l,n))return;if(!r)return void console.error(`GameObject:${e.gameObject.name}'s Tilemap tileset load error`);o=r}if(!o)return;const i=new r.Container,a=this.containerManager.getContainer(l);a&&a.addChildAt(i,0);const c={root:i,layerContainers:[],frameTextures:[],baseTexture:o};this.records[l]=c,this.buildLayers(c,s)}else if(e.type===t.OBSERVER_TYPE.CHANGE){const r=this.records[l];if(!r)return;if("tileset"===(null===(o=null===(n=e.prop)||void 0===n?void 0:n.prop)||void 0===o?void 0:o[0])){const e=this.increaseAsyncId(l),{instance:n}=yield t.resource.getResource(s.tileset);if(!this.validateAsyncId(l,e))return;if(!n)return;this.tearDownChildren(r),r.baseTexture=n,this.buildLayers(r,s)}}else if(e.type===t.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;this.tearDownChildren(e);const t=null===(i=this.containerManager)||void 0===i?void 0:i.getContainer(l);t&&t.removeChild(e.root),e.root.destroy({children:!0}),delete this.records[l]}})}tearDownChildren(e){for(const t of e.layerContainers)e.root.removeChild(t),t.destroy({children:!0});e.layerContainers=[];for(const t of e.frameTextures)try{t.destroy(!1)}catch(e){}e.frameTextures=[]}buildLayers(e,t){var n,o,i,s,l,a,c,d,u,p;const f=e.baseTexture;if(!f)return;const h=t.tileWidth||32,y=t.tileHeight||32,g=null!==(n=t.renderTileWidth)&&void 0!==n?n:h,m=null!==(o=t.renderTileHeight)&&void 0!==o?o:y,v=t.tilesetMargin||0,b=t.tilesetSpacing||0,E=null!==(u=null!==(c=null!==(a=null!==(s=null===(i=f.orig)||void 0===i?void 0:i.width)&&void 0!==s?s:null===(l=f.source)||void 0===l?void 0:l.width)&&void 0!==a?a:f.width)&&void 0!==c?c:null===(d=f.frame)||void 0===d?void 0:d.width)&&void 0!==u?u:0,T=E>0?Math.max(1,Math.floor((E-v+b)/(h+b))):1,A=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:T,w=new Map,x=t=>{var n,o;if(t<=0)return null;const i=t-1,s=w.get(i);if(s)return s;const l=i%A,a=Math.floor(i/A),c=v+l*(h+b),d=v+a*(y+b);try{const t=null!==(o=null!==(n=f.source)&&void 0!==n?n:f.baseTexture)&&void 0!==o?o:f,s=new r.Texture({source:t,frame:new r.Rectangle(c,d,h,y)});return w.set(i,s),e.frameTextures.push(s),s}catch(e){return"undefined"!=typeof console&&console.warn("[Tilemap] Texture slice failed, falling back to base",e),f}};for(const n of t.layers||[]){const t=new r.Container;t.label=null!==(p=n.name)&&void 0!==p?p:"tilemap-layer";const o=n.offsetX||0,i=n.offsetY||0;null!=n.alpha&&(t.alpha=n.alpha),!1===n.visible&&(t.visible=!1);const s=n.data||[],l=n.tint;for(let e=0;e<s.length;e++){const n=s[e];if(n)for(let s=0;s<n.length;s++){const a=n[s];if(!a||a<=0)continue;const c=x(a);if(!c)continue;const d=new r.Sprite(c);d.x=o+s*h,d.y=i+e*y,d.width=g,d.height=m,null!=l&&(d.tint=l),t.addChild(d)}}e.root.addChild(t),e.layerContainers.push(t)}}destroy(){var e;for(const t in this.records){const n=parseInt(t),r=this.records[n];this.tearDownChildren(r);const o=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(n);o&&o.removeChild(r.root),r.root.destroy({children:!0}),delete this.records[n]}}};f.systemName="Tilemap",f=o([t.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1}]})],f);var h=f;return e.Tilemap=p,e.TilemapSystem=h,Object.defineProperty(e,"__esModule",{value:!0}),e}({},EVA,EVA.plugin.renderer,PIXI);globalThis.EVA.plugin.renderer.tilemap=globalThis.EVA.plugin.renderer.tilemap||_EVA_IIFE_tilemap;
|
|
@@ -1,301 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
var eva_js = require('@eva/eva.js');
|
|
6
|
-
var inspectorDecorator = require('@eva/inspector-decorator');
|
|
7
|
-
var pluginRenderer = require('@eva/plugin-renderer');
|
|
8
|
-
var pixi_js = require('pixi.js');
|
|
9
|
-
|
|
10
|
-
/******************************************************************************
|
|
11
|
-
Copyright (c) Microsoft Corporation.
|
|
12
|
-
|
|
13
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
14
|
-
purpose with or without fee is hereby granted.
|
|
15
|
-
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
17
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
18
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
19
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
20
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
21
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
22
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
23
|
-
***************************************************************************** */
|
|
24
|
-
|
|
25
|
-
function __decorate(decorators, target, key, desc) {
|
|
26
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
27
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
28
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
29
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function __metadata(metadataKey, metadataValue) {
|
|
33
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
37
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
38
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
39
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
40
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
41
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
42
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
47
|
-
var e = new Error(message);
|
|
48
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Phaser-style Static Tilemap 渲染组件(MVP)。
|
|
53
|
-
*
|
|
54
|
-
* 行为:
|
|
55
|
-
* 1. 从 `tileset` 资源加载一张 spritesheet 纹理;
|
|
56
|
-
* 2. 每个 layer 对应一个 PixiJS Container;
|
|
57
|
-
* 3. layer 内每个非零 tile 都创建一个 Sprite,texture 由原 tileset 切片得到;
|
|
58
|
-
* 4. tile 在 layer Container 内的位置:`(col * tileW + offsetX, row * tileH + offsetY)`。
|
|
59
|
-
*
|
|
60
|
-
* 不支持(Phase 2 视情况增加):tile flipping、动态修改 API、iso/hex、physics 碰撞、camera。
|
|
61
|
-
*/
|
|
62
|
-
class Tilemap extends eva_js.Component {
|
|
63
|
-
constructor() {
|
|
64
|
-
super(...arguments);
|
|
65
|
-
this.tileset = '';
|
|
66
|
-
this.tileWidth = 32;
|
|
67
|
-
this.tileHeight = 32;
|
|
68
|
-
this.tilesetSpacing = 0;
|
|
69
|
-
this.tilesetMargin = 0;
|
|
70
|
-
this.layers = [];
|
|
71
|
-
}
|
|
72
|
-
init(obj) {
|
|
73
|
-
if (obj)
|
|
74
|
-
Object.assign(this, obj);
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
Tilemap.componentName = 'Tilemap';
|
|
78
|
-
__decorate([
|
|
79
|
-
inspectorDecorator.type('string'),
|
|
80
|
-
__metadata("design:type", String)
|
|
81
|
-
], Tilemap.prototype, "tileset", void 0);
|
|
82
|
-
__decorate([
|
|
83
|
-
inspectorDecorator.type('number'),
|
|
84
|
-
__metadata("design:type", Number)
|
|
85
|
-
], Tilemap.prototype, "tileWidth", void 0);
|
|
86
|
-
__decorate([
|
|
87
|
-
inspectorDecorator.type('number'),
|
|
88
|
-
__metadata("design:type", Number)
|
|
89
|
-
], Tilemap.prototype, "tileHeight", void 0);
|
|
90
|
-
__decorate([
|
|
91
|
-
inspectorDecorator.type('number'),
|
|
92
|
-
__metadata("design:type", Number)
|
|
93
|
-
], Tilemap.prototype, "tilesetSpacing", void 0);
|
|
94
|
-
__decorate([
|
|
95
|
-
inspectorDecorator.type('number'),
|
|
96
|
-
__metadata("design:type", Number)
|
|
97
|
-
], Tilemap.prototype, "tilesetMargin", void 0);
|
|
98
|
-
|
|
99
|
-
let TilemapSystem = class TilemapSystem extends pluginRenderer.Renderer {
|
|
100
|
-
constructor() {
|
|
101
|
-
super(...arguments);
|
|
102
|
-
this.name = 'Tilemap';
|
|
103
|
-
this.records = {};
|
|
104
|
-
}
|
|
105
|
-
init() {
|
|
106
|
-
this.renderSystem = this.game.getSystem(pluginRenderer.RendererSystem);
|
|
107
|
-
this.renderSystem.rendererManager.register(this);
|
|
108
|
-
}
|
|
109
|
-
rendererUpdate(_gameObject) {
|
|
110
|
-
// 静态 tilemap MVP:几何不随 transform.size 变化,无需逐帧刷新。
|
|
111
|
-
}
|
|
112
|
-
componentChanged(changed) {
|
|
113
|
-
var _a, _b, _c;
|
|
114
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
115
|
-
if (changed.componentName !== 'Tilemap')
|
|
116
|
-
return;
|
|
117
|
-
const component = changed.component;
|
|
118
|
-
const gameObjectId = changed.gameObject.id;
|
|
119
|
-
if (changed.type === eva_js.OBSERVER_TYPE.ADD) {
|
|
120
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
121
|
-
let texture = null;
|
|
122
|
-
if (component.tileset) {
|
|
123
|
-
const { instance } = yield eva_js.resource.getResource(component.tileset);
|
|
124
|
-
if (!this.validateAsyncId(gameObjectId, asyncId))
|
|
125
|
-
return;
|
|
126
|
-
if (!instance) {
|
|
127
|
-
console.error(`GameObject:${changed.gameObject.name}'s Tilemap tileset load error`);
|
|
128
|
-
return;
|
|
129
|
-
}
|
|
130
|
-
texture = instance;
|
|
131
|
-
}
|
|
132
|
-
if (!texture)
|
|
133
|
-
return;
|
|
134
|
-
const root = new pixi_js.Container();
|
|
135
|
-
const container = this.containerManager.getContainer(gameObjectId);
|
|
136
|
-
if (container)
|
|
137
|
-
container.addChildAt(root, 0);
|
|
138
|
-
const record = {
|
|
139
|
-
root,
|
|
140
|
-
layerContainers: [],
|
|
141
|
-
frameTextures: [],
|
|
142
|
-
baseTexture: texture,
|
|
143
|
-
};
|
|
144
|
-
this.records[gameObjectId] = record;
|
|
145
|
-
this.buildLayers(record, component);
|
|
146
|
-
}
|
|
147
|
-
else if (changed.type === eva_js.OBSERVER_TYPE.CHANGE) {
|
|
148
|
-
const record = this.records[gameObjectId];
|
|
149
|
-
if (!record)
|
|
150
|
-
return;
|
|
151
|
-
if (((_b = (_a = changed.prop) === null || _a === void 0 ? void 0 : _a.prop) === null || _b === void 0 ? void 0 : _b[0]) === 'tileset') {
|
|
152
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
153
|
-
const { instance } = yield eva_js.resource.getResource(component.tileset);
|
|
154
|
-
if (!this.validateAsyncId(gameObjectId, asyncId))
|
|
155
|
-
return;
|
|
156
|
-
if (!instance)
|
|
157
|
-
return;
|
|
158
|
-
this.tearDownChildren(record);
|
|
159
|
-
record.baseTexture = instance;
|
|
160
|
-
this.buildLayers(record, component);
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
else if (changed.type === eva_js.OBSERVER_TYPE.REMOVE) {
|
|
164
|
-
this.increaseAsyncId(gameObjectId);
|
|
165
|
-
const record = this.records[gameObjectId];
|
|
166
|
-
if (!record)
|
|
167
|
-
return;
|
|
168
|
-
this.tearDownChildren(record);
|
|
169
|
-
const container = (_c = this.containerManager) === null || _c === void 0 ? void 0 : _c.getContainer(gameObjectId);
|
|
170
|
-
if (container)
|
|
171
|
-
container.removeChild(record.root);
|
|
172
|
-
record.root.destroy({ children: true });
|
|
173
|
-
delete this.records[gameObjectId];
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/** 销毁现有 layer/frame 纹理(切换 tileset 或卸载时调用)。 */
|
|
178
|
-
tearDownChildren(record) {
|
|
179
|
-
for (const layer of record.layerContainers) {
|
|
180
|
-
record.root.removeChild(layer);
|
|
181
|
-
layer.destroy({ children: true });
|
|
182
|
-
}
|
|
183
|
-
record.layerContainers = [];
|
|
184
|
-
for (const tex of record.frameTextures) {
|
|
185
|
-
try {
|
|
186
|
-
tex.destroy(false);
|
|
187
|
-
}
|
|
188
|
-
catch ( /* ignore */_a) { /* ignore */ }
|
|
189
|
-
}
|
|
190
|
-
record.frameTextures = [];
|
|
191
|
-
}
|
|
192
|
-
buildLayers(record, component) {
|
|
193
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
194
|
-
const base = record.baseTexture;
|
|
195
|
-
if (!base)
|
|
196
|
-
return;
|
|
197
|
-
const tileW = component.tileWidth || 32;
|
|
198
|
-
const tileH = component.tileHeight || 32;
|
|
199
|
-
const renderW = (_a = component.renderTileWidth) !== null && _a !== void 0 ? _a : tileW;
|
|
200
|
-
const renderH = (_b = component.renderTileHeight) !== null && _b !== void 0 ? _b : tileH;
|
|
201
|
-
const margin = component.tilesetMargin || 0;
|
|
202
|
-
const spacing = component.tilesetSpacing || 0;
|
|
203
|
-
// 推断 columns
|
|
204
|
-
const sourceWidth = (_j = (_g = (_f = (_d = (_c = base.orig) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = base.source) === null || _e === void 0 ? void 0 : _e.width) !== null && _f !== void 0 ? _f : base.width) !== null && _g !== void 0 ? _g : (_h = base.frame) === null || _h === void 0 ? void 0 : _h.width) !== null && _j !== void 0 ? _j : 0;
|
|
205
|
-
const inferredCols = sourceWidth > 0 ? Math.max(1, Math.floor((sourceWidth - margin + spacing) / (tileW + spacing))) : 1;
|
|
206
|
-
const cols = component.tilesetColumns && component.tilesetColumns > 0 ? component.tilesetColumns : inferredCols;
|
|
207
|
-
// 每次重建 frameTextures(以 tile id - 1 索引)。Phaser 中 id 0 = 空,真实 tile id 从 1 开始。
|
|
208
|
-
// 我们提前不知道 layer 用了哪些 id,所以延迟切;为简单先一次性切前 cols * 推断行数 个。
|
|
209
|
-
// 但避免无限切,这里只在 setTexture 时按需切。
|
|
210
|
-
const tilesetCache = new Map();
|
|
211
|
-
const getFrameTexture = (tileId) => {
|
|
212
|
-
var _a, _b;
|
|
213
|
-
if (tileId <= 0)
|
|
214
|
-
return null;
|
|
215
|
-
const idx = tileId - 1;
|
|
216
|
-
const cached = tilesetCache.get(idx);
|
|
217
|
-
if (cached)
|
|
218
|
-
return cached;
|
|
219
|
-
const col = idx % cols;
|
|
220
|
-
const row = Math.floor(idx / cols);
|
|
221
|
-
const x = margin + col * (tileW + spacing);
|
|
222
|
-
const y = margin + row * (tileH + spacing);
|
|
223
|
-
try {
|
|
224
|
-
// PixiJS v8: 用源纹理 + frame Rectangle 创建子纹理
|
|
225
|
-
const source = (_b = (_a = base.source) !== null && _a !== void 0 ? _a : base.baseTexture) !== null && _b !== void 0 ? _b : base;
|
|
226
|
-
const sub = new pixi_js.Texture({
|
|
227
|
-
source,
|
|
228
|
-
frame: new pixi_js.Rectangle(x, y, tileW, tileH),
|
|
229
|
-
});
|
|
230
|
-
tilesetCache.set(idx, sub);
|
|
231
|
-
record.frameTextures.push(sub);
|
|
232
|
-
return sub;
|
|
233
|
-
}
|
|
234
|
-
catch (e) {
|
|
235
|
-
if (typeof console !== 'undefined') {
|
|
236
|
-
// eslint-disable-next-line no-console
|
|
237
|
-
console.warn('[Tilemap] Texture slice failed, falling back to base', e);
|
|
238
|
-
}
|
|
239
|
-
return base;
|
|
240
|
-
}
|
|
241
|
-
};
|
|
242
|
-
for (const layerSpec of component.layers || []) {
|
|
243
|
-
const layerContainer = new pixi_js.Container();
|
|
244
|
-
layerContainer.label = (_k = layerSpec.name) !== null && _k !== void 0 ? _k : 'tilemap-layer';
|
|
245
|
-
const offX = layerSpec.offsetX || 0;
|
|
246
|
-
const offY = layerSpec.offsetY || 0;
|
|
247
|
-
if (layerSpec.alpha != null)
|
|
248
|
-
layerContainer.alpha = layerSpec.alpha;
|
|
249
|
-
if (layerSpec.visible === false)
|
|
250
|
-
layerContainer.visible = false;
|
|
251
|
-
const data = layerSpec.data || [];
|
|
252
|
-
const tint = layerSpec.tint;
|
|
253
|
-
for (let r = 0; r < data.length; r++) {
|
|
254
|
-
const row = data[r];
|
|
255
|
-
if (!row)
|
|
256
|
-
continue;
|
|
257
|
-
for (let c = 0; c < row.length; c++) {
|
|
258
|
-
const id = row[c];
|
|
259
|
-
if (!id || id <= 0)
|
|
260
|
-
continue;
|
|
261
|
-
const tex = getFrameTexture(id);
|
|
262
|
-
if (!tex)
|
|
263
|
-
continue;
|
|
264
|
-
const sp = new pixi_js.Sprite(tex);
|
|
265
|
-
sp.x = offX + c * tileW;
|
|
266
|
-
sp.y = offY + r * tileH;
|
|
267
|
-
sp.width = renderW;
|
|
268
|
-
sp.height = renderH;
|
|
269
|
-
if (tint != null)
|
|
270
|
-
sp.tint = tint;
|
|
271
|
-
layerContainer.addChild(sp);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
record.root.addChild(layerContainer);
|
|
275
|
-
record.layerContainers.push(layerContainer);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
destroy() {
|
|
279
|
-
var _a;
|
|
280
|
-
for (const key in this.records) {
|
|
281
|
-
const id = parseInt(key);
|
|
282
|
-
const record = this.records[id];
|
|
283
|
-
this.tearDownChildren(record);
|
|
284
|
-
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(id);
|
|
285
|
-
if (container)
|
|
286
|
-
container.removeChild(record.root);
|
|
287
|
-
record.root.destroy({ children: true });
|
|
288
|
-
delete this.records[id];
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
};
|
|
292
|
-
TilemapSystem.systemName = 'Tilemap';
|
|
293
|
-
TilemapSystem = __decorate([
|
|
294
|
-
eva_js.decorators.componentObserver({
|
|
295
|
-
Tilemap: [{ prop: ['tileset'], deep: false }],
|
|
296
|
-
})
|
|
297
|
-
], TilemapSystem);
|
|
298
|
-
var TilemapSystem$1 = TilemapSystem;
|
|
299
|
-
|
|
300
|
-
exports.Tilemap = Tilemap;
|
|
301
|
-
exports.TilemapSystem = TilemapSystem$1;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("@eva/eva.js"),t=require("@eva/inspector-decorator"),r=require("@eva/plugin-renderer"),i=require("pixi.js");function n(e,t,r,i){var n,o=arguments.length,s=o<3?t:null===i?i=Object.getOwnPropertyDescriptor(t,r):i;if("object"==typeof Reflect&&"function"==typeof Reflect.decorate)s=Reflect.decorate(e,t,r,i);else for(var l=e.length-1;l>=0;l--)(n=e[l])&&(s=(o<3?n(s):o>3?n(t,r,s):n(t,r))||s);return o>3&&s&&Object.defineProperty(t,r,s),s}function o(e,t){if("object"==typeof Reflect&&"function"==typeof Reflect.metadata)return Reflect.metadata(e,t)}function s(e,t,r,i){return new(r||(r=Promise))(function(n,o){function s(e){try{a(i.next(e))}catch(e){o(e)}}function l(e){try{a(i.throw(e))}catch(e){o(e)}}function a(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(s,l)}a((i=i.apply(e,t||[])).next())})}"function"==typeof SuppressedError&&SuppressedError;class l extends e.Component{constructor(){super(...arguments),this.tileset="",this.tileWidth=32,this.tileHeight=32,this.tilesetSpacing=0,this.tilesetMargin=0,this.layers=[]}init(e){e&&Object.assign(this,e)}}l.componentName="Tilemap",n([t.type("string"),o("design:type",String)],l.prototype,"tileset",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tileWidth",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tileHeight",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tilesetSpacing",void 0),n([t.type("number"),o("design:type",Number)],l.prototype,"tilesetMargin",void 0);let a=class extends r.Renderer{constructor(){super(...arguments),this.name="Tilemap",this.records={}}init(){this.renderSystem=this.game.getSystem(r.RendererSystem),this.renderSystem.rendererManager.register(this)}rendererUpdate(e){}componentChanged(t){var r,n,o;return s(this,void 0,void 0,function*(){if("Tilemap"!==t.componentName)return;const s=t.component,l=t.gameObject.id;if(t.type===e.OBSERVER_TYPE.ADD){const r=this.increaseAsyncId(l);let n=null;if(s.tileset){const{instance:i}=yield e.resource.getResource(s.tileset);if(!this.validateAsyncId(l,r))return;if(!i)return void console.error(`GameObject:${t.gameObject.name}'s Tilemap tileset load error`);n=i}if(!n)return;const o=new i.Container,a=this.containerManager.getContainer(l);a&&a.addChildAt(o,0);const c={root:o,layerContainers:[],frameTextures:[],baseTexture:n};this.records[l]=c,this.buildLayers(c,s)}else if(t.type===e.OBSERVER_TYPE.CHANGE){const i=this.records[l];if(!i)return;if("tileset"===(null===(n=null===(r=t.prop)||void 0===r?void 0:r.prop)||void 0===n?void 0:n[0])){const t=this.increaseAsyncId(l),{instance:r}=yield e.resource.getResource(s.tileset);if(!this.validateAsyncId(l,t))return;if(!r)return;this.tearDownChildren(i),i.baseTexture=r,this.buildLayers(i,s)}}else if(t.type===e.OBSERVER_TYPE.REMOVE){this.increaseAsyncId(l);const e=this.records[l];if(!e)return;this.tearDownChildren(e);const t=null===(o=this.containerManager)||void 0===o?void 0:o.getContainer(l);t&&t.removeChild(e.root),e.root.destroy({children:!0}),delete this.records[l]}})}tearDownChildren(e){for(const t of e.layerContainers)e.root.removeChild(t),t.destroy({children:!0});e.layerContainers=[];for(const t of e.frameTextures)try{t.destroy(!1)}catch(e){}e.frameTextures=[]}buildLayers(e,t){var r,n,o,s,l,a,c,d,u,p;const h=e.baseTexture;if(!h)return;const f=t.tileWidth||32,y=t.tileHeight||32,m=null!==(r=t.renderTileWidth)&&void 0!==r?r:f,v=null!==(n=t.renderTileHeight)&&void 0!==n?n:y,g=t.tilesetMargin||0,b=t.tilesetSpacing||0,C=null!==(u=null!==(c=null!==(a=null!==(s=null===(o=h.orig)||void 0===o?void 0:o.width)&&void 0!==s?s:null===(l=h.source)||void 0===l?void 0:l.width)&&void 0!==a?a:h.width)&&void 0!==c?c:null===(d=h.frame)||void 0===d?void 0:d.width)&&void 0!==u?u:0,T=C>0?Math.max(1,Math.floor((C-g+b)/(f+b))):1,w=t.tilesetColumns&&t.tilesetColumns>0?t.tilesetColumns:T,x=new Map,R=t=>{var r,n;if(t<=0)return null;const o=t-1,s=x.get(o);if(s)return s;const l=o%w,a=Math.floor(o/w),c=g+l*(f+b),d=g+a*(y+b);try{const t=null!==(n=null!==(r=h.source)&&void 0!==r?r:h.baseTexture)&&void 0!==n?n:h,s=new i.Texture({source:t,frame:new i.Rectangle(c,d,f,y)});return x.set(o,s),e.frameTextures.push(s),s}catch(e){return"undefined"!=typeof console&&console.warn("[Tilemap] Texture slice failed, falling back to base",e),h}};for(const r of t.layers||[]){const t=new i.Container;t.label=null!==(p=r.name)&&void 0!==p?p:"tilemap-layer";const n=r.offsetX||0,o=r.offsetY||0;null!=r.alpha&&(t.alpha=r.alpha),!1===r.visible&&(t.visible=!1);const s=r.data||[],l=r.tint;for(let e=0;e<s.length;e++){const r=s[e];if(r)for(let s=0;s<r.length;s++){const a=r[s];if(!a||a<=0)continue;const c=R(a);if(!c)continue;const d=new i.Sprite(c);d.x=n+s*f,d.y=o+e*y,d.width=m,d.height=v,null!=l&&(d.tint=l),t.addChild(d)}}e.root.addChild(t),e.layerContainers.push(t)}}destroy(){var e;for(const t in this.records){const r=parseInt(t),i=this.records[r];this.tearDownChildren(i);const n=null===(e=this.containerManager)||void 0===e?void 0:e.getContainer(r);n&&n.removeChild(i.root),i.root.destroy({children:!0}),delete this.records[r]}}};a.systemName="Tilemap",a=n([e.decorators.componentObserver({Tilemap:[{prop:["tileset"],deep:!1}]})],a);var c=a;exports.Tilemap=l,exports.TilemapSystem=c;
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { Component } from '@eva/eva.js';
|
|
2
|
-
import { ComponentChanged } from '@eva/eva.js';
|
|
3
|
-
import { ContainerManager } from '@eva/plugin-renderer';
|
|
4
|
-
import { GameObject } from '@eva/eva.js';
|
|
5
|
-
import { Renderer } from '@eva/plugin-renderer';
|
|
6
|
-
import { RendererManager } from '@eva/plugin-renderer';
|
|
7
|
-
import { RendererSystem } from '@eva/plugin-renderer';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Phaser-style Static Tilemap 渲染组件(MVP)。
|
|
11
|
-
*
|
|
12
|
-
* 行为:
|
|
13
|
-
* 1. 从 `tileset` 资源加载一张 spritesheet 纹理;
|
|
14
|
-
* 2. 每个 layer 对应一个 PixiJS Container;
|
|
15
|
-
* 3. layer 内每个非零 tile 都创建一个 Sprite,texture 由原 tileset 切片得到;
|
|
16
|
-
* 4. tile 在 layer Container 内的位置:`(col * tileW + offsetX, row * tileH + offsetY)`。
|
|
17
|
-
*
|
|
18
|
-
* 不支持(Phase 2 视情况增加):tile flipping、动态修改 API、iso/hex、physics 碰撞、camera。
|
|
19
|
-
*/
|
|
20
|
-
export declare class Tilemap extends Component<TilemapParams> {
|
|
21
|
-
static componentName: string;
|
|
22
|
-
tileset: string;
|
|
23
|
-
tileWidth: number;
|
|
24
|
-
tileHeight: number;
|
|
25
|
-
tilesetColumns?: number;
|
|
26
|
-
tilesetSpacing: number;
|
|
27
|
-
tilesetMargin: number;
|
|
28
|
-
renderTileWidth?: number;
|
|
29
|
-
renderTileHeight?: number;
|
|
30
|
-
layers: TilemapLayer[];
|
|
31
|
-
init(obj?: TilemapParams): void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* 单个 Tilemap layer 的描述。
|
|
36
|
-
*
|
|
37
|
-
* - `data` 为二维数组,行优先(row-major):data[row][col]。
|
|
38
|
-
* - tile id `0` 表示空格,不渲染;> 0 的 id 会按 `(id - 1)` 索引到 tileset 的第 N 个 tile。
|
|
39
|
-
* - `offsetX/offsetY` 在 layer 级别整体偏移(等价 Phaser createLayer(... ,x,y))。
|
|
40
|
-
*/
|
|
41
|
-
export declare interface TilemapLayer {
|
|
42
|
-
name?: string;
|
|
43
|
-
/** 二维数组 [row][col],为 0 表示空。 */
|
|
44
|
-
data: number[][];
|
|
45
|
-
offsetX?: number;
|
|
46
|
-
offsetY?: number;
|
|
47
|
-
/** layer 级整体不透明度,默认 1。 */
|
|
48
|
-
alpha?: number;
|
|
49
|
-
/** layer 是否可见,默认 true。 */
|
|
50
|
-
visible?: boolean;
|
|
51
|
-
/** 整层着色;按 sprite.tint 写入。 */
|
|
52
|
-
tint?: number;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export declare interface TilemapParams {
|
|
56
|
-
/** Tileset 图像资源 key(对应 DSL assets 中的 image 资源)。 */
|
|
57
|
-
tileset: string;
|
|
58
|
-
/** 单个 tile 在 tileset 中的宽度。 */
|
|
59
|
-
tileWidth: number;
|
|
60
|
-
/** 单个 tile 在 tileset 中的高度。 */
|
|
61
|
-
tileHeight: number;
|
|
62
|
-
/**
|
|
63
|
-
* Tileset 横向有几列 tile。Tileset 图像总宽 ≥ tilesetColumns * tileWidth。
|
|
64
|
-
* 如果不传,会使用 tileset 图实际尺寸 / tileWidth 推断。
|
|
65
|
-
*/
|
|
66
|
-
tilesetColumns?: number;
|
|
67
|
-
/** Tileset 内部 tile 之间的间距,默认 0。 */
|
|
68
|
-
tilesetSpacing?: number;
|
|
69
|
-
/** Tileset 内部 tile 与图像边缘的 margin,默认 0。 */
|
|
70
|
-
tilesetMargin?: number;
|
|
71
|
-
/** 渲染时的 tile 显示宽度;不传则与 tileWidth 一致(用于切片放大)。 */
|
|
72
|
-
renderTileWidth?: number;
|
|
73
|
-
/** 渲染时的 tile 显示高度。 */
|
|
74
|
-
renderTileHeight?: number;
|
|
75
|
-
/** 多个 layer。 */
|
|
76
|
-
layers: TilemapLayer[];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export declare class TilemapSystem extends Renderer {
|
|
80
|
-
static systemName: string;
|
|
81
|
-
name: string;
|
|
82
|
-
private records;
|
|
83
|
-
renderSystem: RendererSystem;
|
|
84
|
-
rendererManager: RendererManager;
|
|
85
|
-
containerManager: ContainerManager;
|
|
86
|
-
init(): void;
|
|
87
|
-
rendererUpdate(_gameObject: GameObject): void;
|
|
88
|
-
componentChanged(changed: ComponentChanged): Promise<void>;
|
|
89
|
-
/** 销毁现有 layer/frame 纹理(切换 tileset 或卸载时调用)。 */
|
|
90
|
-
private tearDownChildren;
|
|
91
|
-
private buildLayers;
|
|
92
|
-
destroy(): void;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export { }
|
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
import { Component, decorators, OBSERVER_TYPE, resource } from '@eva/eva.js';
|
|
2
|
-
import { type } from '@eva/inspector-decorator';
|
|
3
|
-
import { Renderer, RendererSystem } from '@eva/plugin-renderer';
|
|
4
|
-
import { Container, Sprite, Texture, Rectangle } from 'pixi.js';
|
|
5
|
-
|
|
6
|
-
/******************************************************************************
|
|
7
|
-
Copyright (c) Microsoft Corporation.
|
|
8
|
-
|
|
9
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
10
|
-
purpose with or without fee is hereby granted.
|
|
11
|
-
|
|
12
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
13
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
14
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
15
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
16
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
17
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
-
***************************************************************************** */
|
|
20
|
-
|
|
21
|
-
function __decorate(decorators, target, key, desc) {
|
|
22
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
23
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
24
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
25
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function __metadata(metadataKey, metadataValue) {
|
|
29
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
43
|
-
var e = new Error(message);
|
|
44
|
-
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Phaser-style Static Tilemap 渲染组件(MVP)。
|
|
49
|
-
*
|
|
50
|
-
* 行为:
|
|
51
|
-
* 1. 从 `tileset` 资源加载一张 spritesheet 纹理;
|
|
52
|
-
* 2. 每个 layer 对应一个 PixiJS Container;
|
|
53
|
-
* 3. layer 内每个非零 tile 都创建一个 Sprite,texture 由原 tileset 切片得到;
|
|
54
|
-
* 4. tile 在 layer Container 内的位置:`(col * tileW + offsetX, row * tileH + offsetY)`。
|
|
55
|
-
*
|
|
56
|
-
* 不支持(Phase 2 视情况增加):tile flipping、动态修改 API、iso/hex、physics 碰撞、camera。
|
|
57
|
-
*/
|
|
58
|
-
class Tilemap extends Component {
|
|
59
|
-
constructor() {
|
|
60
|
-
super(...arguments);
|
|
61
|
-
this.tileset = '';
|
|
62
|
-
this.tileWidth = 32;
|
|
63
|
-
this.tileHeight = 32;
|
|
64
|
-
this.tilesetSpacing = 0;
|
|
65
|
-
this.tilesetMargin = 0;
|
|
66
|
-
this.layers = [];
|
|
67
|
-
}
|
|
68
|
-
init(obj) {
|
|
69
|
-
if (obj)
|
|
70
|
-
Object.assign(this, obj);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
Tilemap.componentName = 'Tilemap';
|
|
74
|
-
__decorate([
|
|
75
|
-
type('string'),
|
|
76
|
-
__metadata("design:type", String)
|
|
77
|
-
], Tilemap.prototype, "tileset", void 0);
|
|
78
|
-
__decorate([
|
|
79
|
-
type('number'),
|
|
80
|
-
__metadata("design:type", Number)
|
|
81
|
-
], Tilemap.prototype, "tileWidth", void 0);
|
|
82
|
-
__decorate([
|
|
83
|
-
type('number'),
|
|
84
|
-
__metadata("design:type", Number)
|
|
85
|
-
], Tilemap.prototype, "tileHeight", void 0);
|
|
86
|
-
__decorate([
|
|
87
|
-
type('number'),
|
|
88
|
-
__metadata("design:type", Number)
|
|
89
|
-
], Tilemap.prototype, "tilesetSpacing", void 0);
|
|
90
|
-
__decorate([
|
|
91
|
-
type('number'),
|
|
92
|
-
__metadata("design:type", Number)
|
|
93
|
-
], Tilemap.prototype, "tilesetMargin", void 0);
|
|
94
|
-
|
|
95
|
-
let TilemapSystem = class TilemapSystem extends Renderer {
|
|
96
|
-
constructor() {
|
|
97
|
-
super(...arguments);
|
|
98
|
-
this.name = 'Tilemap';
|
|
99
|
-
this.records = {};
|
|
100
|
-
}
|
|
101
|
-
init() {
|
|
102
|
-
this.renderSystem = this.game.getSystem(RendererSystem);
|
|
103
|
-
this.renderSystem.rendererManager.register(this);
|
|
104
|
-
}
|
|
105
|
-
rendererUpdate(_gameObject) {
|
|
106
|
-
// 静态 tilemap MVP:几何不随 transform.size 变化,无需逐帧刷新。
|
|
107
|
-
}
|
|
108
|
-
componentChanged(changed) {
|
|
109
|
-
var _a, _b, _c;
|
|
110
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
111
|
-
if (changed.componentName !== 'Tilemap')
|
|
112
|
-
return;
|
|
113
|
-
const component = changed.component;
|
|
114
|
-
const gameObjectId = changed.gameObject.id;
|
|
115
|
-
if (changed.type === OBSERVER_TYPE.ADD) {
|
|
116
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
117
|
-
let texture = null;
|
|
118
|
-
if (component.tileset) {
|
|
119
|
-
const { instance } = yield resource.getResource(component.tileset);
|
|
120
|
-
if (!this.validateAsyncId(gameObjectId, asyncId))
|
|
121
|
-
return;
|
|
122
|
-
if (!instance) {
|
|
123
|
-
console.error(`GameObject:${changed.gameObject.name}'s Tilemap tileset load error`);
|
|
124
|
-
return;
|
|
125
|
-
}
|
|
126
|
-
texture = instance;
|
|
127
|
-
}
|
|
128
|
-
if (!texture)
|
|
129
|
-
return;
|
|
130
|
-
const root = new Container();
|
|
131
|
-
const container = this.containerManager.getContainer(gameObjectId);
|
|
132
|
-
if (container)
|
|
133
|
-
container.addChildAt(root, 0);
|
|
134
|
-
const record = {
|
|
135
|
-
root,
|
|
136
|
-
layerContainers: [],
|
|
137
|
-
frameTextures: [],
|
|
138
|
-
baseTexture: texture,
|
|
139
|
-
};
|
|
140
|
-
this.records[gameObjectId] = record;
|
|
141
|
-
this.buildLayers(record, component);
|
|
142
|
-
}
|
|
143
|
-
else if (changed.type === OBSERVER_TYPE.CHANGE) {
|
|
144
|
-
const record = this.records[gameObjectId];
|
|
145
|
-
if (!record)
|
|
146
|
-
return;
|
|
147
|
-
if (((_b = (_a = changed.prop) === null || _a === void 0 ? void 0 : _a.prop) === null || _b === void 0 ? void 0 : _b[0]) === 'tileset') {
|
|
148
|
-
const asyncId = this.increaseAsyncId(gameObjectId);
|
|
149
|
-
const { instance } = yield resource.getResource(component.tileset);
|
|
150
|
-
if (!this.validateAsyncId(gameObjectId, asyncId))
|
|
151
|
-
return;
|
|
152
|
-
if (!instance)
|
|
153
|
-
return;
|
|
154
|
-
this.tearDownChildren(record);
|
|
155
|
-
record.baseTexture = instance;
|
|
156
|
-
this.buildLayers(record, component);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
else if (changed.type === OBSERVER_TYPE.REMOVE) {
|
|
160
|
-
this.increaseAsyncId(gameObjectId);
|
|
161
|
-
const record = this.records[gameObjectId];
|
|
162
|
-
if (!record)
|
|
163
|
-
return;
|
|
164
|
-
this.tearDownChildren(record);
|
|
165
|
-
const container = (_c = this.containerManager) === null || _c === void 0 ? void 0 : _c.getContainer(gameObjectId);
|
|
166
|
-
if (container)
|
|
167
|
-
container.removeChild(record.root);
|
|
168
|
-
record.root.destroy({ children: true });
|
|
169
|
-
delete this.records[gameObjectId];
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
/** 销毁现有 layer/frame 纹理(切换 tileset 或卸载时调用)。 */
|
|
174
|
-
tearDownChildren(record) {
|
|
175
|
-
for (const layer of record.layerContainers) {
|
|
176
|
-
record.root.removeChild(layer);
|
|
177
|
-
layer.destroy({ children: true });
|
|
178
|
-
}
|
|
179
|
-
record.layerContainers = [];
|
|
180
|
-
for (const tex of record.frameTextures) {
|
|
181
|
-
try {
|
|
182
|
-
tex.destroy(false);
|
|
183
|
-
}
|
|
184
|
-
catch ( /* ignore */_a) { /* ignore */ }
|
|
185
|
-
}
|
|
186
|
-
record.frameTextures = [];
|
|
187
|
-
}
|
|
188
|
-
buildLayers(record, component) {
|
|
189
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
|
|
190
|
-
const base = record.baseTexture;
|
|
191
|
-
if (!base)
|
|
192
|
-
return;
|
|
193
|
-
const tileW = component.tileWidth || 32;
|
|
194
|
-
const tileH = component.tileHeight || 32;
|
|
195
|
-
const renderW = (_a = component.renderTileWidth) !== null && _a !== void 0 ? _a : tileW;
|
|
196
|
-
const renderH = (_b = component.renderTileHeight) !== null && _b !== void 0 ? _b : tileH;
|
|
197
|
-
const margin = component.tilesetMargin || 0;
|
|
198
|
-
const spacing = component.tilesetSpacing || 0;
|
|
199
|
-
// 推断 columns
|
|
200
|
-
const sourceWidth = (_j = (_g = (_f = (_d = (_c = base.orig) === null || _c === void 0 ? void 0 : _c.width) !== null && _d !== void 0 ? _d : (_e = base.source) === null || _e === void 0 ? void 0 : _e.width) !== null && _f !== void 0 ? _f : base.width) !== null && _g !== void 0 ? _g : (_h = base.frame) === null || _h === void 0 ? void 0 : _h.width) !== null && _j !== void 0 ? _j : 0;
|
|
201
|
-
const inferredCols = sourceWidth > 0 ? Math.max(1, Math.floor((sourceWidth - margin + spacing) / (tileW + spacing))) : 1;
|
|
202
|
-
const cols = component.tilesetColumns && component.tilesetColumns > 0 ? component.tilesetColumns : inferredCols;
|
|
203
|
-
// 每次重建 frameTextures(以 tile id - 1 索引)。Phaser 中 id 0 = 空,真实 tile id 从 1 开始。
|
|
204
|
-
// 我们提前不知道 layer 用了哪些 id,所以延迟切;为简单先一次性切前 cols * 推断行数 个。
|
|
205
|
-
// 但避免无限切,这里只在 setTexture 时按需切。
|
|
206
|
-
const tilesetCache = new Map();
|
|
207
|
-
const getFrameTexture = (tileId) => {
|
|
208
|
-
var _a, _b;
|
|
209
|
-
if (tileId <= 0)
|
|
210
|
-
return null;
|
|
211
|
-
const idx = tileId - 1;
|
|
212
|
-
const cached = tilesetCache.get(idx);
|
|
213
|
-
if (cached)
|
|
214
|
-
return cached;
|
|
215
|
-
const col = idx % cols;
|
|
216
|
-
const row = Math.floor(idx / cols);
|
|
217
|
-
const x = margin + col * (tileW + spacing);
|
|
218
|
-
const y = margin + row * (tileH + spacing);
|
|
219
|
-
try {
|
|
220
|
-
// PixiJS v8: 用源纹理 + frame Rectangle 创建子纹理
|
|
221
|
-
const source = (_b = (_a = base.source) !== null && _a !== void 0 ? _a : base.baseTexture) !== null && _b !== void 0 ? _b : base;
|
|
222
|
-
const sub = new Texture({
|
|
223
|
-
source,
|
|
224
|
-
frame: new Rectangle(x, y, tileW, tileH),
|
|
225
|
-
});
|
|
226
|
-
tilesetCache.set(idx, sub);
|
|
227
|
-
record.frameTextures.push(sub);
|
|
228
|
-
return sub;
|
|
229
|
-
}
|
|
230
|
-
catch (e) {
|
|
231
|
-
if (typeof console !== 'undefined') {
|
|
232
|
-
// eslint-disable-next-line no-console
|
|
233
|
-
console.warn('[Tilemap] Texture slice failed, falling back to base', e);
|
|
234
|
-
}
|
|
235
|
-
return base;
|
|
236
|
-
}
|
|
237
|
-
};
|
|
238
|
-
for (const layerSpec of component.layers || []) {
|
|
239
|
-
const layerContainer = new Container();
|
|
240
|
-
layerContainer.label = (_k = layerSpec.name) !== null && _k !== void 0 ? _k : 'tilemap-layer';
|
|
241
|
-
const offX = layerSpec.offsetX || 0;
|
|
242
|
-
const offY = layerSpec.offsetY || 0;
|
|
243
|
-
if (layerSpec.alpha != null)
|
|
244
|
-
layerContainer.alpha = layerSpec.alpha;
|
|
245
|
-
if (layerSpec.visible === false)
|
|
246
|
-
layerContainer.visible = false;
|
|
247
|
-
const data = layerSpec.data || [];
|
|
248
|
-
const tint = layerSpec.tint;
|
|
249
|
-
for (let r = 0; r < data.length; r++) {
|
|
250
|
-
const row = data[r];
|
|
251
|
-
if (!row)
|
|
252
|
-
continue;
|
|
253
|
-
for (let c = 0; c < row.length; c++) {
|
|
254
|
-
const id = row[c];
|
|
255
|
-
if (!id || id <= 0)
|
|
256
|
-
continue;
|
|
257
|
-
const tex = getFrameTexture(id);
|
|
258
|
-
if (!tex)
|
|
259
|
-
continue;
|
|
260
|
-
const sp = new Sprite(tex);
|
|
261
|
-
sp.x = offX + c * tileW;
|
|
262
|
-
sp.y = offY + r * tileH;
|
|
263
|
-
sp.width = renderW;
|
|
264
|
-
sp.height = renderH;
|
|
265
|
-
if (tint != null)
|
|
266
|
-
sp.tint = tint;
|
|
267
|
-
layerContainer.addChild(sp);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
record.root.addChild(layerContainer);
|
|
271
|
-
record.layerContainers.push(layerContainer);
|
|
272
|
-
}
|
|
273
|
-
}
|
|
274
|
-
destroy() {
|
|
275
|
-
var _a;
|
|
276
|
-
for (const key in this.records) {
|
|
277
|
-
const id = parseInt(key);
|
|
278
|
-
const record = this.records[id];
|
|
279
|
-
this.tearDownChildren(record);
|
|
280
|
-
const container = (_a = this.containerManager) === null || _a === void 0 ? void 0 : _a.getContainer(id);
|
|
281
|
-
if (container)
|
|
282
|
-
container.removeChild(record.root);
|
|
283
|
-
record.root.destroy({ children: true });
|
|
284
|
-
delete this.records[id];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
TilemapSystem.systemName = 'Tilemap';
|
|
289
|
-
TilemapSystem = __decorate([
|
|
290
|
-
decorators.componentObserver({
|
|
291
|
-
Tilemap: [{ prop: ['tileset'], deep: false }],
|
|
292
|
-
})
|
|
293
|
-
], TilemapSystem);
|
|
294
|
-
var TilemapSystem$1 = TilemapSystem;
|
|
295
|
-
|
|
296
|
-
export { Tilemap, TilemapSystem$1 as TilemapSystem };
|