Advertisement
papyhardcore

Player.js

Aug 7th, 2014
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ig.module(
  2.         'game.entities.Player'
  3.         )
  4.         .requires(
  5.                 'impact.entity'
  6.                 )
  7.         .defines(function() {
  8.  
  9.  
  10.             EntityPlayer = ig.Entity.extend({
  11.                
  12.                 collides: ig.Entity.COLLIDES.ACTIVE,
  13.                 type : ig.Entity.TYPE.A,
  14.                 checkAgainst : ig.Entity.B,
  15.                 framerate: 0.1,
  16.                 size: {x: 16, y: 16},
  17.                 zIndex: 10,
  18.                 lastInput: 'none',
  19.                 // Load an animation sheet
  20.                 animUp: new ig.AnimationSheet('media/link/up.png', 16, 24),
  21.                 animDown: new ig.AnimationSheet('media/link/down.png', 16, 24),
  22.                 animRight: new ig.AnimationSheet('media/link/right.png', 16, 24),
  23.                 animSwordDown: new ig.AnimationSheet('media/link/sworddown.png', 34, 34),
  24.                 animSwordUp: new ig.AnimationSheet('media/link/swordup.png', 36, 36),
  25.                 animSwordRight: new ig.AnimationSheet('media/link/swordright.png', 36, 36),
  26.                 padmap: new Object(),
  27.                 nettimer: 50,
  28.                 id : 0,
  29.  
  30.                 //game play
  31.                 health : 3,
  32.                
  33.                 init: function(x, y, settings) {
  34.  
  35.                     this.parent(x, y, settings);
  36.                     this.anims.up = new ig.Animation(this.animUp, this.framerate, [0, 1, 2, 3, 4, 5, 6, 7]);
  37.                     this.anims.down = new ig.Animation(this.animDown, this.framerate, [0, 1, 2, 3, 4, 5, 6]);
  38.                     this.anims.right = new ig.Animation(this.animRight, this.framerate, [0, 1, 2, 3, 4, 5]);
  39.                     this.anims.idleDown = new ig.Animation(this.animDown, this.framerate, [0]);
  40.                     this.anims.idleRight = new ig.Animation(this.animRight, this.framerate, [0]);
  41.                     this.anims.idleUp = new ig.Animation(this.animUp, this.framerate, [0]);
  42.                     this.anims.swordDown = new ig.Animation(this.animSwordDown, 0.1, [0, 1, 2, 3, 4, 5]);
  43.                     this.anims.swordUp = new ig.Animation(this.animSwordUp, 1, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
  44.                     this.anims.swordRight = new ig.Animation(this.animSwordRight, this.framerate, [0, 1, 2, 3, 4, 5, 6, 7, 8]);
  45.  
  46.                    
  47.                     this.padmap["up"] = 0;
  48.                     this.padmap["right"] = 0;
  49.                     this.padmap["left"] = 0;
  50.                     this.padmap["down"] = 0;
  51.                    
  52.                     window.sock.emit('initplayer', this.pos.x, this.pos.y);
  53.                     self = this;
  54.  
  55.                     //reveice id frome server
  56.                     window.sock.on('send_id', function(id) {
  57.                         console.log('send_id called with id : ' + id);
  58.                         this.id = id;
  59.                        
  60.                     });
  61.                     console.log(this.id);
  62.  
  63.                 },
  64.                 update: function() {
  65.                     // This method is called for every frame on each entity.
  66.                     // React to input, or compute the entity's AI here.
  67.  
  68.  
  69.                     moving = 0;
  70.  
  71.                     // si up ou down ET rien d'autre on stocke la valeur.
  72.                     // 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
  73.                     if ((ig.input.state('up') || ig.input.state('down')) && !ig.input.state('left') && !ig.input.state('right')) {
  74.                         this.lastInput = ig.input.state('down') ? 'down' : 'up';
  75.                     }
  76.  
  77.                     if ((ig.input.state('left') || ig.input.state('right')) && !ig.input.state('up') && !ig.input.state('down'))
  78.                         this.lastInput = ig.input.state('left') ? 'left' : 'right';
  79.  
  80.  
  81.                     // si haut + bas ou droite + gauche : stop animation.
  82.                     if (ig.input.state('up') && ig.input.state('down')) {
  83.                         moving = 0;
  84.                         this.vel.y = 0;
  85.                     }
  86.                     if (ig.input.state('left') && ig.input.state('right')) {
  87.                         moving = 0;
  88.                         this.vel.x = 0;
  89.                     }
  90.  
  91.                     // gestion des cas d'input
  92.                     if (ig.input.state('up') && !ig.input.state('down')) {
  93.                         moving = 1;
  94.                         this.vel.y = -100;
  95.                         this.currentAnim = this.anims.up;
  96.                     }
  97.                     if (ig.input.state('down') && !ig.input.state('up')) {
  98.                         moving = 1;
  99.                         this.vel.y = 100;
  100.                         this.currentAnim = this.anims.down;
  101.                     }
  102.                     if (ig.input.state('left') && !ig.input.state('right')) {
  103.                         moving = 1;
  104.                         this.vel.x = -100;
  105.                         // conservation de la dernière direction
  106.                         if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
  107.                             if (ig.input.state('up'))
  108.                                 this.currentAnim = this.anims.up;
  109.                             else
  110.                                 this.currentAnim = this.anims.down;
  111.                         else
  112.                             this.currentAnim = this.anims.right;
  113.                     }
  114.                     if (ig.input.state('right') && !ig.input.state('left')) {
  115.                         moving = 1;
  116.                         this.vel.x = 100;
  117.                         // conservation de la dernière direction
  118.                         if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
  119.                             if (ig.input.state('up'))
  120.                                 this.currentAnim = this.anims.up;
  121.                             else
  122.                                 this.currentAnim = this.anims.down;
  123.                         else
  124.                             this.currentAnim = this.anims.right;
  125.                     }
  126.  
  127.                     if (!ig.input.state('left') && !ig.input.state('right'))
  128.                         this.vel.x = 0;
  129.  
  130.                     if (!ig.input.state('up') && !ig.input.state('down'))
  131.                         this.vel.y = 0;
  132.  
  133.                     // trouvé ici : http://gmc.yoyogames.com/index.php?showtopic=547900
  134.                     // calcul de la vitesse en diagonale
  135.                     if (this.vel.x != 0 && this.vel.y != 0)
  136.                     {
  137.                         this.vel.x = this.vel.x / Math.sqrt(2);
  138.                         this.vel.y = this.vel.y / Math.sqrt(2);
  139.                     }
  140.  
  141.                     // si on bouge pas, on conserve la dernière direction regardée par le personnage
  142.                     if (!moving) {
  143.                         switch (this.lastInput) {
  144.                             case 'left':
  145.                             case 'right':
  146.                                 this.currentAnim = this.anims.idleRight;
  147.                                 break;
  148.                             case 'up':
  149.                                 this.currentAnim = this.anims.idleUp;
  150.                                 break;
  151.                             default:
  152.                                 this.currentAnim = this.anims.idleDown;
  153.                                 break;
  154.                         }
  155.                     }
  156.  
  157.                     // animation de gauche = flip(animation de droite)
  158.                     if (this.vel.x < 0 || (!moving && this.lastInput == 'left'))
  159.                         this.currentAnim.flip.x = true;
  160.                     else
  161.                         this.currentAnim.flip.x = false;
  162.  
  163.  
  164.  
  165.                     //network
  166.                     if (ig.input.state('up')) {
  167.                         this.padmap["up"] = 1;
  168.                     } else {
  169.                         this.padmap["up"] = 0;
  170.                     }
  171.  
  172.                     if (ig.input.state('down')) {
  173.                         this.padmap["down"] = 1;
  174.                     } else {
  175.                         this.padmap["down"] = 0;
  176.                     }
  177.  
  178.                     if (ig.input.state('left')) {
  179.                         this.padmap["left"] = 1;
  180.                     } else {
  181.                         this.padmap["left"] = 0;
  182.                     }
  183.  
  184.                     if (ig.input.state('right')) {
  185.                         this.padmap["right"] = 1;
  186.                     } else {
  187.                         this.padmap["right"] = 0;
  188.                     }
  189.  
  190.  
  191.  
  192.                     this.nettimer--;
  193.  
  194.                     //console.log(this.id);
  195.                     if (this.nettimer == 0) {
  196.  
  197.                         window.sock.emit('update', this.padmap, this.id);
  198.                         this.nettimer = 20;
  199.                     }
  200.  
  201.                     // Call the parent update() method to move the entity
  202.                     // according to its physics
  203.                     this.parent();
  204.                 }
  205.             });
  206.  
  207.  
  208.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement