-
Notifications
You must be signed in to change notification settings - Fork 964
/
Copy pathtiledspriteruntimeobject.ts
357 lines (315 loc) · 9.58 KB
/
tiledspriteruntimeobject.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
* GDevelop JS Platform
* 2013 Florian Rival ([email protected])
*/
namespace gdjs {
/** Initial properties for a Tiled Sprite object */
export type TiledSpriteObjectDataType = {
/** Default width of the object, if the instance has no custom width. */
width: number;
/** Default height of the object, if the instance has no custom height. */
height: number;
texture: string;
};
export type TiledSpriteObjectData = ObjectData & TiledSpriteObjectDataType;
export type TiledSpriteNetworkSyncDataType = {
wid: number;
hei: number;
xo: number;
yo: number;
op: number;
color: string;
};
export type TiledSpriteNetworkSyncData = ObjectNetworkSyncData &
TiledSpriteNetworkSyncDataType;
/**
* The TiledSpriteRuntimeObject displays a tiled texture.
*/
export class TiledSpriteRuntimeObject
extends gdjs.RuntimeObject
implements gdjs.Resizable, gdjs.OpacityHandler
{
_xOffset: float = 0;
_yOffset: float = 0;
opacity: float = 255;
// Width and height can be stored because they do not depend on the
// size of the texture being used (contrary to most objects).
_width: float;
_height: float;
_renderer: gdjs.TiledSpriteRuntimeObjectRenderer;
/**
* @param instanceContainer The container the object belongs to.
* @param tiledSpriteObjectData The initial properties of the object
*/
constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
tiledSpriteObjectData: TiledSpriteObjectData
) {
super(instanceContainer, tiledSpriteObjectData);
this._renderer = new gdjs.TiledSpriteRuntimeObjectRenderer(
this,
instanceContainer,
tiledSpriteObjectData.texture
);
this._width = 0;
this._height = 0;
this.setWidth(tiledSpriteObjectData.width);
this.setHeight(tiledSpriteObjectData.height);
// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
this.onCreated();
}
updateFromObjectData(oldObjectData, newObjectData): boolean {
if (oldObjectData.texture !== newObjectData.texture) {
this.setTexture(newObjectData.texture, this.getRuntimeScene());
}
if (oldObjectData.width !== newObjectData.width) {
this.setWidth(newObjectData.width);
}
if (oldObjectData.height !== newObjectData.height) {
this.setHeight(newObjectData.height);
}
return true;
}
getNetworkSyncData(): TiledSpriteNetworkSyncData {
return {
...super.getNetworkSyncData(),
wid: this.getWidth(),
hei: this.getHeight(),
xo: this.getXOffset(),
yo: this.getYOffset(),
op: this.getOpacity(),
color: this.getColor(),
};
}
updateFromNetworkSyncData(
networkSyncData: TiledSpriteNetworkSyncData,
options?: { skipMultiplayerInstructions: boolean }
): void {
super.updateFromNetworkSyncData(networkSyncData, options);
// Texture is not synchronized, see if this is asked or not.
if (networkSyncData.wid !== undefined) {
this.setWidth(networkSyncData.wid);
}
if (networkSyncData.hei !== undefined) {
this.setHeight(networkSyncData.hei);
}
if (networkSyncData.xo !== undefined) {
this.setXOffset(networkSyncData.xo);
}
if (networkSyncData.yo !== undefined) {
this.setYOffset(networkSyncData.yo);
}
if (networkSyncData.op !== undefined) {
this.setOpacity(networkSyncData.op);
}
if (networkSyncData.color !== undefined) {
this.setColor(networkSyncData.color);
}
}
getRendererObject() {
return this._renderer.getRendererObject();
}
onDestroyed(): void {
super.onDestroyed();
this._renderer.destroy();
}
/**
* Initialize the extra parameters that could be set for an instance.
*/
extraInitializationFromInitialInstance(initialInstanceData: InstanceData) {
if (initialInstanceData.customSize) {
this.setWidth(initialInstanceData.width);
this.setHeight(initialInstanceData.height);
}
if (initialInstanceData.opacity !== undefined) {
this.setOpacity(initialInstanceData.opacity);
}
}
/**
* Set the X position of the Tiled Sprite object.
* @param x The new X position.
*/
setX(x: float): void {
super.setX(x);
this._renderer.updatePosition();
}
/**
* Set the Y position of the Tiled Sprite object.
* @param y The new Y position.
*/
setY(y: float): void {
super.setY(y);
this._renderer.updatePosition();
}
/**
* Assign a new texture to the Tiled Sprite object.
* @param textureName The name of the image texture resource.
* @param instanceContainer The container in which the texture is used.
*/
setTexture(
textureName: string,
instanceContainer: gdjs.RuntimeInstanceContainer
): void {
this._renderer.setTexture(textureName, instanceContainer);
}
/**
* Set the angle of the Tiled Sprite object.
* @param angle The new angle.
*/
setAngle(angle: float): void {
super.setAngle(angle);
this._renderer.updateAngle();
}
/**
* Get the width of the Tiled Sprite object.
* @returns The width of the Tiled Sprite object
*/
getWidth(): float {
return this._width;
}
/**
* Get the height of the Tiled Sprite object.
* @returns The height of the Tiled Sprite object
*/
getHeight(): float {
return this._height;
}
/**
* Set the width of the Tiled Sprite object.
* @param width The new width.
*/
setWidth(width: float): void {
if (this._width === width) return;
this._width = width;
this._renderer.setWidth(width);
this.invalidateHitboxes();
}
/**
* Set the height of the Tiled Sprite object.
* @param height The new height.
*/
setHeight(height: float): void {
if (this._height === height) return;
this._height = height;
this._renderer.setHeight(height);
this.invalidateHitboxes();
}
/**
* Set the size of the Tiled Sprite object.
* @param width The new width.
* @param height The new height.
*/
setSize(width: float, height: float): void {
this.setWidth(width);
this.setHeight(height);
}
/**
* Set the offset on the X-axis when displaying the image of the Tiled Sprite object.
* @param xOffset The new offset on the X-axis.
*/
setXOffset(xOffset: number): void {
this._xOffset = xOffset;
this._renderer.updateXOffset();
}
/**
* Set the offset on the Y-axis when displaying the image of the Tiled Sprite object.
* @param yOffset The new offset on the Y-axis.
*/
setYOffset(yOffset: number): void {
this._yOffset = yOffset;
this._renderer.updateYOffset();
}
/**
* Get the offset on the X-axis of the Tiled Sprite object.
* @returns The offset on the X-axis
*/
getXOffset(): number {
return this._xOffset;
}
/**
* Get the offset on the Y-axis of the Tiled Sprite object.
* @returns The offset on the Y-axis
*/
getYOffset(): number {
return this._yOffset;
}
setOpacity(opacity: float): void {
if (opacity < 0) {
opacity = 0;
}
if (opacity > 255) {
opacity = 255;
}
this.opacity = opacity;
this._renderer.updateOpacity();
}
getOpacity(): number {
return this.opacity;
}
/**
* Change the tint of the tiled sprite object.
*
* @param rgbColor The color, in RGB format ("128;200;255").
*/
setColor(rgbColor: string): void {
this._renderer.setColor(rgbColor);
}
/**
* Get the tint of the tiled sprite object.
*
* @returns The color, in RGB format ("128;200;255").
*/
getColor(): string {
return this._renderer.getColor();
}
// Implement support for get/set scale:
/**
* Get the scale of the object (or the geometric mean of the X and Y scale in case they are different).
*
* @return the scale of the object (or the geometric mean of the X and Y scale in case they are different).
*/
getScale(): float {
const scaleX = Math.abs(this.getScaleX());
const scaleY = Math.abs(this.getScaleY());
return scaleX === scaleY ? scaleX : Math.sqrt(scaleX * scaleY);
}
/**
* Get x-scale of the tiled sprite object.
*/
getScaleX(): float {
return this._width / this._renderer.getTextureWidth();
}
/**
* Get y-scale of the tiled sprite object.
*/
getScaleY(): float {
return this._height / this._renderer.getTextureHeight();
}
/**
* Set the tiled sprite object scale.
* @param newScale The new scale for the tiled sprite object.
*/
setScale(newScale: float): void {
this.setWidth(this._renderer.getTextureWidth() * newScale);
this.setHeight(this._renderer.getTextureHeight() * newScale);
}
/**
* Set the tiled sprite object x-scale.
* @param newScale The new x-scale for the tiled sprite object.
*/
setScaleX(newScale: float): void {
this.setWidth(this._renderer.getTextureWidth() * newScale);
}
/**
* Set the tiled sprite object y-scale.
* @param newScale The new y-scale for the tiled sprite object.
*/
setScaleY(newScale: float): void {
this.setHeight(this._renderer.getTextureHeight() * newScale);
}
}
gdjs.registerObject(
'TiledSpriteObject::TiledSprite',
gdjs.TiledSpriteRuntimeObject
);
}