Advertisement
svetlai

Air Hockey AI Engine

Jul 27th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // increase for more difficult
  2. var difficulty = 2;
  3. function startGame() {
  4.     //........................
  5.     moveEnemy(difficulty);
  6.     //........................
  7.  
  8.     function moveEnemy(difficulty) {
  9.         var
  10.             speed = getEnemyMoveSpeed(difficulty);
  11.     //.........................
  12.     }
  13.  
  14.     //..........................
  15.  
  16.     function getEnemyMoveSpeed(difficulty){
  17.     var dx = enemy.x - disc.x + 2.5,
  18.             dy = enemy.y - disc.y + 2.5,
  19.             distance = Math.sqrt(dx * dx + dy * dy);
  20.  
  21.         distance = distance === 0 ? 0.1 : distance;
  22.  
  23.         return {
  24.             x: Math.abs((dx / distance) * difficulty),
  25.             y: Math.abs((dy / distance) * difficulty)
  26.         }      
  27.     }
  28.  
  29.     //..........................
  30. }
  31.  
  32. // -------------------------------------------------- old stuff
  33. var canvasDrawer = new CanvasDrawer(),
  34.     participantRadius = 15,
  35.     startPlayerX = 20,
  36.     disc = new Disc(canvasDrawer.canvasWidth / 2, canvasDrawer.canvasHeight / 2, 12),
  37.     player = new Player('Pesho', 20, 180, participantRadius),
  38.     enemy = new Enemy('Gosho', canvasDrawer.canvasWidth - 20, canvasDrawer.canvasHeight / 2, participantRadius);
  39.  
  40. function startGame() {
  41.     canvasDrawer.clear();
  42.     canvasDrawer.drawParticipant(player, "red");
  43.     canvasDrawer.drawParticipant(enemy, "blue");
  44.     canvasDrawer.drawDisc(disc);
  45.     player.move();
  46.     disc.move();
  47.     detectCollisionWithDisc(player, disc);
  48.     detectCollisionWithDisc(enemy, disc);
  49.     detectDiscCollisionWithWalls(disc, canvasDrawer);
  50.     moveEnemy();
  51.    
  52.     function detectCollisionWithDisc(player, disc) {
  53.         var dx = player.x - disc.x,
  54.             dy = player.y - disc.y,
  55.             distance = Math.sqrt(dx * dx + dy * dy);
  56.  
  57.         if (distance < player.radius + disc.radius) {
  58.             if (distance === 0) {
  59.                 distance = 0.1;
  60.             }
  61.  
  62.             var unitX = dx / distance,
  63.                 unitY = dy / distance,
  64.  
  65.                 force = -2,
  66.                 forceX = unitX * force,
  67.                 forceY = unitY * force;
  68.  
  69.             disc.velocity.x += forceX;
  70.             disc.velocity.y += forceY;
  71.  
  72.             return true;
  73.         } else {
  74.             return false;
  75.         }
  76.     }
  77.    
  78.     function detectDiscCollisionWithWalls(disc, canvasDrawer) {
  79.         // bounce off the floor
  80.         if (disc.y > canvasDrawer.canvasHeight - disc.radius) {
  81.             disc.y = canvasDrawer.canvasHeight - disc.radius;
  82.             disc.velocity.y = -Math.abs(disc.velocity.y);
  83.         }
  84.  
  85.         // bounce off ceiling
  86.         if (disc.y < disc.radius + 0) {
  87.             disc.y = disc.radius + 0;
  88.             disc.velocity.y = Math.abs(disc.velocity.y);
  89.         }
  90.         // bounce off right wall
  91.         if (disc.x > canvasDrawer.canvasWidth - disc.radius) {
  92.             disc.x = canvasDrawer.canvasWidth - disc.radius;
  93.             disc.velocity.x = -Math.abs(disc.velocity.x);
  94.         }
  95.         // bounce off left wall
  96.         if (disc.x < disc.radius) {
  97.             disc.x = disc.radius;
  98.             disc.velocity.x = Math.abs(disc.velocity.x);
  99.         }
  100.     }
  101.  
  102.     // enemy movement
  103.     function moveEnemy() {
  104.         var playGroundMiddle = canvasDrawer.canvasWidth / 2,
  105.             enemyDefenceLine = canvasDrawer.canvasWidth * 5 / 8,
  106.             speed = getEnemyMoveSpeed();
  107.            
  108.         if (disc.x > canvasDrawer.canvasWidth / 2) {
  109.             // if disc is passed defence line but not enemy x
  110.             if (disc.x > enemyDefenceLine && enemy.x > enemyDefenceLine) {     
  111.                 //enemy.move("left", speed.x);
  112.                 moveEnemyX(speed.x);
  113.                 moveEnemyY(speed.y);
  114.             }
  115.             moveEnemyY(speed.y);               
  116.         } else {
  117.             enemy.reset();
  118.         }
  119.     }
  120.    
  121.     function moveEnemyY(speed){
  122.         if (disc.y < enemy.y && disc.x < enemy.x) {
  123.             enemy.move("up", speed);
  124.         } else if (disc.y > enemy.y && disc.x < enemy.x) {
  125.             enemy.move("down", speed);
  126.         }
  127.     }
  128.    
  129.     function moveEnemyX(speed) {
  130.         // if disc is passed enemy x   
  131.         if (disc.x >= enemy.x) {
  132.             enemy.reset();
  133.         } else {
  134.             enemy.move("left", speed);
  135.         }
  136.     }
  137.    
  138.     // TODO
  139.     function getEnemyMoveSpeed(){
  140.         return {
  141.             x: 2.5,
  142.             y: 2.5
  143.         }
  144.     }
  145.  
  146.     requestAnimationFrame(function () {
  147.         startGame();
  148.     });
  149. }
  150.  
  151. startGame();
  152.  
  153. // ENEMY
  154. var Enemy = (function (parent) {
  155.     Enemy.extends(parent);
  156.  
  157.     function Enemy(name, x, y, radius) {
  158.         parent.call(this, name, x, y, radius);
  159.         //this.imagePath = './Images/enemy.png'; //imagePath
  160.         this.velocity = {
  161.             x: 0,
  162.             y: 0
  163.         },
  164.         this.startPosition = {
  165.             'x': x,
  166.             'y': y
  167.         }
  168.     }
  169.    
  170.     Enemy.prototype.move = function (direction, speed) {
  171.         switch(direction) {
  172.             case "left":
  173.                 this.x -= speed - this.velocity.x;
  174.                 break;
  175.             case "right":
  176.                 this.x += speed + this.velocity.x;
  177.                 break;
  178.             case "up":
  179.                 this.y -= speed - this.velocity.y;
  180.                 break;
  181.             case "down":
  182.                 this.y += speed + this.velocity.y;
  183.                 break;
  184.         }
  185.     }
  186.    
  187.     Enemy.prototype.reset = function() {
  188.         var resetSpeed = getEnemyResetSpeed(this, this.startPosition);
  189.    
  190.             resetEnemyX(this, resetSpeed, this.startPosition);
  191.         if (this.y > this.startPosition.y) {
  192.             resetEnemyY(this, resetSpeed, this.startPosition, -1);
  193.         } else {
  194.             resetEnemyY(this, resetSpeed, this.startPosition);
  195.         }
  196.     }
  197.  
  198.     // reset helper functions
  199.     function resetEnemyX(enemy, resetSpeed, startPosition) {
  200.         var enemyX = enemy.x + resetSpeed.x;           
  201.             enemy.x = enemyX > startPosition.x ? startPosition.x : enemyX;
  202.     }
  203.    
  204.     function resetEnemyY(enemy, resetSpeed, startPosition, direction) {
  205.         var direction = direction || 1,
  206.             enemyY = enemy.y + resetSpeed.y * direction;
  207.  
  208.             if (direction < 0){
  209.                 enemy.y = enemyY < startPosition.y ? startPosition.y : enemyY;
  210.             } else {
  211.                 enemy.y = enemyY > startPosition.y ? startPosition.y : enemyY;
  212.             }
  213.     }  
  214.    
  215.     function getEnemyResetSpeed(enemy, startPosition) {
  216.         var dx = enemy.x - startPosition.x,
  217.             dy = enemy.y - startPosition.y,
  218.             distance = Math.sqrt(dx * dx + dy * dy);
  219.  
  220.         distance = distance === 0 ? distance = 0.1 : distance;
  221.  
  222.         return {
  223.             x: Math.abs((dx / distance) * 2),
  224.             y: Math.abs((dy / distance) * 2)
  225.         }
  226.     }
  227.  
  228.     return Enemy;
  229. }(Participant));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement