-
Notifications
You must be signed in to change notification settings - Fork 759
/
spriteruntimeobject.ts
953 lines (868 loc) 路 28.1 KB
/
spriteruntimeobject.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
/*
* GDevelop JS Platform
* Copyright 2013-2016 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
namespace gdjs {
/** Represents the data of a {@link gdjs.SpriteRuntimeObject}. */
export type SpriteObjectDataType = {
/** Update the object even if he is not visible?. */
updateIfNotVisible: boolean;
/** The list of {@link SpriteAnimationData} representing {@link gdjs.SpriteAnimation} instances. */
animations: Array<SpriteAnimationData>;
};
export type SpriteObjectData = ObjectData & SpriteObjectDataType;
export type SpriteNetworkSyncDataType = {
anim: SpriteAnimatorNetworkSyncData;
ifx: boolean;
ify: boolean;
sx: float;
sy: float;
op: float;
color: string;
};
export type SpriteNetworkSyncData = ObjectNetworkSyncData &
SpriteNetworkSyncDataType;
/**
* The SpriteRuntimeObject represents an object that can display images.
*/
export class SpriteRuntimeObject
extends gdjs.RuntimeObject
implements
gdjs.Resizable,
gdjs.Scalable,
gdjs.Flippable,
gdjs.Animatable,
gdjs.OpacityHandler {
_animator: gdjs.SpriteAnimator<any>;
_scaleX: float = 1;
_scaleY: float = 1;
_blendMode: integer = 0;
_flippedX: boolean = false;
_flippedY: boolean = false;
opacity: float = 255;
_updateIfNotVisible: boolean;
_renderer: gdjs.SpriteRuntimeObjectRenderer;
_animationFrameDirty = true;
/**
* @param instanceContainer The container the object belongs to
* @param spriteObjectData The object data used to initialize the object
*/
constructor(
instanceContainer: gdjs.RuntimeInstanceContainer,
spriteObjectData: ObjectData & SpriteObjectDataType
) {
super(instanceContainer, spriteObjectData);
this._updateIfNotVisible = !!spriteObjectData.updateIfNotVisible;
this._renderer = new gdjs.SpriteRuntimeObjectRenderer(
this,
instanceContainer
);
this._animator = new gdjs.SpriteAnimator(
spriteObjectData.animations,
gdjs.SpriteRuntimeObjectRenderer.getAnimationFrameTextureManager(
instanceContainer.getGame().getImageManager()
)
);
this._updateAnimationFrame();
// *ALWAYS* call `this.onCreated()` at the very end of your object constructor.
this.onCreated();
}
reinitialize(spriteObjectData: SpriteObjectData) {
super.reinitialize(spriteObjectData);
const instanceContainer = this.getInstanceContainer();
this._animator.reinitialize(spriteObjectData.animations);
this._scaleX = 1;
this._scaleY = 1;
this._blendMode = 0;
this._flippedX = false;
this._flippedY = false;
this.opacity = 255;
this._updateIfNotVisible = !!spriteObjectData.updateIfNotVisible;
this._renderer.reinitialize(this, instanceContainer);
this._updateAnimationFrame();
// *ALWAYS* call `this.onCreated()` at the very end of your object reinitialize method.
this.onCreated();
}
updateFromObjectData(
oldObjectData: SpriteObjectData,
newObjectData: SpriteObjectData
): boolean {
this._animator.updateFromObjectData(
oldObjectData.animations,
newObjectData.animations
);
this._updateIfNotVisible = !!newObjectData.updateIfNotVisible;
this.invalidateHitboxes();
return true;
}
getNetworkSyncData(): SpriteNetworkSyncData {
return {
...super.getNetworkSyncData(),
anim: this._animator.getNetworkSyncData(),
ifx: this.isFlippedX(),
ify: this.isFlippedY(),
sx: this._scaleX,
sy: this._scaleY,
op: this.opacity,
color: this.getColor(),
};
}
updateFromNetworkSyncData(newNetworkSyncData: SpriteNetworkSyncData) {
super.updateFromNetworkSyncData(newNetworkSyncData);
if (newNetworkSyncData.ifx !== undefined) {
this.flipX(newNetworkSyncData.ifx);
}
if (newNetworkSyncData.ify !== undefined) {
this.flipY(newNetworkSyncData.ify);
}
if (newNetworkSyncData.sx !== undefined) {
this.setScaleX(Math.abs(newNetworkSyncData.sx));
}
if (newNetworkSyncData.sy !== undefined) {
this.setScaleY(Math.abs(newNetworkSyncData.sy));
}
if (newNetworkSyncData.op !== undefined) {
this.setOpacity(newNetworkSyncData.op);
}
if (newNetworkSyncData.anim) {
this._animator.updateFromNetworkSyncData(newNetworkSyncData.anim);
// TODO: optimize updating the animation frame only if needed.
this._updateAnimationFrame();
}
if (
newNetworkSyncData.ifx !== undefined ||
newNetworkSyncData.ify !== undefined ||
newNetworkSyncData.sx !== undefined ||
newNetworkSyncData.sy !== undefined ||
newNetworkSyncData.anim !== undefined
) {
this.invalidateHitboxes();
}
if (newNetworkSyncData.color !== undefined) {
this.setColor(newNetworkSyncData.color);
}
}
/**
* Initialize the extra parameters that could be set for an instance.
* @param initialInstanceData The extra parameters
*/
extraInitializationFromInitialInstance(initialInstanceData: InstanceData) {
if (initialInstanceData.numberProperties) {
for (
let i = 0, len = initialInstanceData.numberProperties.length;
i < len;
++i
) {
const extraData = initialInstanceData.numberProperties[i];
if (extraData.name === 'animation') {
this.setAnimationIndex(extraData.value);
}
}
}
if (initialInstanceData.customSize) {
this.setWidth(initialInstanceData.width);
this.setHeight(initialInstanceData.height);
}
}
/**
* Update the current frame of the object according to the elapsed time on the scene.
*/
update(instanceContainer: gdjs.RuntimeInstanceContainer): void {
//Playing the animation of all objects including the ones outside the screen can be
//costly when the scene is big with a lot of animated objects. By default, we skip
//updating the object if it is not visible.
if (
!this._updateIfNotVisible &&
!this._renderer.getRendererObject().visible
) {
return;
}
const hasFrameChanged = this._animator.step(this.getElapsedTime() / 1000);
if (hasFrameChanged) {
this._updateAnimationFrame();
// TODO: Hitboxes may not need an update if every frames has the same ones.
this.invalidateHitboxes();
}
this._renderer.ensureUpToDate();
}
/**
* Ensure the sprite is ready to be displayed: the proper animation frame
* is set and the renderer is up to date (position, angle, alpha, flip, blend mode...).
*/
updatePreRender(instanceContainer: gdjs.RuntimeInstanceContainer): void {
if (this._animationFrameDirty) {
this._updateAnimationFrame();
}
this._renderer.ensureUpToDate();
}
/**
* Update `this._animationFrame` according to the current animation/direction/frame values
* (`this._currentAnimation`, `this._currentDirection`, `this._currentFrame`) and set
* `this._animationFrameDirty` back to false.
*
* Trigger a call to the renderer to change the texture being shown (if the animation/direction/frame
* is valid).
* If invalid, `this._animationFrame` will be `null` after calling this function.
*/
_updateAnimationFrame() {
this._animationFrameDirty = false;
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame) {
this._renderer.updateFrame(animationFrame);
}
}
getRendererObject() {
return this._renderer.getRendererObject();
}
/**
* Update the hit boxes for the object.
* Fallback to the default implementation (rotated bounding box) if there is no custom
* hitboxes defined for the current animation frame.
*/
updateHitBoxes(): void {
const animationFrame = this._animator.getCurrentFrame();
if (!animationFrame) {
return;
}
if (!animationFrame.hasCustomHitBoxes) {
return super.updateHitBoxes();
}
//logger.log("Update for "+this.name); //Uncomment to track updates
//(and in particular be sure that unnecessary update are avoided).
//Update the current hitboxes with the frame custom hit boxes
//and apply transformations.
for (let i = 0; i < animationFrame.customHitBoxes.length; ++i) {
if (i >= this.hitBoxes.length) {
this.hitBoxes.push(new gdjs.Polygon());
}
for (
let j = 0;
j < animationFrame.customHitBoxes[i].vertices.length;
++j
) {
if (j >= this.hitBoxes[i].vertices.length) {
this.hitBoxes[i].vertices.push([0, 0]);
}
this._transformToGlobal(
animationFrame.customHitBoxes[i].vertices[j][0],
animationFrame.customHitBoxes[i].vertices[j][1],
this.hitBoxes[i].vertices[j]
);
}
this.hitBoxes[i].vertices.length =
animationFrame.customHitBoxes[i].vertices.length;
}
this.hitBoxes.length = animationFrame.customHitBoxes.length;
}
//Rotate and scale and flipping have already been applied to the point by _transformToGlobal.
//Animations :
/**
* Change the animation being played.
* @param newAnimation The index of the new animation to be played
* @deprecated Use `setAnimationIndex` instead
*/
setAnimation(newAnimation: integer): void {
this.setAnimationIndex(newAnimation);
}
setAnimationIndex(newAnimation: integer): void {
const hasAnimationChanged = this._animator.setAnimationIndex(
newAnimation
);
if (hasAnimationChanged) {
//TODO: This may be unnecessary.
this._renderer.update();
this._animationFrameDirty = true;
this.invalidateHitboxes();
}
}
setAnimationName(newAnimationName: string): void {
const hasAnimationChanged = this._animator.setAnimationName(
newAnimationName
);
if (hasAnimationChanged) {
//TODO: This may be unnecessary.
this._renderer.update();
this._animationFrameDirty = true;
this.invalidateHitboxes();
}
}
/**
* Get the index of the animation being played.
* @return The index of the new animation being played
* @deprecated Use `getAnimationIndex` instead
*/
getAnimation(): integer {
return this.getAnimationIndex();
}
getAnimationIndex(): integer {
return this._animator.getAnimationIndex();
}
getAnimationName(): string {
return this._animator.getAnimationName();
}
isCurrentAnimationName(name: string): boolean {
return this.getAnimationName() === name;
}
/**
* Change the angle (or direction index) of the object
* @param The new angle (or direction index) to be applied
*/
setDirectionOrAngle(newValue: float): void {
const actualValue = this._animator.setDirectionOrAngle(
this.angle,
newValue
);
if (actualValue !== null) {
this.angle = actualValue;
//TODO: This may be unnecessary.
this._renderer.update();
this._animationFrameDirty = true;
this.invalidateHitboxes();
this._renderer.updateAngle();
}
}
getDirectionOrAngle(): float {
return this._animator.getDirectionOrAngle(this.angle);
}
/**
* Change the current frame displayed by the animation
* @param newFrame The index of the frame to be displayed
*/
setAnimationFrame(newFrame: integer): void {
const hasFrameChanged = this._animator.setAnimationFrameIndex(newFrame);
if (hasFrameChanged) {
this._animationFrameDirty = true;
this.invalidateHitboxes();
}
}
/**
* Get the index of the current frame displayed by the animation
* @return newFrame The index of the frame being displayed
*/
getAnimationFrame(): integer {
return this._animator.getAnimationFrameIndex();
}
getAnimationElapsedTime(): float {
return this._animator.getAnimationElapsedTime();
}
setAnimationElapsedTime(time: float): void {
const hasFrameChanged = this._animator.getAnimationElapsedTime();
if (hasFrameChanged) {
this._animationFrameDirty = true;
this.invalidateHitboxes();
}
}
getAnimationDuration(): float {
return this._animator.getAnimationDuration();
}
getAnimationFrameCount(): integer {
return this._animator.getAnimationFrameCount();
}
/**
* @deprecated
* Return true if animation has ended.
* Prefer using `hasAnimationEnded2`. This method returns true as soon as
* the animation enters the last frame, not at the end of the last frame.
*/
hasAnimationEndedLegacy(): boolean {
return this._animator.hasAnimationEndedLegacy();
}
/**
* Return true if animation has ended.
* The animation had ended if:
* - it's not configured as a loop;
* - the current frame is the last frame;
* - the last frame has been displayed long enough.
*
* @deprecated Use `hasAnimationEnded` instead.
*/
hasAnimationEnded2(): boolean {
return this._animator.hasAnimationEnded();
}
hasAnimationEnded(): boolean {
return this._animator.hasAnimationEnded();
}
/**
* @deprecated Use `isAnimationPaused` instead.
*/
animationPaused(): boolean {
return this._animator.isAnimationPaused();
}
isAnimationPaused(): boolean {
return this._animator.isAnimationPaused();
}
pauseAnimation(): void {
this._animator.pauseAnimation();
}
/**
* @deprecated Use `resumeAnimation` instead.
*/
playAnimation(): void {
this._animator.resumeAnimation();
}
resumeAnimation(): void {
this._animator.resumeAnimation();
}
getAnimationSpeedScale(): float {
return this._animator.getAnimationSpeedScale();
}
setAnimationSpeedScale(ratio: float): void {
this._animator.setAnimationSpeedScale(ratio);
}
//Position :
/**
* Get the position on X axis on the scene of the given point.
* @param name The point name
* @return the position on X axis on the scene of the given point.
*/
getPointX(name: string): float {
const animationFrame = this._animator.getCurrentFrame();
if (name.length === 0 || animationFrame === null) {
return this.getX();
}
const pt = animationFrame.getPoint(name);
const pos = gdjs.staticArray(SpriteRuntimeObject.prototype.getPointX);
this._transformToGlobal(pt.x, pt.y, pos);
return pos[0];
}
/**
* Get the position on Y axis on the scene of the given point.
* @param name The point name
* @return the position on Y axis on the scene of the given point.
*/
getPointY(name: string): float {
const animationFrame = this._animator.getCurrentFrame();
if (name.length === 0 || animationFrame === null) {
return this.getY();
}
const pt = animationFrame.getPoint(name);
const pos = gdjs.staticArray(SpriteRuntimeObject.prototype.getPointY);
this._transformToGlobal(pt.x, pt.y, pos);
return pos[1];
}
/**
* Get the positions on X and Y axis on the scene of the given point.
* @param name The point name
* @return An array of the position on X and Y axis on the scene of the given point.
*/
getPointPosition(name: string): [x: float, y: float] {
const animationFrame = this._animator.getCurrentFrame();
if (name.length === 0 || animationFrame === null) {
return [this.getX(), this.getY()];
}
const pt = animationFrame.getPoint(name);
const pos = gdjs.staticArray(SpriteRuntimeObject.prototype.getPointX);
this._transformToGlobal(pt.x, pt.y, pos);
return [pos[0], pos[1]];
}
/**
* Return an array containing the coordinates of the point passed as parameter
* in world coordinates (as opposed to the object local coordinates).
*
* Beware: this._animationFrame and this._sprite must *not* be null!
*
* All transformations (flipping, scale, rotation) are supported.
*
* @param x The X position of the point, in object coordinates.
* @param y The Y position of the point, in object coordinates.
* @param result Array that will be updated with the result
* (x and y position of the point in global coordinates).
*/
private _transformToGlobal(x: float, y: float, result: float[]) {
const animationFrame = this._animator.getCurrentFrame() as SpriteAnimationFrame<
any
>;
let cx = animationFrame.center.x;
let cy = animationFrame.center.y;
//Flipping
if (this._flippedX) {
x = x + (cx - x) * 2;
}
if (this._flippedY) {
y = y + (cy - y) * 2;
}
//Scale
const absScaleX = Math.abs(this._scaleX);
const absScaleY = Math.abs(this._scaleY);
x *= absScaleX;
y *= absScaleY;
cx *= absScaleX;
cy *= absScaleY;
//Rotation
const angleInRadians = (this.angle / 180) * Math.PI;
const cosValue = Math.cos(
// Only compute cos and sin once (10% faster than doing it twice)
angleInRadians
);
const sinValue = Math.sin(angleInRadians);
const xToCenterXDelta = x - cx;
const yToCenterYDelta = y - cy;
x = cx + cosValue * xToCenterXDelta - sinValue * yToCenterYDelta;
y = cy + sinValue * xToCenterXDelta + cosValue * yToCenterYDelta;
result.length = 2;
result[0] = x + (this.x - animationFrame.origin.x * absScaleX);
result[1] = y + (this.y - animationFrame.origin.y * absScaleY);
}
/**
* Get the X position, on the scene, of the origin of the texture of the object.
* @return the X position, on the scene, of the origin of the texture of the object.
*/
getDrawableX(): float {
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame === null) {
return this.x;
}
const absScaleX = Math.abs(this._scaleX);
if (!this._flippedX) {
return this.x - animationFrame.origin.x * absScaleX;
} else {
return (
this.x +
(-animationFrame.origin.x -
this._renderer.getUnscaledWidth() +
2 * animationFrame.center.x) *
absScaleX
);
}
}
/**
* Get the Y position, on the scene, of the origin of the texture of the object.
* @return the Y position, on the scene, of the origin of the texture of the object.
*/
getDrawableY(): float {
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame === null) {
return this.y;
}
const absScaleY = Math.abs(this._scaleY);
if (!this._flippedY) {
return this.y - animationFrame.origin.y * absScaleY;
} else {
return (
this.y +
(-animationFrame.origin.y -
this._renderer.getUnscaledHeight() +
2 * animationFrame.center.y) *
absScaleY
);
}
}
/**
* Get the X position of the center of the object, relative to top-left of the texture of the object (`getDrawableX`).
* @return X position of the center of the object, relative to `getDrawableX()`.
*/
getCenterX(): float {
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame === null) {
return 0;
}
if (!this._flippedX) {
//Just need to multiply by the scale as it is the center.
return animationFrame.center.x * Math.abs(this._scaleX);
} else {
return (
(this._renderer.getUnscaledWidth() - animationFrame.center.x) *
Math.abs(this._scaleX)
);
}
}
/**
* Get the Y position of the center of the object, relative to top-left of the texture of the object (`getDrawableY`).
* @return Y position of the center of the object, relative to `getDrawableY()`.
*/
getCenterY(): float {
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame === null) {
return 0;
}
if (!this._flippedY) {
//Just need to multiply by the scale as it is the center.
return animationFrame.center.y * Math.abs(this._scaleY);
} else {
return (
(this._renderer.getUnscaledHeight() - animationFrame.center.y) *
Math.abs(this._scaleY)
);
}
}
/**
* Set the X position of the (origin of the) object.
* @param x The new X position.
*/
setX(x: float): void {
if (x === this.x) {
return;
}
this.x = x;
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame !== null) {
this.invalidateHitboxes();
this._renderer.updateX();
}
}
/**
* Set the Y position of the (origin of the) object.
* @param y The new Y position.
*/
setY(y: float): void {
if (y === this.y) {
return;
}
this.y = y;
const animationFrame = this._animator.getCurrentFrame();
if (animationFrame !== null) {
this.invalidateHitboxes();
this._renderer.updateY();
}
}
/**
* Set the angle of the object.
* @param angle The new angle, in degrees.
*/
setAngle(angle: float): void {
const actualValue = this._animator.setAngle(this.angle, angle);
if (actualValue !== null) {
this.angle = actualValue;
this.invalidateHitboxes();
this._renderer.updateAngle();
}
}
/**
* Get the angle of the object.
* @return The angle, in degrees.
*/
getAngle(): float {
return this._animator.getAngle(this.angle);
}
//Visibility and display :
setBlendMode(newMode): void {
if (this._blendMode === newMode) {
return;
}
this._blendMode = newMode;
this._renderer.update();
}
getBlendMode() {
return this._blendMode;
}
setOpacity(opacity: float): void {
if (opacity < 0) {
opacity = 0;
}
if (opacity > 255) {
opacity = 255;
}
this.opacity = opacity;
this._renderer.updateOpacity();
}
getOpacity(): float {
return this.opacity;
}
/**
* Hide (or show) the object
* @param enable true to hide the object, false to show it again.
*/
hide(enable: boolean): void {
if (enable === undefined) {
enable = true;
}
this.hidden = enable;
this._renderer.updateVisibility();
}
/**
* Change the tint of the 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 sprite object.
*
* @returns The color, in RGB format ("128;200;255").
*/
getColor(): string {
return this._renderer.getColor();
}
flipX(enable: boolean) {
if (enable !== this._flippedX) {
this._scaleX *= -1;
this._flippedX = enable;
this.invalidateHitboxes();
this._renderer.update();
}
}
flipY(enable: boolean) {
if (enable !== this._flippedY) {
this._scaleY *= -1;
this._flippedY = enable;
this.invalidateHitboxes();
this._renderer.update();
}
}
isFlippedX(): boolean {
return this._flippedX;
}
isFlippedY(): boolean {
return this._flippedY;
}
//Scale and size :
/**
* Get the width of the object.
*
* @return The width of the object, in pixels.
*/
getWidth(): float {
if (this._animationFrameDirty) {
this._updateAnimationFrame();
}
return this._renderer.getWidth();
}
/**
* Get the height of the object.
*
* @return The height of the object, in pixels.
*/
getHeight(): float {
if (this._animationFrameDirty) {
this._updateAnimationFrame();
}
return this._renderer.getHeight();
}
setWidth(newWidth: float): void {
if (this._animationFrameDirty) {
this._updateAnimationFrame();
}
const unscaledWidth = this._renderer.getUnscaledWidth();
if (unscaledWidth !== 0) {
this.setScaleX(newWidth / unscaledWidth);
}
}
setHeight(newHeight: float): void {
if (this._animationFrameDirty) {
this._updateAnimationFrame();
}
const unscaledHeight = this._renderer.getUnscaledHeight();
if (unscaledHeight !== 0) {
this.setScaleY(newHeight / unscaledHeight);
}
}
setSize(newWidth: float, newHeight: float): void {
this.setWidth(newWidth);
this.setHeight(newHeight);
}
/**
* Change the scale on X and Y axis of the object.
*
* @param newScale The new scale (must be greater than 0).
*/
setScale(newScale: float): void {
if (newScale < 0) {
newScale = 0;
}
if (
newScale === Math.abs(this._scaleX) &&
newScale === Math.abs(this._scaleY)
) {
return;
}
this._scaleX = newScale * (this._flippedX ? -1 : 1);
this._scaleY = newScale * (this._flippedY ? -1 : 1);
this._renderer.update();
this.invalidateHitboxes();
}
/**
* Change the scale on X axis of the object (changing its width).
*
* @param newScale The new scale (must be greater than 0).
*/
setScaleX(newScale: float): void {
if (newScale < 0) {
newScale = 0;
}
if (newScale === Math.abs(this._scaleX)) {
return;
}
this._scaleX = newScale * (this._flippedX ? -1 : 1);
this._renderer.update();
this.invalidateHitboxes();
}
/**
* Change the scale on Y axis of the object (changing its height).
*
* @param newScale The new scale (must be greater than 0).
*/
setScaleY(newScale: float): void {
if (newScale < 0) {
newScale = 0;
}
if (newScale === Math.abs(this._scaleY)) {
return;
}
this._scaleY = newScale * (this._flippedY ? -1 : 1);
this._renderer.update();
this.invalidateHitboxes();
}
/**
* Get the scale of the object (or the arithmetic mean of the X and Y scale in case they are different).
*
* @return the scale of the object (or the arithmetic mean of the X and Y scale in case they are different).
* @deprecated Use `getScale` instead.
*/
getScaleMean(): float {
return (Math.abs(this._scaleX) + Math.abs(this._scaleY)) / 2.0;
}
/**
* 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._scaleX);
const scaleY = Math.abs(this._scaleY);
return scaleX === scaleY ? scaleX : Math.sqrt(scaleX * scaleY);
}
/**
* Get the scale of the object on Y axis.
*
* @return the scale of the object on Y axis
*/
getScaleY(): float {
return Math.abs(this._scaleY);
}
/**
* Get the scale of the object on X axis.
*
* @return the scale of the object on X axis
*/
getScaleX(): float {
return Math.abs(this._scaleX);
}
//Other :
/**
* @param obj The target object
* @param scene The scene containing the object
* @deprecated
*/
turnTowardObject(obj: gdjs.RuntimeObject | null, scene: gdjs.RuntimeScene) {
if (obj === null) {
return;
}
this.rotateTowardPosition(
obj.getDrawableX() + obj.getCenterX(),
obj.getDrawableY() + obj.getCenterY(),
0,
scene
);
}
}
gdjs.registerObject(
'Sprite',
//Notify gdjs of the object existence.
gdjs.SpriteRuntimeObject
);
//Others initialization and internal state management :
SpriteRuntimeObject.supportsReinitialization = true;
}