@@ -10,7 +10,6 @@ Object.assign(pc, function () {
10
10
* @param {pc.Entity } entity - The Entity that this Component is attached to.
11
11
* @property {pc.ScriptType[] } scripts An array of all script instances attached to an entity. This Array shall not be modified by developer.
12
12
*/
13
-
14
13
var ScriptComponent = function ScriptComponent ( system , entity ) {
15
14
pc . Component . call ( this , system , entity ) ;
16
15
@@ -460,41 +459,63 @@ Object.assign(pc, function () {
460
459
}
461
460
} ,
462
461
462
+ /* eslint-disable jsdoc/no-undefined-types */
463
463
/**
464
464
* @function
465
465
* @name pc.ScriptComponent#has
466
- * @description Detect if script is attached to an entity using name of { @link pc.ScriptType} .
467
- * @param {string } name - The name of the Script Type .
466
+ * @description Detect if script is attached to an entity.
467
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of { @link pc.ScriptType} .
468
468
* @returns {boolean } If script is attached to an entity.
469
469
* @example
470
470
* if (entity.script.has('playerController')) {
471
471
* // entity has script
472
472
* }
473
473
*/
474
- has : function ( name ) {
475
- return ! ! this . _scriptsIndex [ name ] ;
474
+ /* eslint-enable jsdoc/no-undefined-types */
475
+ has : function ( nameOrType ) {
476
+ if ( typeof nameOrType === 'string' ) {
477
+ return ! ! this . _scriptsIndex [ nameOrType ] ;
478
+ }
479
+
480
+ if ( ! nameOrType ) return false ;
481
+ var scriptType = nameOrType ;
482
+ var scriptName = scriptType . __name ;
483
+ var scriptData = this . _scriptsIndex [ scriptName ] ;
484
+ var scriptInstance = scriptData && scriptData . instance ;
485
+ return scriptInstance instanceof scriptType ; // will return false if scriptInstance undefined
476
486
} ,
477
487
488
+ /* eslint-disable jsdoc/no-undefined-types */
478
489
/**
479
490
* @function
480
491
* @name pc.ScriptComponent#get
481
- * @description Get a script instance (if attached) using name of { @link pc.ScriptType} .
482
- * @param {string } name - The name of the Script Type .
492
+ * @description Get a script instance (if attached).
493
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of { @link pc.ScriptType} .
483
494
* @returns {pc.ScriptType|null } If script is attached, the instance is returned. Otherwise null is returned.
484
495
* @example
485
496
* var controller = entity.script.get('playerController');
486
497
*/
487
- get : function ( name ) {
488
- var index = this . _scriptsIndex [ name ] ;
489
- return ( index && index . instance ) || null ;
498
+ /* eslint-enable jsdoc/no-undefined-types */
499
+ get : function ( nameOrType ) {
500
+ if ( typeof nameOrType === 'string' ) {
501
+ var data = this . _scriptsIndex [ nameOrType ] ;
502
+ return data ? data . instance : null ;
503
+ }
504
+
505
+ if ( ! nameOrType ) return null ;
506
+ var scriptType = nameOrType ;
507
+ var scriptName = scriptType . __name ;
508
+ var scriptData = this . _scriptsIndex [ scriptName ] ;
509
+ var scriptInstance = scriptData && scriptData . instance ;
510
+ return scriptInstance instanceof scriptType ? scriptInstance : null ;
490
511
} ,
491
512
492
513
/* eslint-disable jsdoc/no-undefined-types */
493
514
/**
494
515
* @function
495
516
* @name pc.ScriptComponent#create
496
- * @description Create a script instance using name of a { @link pc.ScriptType} and attach to an entity script component.
497
- * @param {string|Class<pc.ScriptType> } name - The name of the Script Type ( or alternatively the {@link pc.ScriptType} to instantiate) .
517
+ * @description Create a script instance and attach to an entity script component.
518
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of {@link pc.ScriptType}.
498
519
* @param {object } [args] - Object with arguments for a script.
499
520
* @param {boolean } [args.enabled] - If script instance is enabled after creation. Defaults to true.
500
521
* @param {object } [args.attributes] - Object with values for attributes (if any), where key is name of an attribute.
@@ -511,12 +532,12 @@ Object.assign(pc, function () {
511
532
* });
512
533
*/
513
534
/* eslint-enable jsdoc/no-undefined-types */
514
- create : function ( name , args ) {
535
+ create : function ( nameOrType , args ) {
515
536
var self = this ;
516
537
args = args || { } ;
517
538
518
- var scriptType = name ;
519
- var scriptName = name ;
539
+ var scriptType = nameOrType ;
540
+ var scriptName = nameOrType ;
520
541
521
542
// shorthand using script name
522
543
if ( typeof scriptType === 'string' ) {
@@ -526,7 +547,7 @@ Object.assign(pc, function () {
526
547
}
527
548
528
549
if ( scriptType ) {
529
- if ( ! this . _scriptsIndex [ scriptType . __name ] || ! this . _scriptsIndex [ scriptType . __name ] . instance ) {
550
+ if ( ! this . _scriptsIndex [ scriptName ] || ! this . _scriptsIndex [ scriptName ] . instance ) {
530
551
// create script instance
531
552
var scriptInstance = new scriptType ( {
532
553
app : this . system . app ,
@@ -542,22 +563,22 @@ Object.assign(pc, function () {
542
563
543
564
this . _insertScriptInstance ( scriptInstance , ind , len ) ;
544
565
545
- this . _scriptsIndex [ scriptType . __name ] = {
566
+ this . _scriptsIndex [ scriptName ] = {
546
567
instance : scriptInstance ,
547
568
onSwap : function ( ) {
548
- self . swap ( scriptType . __name ) ;
569
+ self . swap ( scriptName ) ;
549
570
}
550
571
} ;
551
572
552
- this [ scriptType . __name ] = scriptInstance ;
573
+ this [ scriptName ] = scriptInstance ;
553
574
554
575
if ( ! args . preloading )
555
576
scriptInstance . __initializeAttributes ( ) ;
556
577
557
- this . fire ( 'create' , scriptType . __name , scriptInstance ) ;
558
- this . fire ( 'create:' + scriptType . __name , scriptInstance ) ;
578
+ this . fire ( 'create' , scriptName , scriptInstance ) ;
579
+ this . fire ( 'create:' + scriptName , scriptInstance ) ;
559
580
560
- this . system . app . scripts . on ( 'swap:' + scriptType . __name , this . _scriptsIndex [ scriptType . __name ] . onSwap ) ;
581
+ this . system . app . scripts . on ( 'swap:' + scriptName , this . _scriptsIndex [ scriptName ] . onSwap ) ;
561
582
562
583
if ( ! args . preloading ) {
563
584
@@ -592,45 +613,48 @@ Object.assign(pc, function () {
592
613
return null ;
593
614
} ,
594
615
616
+ /* eslint-disable jsdoc/no-undefined-types */
595
617
/**
596
618
* @function
597
619
* @name pc.ScriptComponent#destroy
598
620
* @description Destroy the script instance that is attached to an entity.
599
- * @param {string } name - The name of the Script Type .
621
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of { @link pc.ScriptType} .
600
622
* @returns {boolean } If it was successfully destroyed.
601
623
* @example
602
624
* entity.script.destroy('playerController');
603
625
*/
604
- destroy : function ( name ) {
605
- var scriptName = name ;
606
- var scriptType = name ;
626
+ /* eslint-enable jsdoc/no-undefined-types */
627
+ destroy : function ( nameOrType ) {
628
+ var scriptName = nameOrType ;
629
+ var scriptType = nameOrType ;
607
630
608
631
// shorthand using script name
609
632
if ( typeof scriptType === 'string' ) {
610
633
scriptType = this . system . app . scripts . get ( scriptType ) ;
611
- if ( scriptType )
612
- scriptName = scriptType . __name ;
634
+ } else if ( scriptType ) {
635
+ scriptName = scriptType . __name ;
613
636
}
614
637
615
638
var scriptData = this . _scriptsIndex [ scriptName ] ;
616
639
delete this . _scriptsIndex [ scriptName ] ;
617
640
if ( ! scriptData ) return false ;
618
641
619
- if ( scriptData . instance && ! scriptData . instance . _destroyed ) {
620
- scriptData . instance . enabled = false ;
621
- scriptData . instance . _destroyed = true ;
642
+ var scriptInstance = scriptData . instance ;
643
+ if ( scriptInstance && ! scriptInstance . _destroyed ) {
644
+ scriptInstance . enabled = false ;
645
+ scriptInstance . _destroyed = true ;
622
646
623
647
// if we are not currently looping through our scripts
624
648
// then it's safe to remove the script
625
649
if ( ! this . _isLoopingThroughScripts ) {
626
- var ind = this . _removeScriptInstance ( scriptData . instance ) ;
650
+ var ind = this . _removeScriptInstance ( scriptInstance ) ;
627
651
if ( ind >= 0 ) {
628
652
this . _resetExecutionOrder ( ind , this . _scripts . length ) ;
629
653
}
630
654
} else {
631
655
// otherwise push the script in _destroyedScripts and
632
656
// remove it from _scripts when the loop is over
633
- this . _destroyedScripts . push ( scriptData . instance ) ;
657
+ this . _destroyedScripts . push ( scriptInstance ) ;
634
658
}
635
659
}
636
660
@@ -639,23 +663,37 @@ Object.assign(pc, function () {
639
663
640
664
delete this [ scriptName ] ;
641
665
642
- this . fire ( 'destroy' , scriptName , scriptData . instance || null ) ;
643
- this . fire ( 'destroy:' + scriptName , scriptData . instance || null ) ;
666
+ this . fire ( 'destroy' , scriptName , scriptInstance || null ) ;
667
+ this . fire ( 'destroy:' + scriptName , scriptInstance || null ) ;
644
668
645
- if ( scriptData . instance )
646
- scriptData . instance . fire ( 'destroy' ) ;
669
+ if ( scriptInstance )
670
+ scriptInstance . fire ( 'destroy' ) ;
647
671
648
672
return true ;
649
673
} ,
650
674
651
- swap : function ( script ) {
652
- var scriptType = script ;
675
+ /* eslint-disable jsdoc/no-undefined-types */
676
+ /**
677
+ * @private
678
+ * @function
679
+ * @name pc.ScriptComponent#swap
680
+ * @description Swap the script instance.
681
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of {@link pc.ScriptType}.
682
+ * @returns {boolean } If it was successfully swapped.
683
+ */
684
+ /* eslint-enable jsdoc/no-undefined-types */
685
+ swap : function ( nameOrType ) {
686
+ var scriptName = nameOrType ;
687
+ var scriptType = nameOrType ;
653
688
654
689
// shorthand using script name
655
- if ( typeof scriptType === 'string' )
690
+ if ( typeof scriptType === 'string' ) {
656
691
scriptType = this . system . app . scripts . get ( scriptType ) ;
692
+ } else if ( scriptType ) {
693
+ scriptName = scriptType . __name ;
694
+ }
657
695
658
- var old = this . _scriptsIndex [ scriptType . __name ] ;
696
+ var old = this . _scriptsIndex [ scriptName ] ;
659
697
if ( ! old || ! old . instance ) return false ;
660
698
661
699
var scriptInstanceOld = old . instance ;
@@ -675,8 +713,8 @@ Object.assign(pc, function () {
675
713
676
714
// add to component
677
715
this . _scripts [ ind ] = scriptInstance ;
678
- this . _scriptsIndex [ scriptType . __name ] . instance = scriptInstance ;
679
- this [ scriptType . __name ] = scriptInstance ;
716
+ this . _scriptsIndex [ scriptName ] . instance = scriptInstance ;
717
+ this [ scriptName ] = scriptInstance ;
680
718
681
719
// set execution order and make sure we update
682
720
// our update and postUpdate lists
@@ -697,8 +735,8 @@ Object.assign(pc, function () {
697
735
698
736
this . _scriptMethod ( scriptInstance , ScriptComponent . scriptMethods . swap , scriptInstanceOld ) ;
699
737
700
- this . fire ( 'swap' , scriptType . __name , scriptInstance ) ;
701
- this . fire ( 'swap:' + scriptType . __name , scriptInstance ) ;
738
+ this . fire ( 'swap' , scriptName , scriptInstance ) ;
739
+ this . fire ( 'swap:' + scriptName , scriptInstance ) ;
702
740
703
741
return true ;
704
742
} ,
@@ -799,31 +837,42 @@ Object.assign(pc, function () {
799
837
}
800
838
} ,
801
839
840
+ /* eslint-disable jsdoc/no-undefined-types */
802
841
/**
803
842
* @function
804
843
* @name pc.ScriptComponent#move
805
844
* @description Move script instance to different position to alter update order of scripts within entity.
806
- * @param {string } name - The name of the Script Type .
845
+ * @param {string|Class<pc.ScriptType> } nameOrType - The name or type of { @link pc.ScriptType} .
807
846
* @param {number } ind - New position index.
808
847
* @returns {boolean } If it was successfully moved.
809
848
* @example
810
849
* entity.script.move('playerController', 0);
811
850
*/
812
- move : function ( name , ind ) {
851
+ /* eslint-enable jsdoc/no-undefined-types */
852
+ move : function ( nameOrType , ind ) {
813
853
var len = this . _scripts . length ;
814
854
if ( ind >= len || ind < 0 )
815
855
return false ;
816
856
817
- var scriptName = name ;
857
+ var scriptType = nameOrType ;
858
+ var scriptName = nameOrType ;
818
859
819
- if ( typeof scriptName !== 'string' )
820
- scriptName = name . __name ;
860
+ if ( typeof scriptName !== 'string' ) {
861
+ scriptName = nameOrType . __name ;
862
+ } else {
863
+ scriptType = null ;
864
+ }
821
865
822
866
var scriptData = this . _scriptsIndex [ scriptName ] ;
823
867
if ( ! scriptData || ! scriptData . instance )
824
868
return false ;
825
869
826
- var indOld = this . _scripts . indexOf ( scriptData . instance ) ;
870
+ // if script type specified, make sure instance of said type
871
+ var scriptInstance = scriptData . instance ;
872
+ if ( scriptType && ! ( scriptInstance instanceof scriptType ) )
873
+ return false ;
874
+
875
+ var indOld = this . _scripts . indexOf ( scriptInstance ) ;
827
876
if ( indOld === - 1 || indOld === ind )
828
877
return false ;
829
878
@@ -835,8 +884,8 @@ Object.assign(pc, function () {
835
884
this . _updateList . sort ( ) ;
836
885
this . _postUpdateList . sort ( ) ;
837
886
838
- this . fire ( 'move' , scriptName , scriptData . instance , ind , indOld ) ;
839
- this . fire ( 'move:' + scriptName , scriptData . instance , ind , indOld ) ;
887
+ this . fire ( 'move' , scriptName , scriptInstance , ind , indOld ) ;
888
+ this . fire ( 'move:' + scriptName , scriptInstance , ind , indOld ) ;
840
889
841
890
return true ;
842
891
}
0 commit comments