-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtimeobject.js
1142 lines (999 loc) · 34.6 KB
/
runtimeobject.js
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
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* GDevelop JS Platform
* Copyright 2013-2015 Florian Rival (Florian.Rival@gmail.com). All rights reserved.
* This project is released under the MIT License.
*/
/**
* RuntimeObject represents an object being used on a RuntimeScene.
*
* The constructor can be called on an already existing RuntimeObject:
* In this case, the constructor will try to reuse as much already existing members
* as possible (recycling).
*
* However, you should not be calling the constructor on an already existing object
* which is not a RuntimeObject.
*
* @namespace gdjs
* @class RuntimeObject
* @constructor
* @param runtimeScene The RuntimeScene owning the object.
* @param objectData The data defining the object
*/
gdjs.RuntimeObject = function(runtimeScene, objectData)
{
this.name = objectData.name || "";
this._nameId = gdjs.RuntimeObject.getNameIdentifier(this.name);
this.type = objectData.type || "";
this.x = 0;
this.y = 0;
this.angle = 0;
this.zOrder = 0;
this.hidden = false;
this.layer = "";
this.livingOnScene = true;
this.id = runtimeScene.createNewUniqueId();
this._runtimeScene = runtimeScene; //This could/should be avoided.
//Hit boxes:
if ( this._defaultHitBoxes === undefined ) {
this._defaultHitBoxes = [];
this._defaultHitBoxes.push(gdjs.Polygon.createRectangle(0,0));
}
this.hitBoxes = this._defaultHitBoxes;
this.hitBoxesDirty = true;
if ( this.aabb === undefined )
this.aabb = { min:[0,0], max:[0,0] };
else {
this.aabb.min[0] = 0; this.aabb.min[1] = 0;
this.aabb.max[0] = 0; this.aabb.max[1] = 0;
}
//Variables:
if ( !this._variables )
this._variables = new gdjs.VariablesContainer(objectData ? objectData.variables : undefined);
else
gdjs.VariablesContainer.call(this._variables, objectData ? objectData.variables : undefined);
//Forces:
if ( this._forces === undefined )
this._forces = [];
else
this.clearForces();
//A force returned by getAverageForce method:
if (this._averageForce === undefined) this._averageForce = new gdjs.Force(0,0,false);
//Behaviors:
if (this._behaviors === undefined)
this._behaviors = []; //Contains the behaviors of the object
if (this._behaviorsTable === undefined)
this._behaviorsTable = new Hashtable(); //Also contains the behaviors: Used when a behavior is accessed by its name ( see getBehavior ).
else
this._behaviorsTable.clear();
var that = this;
var i = 0;
gdjs.iterateOverArray(objectData.behaviors, function(autoData) {
var Ctor = gdjs.getBehaviorConstructor(autoData.type);
//Try to reuse already existing behaviors.
if ( i < that._behaviors.length ) {
if ( that._behaviors[i] instanceof Ctor )
Ctor.call(that._behaviors[i], runtimeScene, autoData, that);
else
that._behaviors[i] = new Ctor(runtimeScene, autoData, that);
}
else that._behaviors.push(new Ctor(runtimeScene, autoData, that));
that._behaviorsTable.put(autoData.name, that._behaviors[i]);
i++;
});
this._behaviors.length = i;//Make sure to delete already existing behaviors which are not used anymore.
};
gdjs.RuntimeObject.forcesGarbage = []; //Global container for unused forces, avoiding recreating forces each tick.
//Common members functions related to the object and its runtimeScene :
/**
* Called each time the scene is rendered.
*
* @method updateTime
* @param elapsedTime {Number} The time elapsedTime since the last frame, in **seconds**.
*/
gdjs.RuntimeObject.prototype.updateTime = function(elapsedTime) {
//Nothing to do.
};
/**
* Called when the object is created from an initial instance at the startup of the scene.<br>
* Note this.common properties ( position, angle, z order... ) have already been setup.
*
* @method extraInitializationFromInitialInstance
* @param initialInstanceData The data of the initial instance.
*/
gdjs.RuntimeObject.prototype.extraInitializationFromInitialInstance = function(initialInstanceData) {
//Nothing to do.
};
/**
* Remove an object from a scene.<br>
* Extensions writers, do not change this method. Instead, redefine the onDeletedFromScene method.
* @method deleteFromScene
* @param runtimeScene The RuntimeScene owning the object.
*/
gdjs.RuntimeObject.prototype.deleteFromScene = function(runtimeScene) {
if ( this.livingOnScene ) {
runtimeScene.markObjectForDeletion(this);
this.livingOnScene = false;
}
};
/**
* Called when the object is removed from its scene.
*
* @method onDeletedFromScene
* @param runtimeScene The RuntimeScene owning the object.
*/
gdjs.RuntimeObject.prototype.onDeletedFromScene = function(runtimeScene) {
var theLayer = runtimeScene.getLayer(this.layer);
this.exposePIXIDisplayObject(function(displayObject) {
theLayer.removePIXIContainerChild(displayObject);
});
};
//Rendering:
/**
* Called with a callback function that should be called for
* each PIXI.DisplayObject used by the object
*
* @method exposePIXIDisplayObject
* @param cb The callback to be called with a PIXI.DisplayObject
*/
gdjs.RuntimeObject.prototype.exposePIXIDisplayObject = function(cb) {
};
//Common properties:
/**
* Get the name of the object.
* @method getName
* @return {String} The object's name.
*/
gdjs.RuntimeObject.prototype.getName = function() {
return this.name;
};
/**
* Get the name identifier of the object.
* @method getNameId
* @return {Number} The object's name identifier.
*/
gdjs.RuntimeObject.prototype.getNameId = function() {
return this._nameId;
};
/**
* Get the unique identifier of the object.<br>
* The identifier is set by the runtimeScene owning the object.<br>
* You can also use the id property (this._object.id) for increased efficiency instead of
* calling this method.
*
* @method getUniqueId
* @return {Number} The object identifier
*/
gdjs.RuntimeObject.prototype.getUniqueId = function() {
return this.id;
}
;
/**
* Set the position of the object.
*
* @method setPosition
* @param x {Number} The new X position
* @param y {Number} The new Y position
*/
gdjs.RuntimeObject.prototype.setPosition = function(x,y) {
this.setX(x);
this.setY(y);
};
/**
* Set the X position of the object.
*
* @method setX
* @param x {Number} The new X position
*/
gdjs.RuntimeObject.prototype.setX = function(x) {
if ( x === this.x ) return;
this.x = x;
this.hitBoxesDirty = true;
};
/**
* Get the X position of the object.
*
* @method getX
* @return {Number} The X position of the object
*/
gdjs.RuntimeObject.prototype.getX = function() {
return this.x;
};
/**
* Set the Y position of the object.
*
* @method setY
* @param y {Number} The new Y position
*/
gdjs.RuntimeObject.prototype.setY = function(y) {
if ( y === this.y ) return;
this.y = y;
this.hitBoxesDirty = true;
};
/**
* Get the Y position of the object.
*
* @method getY
* @return {Number} The Y position of the object
*/
gdjs.RuntimeObject.prototype.getY = function() {
return this.y;
};
/**
* Get the X position of the rendered object.<br>
* For most objects, this will returns the same value as getX(). But if the object
* has an origin this.is not the same as the point (0,0) of the object displayed,
* getDrawableX will differs.
*
* @method getDrawableX
* @return {Number} The X position of the rendered object.
*/
gdjs.RuntimeObject.prototype.getDrawableX = function() {
return this.getX();
};
/**
* Get the Y position of the rendered object.<br>
* For most objects, this will returns the same value as getY(). But if the object
* has an origin this.is not the same as the point (0,0) of the object displayed,
* getDrawableY will differs.
*
* @method getDrawableY
* @return {Number} The Y position of the rendered object.
*/
gdjs.RuntimeObject.prototype.getDrawableY = function() {
return this.getY();
};
gdjs.RuntimeObject.prototype.rotateTowardPosition = function(x, y, speed, scene) {
this.rotateTowardAngle(Math.atan2(y - (this.getDrawableY() + this.getCenterY()),
x - (this.getDrawableX() + this.getCenterX()))*180/Math.PI, speed, scene);
};
gdjs.RuntimeObject.prototype.rotateTowardAngle = function(angle, speed, runtimeScene) {
if (speed === 0) {
this.setAngle(angle);
return;
}
var angularDiff = gdjs.evtTools.common.angleDifference(this.getAngle(), angle);
var diffWasPositive = angularDiff >= 0;
var newAngle = this.getAngle() + (diffWasPositive ? -1.0 : 1.0) * speed * runtimeScene.getElapsedTime()/1000;
if (gdjs.evtTools.common.angleDifference(newAngle, angle) > 0 ^ diffWasPositive)
newAngle = angle;
this.setAngle(newAngle);
if (this.getAngle() != newAngle) //Objects like sprite in 8 directions does not handle small increments...
this.setAngle(angle); //...so force them to be in the path angle anyway.
};
gdjs.RuntimeObject.prototype.rotate = function(speed, runtimeScene) {
this.setAngle(this.getAngle()+speed*runtimeScene.getElapsedTime()/1000);
};
/**
* Set the angle of the object.
*
* @method setAngle
* @param angle {Number} The new angle of the object
*/
gdjs.RuntimeObject.prototype.setAngle = function(angle) {
if ( this.angle === angle ) return;
this.angle = angle;
this.hitBoxesDirty = true;
};
/**
* Get the rotation of the object.
*
* @method getAngle
* @return {Number} The rotation of the object
*/
gdjs.RuntimeObject.prototype.getAngle = function() {
return this.angle;
};
/**
* Set the layer of the object.
*
* @method setLayer
* @return {String} The new layer of the object
*/
gdjs.RuntimeObject.prototype.setLayer = function(layer) {
if (layer === this.layer) return;
this.layer = layer;
var theLayer = this._runtimeScene.getLayer(this.layer);
var that = this;
this.exposePIXIDisplayObject(function (displayObject) {
theLayer.removePIXIContainerChild(displayObject);
theLayer.addChildToPIXIContainer(displayObject, that.zOrder);
});
};
/**
* Get the layer of the object.
*
* @method getLayer
* @return {String} The layer of the object
*/
gdjs.RuntimeObject.prototype.getLayer = function() {
return this.layer;
};
/**
* Return true if the object is on the specified layer
*
* @method isOnLayer
* @param layer {String} The layer to be tested.
* @return {Boolean} true if the object is on the specified layer
*/
gdjs.RuntimeObject.prototype.isOnLayer = function(layer) {
return this.layer === layer;
};
/**
* Set the Z order of the object.
*
* @method setZOrder
* @param z {Number} The new Z order position of the object
*/
gdjs.RuntimeObject.prototype.setZOrder = function(z) {
if ( z === this.zOrder ) return;
this.zOrder = z;
var theLayer = this._runtimeScene.getLayer(this.layer);
this.exposePIXIDisplayObject(function(displayObject) {
theLayer.changePIXIContainerChildZOrder(displayObject, z);
});
};
/**
* Get the Z order of the object.
*
* @method getZOrder
* @return {Number} The Z order of the object
*/
gdjs.RuntimeObject.prototype.getZOrder = function() {
return this.zOrder;
};
/**
* Get the container of the object variables
* @method getVariables
* @return {variablesContainer} The variables of the object
*/
gdjs.RuntimeObject.prototype.getVariables = function() {
return this._variables;
};
/**
* Get the value of a variable considered as a number. Equivalent of variable.getAsNumber()
* @method getVariableNumber
* @param variable The variable to be accessed
* @return The value of the specified variable
* @static
*/
gdjs.RuntimeObject.getVariableNumber = function(variable) {
return variable.getAsNumber();
};
gdjs.RuntimeObject.prototype.getVariableNumber = gdjs.RuntimeObject.getVariableNumber;
/**
* Get the value of a variable considered as a string. Equivalent of variable.getAsString()
* @method getVariableString
* @param variable The variable to be accessed
* @return The string of the specified variable
* @static
*/
gdjs.RuntimeObject.getVariableString = function(variable) {
return variable.getAsString();
};
gdjs.RuntimeObject.prototype.getVariableString = gdjs.RuntimeObject.getVariableString;
/**
* Shortcut to set the value of a variable considered as a number
* @method setVariableNumber
* @param variable The variable to be changed
* @param newValue {Number} The value to be set
*/
gdjs.RuntimeObject.setVariableNumber = function(variable, newValue) {
variable.setNumber(newValue);
};
gdjs.RuntimeObject.prototype.setVariableNumber = gdjs.RuntimeObject.setVariableNumber;
/**
* Shortcut to set the value of a variable considered as a string
* @method setVariableNumber
* @param variable The variable to be changed
* @param newValue {String} The value to be set
*/
gdjs.RuntimeObject.setVariableString = function(variable, newValue) {
variable.setString(newValue);
};
gdjs.RuntimeObject.prototype.setVariableString = gdjs.RuntimeObject.setVariableString;
/**
* @method variableChildExists
* @static
* @private
* @param variable The variable to be tested
* @param childName {String} The name of the child
*/
gdjs.RuntimeObject.variableChildExists = function(variable, childName) {
return variable.hasChild(childName);
};
gdjs.RuntimeObject.prototype.variableChildExists = gdjs.RuntimeObject.variableChildExists;
/**
* @method variableRemoveChild
* @static
* @private
* @param variable The variable to be changed
* @param childName {String} The name of the child
*/
gdjs.RuntimeObject.variableRemoveChild = function(variable, childName) {
return variable.removeChild(childName);
};
gdjs.RuntimeObject.prototype.variableRemoveChild = gdjs.RuntimeObject.variableRemoveChild;
/**
* Shortcut to test if a variable exists for the object.
* @method hasVariable
* @param name {String} The variable to be tested
*/
gdjs.RuntimeObject.prototype.hasVariable = function(name) {
return this._variables.has(name);
};
/**
* Hide or show the object
* @method hide
* @param enable {Boolean} Set it to true to hide the object, false to show it.
*/
gdjs.RuntimeObject.prototype.hide = function(enable) {
if (enable === undefined) enable = true;
this.hidden = enable;
};
/**
* Return true if the object is not hidden.
* @method isVisible
* @return {Boolean} true if the object is not hidden.
*/
gdjs.RuntimeObject.prototype.isVisible = function() {
return !this.hidden;
};
/**
* Return true if the object is hidden.
* @method isHidden
* @return {Boolean} true if the object is hidden.
*/
gdjs.RuntimeObject.prototype.isHidden = function() {
return this.hidden;
};
/**
* Return the width of the object
* @method getWidth
* @return {Number} The width of the object
*/
gdjs.RuntimeObject.prototype.getWidth = function() {
return 0;
};
/**
* Return the width of the object
* @method getHeight
* @return {Number} The height of the object
*/
gdjs.RuntimeObject.prototype.getHeight = function() {
return 0;
};
/**
* Return the X position of the object center, relative to the object position.
* @method getCenterX
*/
gdjs.RuntimeObject.prototype.getCenterX = function() {
return this.getWidth() / 2;
};
/**
* Return the Y position of the object center, relative to the object position.
* @method getCenterY
*/
gdjs.RuntimeObject.prototype.getCenterY = function() {
return this.getHeight() / 2;
};
//Forces :
/**
* Get a force from the garbage, or create a new force is garbage is empty.<br>
* To be used each time a force is created so as to avoid temporaries objects.
*
* @method getRecycledForce
* @private
* @param x {Number} The x coordinates of the force
* @param y {Number} The y coordinates of the force
* @param clearing {Number} Set the force clearing
*/
gdjs.RuntimeObject.prototype._getRecycledForce = function(x, y, clearing) {
if ( gdjs.RuntimeObject.forcesGarbage.length === 0 )
return new gdjs.Force(x, y, clearing);
else {
var recycledForce = gdjs.RuntimeObject.forcesGarbage.pop();
recycledForce.setX(x);
recycledForce.setY(y);
recycledForce.setClearing(clearing);
return recycledForce;
}
};
/**
* Add a force to the object to make it moving.
* @method addForce
* @param x {Number} The x coordinates of the force
* @param y {Number} The y coordinates of the force
* @param clearing {Number} Set the force clearing
*/
gdjs.RuntimeObject.prototype.addForce = function(x,y, clearing) {
this._forces.push(this._getRecycledForce(x, y, clearing));
};
/**
* Add a force using polar coordinates.
* @method addPolarForce
* @param angle {Number} The angle of the force
* @param len {Number} The length of the force
* @param clearing {Number} Set the force clearing
*/
gdjs.RuntimeObject.prototype.addPolarForce = function(angle, len, clearing) {
var forceX = Math.cos(angle/180*3.14159)*len;
var forceY = Math.sin(angle/180*3.14159)*len;
this._forces.push(this._getRecycledForce(forceX, forceY, clearing));
};
/**
* Add a force oriented toward a position
* @method addForceTowardPosition
* @param x {Number} The target x position
* @param y {Number} The target y position
* @param len {Number} The force length, in pixels.
* @param clearing {Number} Set the force clearing
*/
gdjs.RuntimeObject.prototype.addForceTowardPosition = function(x,y, len, clearing) {
var angle = Math.atan2(y - (this.getDrawableY()+this.getCenterY()),
x - (this.getDrawableX()+this.getCenterX()));
var forceX = Math.cos(angle)*len;
var forceY = Math.sin(angle)*len;
this._forces.push(this._getRecycledForce(forceX, forceY, clearing));
};
/**
* Add a force oriented toward another object.<br>
* (Shortcut for addForceTowardPosition)
* @method addForceTowardObject
* @param obj The target object
* @param len {Number} The force length, in pixels.
* @param clearing {Number} Set the force clearing
*/
gdjs.RuntimeObject.prototype.addForceTowardObject = function(obj, len, clearing) {
if ( obj == null ) return;
this.addForceTowardPosition(obj.getDrawableX() + obj.getCenterX(),
obj.getDrawableY() + obj.getCenterY(),
len, clearing);
};
/**
* Deletes all forces applied on the object
* @method clearForces
*/
gdjs.RuntimeObject.prototype.clearForces = function() {
gdjs.RuntimeObject.forcesGarbage.push.apply(gdjs.RuntimeObject.forcesGarbage, this._forces);
this._forces.length = 0;
};
/**
* Return true if no forces are applied on the object.
* @method hasNoForces
* @return {Boolean} true if no forces are applied on the object.
*/
gdjs.RuntimeObject.prototype.hasNoForces = function() {
return this._forces.length === 0;
};
/**
* Called once a step by runtimeScene to update forces magnitudes and
* remove null ones.
* @method updateForces
*/
gdjs.RuntimeObject.prototype.updateForces = function(elapsedTime) {
for(var i = 0;i<this._forces.length;) {
if(this._forces[i].getClearing() === 0 || this._forces[i].getLength() <= 0.001)
{
gdjs.RuntimeObject.forcesGarbage.push(this._forces[i]);
this._forces.remove(i);
}
else
{
this._forces[i].setLength(this._forces[i].getLength() - this._forces[i].getLength() * ( 1 - this._forces[i].getClearing() ) * elapsedTime);
++i;
}
}
};
/**
* Return a force which is the sum of all forces applied on the object.
*
* @method getAverageForce
* @return {force} A force object.
*/
gdjs.RuntimeObject.prototype.getAverageForce = function() {
var averageX = 0;
var averageY = 0;
for(var i = 0, len = this._forces.length;i<len;++i) {
averageX += this._forces[i].getX();
averageY += this._forces[i].getY();
}
this._averageForce.setX(averageX);
this._averageForce.setY(averageY);
return this._averageForce;
};
/**
* Return true if the average angle of the forces applied on the object
* is in a given range.
*
* @method averageForceAngleIs
* @param angle {Number} The angle to be tested.
* @param toleranceInDegrees {Number} The length of the range :
* @return {Boolean} true if the difference between the average angle of the forces
* and the angle parameter is inferior to toleranceInDegrees parameter.
*/
gdjs.RuntimeObject.prototype.averageForceAngleIs = function(angle, toleranceInDegrees) {
var averageAngle = this.getAverageForce().getAngle();
if ( averageAngle < 0 ) averageAngle += 360;
return Math.abs(angle-averageAngle) < toleranceInDegrees/2;
};
//Hit boxes and collision :
/**
* Get the hit boxes for the object.<br>
* The default implementation returns a basic bouding box based on the result of getWidth and
* getHeight. You should probably redefine updateHitBoxes instead of this function.
*
* @method getHitBoxes
* @return {Array} An array composed of polygon.
*/
gdjs.RuntimeObject.prototype.getHitBoxes = function() {
//Avoid a naive implementation requiring to recreate temporaries each time
//the function is called:
//(var rectangle = gdjs.Polygon.createRectangle(this.getWidth(), this.getHeight());
//...)
if ( this.hitBoxesDirty ) {
this.updateHitBoxes();
this.updateAABB();
this.hitBoxesDirty = false;
}
return this.hitBoxes;
};
/**
* Update the hit boxes for the object.<br>
* The default implementation set a basic bouding box based on the result of getWidth and
* getHeight.
*
* You should not call this function by yourself, it is called when necessary by getHitBoxes method.
* However, you can redefine it if your object need custom hit boxes.
*
* @method updateHitBoxes
*/
gdjs.RuntimeObject.prototype.updateHitBoxes = function() {
//Ensure we're using the default hitbox (a single rectangle)
this.hitBoxes = this._defaultHitBoxes;
var width = this.getWidth();
var height = this.getHeight();
this.hitBoxes[0].vertices[0][0] =-width/2.0;
this.hitBoxes[0].vertices[0][1] =-height/2.0;
this.hitBoxes[0].vertices[1][0] =+width/2.0;
this.hitBoxes[0].vertices[1][1] =-height/2.0;
this.hitBoxes[0].vertices[2][0] =+width/2.0;
this.hitBoxes[0].vertices[2][1] =+height/2.0;
this.hitBoxes[0].vertices[3][0] =-width/2.0;
this.hitBoxes[0].vertices[3][1] =+height/2.0;
this.hitBoxes[0].rotate(this.getAngle()/180*3.14159);
this.hitBoxes[0].move(this.getDrawableX()+this.getCenterX(), this.getDrawableY()+this.getCenterY());
};
//Experimental
gdjs.RuntimeObject.prototype.getAABB = function() {
if ( this.hitBoxesDirty ) {
this.updateHitBoxes();
this.updateAABB();
this.hitBoxesDirty = false;
}
return this.aabb;
};
gdjs.RuntimeObject.prototype.updateAABB = function() {
this.aabb.min[0] = this.getDrawableX();
this.aabb.min[1] = this.getDrawableY();
this.aabb.max[0] = this.getDrawableX()+this.getWidth();
this.aabb.max[1] = this.getDrawableY()+this.getHeight();
};
//Behaviors:
/**
* Call each behavior stepPreEvents method.
* @method stepBehaviorsPreEvents
*/
gdjs.RuntimeObject.prototype.stepBehaviorsPreEvents = function(runtimeScene) {
for(var i = 0, len = this._behaviors.length;i<len;++i) {
this._behaviors[i].stepPreEvents(runtimeScene);
}
};
/**
* Call each behavior stepPostEvents method.
* @method stepBehaviorsPostEvents
*/
gdjs.RuntimeObject.prototype.stepBehaviorsPostEvents = function(runtimeScene) {
for(var i = 0, len = this._behaviors.length;i<len;++i) {
this._behaviors[i].stepPostEvents(runtimeScene);
}
};
/**
* Get a behavior from its name.<br>
* Be careful, the behavior must exists, no check is made on the name.
* @method getBehavior
* @param name {String} The behavior name.
*/
gdjs.RuntimeObject.prototype.getBehavior = function(name) {
return this._behaviorsTable.get(name);
};
/**
* Check if a behavior is used by the object.
*
* @method hasBehavior
* @param name {String} The behavior name.
*/
gdjs.RuntimeObject.prototype.hasBehavior = function(name) {
return this._behaviorsTable.containsKey(name);
};
/**
* De/activate a behavior of the object.
*
* @method activateBehavior
* @param name {String} The behavior name.
* @param enable {Boolean} true to activate the behavior
*/
gdjs.RuntimeObject.prototype.activateBehavior = function(name, enable) {
if ( this._behaviorsTable.containsKey(name) ) {
this._behaviorsTable.get(name).activate(enable);
}
};
/**
* Check if a behavior is activated
*
* @method behaviorActivated
* @param name {String} The behavior name.
* @return true if the behavior is activated.
*/
gdjs.RuntimeObject.prototype.behaviorActivated = function(name) {
if ( this._behaviorsTable.containsKey(name) ) {
this._behaviorsTable.get(name).activated();
}
return false;
};
//Other :
/**
* Separate the object from others objects, using their hitboxes.
* @method separateFromObjects
* @param objectsLists Tables of objects
* @return true if the object was moved
*/
gdjs.RuntimeObject.prototype.separateFromObjects = function(objectsLists) {
//Prepare the list of objects to iterate over.
var objects = [];
var lists = objectsLists.values();
for(var i = 0, len = lists.length;i<len;++i) {
objects.push.apply(objects, lists[i]);
}
var moved = false;
var xMove = 0; var yMove = 0;
var hitBoxes = this.getHitBoxes();
//Check if their is a collision with each object
for(var i = 0, len = objects.length;i<len;++i) {
if ( objects[i].id != this.id ) {
var otherHitBoxes = objects[i].getHitBoxes();
for(var k = 0, lenk = hitBoxes.length;k<lenk;++k) {
for(var l = 0, lenl = otherHitBoxes.length;l<lenl;++l) {
var result = gdjs.Polygon.collisionTest(hitBoxes[k], otherHitBoxes[l]);
if ( result.collision ) {
xMove += result.move_axis[0];
yMove += result.move_axis[1];
moved = true;
}
}
}
}
}
//Move according to the results returned by the collision algorithm.
this.setPosition(this.getX()+xMove, this.getY()+yMove);
return moved;
};
/**
* Get the distance, in pixels, to another object.
* @method getDistanceToObject
* @param otherObject The other object
*/
gdjs.RuntimeObject.prototype.getDistanceToObject = function(otherObject) {
return Math.sqrt(this.getSqDistanceToObject(otherObject));
};
/**
* Get the squared distance, in pixels, to another object.
* @method getSqDistanceToObject
* @param otherObject The other object
*/
gdjs.RuntimeObject.prototype.getSqDistanceToObject = function(otherObject) {
if ( otherObject === null ) return 0;
var x = this.getX()+this.getCenterX() - (otherObject.getX()+otherObject.getCenterX());
var y = this.getY()+this.getCenterY() - (otherObject.getY()+otherObject.getCenterY());
return x*x+y*y;
};
/**
* Get the squared distance, in pixels, to a position.
* @method getSqDistanceTo
* @param pointX {Number} X position
* @param pointY {Number} Y position
*/
gdjs.RuntimeObject.prototype.getSqDistanceTo = function(pointX, pointY) {
var x = this.getX()+this.getCenterX() - pointX;
var y = this.getY()+this.getCenterY() - pointY;
return x*x+y*y;
};
/**
* Put the object around a position, with a specific distance and angle.<br>
* The distance is computed between the position and the center of the object.
*
* @method putAround
* @param x {Number} The x position of the target
* @param y {Number} The y position of the target
* @param distance {Number} The distance between the object and the target
* @param angleInDegrees {Number} The angle between the object and the target, in degrees.
*/
gdjs.RuntimeObject.prototype.putAround = function(x,y,distance,angleInDegrees) {
var angle = angleInDegrees/180*3.14159;
this.setX( x + Math.cos(angle)*distance - this.getCenterX() );
this.setY( y + Math.sin(angle)*distance - this.getCenterY() );
};
/**
* Put the object around another object, with a specific distance and angle.<br>
* The distance is computed between the centers of the objects.
*
* @method putAround
* @param obj The target object
* @param distance {Number} The distance between the object and the target
* @param angleInDegrees {Number} The angle between the object and the target, in degrees.
*/
gdjs.RuntimeObject.prototype.putAroundObject = function(obj,distance,angleInDegrees) {
this.putAround(obj.getX()+obj.getCenterX(), obj.getY()+obj.getCenterY(),
distance, angleInDegrees);
};
/**
* @method separateObjectsWithoutForces
* @deprecated
* @param objectsLists Tables of objects
*/
gdjs.RuntimeObject.prototype.separateObjectsWithoutForces = function(objectsLists) {
//Prepare the list of objects to iterate over.
var objects = [];
var lists = objectsLists.values();
for(var i = 0, len = lists.length;i<len;++i) {
objects.push.apply(objects, lists[i]);
}
for(var i = 0, len = objects.length;i<len;++i) {
if ( objects[i].id != this.id ) {
if ( this.getDrawableX() < objects[i].getDrawableX() ){
this.setX( objects[i].getDrawableX() - this.getWidth() );
}
else if ( this.getDrawableX()+this.getWidth() > objects[i].getDrawableX()+objects[i].getWidth() ){
this.setX( objects[i].getDrawableX()+objects[i].getWidth() );
}
if ( this.getDrawableY() < objects[i].getDrawableY() ){
this.setY( objects[i].getDrawableY() - this.getHeight() );
}
else if ( this.getDrawableY()+this.getHeight() > objects[i].getDrawableY()+objects[i].getHeight() ){
this.setY( objects[i].getDrawableY()+objects[i].getHeight() );
}
}
}
};
/**
* @method SeparateObjectsWithForces
* @deprecated
* @param objectsLists Tables of objects
*/
gdjs.RuntimeObject.prototype.separateObjectsWithForces = function(objectsLists, len) {
if ( len == undefined ) len = 10;
//Prepare the list of objects to iterate over.
var objects = [];
var lists = objectsLists.values();
for(var i = 0, len = lists.length;i<len;++i) {
objects.push.apply(objects, lists[i]);