Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Q.component('WalkAround', {
- added: function () {
- var entity = this.entity;
- Q._defaults(entity.p, {
- vx: 0,
- vy: 0,
- direction: 2,
- collisionMask: Q.SPRITE_DEFAULT
- });
- entity.on('step', this, "step");
- entity.on('hit', this, 'collision');
- },
- collision: function (col, last) {
- var entity = this.entity,
- p = entity.p;
- if (col.normalX > 0) { // bump left;
- p.vy = -Math.abs(p.vy);
- p.vx = -Math.abs(p.vx);
- }
- if (col.normalY > 0) { // bump top;
- p.vy = -Math.abs(p.vy);
- p.vx = Math.abs(p.vx);
- }
- if (col.normalX < 0) { // bump right;
- p.vy = Math.abs(p.vy);
- p.vx = Math.abs(p.vx);
- }
- if (col.normalY < 0) { // bump bottom;
- p.vy = Math.abs(p.vy);
- p.vx = -Math.abs(p.vx);
- }
- },
- step: function (dt) {
- var p = this.entity.p;
- p.x += p.vx * dt;
- p.y += p.vy * dt;
- this.entity.stage.collide(this.entity);
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement