Advertisement
papyhardcore

Untitled

Aug 5th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. update: function() {
  2.                     // This method is called for every frame on each entity.
  3.                     // React to input, or compute the entity's AI here.
  4.  
  5.  
  6.                     moving = 0;
  7.  
  8.                     // si up ou down ET rien d'autre on stocke la valeur.
  9.                     // objectif : garder l'animation précédente pour qu'en diagonale, le personnage ne soit pas toujours à droite ou à gauche en animation s'il a commencé par haut ou bas
  10.                     if ((ig.input.state('up') || ig.input.state('down')) && !ig.input.state('left') && !ig.input.state('right')) {
  11.                         this.lastInput = ig.input.state('down') ? 'down' : 'up';
  12.                     }
  13.  
  14.                     if ((ig.input.state('left') || ig.input.state('right')) && !ig.input.state('up') && !ig.input.state('down'))
  15.                         this.lastInput = ig.input.state('left') ? 'left' : 'right';
  16.  
  17.  
  18.                     // si haut + bas ou droite + gauche : stop animation.
  19.                     if (ig.input.state('up') && ig.input.state('down')) {
  20.                         moving = 0;
  21.                         this.vel.y = 0;
  22.                     }
  23.                     if (ig.input.state('left') && ig.input.state('right')) {
  24.                         moving = 0;
  25.                         this.vel.x = 0;
  26.                     }
  27.  
  28.                     // gestion des cas d'input
  29.                     if (ig.input.state('up') && !ig.input.state('down')) {
  30.                         moving = 1;
  31.                         this.vel.y = -100;
  32.                         this.currentAnim = this.anims.up;
  33.                     }
  34.                     if (ig.input.state('down') && !ig.input.state('up')) {
  35.                         moving = 1;
  36.                         this.vel.y = 100;
  37.                         this.currentAnim = this.anims.down;
  38.                     }
  39.                     if (ig.input.state('left') && !ig.input.state('right')) {
  40.                         moving = 1;
  41.                         this.vel.x = -100;
  42.                         // conservation de la dernière direction
  43.                         if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
  44.                             if (ig.input.state('up'))
  45.                                 this.currentAnim = this.anims.up;
  46.                             else
  47.                                 this.currentAnim = this.anims.down;
  48.                         else
  49.                             this.currentAnim = this.anims.right;
  50.                     }
  51.                     if (ig.input.state('right') && !ig.input.state('left')) {
  52.                         moving = 1;
  53.                         this.vel.x = 100;
  54.                         // conservation de la dernière direction
  55.                         if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
  56.                             if (ig.input.state('up'))
  57.                                 this.currentAnim = this.anims.up;
  58.                             else
  59.                                 this.currentAnim = this.anims.down;
  60.                         else
  61.                             this.currentAnim = this.anims.right;
  62.                     }
  63.  
  64.                     if (!ig.input.state('left') && !ig.input.state('right'))
  65.                         this.vel.x = 0;
  66.  
  67.                     if (!ig.input.state('up') && !ig.input.state('down'))
  68.                         this.vel.y = 0;
  69.  
  70.                     // trouvé ici : http://gmc.yoyogames.com/index.php?showtopic=547900
  71.                     // calcul de la vitesse en diagonale
  72.                     if (this.vel.x != 0 && this.vel.y != 0)
  73.                     {
  74.                         this.vel.x = this.vel.x / Math.sqrt(2);
  75.                         this.vel.y = this.vel.y / Math.sqrt(2);
  76.                     }
  77.  
  78.                     // si on bouge pas, on conserve la dernière direction regardée par le personnage
  79.                     if (!moving) {
  80.                         switch (this.lastInput) {
  81.                             case 'left':
  82.                             case 'right':
  83.                                 this.currentAnim = this.anims.idleRight;
  84.                                 break;
  85.                             case 'up':
  86.                                 this.currentAnim = this.anims.idleUp;
  87.                                 break;
  88.                             default:
  89.                                 this.currentAnim = this.anims.idleDown;
  90.                                 break;
  91.                         }
  92.                     }
  93.  
  94.                     // animation de gauche = flip(animation de droite)
  95.                     if (this.vel.x < 0 || (!moving && this.lastInput == 'left'))
  96.                         this.currentAnim.flip.x = true;
  97.                     else
  98.                         this.currentAnim.flip.x = false;
  99.  
  100.  
  101.  
  102.                     //network
  103.                     if (ig.input.state('up')) {
  104.                         this.padmap["up"] = 1;
  105.                     } else {
  106.                         this.padmap["up"] = 0;
  107.                     }
  108.  
  109.                     if (ig.input.state('down')) {
  110.                         this.padmap["down"] = 1;
  111.                     } else {
  112.                         this.padmap["down"] = 0;
  113.                     }
  114.  
  115.                     if (ig.input.state('left')) {
  116.                         this.padmap["left"] = 1;
  117.                     } else {
  118.                         this.padmap["left"] = 0;
  119.                     }
  120.  
  121.                     if (ig.input.state('right')) {
  122.                         this.padmap["right"] = 1;
  123.                     } else {
  124.                         this.padmap["right"] = 0;
  125.                     }
  126.  
  127.  
  128.  
  129.                     this.nettimer--;
  130.  
  131.                     //console.log(this.id);
  132.                     if (this.nettimer == 0) {
  133.  
  134.                         sock.emit('update', this.padmap, this.id);
  135.                         this.nettimer = 20;
  136.                     }
  137.  
  138.                     // Call the parent update() method to move the entity
  139.                     // according to its physics
  140.                     this.parent();
  141.                 }
  142.             });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement