Advertisement
jerone

Untitled

Apr 5th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Q.component('WalkAround', {
  2.     added: function () {
  3.         var entity = this.entity;
  4.         Q._defaults(entity.p, {
  5.             vx: 0,
  6.             vy: 0,
  7.             direction: 2,
  8.             collisionMask: Q.SPRITE_DEFAULT
  9.         });
  10.         entity.on('step', this, "step");
  11.         entity.on('hit', this, 'collision');
  12.     },
  13.  
  14.     collision: function (col, last) {
  15.         var entity = this.entity,
  16.             p = entity.p;
  17.         if (col.normalX > 0) { // bump left;
  18.             p.vy = -Math.abs(p.vy);
  19.             p.vx = -Math.abs(p.vx);
  20.         }
  21.         if (col.normalY > 0) { // bump top;
  22.             p.vy = -Math.abs(p.vy);
  23.             p.vx = Math.abs(p.vx);
  24.         }
  25.         if (col.normalX < 0) { // bump right;
  26.             p.vy = Math.abs(p.vy);
  27.             p.vx = Math.abs(p.vx);
  28.         }
  29.         if (col.normalY < 0) { // bump bottom;
  30.             p.vy = Math.abs(p.vy);
  31.             p.vx = -Math.abs(p.vx);
  32.         }
  33.     },
  34.  
  35.     step: function (dt) {
  36.         var p = this.entity.p;
  37.         p.x += p.vx * dt;
  38.         p.y += p.vy * dt;
  39.         this.entity.stage.collide(this.entity);
  40.     }
  41. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement