No problem:
box.js
ig.module(
'game.entities.box'
)
.requires(
'impact.entity',
'game.entities.playerbox'
)
.defines(function () {
EntityBox = ig.Entity.extend({
size: {
x: 36,
y: 36
},
offset: {
x: 2,
y: 6
},
maxVel: {
x: 200,
y: 230
},
zIndex: 2,
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.BOTH,
collides: ig.Entity.COLLIDES.FIXED,
animSheet: new ig.AnimationSheet('media/box.png', 40, 44),
init: function (x, y, settings) {
this.parent(x, y, settings);
this.addAnim('idle', 1, [0]);
ig.game.box = this;
},
facingCorrect: function (other) {
return ((!other.flip && other.pos.x < this.pos.x) || (other.flip && other.pos.x > this.pos.x));
},
update: function () {
// AND statement with facing check.
if (this.distanceTo(ig.game.player) < 38 && ig.input.pressed('pickup') && this.facingCorrect(ig.game.player) && this.onetick) {
ig.game.player.kill();
this.kill();
var left = (ig.game.player.pos.x < this.pos.x), // Check if the player is on the left side of the box
x = (!left || ig.input.state('forward') || ig.input.state('back')) ? this.pos.x + 11 : ig.game.player.pos.x;
ig.game.spawnEntity(EntityPlayerbox, x + 2, this.pos.y, {
flip: !left
}); // spawn with the flip setting.
};
this.onetick = true;
if (this.pos.x > ig.system.width) {
this.pos.x = 0;
this.currentAnim.draw(this.pos.x);
} else if (this.pos.x < -this.size.x / 2) {
this.pos.x = ig.system.width;
}
if (this.pos.y > ig.system.height) {
this.pos.y = 0;
} else if (this.pos.y < -this.size.y / 2) {
this.pos.y = ig.system.height;
}
this.parent();
}
});
});
player.js
ig.module(
'game.entities.player'
)
.requires(
'impact.entity'
)
.defines(function () {
EntityPlayer = ig.Entity.extend({
size: {
x: 34,
y: 56
},
offset: {
x: 1,
y: 14
},
maxVel: {
x: 200,
y: 200
},
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.PASSIVE,
animSheet: new ig.AnimationSheet('media/player.png', 36, 72),
flip: false,
accelGround: 400,
accelAir: 200,
jump: 200,
zIndex: 3,
init: function (x, y, settings) {
this.parent(x, y, settings);
this.addAnim('idle', 1, [0]);
this.addAnim('run', .11, [1, 2, 3, 4, 5, 6, 7, 8]);
this.addAnim('jump', 1, [9]);
this.addAnim('fall', 1, [9]);
this.jumpTimer = new ig.Timer();
this.jumpAttackVel = -35;
this.jumpSustainVel = -150;
ig.game.player = this;
},
update: function () {
var accel = this.standing ? this.accelGround : this.accelAir;
var playerSpeed = 170;
if (ig.input.state('forward')) {
this.vel.x = playerSpeed;
this.flip = false;
} else if (ig.input.state('back')) {
this.vel.x = -playerSpeed;
this.flip = true;
} else {
this.vel.x = 0;
}
if (ig.input.pressed('jump') && this.standing) {
this.jumpTimer.set(0);
this.vel.y = this.jumpAttackVel;
} else if (ig.input.state('jump') && this.jumpTimer.delta() < 0.25) {
this.vel.y = this.jumpSustainVel;;
} else {
this.accel.y = 0;
}
if (this.pos.x > ig.system.width) {
this.pos.x = 0;
} else if (this.pos.x < -this.size.x / 2) {
this.pos.x = ig.system.width;
}
if (this.pos.y > ig.system.height) {
this.pos.y = 0;
} else if (this.pos.y < -this.size.y / 2) {
this.pos.y = ig.system.height;
}
if (this.vel.y < 0) {
this.currentAnim = this.anims.jump;
} else if (this.vel.y > 0) {
this.currentAnim = this.anims.fall;
} else if (this.vel.x != 0) {
this.currentAnim = this.anims.run;
} else {
this.currentAnim = this.anims.idle;
}
this.currentAnim.flip.x = this.flip;
this.parent();
}
});
});
door.js
ig.module(
'game.entities.door'
)
.requires(
'impact.entity'
)
.defines(function () {
EntityDoor = ig.Entity.extend({
size: {
x: 24,
y: 74
},
offset: {
x: 6,
y: 28
},
animSheet: new ig.AnimationSheet('media/door.png', 36, 108),
zIndex: 1,
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.BOTH,
init: function (x, y, settings) {
this.parent(x, y, settings);
this.addAnim('closed', 1, [0]);
this.addAnim('open', 1, [7]);
this.addAnim('opening', .3, [1, 2, 3, 4, 5, 6], true);
this.addAnim('closing', .3, [7, 6, 5, 4, 3, 2, 1, 0], true);
},
update: function () {
this.collides = ig.Entity.COLLIDES.FIXED;
this.parent();
}
});
});