Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**** AI movement action (every 60 ms) ****/
- $AIMoveTrainInterval = window.setInterval(function() {
- // If the AI is active
- if($aiToggle == true) {
- // If the AI engine has not been destroyed
- if(!$aiTrain.find(".engine").hasClass("destroyed")) {
- // If the AI train has not reached the right end of the scene (Collision detection)
- if(parseInt($aiTrain.css("left")) + $aiTrain.width() < $("#scene").width() && $AIMoveTrainDirection == 'right') {
- // Move the AI train to the right
- $aiTrain.css("left",parseInt($aiTrain.css("left"))+2);
- }
- // If the AI train has not reached the left end of the scene (Collision detection)
- if(parseInt($aiTrain.css("left")) > 1 && $AIMoveTrainDirection == 'left') {
- // Move the AI train to the left
- $aiTrain.css("left",parseInt($aiTrain.css("left"))-2);
- }
- }
- }
- },60);
- // Interval for random moving direction for the AI
- $AIMoveDecisionInterval = window.setInterval(function() {
- // Pick a random direction
- var $AIRandomDirection = Math.floor(Math.random() * $AIMoveTrainDecisions.length);
- // If the player train moves
- if($movePlayerTrainInterval != null) {
- // If the AI train does not move in the same direction as the player train
- if($AIMoveTrainDirection != $movePlayerTrainDirection) {
- // There is a 10% chance that the AI train WILL NOT move in the same direction as the player train
- $AIMoveTrainDirection = $(this).rand(0,100) < 10 ? $AIMoveTrainDecisions[$AIRandomDirection] : $movePlayerTrainDirection;
- // If the player train moves
- } else {
- // There is 80% chance that the AI train WILL NOT pick a random direction
- $AIMoveTrainDirection = $(this).rand(0,100) > 80 ? $AIMoveTrainDecisions[$AIRandomDirection] : $movePlayerTrainDirection;
- }
- } else {
- // If the AI Train does not move
- if($AIMoveTrainDirection == 'undefined') {
- $AIMoveTrainDirection = $AIMoveTrainDecisions[$AIRandomDirection];
- } else {
- // Pick a random Number
- var $random = $(this).rand(0,100);
- // If the AI train moves to the left, there is a 90% chance that the AI train will continue moving left
- $AIMoveTrainDirection = $AIMoveTrainDirection == 'left' && $random <= 90 ? 'left' : $AIMoveTrainDecisions[$AIRandomDirection];
- // If the AI train moves to the right, there is a 90% chance that the AI train will continue moving right
- $AIMoveTrainDirection = $AIMoveTrainDirection == 'right' && $random <= 90 ? 'right' : $AIMoveTrainDecisions[$AIRandomDirection];
- // IF the AI train has stopped, there is a 20% chance that the AI train will stay still
- $AIMoveTrainDirection = $AIMoveTrainDirection == 'stop' && $random <= 20 ? 'stop' : $AIMoveTrainDecisions[$AIRandomDirection];
- }
- }
- },$AIMoveTrainDirection == 'undefined' ? 500 : 5000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement