Advertisement
papyhardcore

Player.js

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