Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- update: function() {
- // This method is called for every frame on each entity.
- // React to input, or compute the entity's AI here.
- moving = 0;
- // si up ou down ET rien d'autre on stocke la valeur.
- // 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
- if ((ig.input.state('up') || ig.input.state('down')) && !ig.input.state('left') && !ig.input.state('right')) {
- this.lastInput = ig.input.state('down') ? 'down' : 'up';
- }
- if ((ig.input.state('left') || ig.input.state('right')) && !ig.input.state('up') && !ig.input.state('down'))
- this.lastInput = ig.input.state('left') ? 'left' : 'right';
- // si haut + bas ou droite + gauche : stop animation.
- if (ig.input.state('up') && ig.input.state('down')) {
- moving = 0;
- this.vel.y = 0;
- }
- if (ig.input.state('left') && ig.input.state('right')) {
- moving = 0;
- this.vel.x = 0;
- }
- // gestion des cas d'input
- if (ig.input.state('up') && !ig.input.state('down')) {
- moving = 1;
- this.vel.y = -100;
- this.currentAnim = this.anims.up;
- }
- if (ig.input.state('down') && !ig.input.state('up')) {
- moving = 1;
- this.vel.y = 100;
- this.currentAnim = this.anims.down;
- }
- if (ig.input.state('left') && !ig.input.state('right')) {
- moving = 1;
- this.vel.x = -100;
- // conservation de la dernière direction
- if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
- if (ig.input.state('up'))
- this.currentAnim = this.anims.up;
- else
- this.currentAnim = this.anims.down;
- else
- this.currentAnim = this.anims.right;
- }
- if (ig.input.state('right') && !ig.input.state('left')) {
- moving = 1;
- this.vel.x = 100;
- // conservation de la dernière direction
- if (this.lastInput != 'left' && this.lastInput != 'right' && (ig.input.state('up') || ig.input.state('down')))
- if (ig.input.state('up'))
- this.currentAnim = this.anims.up;
- else
- this.currentAnim = this.anims.down;
- else
- this.currentAnim = this.anims.right;
- }
- if (!ig.input.state('left') && !ig.input.state('right'))
- this.vel.x = 0;
- if (!ig.input.state('up') && !ig.input.state('down'))
- this.vel.y = 0;
- // trouvé ici : http://gmc.yoyogames.com/index.php?showtopic=547900
- // calcul de la vitesse en diagonale
- if (this.vel.x != 0 && this.vel.y != 0)
- {
- this.vel.x = this.vel.x / Math.sqrt(2);
- this.vel.y = this.vel.y / Math.sqrt(2);
- }
- // si on bouge pas, on conserve la dernière direction regardée par le personnage
- if (!moving) {
- switch (this.lastInput) {
- case 'left':
- case 'right':
- this.currentAnim = this.anims.idleRight;
- break;
- case 'up':
- this.currentAnim = this.anims.idleUp;
- break;
- default:
- this.currentAnim = this.anims.idleDown;
- break;
- }
- }
- // animation de gauche = flip(animation de droite)
- if (this.vel.x < 0 || (!moving && this.lastInput == 'left'))
- this.currentAnim.flip.x = true;
- else
- this.currentAnim.flip.x = false;
- //network
- if (ig.input.state('up')) {
- this.padmap["up"] = 1;
- } else {
- this.padmap["up"] = 0;
- }
- if (ig.input.state('down')) {
- this.padmap["down"] = 1;
- } else {
- this.padmap["down"] = 0;
- }
- if (ig.input.state('left')) {
- this.padmap["left"] = 1;
- } else {
- this.padmap["left"] = 0;
- }
- if (ig.input.state('right')) {
- this.padmap["right"] = 1;
- } else {
- this.padmap["right"] = 0;
- }
- this.nettimer--;
- //console.log(this.id);
- if (this.nettimer == 0) {
- sock.emit('update', this.padmap, this.id);
- this.nettimer = 20;
- }
- // Call the parent update() method to move the entity
- // according to its physics
- this.parent();
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement