Advertisement
Scarabaeus

Transarctia AI train movement

Sep 27th, 2014
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**** AI movement action (every 60 ms) ****/
  2.    
  3.     $AIMoveTrainInterval = window.setInterval(function() { 
  4.    
  5.         // If the AI is active
  6.         if($aiToggle == true) {
  7.            
  8.             // If the AI engine has not been destroyed
  9.             if(!$aiTrain.find(".engine").hasClass("destroyed")) {          
  10.                
  11.                 // If the AI train has not reached the right end of the scene (Collision detection)
  12.                 if(parseInt($aiTrain.css("left")) + $aiTrain.width() < $("#scene").width() && $AIMoveTrainDirection == 'right') {
  13.                
  14.                     // Move the AI train to the right
  15.                     $aiTrain.css("left",parseInt($aiTrain.css("left"))+2);
  16.                 }
  17.                
  18.                 // If the AI train has not reached the left end of the scene (Collision detection)
  19.                 if(parseInt($aiTrain.css("left")) > 1 && $AIMoveTrainDirection == 'left') {
  20.                
  21.                     // Move the AI train to the left
  22.                     $aiTrain.css("left",parseInt($aiTrain.css("left"))-2);
  23.                 }
  24.             }
  25.         }
  26.     },60);
  27.    
  28.     // Interval for random moving direction for the AI
  29.     $AIMoveDecisionInterval = window.setInterval(function() {
  30.        
  31.         // Pick a random direction
  32.         var $AIRandomDirection = Math.floor(Math.random() * $AIMoveTrainDecisions.length);
  33.    
  34.         // If the player train moves
  35.         if($movePlayerTrainInterval != null) {
  36.        
  37.             // If the AI train does not move in the same direction as the player train
  38.             if($AIMoveTrainDirection != $movePlayerTrainDirection) {
  39.            
  40.                 // There is a 10% chance that the AI train WILL NOT move in the same direction as the player train
  41.                 $AIMoveTrainDirection = $(this).rand(0,100) < 10 ? $AIMoveTrainDecisions[$AIRandomDirection] : $movePlayerTrainDirection;
  42.                
  43.             // If the player train moves
  44.             } else {
  45.            
  46.                 // There is 80% chance that the AI train WILL NOT pick a random direction              
  47.                 $AIMoveTrainDirection = $(this).rand(0,100) > 80 ? $AIMoveTrainDecisions[$AIRandomDirection] : $movePlayerTrainDirection;
  48.             }
  49.            
  50.         } else {
  51.            
  52.             // If the AI Train does not move
  53.             if($AIMoveTrainDirection == 'undefined') {
  54.                 $AIMoveTrainDirection = $AIMoveTrainDecisions[$AIRandomDirection];
  55.             } else {   
  56.            
  57.                 // Pick a random Number
  58.                 var $random = $(this).rand(0,100);
  59.                
  60.                 // If the AI train moves to the left, there is a 90% chance that the AI train will continue moving left
  61.                 $AIMoveTrainDirection = $AIMoveTrainDirection == 'left' && $random <= 90 ? 'left' : $AIMoveTrainDecisions[$AIRandomDirection];
  62.                
  63.                 // If the AI train moves to the right, there is a 90% chance that the AI train will continue moving right
  64.                 $AIMoveTrainDirection = $AIMoveTrainDirection == 'right' && $random <= 90 ? 'right' : $AIMoveTrainDecisions[$AIRandomDirection];
  65.                
  66.                 // IF the AI train has stopped, there is a 20% chance that the AI train will stay still
  67.                 $AIMoveTrainDirection = $AIMoveTrainDirection == 'stop' && $random <= 20 ? 'stop' : $AIMoveTrainDecisions[$AIRandomDirection];
  68.             }          
  69.         }
  70.        
  71.     },$AIMoveTrainDirection == 'undefined' ? 500 : 5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement