notalentgeek

Phaser - ObjectPlayer

Oct 6th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ObjectPlayer                                                    = function(
  2.  
  3.     _exhibitionStartString  ,
  4.     _floorObjectArray       ,
  5.     _roomObjectArray        ,
  6.     _exhibitionObjectArray  ,
  7.     _playerObjectArray      ,
  8.     _panelIndexNum          ,
  9.     _offsetYNum
  10.    
  11. ){
  12.  
  13.     if(
  14.  
  15.         typeof _exhibitionStartString                           === 'string' &&
  16.         typeof _floorObjectArray                                === 'object' &&
  17.         typeof _roomObjectArray                                 === 'object' &&
  18.         typeof _exhibitionObjectArray                           === 'object' &&
  19.         typeof _playerObjectArray                               === 'object' &&
  20.         typeof _panelIndexNum                                   === 'number' &&
  21.         typeof _offsetYNum                                      === 'number'
  22.  
  23.     ){
  24.  
  25.         this.floorObjectArray                                   = _floorObjectArray;        /*Initiate reference to the main floor object array.*/
  26.         this.roomObjectArray                                    = _roomObjectArray;         /*Initiate reference to the main room object array.*/
  27.         this.exhibitionObjectArray                              = _exhibitionObjectArray;   /*Initiate reference to the main exhibition bject array.*/
  28.         this.playerObjectArray                                  = _playerObjectArray;       /*Initiate reference to the main player object array.*/
  29.         this.siblingObjectArray                                 = new Array();              /*Empty array to be used later on to store which object share teh same parent with this player.*/
  30.  
  31.         this.exhibitionCurrentString                            = undefined;                /*The current exhibition of which this player resides in.*/
  32.         this.exhibitionTargetStringArray                        = new Array();              /*Current target exhibition of which has more priority over other exhibitions.*/
  33.         this.exhibitionVisitedStringArray                       = new Array();              /*List of exhibition that has been visited by this player.*/
  34.         this.tagMixedArray                                      = new Array();              /*List of all favorites tags gathered by this player by visiting exhibition.*/
  35.        
  36.         this.timeCurrentExhibitionNum                           = 0;                        /*PENDING: The amount of time this player spent on the current exhibition. Need to be changed to be calculated in second.*/
  37.         this.timeTotalNum                                       = 0;                        /*PENDING: The current amount of time this player spent on the whole museum visit. Can be achieved by adding all this.timeCurrentExhibition.*/
  38.  
  39.         this.panelHoverBool                                     = false;                    /*Wether a mouse pointer is above this player object.*/
  40.         this.panelIndexNum                                      = _panelIndexNum;           /*The index number of this player object among other player object (must be unique).*/
  41.         this.panelObject                                        = undefined;                /*The panel object for this object player.*/
  42.         this.panelLabelObject                                   = undefined;                /*The panel label object for this object player (contains test instead of sprite).*/
  43.         this.panelCardObject                                    = undefined;                /*The panel object for this object player.*/
  44.         this.panelCardLabelObject                               = undefined;                /*The panel label object for this object player (contains test instead of sprite).*/
  45.         this.panelXNum                                          = 0;                        /*Variable to hold the value of x position from panel object.*/
  46.         this.panelYNum                                          = 0;                        /*Variable to hold the value of y position from panel object.*/
  47.         this.panelWidthNum                                      = 0;                        /*Variable to hold the value of width from panel object.*/
  48.         this.panelHeightNum                                     = 0;                        /*Variable to hold the value of height from panel object.*/
  49.         this.panelCardXNum                                      = 0;                        /*Variable to hold the value of x position from panel card object.*/
  50.         this.panelCardYNum                                      = 0;                        /*Variable to hold the value of y position from panel card object.*/
  51.         this.panelCardWidthNum                                  = 0;                        /*Variable to hold the value of width from panel card object.*/
  52.         this.panelCardHeightNum                                 = 0;                        /*Variable to hold the value of height from panel card object.*/
  53.         this.panelSpriteString                                  = 'ImagePanelNew4';         /*Hold the string reference to sprite panel for panel label.*/
  54.         this.offsetYNum                                         = _offsetYNum;              /*Offset y for each stacked player's panel object.*/
  55.         this.fontSizeLabelNum                                   = 32;                       /*Font size for panel label.*/
  56.         this.fontSizeCardNum                                    = 32;                       /*Font size for panel card label.*/
  57.  
  58.         /*Initial setup for the this.exhibitionCurrent to _exhibitionStart and also add that things to this.exhibitionVisited.*/
  59.         this.ExhibitionMoveString                               (
  60.  
  61.             _exhibitionStartString,
  62.             this.floorObjectArray,
  63.             this.roomObjectArray,
  64.             this.exhibitionObjectArray,
  65.             this.offsetYNum
  66.  
  67.         );
  68.  
  69.     }
  70.     else{
  71.  
  72.         console.log                                             ((typeof _exhibitionStartString)    + ' is not a string.' );
  73.         console.log                                             ((typeof _floorObjectArray)         + ' is not an object.');
  74.         console.log                                             ((typeof _roomObjectArray)          + ' is not an object.');
  75.         console.log                                             ((typeof _exhibitionObjectArray)    + ' is not an object.');
  76.         console.log                                             ((typeof _playerObjectArray)        + ' is not an object.');
  77.         console.log                                             ((typeof _panelIndexNum)            + ' is not an number.');
  78.         console.log                                             ((typeof _offsetYNum)               + ' is not an number.');
  79.  
  80.     }
  81.  
  82. };
  83. ObjectPlayer.prototype.constructor                              = ObjectPlayer;
  84.  
  85. /*Add or remove this player from the parent object.*/
  86. ObjectPlayer.prototype.AddRemoveChildObjectArray                = function(_isAdd){
  87.  
  88.     if(typeof _isAdd === 'boolean'){
  89.  
  90.         /*Make reference to the current exhibition visited.*/
  91.         var exhibitionCurrentObject                             = this.FindObject(this.exhibitionObjectArray, this.exhibitionCurrentString);
  92.         /*Add this player to the child of the exhibition object.*/
  93.         if      (_isAdd )                                       { exhibitionCurrentObject.childObjectArray.push(this); }
  94.         /*Remove this player from the child of the exhibition object.*/
  95.         else if (!_isAdd)                                       {
  96.  
  97.             var indexNum                                        = exhibitionCurrentObject.childObjectArray.indexOf(this);
  98.                                                                   exhibitionCurrentObject.childObjectArray.splice(indexNum, 1);
  99.  
  100.         }
  101.  
  102.         return exhibitionCurrentObject.childObjectArray;
  103.  
  104.     }
  105.     else{
  106.  
  107.         console.log                                             ((typeof _isAdd) + ' is not a boolean.');
  108.         return undefined;
  109.  
  110.     }
  111.  
  112. }
  113.  
  114. /*AIAutoBool is a function that move this player object automatically to the exahibition.
  115. This function returns true if the player just move to new exhibition and false if the player
  116.     stay in the current exhibition.*/
  117. ObjectPlayer.prototype.AIAutoString                             = function(){
  118.  
  119.     /*Check wether this player has already visited most exhibitions in the museum.
  120.     I checked the whether the exhibition visited has the same amount of length with total exhibition length.
  121.     It is not necessary for this player to have all exhibitions visited due to there is a chance that this player
  122.         visited same exhibitions twice or more.*/
  123.     if(this.exhibitionObjectArray.length                        >= this.exhibitionVisitedStringArray.length){
  124.  
  125.         /*Increase the amount of time of this player in the current exhibition the visitor visits.
  126.         The more time this player spent time in the exhibition the more chance the visitor will move to the
  127.             new exhibition.
  128.         PENDING: Change the time increment per second add and per frame.*/
  129.         this.timeCurrentExhibitionNum                           ++;
  130.  
  131.         if(Math.random() > (1 - (this.timeCurrentExhibitionNum/100))){
  132.  
  133.             this.DetermineExhibitionTargetStringArray();
  134.  
  135.             /*
  136.             <<This is an old method to generate target exhibitions.>>
  137.             <<Generate random index number.
  138.             The index number refer to he array list of exhibition objects.
  139.             The index number must not go below 0 and must not go above the length of total exhibition objects.>>
  140.             var indexNum = Math.floor((Math.random()*this.exhibitionObjectArray.length) + 0);
  141.  
  142.             <<If the index number is the same with the index of the current exhibition then you need to generate
  143.                 new index number until the index is not the same with currently visited exhibition.>>
  144.             while(this.FindIndexNameAltNum(this.exhibitionObjectArray, this.exhibitionCurrentString) == indexNum){
  145.  
  146.                 indexNum = Math.floor((Math.random()*this.exhibitionObjectArray.length) + 0);
  147.  
  148.             }
  149.  
  150.             <<If indexNum generates number for exhibition index that has been visited before,
  151.                 this player has a chance of 75% to visit the exhibitions those are not yet visited.
  152.             It is like any visitors in any museums has less chance to visit the exhibition he/she has visited
  153.                 before.
  154.             The code below is to check the availability of index exhibition name in the array of visited exhibitions.
  155.             If the number is above -1 then it means the exhibition at indexNum had been visited before.>>
  156.             if(this.exhibitionVisitedStringArray.indexOf(this.exhibitionObjectArray[indexNum].objectNameAltString) > -1){
  157.  
  158.                 if(Math.random()    > 0.75){
  159.  
  160.                     <<Loop counter to prevent near infinite while loop.
  161.                     In case the visitor has visited most of the exhibition it will increase the loop of below while loop,
  162.                         so I add a counter to break the loop in case it is alraedy looped with the same amount of exhibitions in the museum.>>
  163.                     var loopNum     = 0;
  164.  
  165.                     <<A while loop to generates an index number for exhibition until the index exhibition is an exhibition that
  166.                         this visitor never visited before.>>
  167.                     while(this.exhibitionVisitedStringArray.indexOf(this.exhibitionObjectArray[indexNum].objectNameAltString) > -1){
  168.  
  169.                         loopNum     ++;
  170.                         indexNum    = Math.floor((Math.random()*this.exhibitionObjectArray.length) + 0);
  171.                         if(loopNum  >= this.exhibitionObjectArray.length){ break; }
  172.  
  173.  
  174.                     }
  175.                 }
  176.  
  177.             }
  178.             */
  179.  
  180.             /*Move player to the new exhibition.*/
  181.             var randomIndexNum      = Math.floor((Math.random()*this.exhibitionTargetStringArray.length) + 0);
  182.             var newExhibitionString = this.ExhibitionMoveString(
  183.  
  184.                 this.exhibitionTargetStringArray[randomIndexNum],
  185.                 this.floorObjectArray,
  186.                 this.roomObjectArray,
  187.                 this.exhibitionObjectArray,
  188.                 this.offsetYNum
  189.  
  190.             );
  191.  
  192.             this.timeCurrentExhibitionNum   = 0;    /*Reset timer.*/
  193.             return                          newExhibitionString;
  194.  
  195.         }
  196.         else{ return undefined; }
  197.  
  198.     }
  199.     else{
  200.  
  201.         /*Assign new sprite if this player finished on visiting the museum.*/
  202.         this.panelSpriteString = 'ImagePanelNew5';
  203.         /*Re - create graphical representation of this player.*/
  204.         this.CreatePanelObject(this.offsetYNum);
  205.  
  206.         return undefined;
  207.  
  208.     }
  209.  
  210. };
  211.  
  212. /*This function is to calculate how many player object share the same parent object.*/
  213. ObjectPlayer.prototype.CalculateSiblingObjectArray              = function(){
  214.  
  215.     /*Always empty the object array, due to we will fill with the new one.*/
  216.     this.siblingObjectArray                                     = [];
  217.  
  218.     /*Iterate through the object player array.*/
  219.     for(var i = 0; i < this.playerObjectArray.length; i ++)     {
  220.  
  221.         /*If an object share the same parent name than add the object to this.siblingObjectArray.*/
  222.         if(this.playerObjectArray[i].exhibitionCurrentString    == this.exhibitionCurrentString){
  223.  
  224.             this.siblingObjectArray.push(this.playerObjectArray[i]);
  225.  
  226.         }
  227.  
  228.     }
  229.  
  230.     return this.siblingObjectArray;
  231.  
  232. };
  233.  
  234. /*Ascending comparison function for calculating how many points does a tag has in this player.*/
  235. /*
  236. ObjectPlayer.prototype.CompareTagNum                            = function(_elementMixedArray1, _elementMixedArray2){
  237.  
  238.     return _elementMixedArray1[1] - _elementMixedArray2[1];
  239.  
  240. };
  241. */
  242. /*Descending comparison function for calculating how many points does a tag has in this player.*/
  243. ObjectPlayer.prototype.CompareTagNum                            = function(_elementMixedArray1, _elementMixedArray2){
  244.  
  245.     return _elementMixedArray2[1] - _elementMixedArray1[1];
  246.  
  247. };
  248.  
  249. /*Create the graphical user interface of this player.*/
  250. ObjectPlayer.prototype.CreatePanelObject                        = function(){
  251.  
  252.     /*Take reference to this player current exhibition.*/
  253.     var exhibitionCurrentObject                                 = this.FindObject(this.exhibitionObjectArray, this.exhibitionCurrentString);
  254.    
  255.     /*Take reference to this player index within exhibition object child array.*/
  256.     var indexNum                                                = this.FindIndexObjectNum(exhibitionCurrentObject.childObjectArray, this);
  257.  
  258.     this.panelWidthNum                                          = exhibitionCurrentObject.panelWidthNum;
  259.     this.panelHeightNum                                         = exhibitionCurrentObject.panelHeightNum;
  260.  
  261.     /*Destroy the previously created panel.*/
  262.     if(this.panelObject         != undefined)                   { this.panelObject.destroy();       }
  263.     if(this.panelLabelObject    != undefined)                   { this.panelLabelObject.destroy();  }
  264.  
  265.     this.panelObject                                            = game.add.sprite(
  266.  
  267.         exhibitionCurrentObject.panelXNum,
  268.         exhibitionCurrentObject.panelYNum + ((indexNum + 1)*this.offsetYNum) + ((indexNum + 1)*this.panelHeightNum),
  269.         this.panelSpriteString
  270.  
  271.     );
  272.  
  273.     this.panelXNum                                              = this.panelObject.x;
  274.     this.panelYNum                                              = this.panelObject.y;
  275.     this.panelObject.width                                      = this.panelWidthNum;
  276.     this.panelObject.height                                     = this.panelHeightNum;
  277.     this.panelObject.inputEnabled                               = true;
  278.  
  279.     this.panelLabelObject                                       = game.add.text(
  280.  
  281.         (this.panelXNum + this.panelWidthNum/2),
  282.         (this.panelYNum + this.panelHeightNum/2),
  283.         this.panelIndexNum,
  284.         {
  285.             'align'     : 'center',
  286.             'fontSize'  : this.fontSizeNum
  287.         }
  288.  
  289.     );
  290.     this.panelLabelObject.inputEnabled                          = true;
  291.     this.panelLabelObject.anchor                                .setTo(0.5, 0.5);
  292.  
  293.     return this.panelObject;
  294.  
  295. };
  296.  
  297. ObjectPlayer.prototype.DetermineExhibitionTargetStringArray     = function(){
  298.  
  299.     /*Need to empty the array whenever this player is looking for another exhibitions.*/
  300.     this.exhibitionTargetStringArray = [];
  301.  
  302.     /*Stage one sort.
  303.     Stage one sort is to remove the currently visited exhibition from the target exhibition index.
  304.     So that the player have no chance on visiting the exhibition that he/she  currently visits.*/
  305.     for(var i = 0; i < this.exhibitionObjectArray.length; i ++){
  306.  
  307.         /*Compare the current exihibition with the object exhibitiob array.
  308.         After that remove the object exhibition that is the current exhibition and put the rest
  309.             of the exhibition in the target exhibition array string.*/
  310.         if(this.exhibitionCurrentString != this.exhibitionObjectArray[i].objectNameAltString){ this.exhibitionTargetStringArray.push(this.exhibitionObjectArray[i].objectNameAltString); }
  311.  
  312.     }
  313.     //console.log('Stage one: ' + this.exhibitionTargetStringArray.length);
  314.  
  315.     /*Stage two sort.
  316.     Remove all exhibition target that is full of visitor.*/
  317.     for(var i = 0; i < this.exhibitionObjectArray.length; i ++){
  318.  
  319.         if(
  320.  
  321.             (this.exhibitionObjectArray[i].isFullBool    == true)                                               &&
  322.             (this.exhibitionCurrentString                != this.exhibitionObjectArray[i].objectNameAltString)
  323.  
  324.         ){
  325.            
  326.             var indexNum                                        = this.exhibitionTargetStringArray.indexOf(this.exhibitionObjectArray[i].objectNameAltString);
  327.             this.exhibitionTargetStringArray                    .splice(indexNum, 1);
  328.             if(this.exhibitionTargetStringArray.length == 3)    { return this.exhibitionTargetStringArray; }
  329.  
  330.         }
  331.  
  332.     }
  333.  
  334.     /*Stage three sort.
  335.     Stage three sort is to make the exhibition that has been visited before has 90% chance to make into target exhibition.
  336.     For example the visitor is now in the Exhibition C as he/she used to visits Exhibition A and Exhibition B before,
  337.         the system now will let Exhibition A and Exhibition B to have 10% chance to be not removed from the target
  338.         exhibition array.*/
  339.     for(var i = 0; i < this.exhibitionVisitedStringArray.length; i ++){
  340.  
  341.         for(var j = 0; j < this.exhibitionTargetStringArray.length; j ++){
  342.  
  343.             /*Compare the target exhibitions with all visited exhibition.
  344.             If it matches then the corresponding exhibition has 90% chance to be deleted
  345.                 from target exhibition array.*/
  346.             if(this.exhibitionVisitedStringArray[i] == this.exhibitionTargetStringArray[j]){
  347.  
  348.                 if(Math.random() < 0.90){
  349.  
  350.                     this.exhibitionTargetStringArray.splice(j, 1);
  351.                     i --;
  352.  
  353.                 }
  354.  
  355.                 /*After each splice make sure to have the exhibition target length to be 3.
  356.                 If not 3 elements in the target exhibition array, then return the last 3 elements
  357.                     of target exhibition array ever exist.*/
  358.                 if(this.exhibitionTargetStringArray.length == 3){ return this.exhibitionTargetStringArray; }
  359.  
  360.             }
  361.  
  362.         }
  363.  
  364.     }
  365.     //console.log('Stage three: ' + this.exhibitionTargetStringArray.length);
  366.  
  367.     /*Stage four sort.
  368.     So now this application compare the the most visited tags from this player profile (take three most visited tags)
  369.         and compared to the exhibition tag.
  370.     Each exhibition has 3 tags so,
  371.         if nothing match the exhibition is excluded from from the target exhibition array,
  372.         if one tag is match the exhibition has 66% chance of being removed from the target exhibition array,
  373.         if two tags are match the exhibition has 33% chance of being removed from the target exhibition array,
  374.         if three tags are match the exhibition will stay in the target exhibition array.*/
  375.     var tempTagStringArray = new Array(3);
  376.     for(var i = 0; i < tempTagStringArray.length; i ++){ tempTagStringArray[i] = this.tagMixedArray[i][0]; }
  377.     for(var i = 0; i < this.exhibitionTargetStringArray.length; i ++){
  378.  
  379.         var exhibitionTargetObject      = this.FindObject(this.exhibitionObjectArray, this.exhibitionTargetStringArray[i]);
  380.         var tagSameCountNum             = 0;
  381.         for(var j = 0; j < exhibitionTargetObject.tagStringArray.length; j ++){
  382.  
  383.             for(var k = 0; k < tempTagStringArray.length; k ++){
  384.  
  385.                 if(exhibitionTargetObject.tagStringArray[j] == tempTagStringArray[k]){ tagSameCountNum ++; }
  386.  
  387.             }
  388.  
  389.         }
  390.        
  391.         if      (tagSameCountNum == 0)          {                           this.exhibitionTargetStringArray.splice(i, 1); i --; }
  392.         else if (tagSameCountNum == 1)          { if(Math.random() < 0.66){ this.exhibitionTargetStringArray.splice(i, 1); i --; } }
  393.         else if (tagSameCountNum == 2)          { if(Math.random() < 0.33){ this.exhibitionTargetStringArray.splice(i, 1); i --; } }
  394.         else if (tagSameCountNum == 3)          {  }
  395.  
  396.         /*After each splice make sure to have the exhibition target length to be 3.
  397.         If not 3 elements in the target exhibition array, then return the last 3 elements
  398.             of target exhibition array ever exist.*/
  399.         if(this.exhibitionTargetStringArray.length == 3){ return this.exhibitionTargetStringArray; }
  400.  
  401.     }
  402.     //console.log('Stage four: ' + this.exhibitionTargetStringArray.length);
  403.  
  404.     var exhibitionCurrentObject         = this.FindObject   (this.exhibitionObjectArray     , this.exhibitionCurrentString);
  405.     var exhibitionRoomString            = exhibitionCurrentObject.objectParentNameAltString;
  406.     var exhibitionRoomObject            = this.FindObject   (this.roomObjectArray           , exhibitionRoomString);
  407.     var exhibitionFloorString           = exhibitionRoomObject.objectParentNameAltString;
  408.     var exhibitionFloorObject           = this.FindObject   (this.floorObjectArray          , exhibitionFloorString);
  409.  
  410.     /*Stage five sort.
  411.     The fourth sort is to make the exhibition target that are not in the same floor of which player's
  412.         current exhibition to have 50% chance of stay.*/
  413.     for(var i = 0; i < this.exhibitionTargetStringArray.length; i ++){
  414.  
  415.         var exhibitionTargetObject      =  this.FindObject      (this.exhibitionObjectArray         , this.exhibitionTargetStringArray[i]);
  416.         var exhibitionTargetRoomString  =  exhibitionTargetObject.objectParentNameAltString;
  417.         var exhibitionTargetRoomObject  =  this.FindObject      (this.roomObjectArray               , exhibitionTargetRoomString);
  418.         var exhibitionTargetFloorString =  exhibitionTargetRoomObject.objectParentNameAltString;
  419.         var exhibitionTargetFloorObject =  this.FindObject      (this.floorObjectArray              , exhibitionTargetFloorString);
  420.  
  421.         if(exhibitionTargetFloorString  != exhibitionFloorString){ if(Math.random() < 0.50){ this.exhibitionTargetStringArray.splice(i, 1); i --; } }
  422.  
  423.         /*After each splice make sure to have the exhibition target length to be 3.
  424.         If not 3 elements in the target exhibition array, then return the last 3 elements
  425.             of target exhibition array ever exist.*/
  426.         if(this.exhibitionTargetStringArray.length == 3){ return this.exhibitionTargetStringArray; }
  427.  
  428.     }
  429.     //console.log('Stage five: ' + this.exhibitionTargetStringArray.length);
  430.  
  431.     /*Stage six sort.
  432.     The fifth sort is to make the exhibition target that are not in the same room of which player's
  433.         current exhibition to have 50% chance of stay.*/
  434.     for(var i = 0; i < this.exhibitionTargetStringArray.length; i ++){
  435.  
  436.         var exhibitionTargetObject      =  this.FindObject      (this.exhibitionObjectArray         , this.exhibitionTargetStringArray[i]);
  437.         var exhibitionTargetRoomString  =  exhibitionTargetObject.objectParentNameAltString;
  438.         var exhibitionTargetRoomObject  =  this.FindObject      (this.roomObjectArray               , exhibitionTargetRoomString);
  439.        
  440.         if(exhibitionTargetRoomString  != exhibitionRoomString){ if(Math.random() < 0.50){ this.exhibitionTargetStringArray.splice(i, 1); i --; } }
  441.  
  442.         /*After each splice make sure to have the exhibition target length to be 3.
  443.         If not 3 elements in the target exhibition array, then return the last 3 elements
  444.             of target exhibition array ever exist.*/
  445.         if(this.exhibitionTargetStringArray.length == 3){ return this.exhibitionTargetStringArray; }
  446.  
  447.     }
  448.     //console.log('Stage six: ' + this.exhibitionTargetStringArray.length);
  449.  
  450.     /*In case in the end of final sort the target exhibition array have length more than 3, make it just have 3 elements.*/
  451.     this.exhibitionTargetStringArray.splice(3, this.exhibitionObjectArray.length);
  452.  
  453.     return this.exhibitionTargetStringArray;
  454.  
  455. };
  456.  
  457. /*A function to move this player to new exhibition.*/
  458. ObjectPlayer.prototype.ExhibitionMoveString                     = function(
  459.     _exhibitionNameAltString    ,
  460.     _floorObjectArray           ,
  461.     _roomObjectArray            ,
  462.     _exhibitionObjectArray      ,
  463.     _offsetYNum
  464. ){
  465.  
  466.     /*Verification of argument inputted.*/
  467.     if(
  468.  
  469.         typeof _exhibitionNameAltString                         === 'string' &&
  470.         typeof _floorObjectArray                                === 'object' &&
  471.         typeof _roomObjectArray                                 === 'object' &&
  472.         typeof _exhibitionObjectArray                           === 'object' &&
  473.         typeof _offsetYNum                                      === 'number'
  474.        
  475.  
  476.     ){
  477.  
  478.         /*If this player move into new exhibition then remove it graphical representation from the current exhibition before this player
  479.             change exhibition.*/
  480.         if(this.exhibitionCurrentString != undefined)           { this.AddRemoveChildObjectArray(false); }
  481.  
  482.         /*Add calculation for the current exhibition array before the this player is moved into new exhibition.
  483.         For example like removing the amount of current visitor.*/
  484.         if(this.exhibitionCurrentString                         != undefined){
  485.  
  486.             var exhibitionCurrentObject                         = this.FindObject(_exhibitionObjectArray  , this.exhibitionCurrentString);
  487.             var roomCurrentObject                               = this.FindObject(_roomObjectArray        , exhibitionCurrentObject         .objectParentNameAltString);
  488.             var floorCurrentObject                              = this.FindObject(_floorObjectArray       , roomCurrentObject               .objectParentNameAltString);
  489.             exhibitionCurrentObject                             .visitorCurrentNum --;
  490.             roomCurrentObject                                   .visitorCurrentNum --;
  491.             floorCurrentObject                                  .visitorCurrentNum --;
  492.  
  493.         }
  494.  
  495.         this.exhibitionCurrentString                            = _exhibitionNameAltString;         /*Change the current eexhibition.*/
  496.         this.exhibitionVisitedStringArray                       .push(_exhibitionNameAltString);    /*Push the name of the newly visited exhibition to the array of visited exhibition.*/
  497.        
  498.         /*PENDING: Add a code to check whether the visited exhibition is in the museum.*/
  499.  
  500.         /*Adding one additional visitor to a new exhibition.*/
  501.         var exhibitionCurrentObject                             = this.FindObject(_exhibitionObjectArray  , this.exhibitionCurrentString);
  502.         var roomCurrentObject                                   = this.FindObject(_roomObjectArray        , exhibitionCurrentObject         .objectParentNameAltString);
  503.         var floorCurrentObject                                  = this.FindObject(_floorObjectArray       , roomCurrentObject               .objectParentNameAltString);
  504.         exhibitionCurrentObject                                 .visitorCurrentNum  ++;
  505.         roomCurrentObject                                       .visitorCurrentNum  ++;
  506.         floorCurrentObject                                      .visitorCurrentNum  ++;
  507.         exhibitionCurrentObject                                 .visitorTotalNum    ++;
  508.         roomCurrentObject                                       .visitorTotalNum    ++;
  509.         floorCurrentObject                                      .visitorTotalNum    ++;
  510.  
  511.         /*These codes below is to add tags into player array.
  512.         And then it gives value for every tags inside the array.*/
  513.         for(var i = 0; i < exhibitionCurrentObject.tagStringArray.length; i ++){
  514.  
  515.             /*Add the tags from exhibition to the this.tagMixedArray.
  516.             this.tagMixedStringArray is an array that contains two dimensional array.
  517.             For its contents the first dimension is the tag nalternative name.
  518.             While, the latest dimension is the score of the array for this player.*/
  519.             var tagMixedArray                                   = new Array(2);
  520.             tagMixedArray[0]                                    = exhibitionCurrentObject.tagStringArray[i];
  521.  
  522.             var isNewBool                                       = true;         /*Whether the tag is new to the array or there is already existing one.*/
  523.             var indexNum                                        = undefined;    /*If there is the corresponding tag already in the array return its index with this variable, otherwise it keeps undefined.*/
  524.  
  525.             /*Check inside this.tagMixedArray to see if the newly received tags
  526.                 are inside the array already.
  527.             The checking is one per tag.*/
  528.             for(var j = 0; j < this.tagMixedArray.length; j ++){
  529.  
  530.                 /*If one or more tags is found within the this.tagMixedArray,
  531.                     then set isNewBool to false and set the indexNum for future access.*/
  532.                 if(tagMixedArray[0] == this.tagMixedArray[j][0]){
  533.  
  534.                     isNewBool                                   = false;
  535.                     indexNum                                    = j;
  536.                     break;
  537.  
  538.                 }
  539.  
  540.             }
  541.  
  542.             /*If the tag is new to the this.tagMixedArray, then add 1 to the second array element and
  543.                 push the string value of tag name and number value of tag value to the this.tagMixedArray.*/
  544.             if(isNewBool == true){
  545.  
  546.                 tagMixedArray[1]                                = 1;
  547.                 this.tagMixedArray                              .push(tagMixedArray);
  548.  
  549.             }
  550.             /*If the tag already exist in this.tagMixedArray then using the indexNum to increase the visiting value with additional
  551.                 one value.*/
  552.             else if (!isNewBool){ this.tagMixedArray[indexNum][1] = this.tagMixedArray[indexNum][1] + 1; }
  553.  
  554.         }
  555.  
  556.         this.SortArray(this.tagMixedArray, this.CompareTagNum);     /*Sort the array so that the highest point of tags is always on top.*/
  557.         this.AddRemoveChildObjectArray(true);                       /*Add this player to the new exhibition parent.*/
  558.         this.CalculateSiblingObjectArray();                         /*Re calculate the sibling array.*/
  559.         this.CreatePanelObject(this.offsetYNum);                    /*Create graphical representation of this player.*/
  560.  
  561.         /*For every player moved, update the panel position of all player.*/
  562.         for(var i = 0; i < this.playerObjectArray.length; i ++){ this.playerObjectArray[i].CreatePanelObject(); }
  563.  
  564.         return this.exhibitionCurrentString;                        /*Return the array of visited exhbition.*/
  565.  
  566.     }
  567.     else{
  568.  
  569.         console.log     ((typeof _exhibitionNameAltString)  + ' is not a string.');
  570.         console.log     ((typeof _floorObjectArray)         + ' is not a object.');
  571.         console.log     ((typeof _roomObjectArray)          + ' is not a object.');
  572.         console.log     ((typeof _exhibitionObjectArray)    + ' is not a object.');
  573.         console.log     ((typeof _offsetYNum)               + ' is not a number.');
  574.         return          undefined;
  575.  
  576.     }
  577.  
  578. };
  579.  
  580. /*A function to find the exhibition index in an array of object exhibition, based on exhibition's
  581.     name alt.*/
  582. ObjectPlayer.prototype.FindIndexNameAltNum                      = function(_exhibitionObjectArray, _exhibitionNameAltString){
  583.  
  584.     if(
  585.  
  586.         (typeof _exhibitionObjectArray                          === 'object') &&
  587.         (typeof _exhibitionNameAltString                        === 'string')
  588.  
  589.     ){
  590.  
  591.         /*Loop through the array.*/
  592.         for(var i = 0; i < _exhibitionObjectArray.length; i ++){
  593.  
  594.             /*Check the variable name of nameObjectAlt one by one per array element.
  595.             i is the index number when the variable name equals with the variable value.*/
  596.             if(_exhibitionObjectArray[i]['objectNameAltString'] == _exhibitionNameAltString){ return i; }
  597.  
  598.         }
  599.         return undefined;
  600.  
  601.     }
  602.     else{
  603.  
  604.         console.log((typeof _exhibitionObjectArray)     + ' is not an object.');
  605.         console.log((typeof _exhibitionNameAltString)   + ' is not a string.');
  606.         return undefined;
  607.  
  608.     }
  609.  
  610. };
  611.  
  612. /*A function to find the exhibition index in an array of object exhibition, based on object.*/
  613. ObjectPlayer.prototype.FindIndexObjectNum                       = function(_playerObjectArray, _exhibitionObject){
  614.  
  615.     if(
  616.  
  617.         (typeof _playerObjectArray                              === 'object') &&
  618.         (typeof _exhibitionObject                               === 'object')
  619.  
  620.     ){
  621.  
  622.         /*Loop through the array.*/
  623.         for(var i = 0; i < _playerObjectArray.length; i ++){
  624.  
  625.             /*Check the variable name of nameObjectAlt one by one per array element.
  626.             i is the index number when the variable name equals with the variable value.*/
  627.             if(_playerObjectArray[i] == _exhibitionObject){ return i; }
  628.  
  629.         }
  630.         return undefined;
  631.  
  632.     }
  633.     else{
  634.  
  635.         console.log((typeof _playerObjectArray)         + ' is not an object.');
  636.         console.log((typeof _exhibitionObject)          + ' is not an object.');
  637.         return undefined;
  638.  
  639.     }
  640.  
  641. };
  642.  
  643. /*Using the function to find object index, I created another function to return the object instead of the index.*/
  644. ObjectPlayer.prototype.FindObject                               = function(_objectArray, _exhibitionNameAltString){
  645.  
  646.     if(
  647.  
  648.         (typeof _objectArray                                    === 'object') &&
  649.         (typeof _exhibitionNameAltString                        === 'string')
  650.  
  651.     ){ return _objectArray[this.FindIndexNameAltNum(_objectArray, _exhibitionNameAltString)]; }
  652.     else{
  653.  
  654.         console.log((typeof _objectArray)                       + ' is not an object.');
  655.         console.log((typeof _exhibitionNameAltString)           + ' is not a string.');
  656.         return undefined;
  657.  
  658.     }
  659.  
  660. };
  661.  
  662. /*This is an update function to update the reference that were put in the constructor.
  663. For every value that is need to be updated you put it here in the update function.
  664. This function's arguments are the value that is need to be updated every tick of the program.
  665. PENDING: I need to create so that this Update function get updated per everytime there is value change
  666.     in the current exhibition.*/
  667. ObjectPlayer.prototype.UpdateVoid                               = function(
  668.  
  669.     _floorObjectArray       ,
  670.     _roomObjectArray        ,
  671.     _exhibitionObjectArray  ,
  672.     _playerObjectArray
  673.  
  674. ){
  675.  
  676.     if(
  677.  
  678.         typeof _floorObjectArray                                === 'object' &&
  679.         typeof _roomObjectArray                                 === 'object' &&
  680.         typeof _exhibitionObjectArray                           === 'object' &&
  681.         typeof _playerObjectArray                               === 'object'
  682.  
  683.     ){
  684.  
  685.         this.floorObjectArray                                   = _floorObjectArray;
  686.         this.roomObjectArray                                    = _roomObjectArray;
  687.         this.exhibitionObjectArray                              = _exhibitionObjectArray;
  688.         this.playerObjectArray                                  = _playerObjectArray;
  689.  
  690.         /*Check pointer over the panel object sprite.*/
  691.         if(
  692.             this.panelObject        .input.pointerOver()        == true         ||
  693.             this.panelLabelObject   .input.pointerOver()        == true
  694.         ){  this.panelHoverBool = true;  }
  695.         else if(
  696.  
  697.             this.panelObject        .input.pointerOver()        == false        &&
  698.             this.panelLabelObject   .input.pointerOver()        == false
  699.  
  700.         ){  this.panelHoverBool = false; }
  701.  
  702.     }
  703.     else{
  704.  
  705.         console.log                                             ((typeof _floorObjectArray)         + ' is not an object.');
  706.         console.log                                             ((typeof _roomObjectArray)          + ' is not an object.');
  707.         console.log                                             ((typeof _exhibitionObjectArray)    + ' is not an object.');
  708.         console.log                                             ((typeof _playerObjectArray)        + ' is not an object.');
  709.  
  710.     }
  711.  
  712. };
  713.  
  714. /*Generic sorting array function.*/
  715. ObjectPlayer.prototype.SortArray                                = function(_targetArray, _compareFunction){
  716.  
  717.     if(
  718.  
  719.         typeof _targetArray     !== 'object' &&
  720.         typeof _compareFunction !== 'function'
  721.  
  722.     ){ return 'undefined'; }
  723.  
  724.     _targetArray.sort(_compareFunction);
  725.     return _targetArray;
  726.  
  727. };
Advertisement
Add Comment
Please, Sign In to add comment