Skip to content

Instantly share code, notes, and snippets.

@morgondag
Last active December 20, 2015 00:39
Show Gist options
  • Save morgondag/a5a4742c0324a575365d to your computer and use it in GitHub Desktop.
Save morgondag/a5a4742c0324a575365d to your computer and use it in GitHub Desktop.
/* hack time!
https://dl.dropboxusercontent.com/u/3091827/player.zip
*/
ig.module(
'game.entities.player'
).requires(
'impact.entity'
).defines(function() {
EntityPlayer = ig.Entity.extend({
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.PASSIVE,
zIndex: 1,
minBounceVelocity: 40,
gravityFactor: 1,
size:{
x: 32
,y: 48
},
spawned: false,
standing: true,
init: function(x, y, settings) {
this.parent(x, y, settings);
this.maxVel.y = 500;
this.maxVel.x = 180;
this.jumpTimer = new ig.Timer();
this.jumpAttackVel = -40;
this.jumpSustainVel = -300;
},
update: function() {
this.parent();
if(!this.spawned){
this.spawned = true;
ig.game.spawnEntity(EntityPlayerHead, this.pos.x, this.pos.y,{target:this});
ig.game.spawnEntity(EntityPlayerArm, this.pos.x, this.pos.y,{target:this,arm:'left'});
ig.game.spawnEntity(EntityPlayerArm, this.pos.x, this.pos.y,{target:this,arm:'right'});
ig.game.spawnEntity(EntityPlayerFeet, this.pos.x, this.pos.y,{target:this});
ig.game.spawnEntity(EntityPlayerFeet2, this.pos.x, this.pos.y,{target:this});
ig.game.spawnEntity(EntityPlayerBody, this.pos.x, this.pos.y,{target:this});
ig.game.sortEntities();
this.spawned2 = true;
}
if(this.spawned2){
this.spawned2 = false;
}
if(ig.input.state('left')) {
this.accel.x = -5000;
} else if(ig.input.state('right')) {
this.accel.x = 5000;
} else {
this.accel.x = 0
this.vel.x = 0;
this.walking = false;
}
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;
}
}
});
EntityPlayerHead = ig.Entity.extend({
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NONE,
zIndex: 4,
minBounceVelocity: 1,
size:{
x: 32
,y: 48
},
animSheet: new ig.AnimationSheet('media/player/head.png', 32, 48),
init: function(x, y, settings) {
this.addAnim('idle1', 0.1, [0,1,1,1,0,0,0,0,0,0,0,0]);
this.addAnim('idle2', 0.1, [6,7,7,7,6,6,6,6,6,6,6,6]);
this.parent(x, y, settings);
var ransound = Math.floor(Math.random() * (2) + 1);
if(ransound == 1){
this.currentAnim = this.anims.idle1;
} else if(ransound == 2){
this.currentAnim = this.anims.idle2;
}
},
update: function() {
this.parent();
this.pos.x = this.target.pos.x;
this.pos.y = this.target.pos.y;
if(ig.input.state('right')) this.flipx = false;
if(ig.input.state('left')) this.flipx = true;
this.currentAnim.flip.x = this.flipx;
if(ig.input.state('down')) this.pos.y = this.pos.y +10;
}
});
EntityPlayerArm = ig.Entity.extend({
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NONE,
zIndex: 3,
minBounceVelocity: 1,
size:{
x: 32
,y: 48
},
arm:'',
animSheet: new ig.AnimationSheet('media/player/arm.png', 32,48),
up: true,
init: function(x, y, settings) {
this.addAnim('idle', 0.3, [0]);
this.parent(x, y, settings);
if(this.arm =='left'){
this.currentAnim.pivot.x =14;
this.currentAnim.pivot.y = 22;
} else {
this.currentAnim.flip.x = true;
this.currentAnim.pivot.x =18;
this.currentAnim.pivot.y = 22;
}
},
update: function() {
this.parent();
this.pos.x = this.target.pos.x;
this.pos.y = this.target.pos.y;
if(this.arm =='left'){
if(ig.input.state('right')){
if(this.up){
this.currentAnim.angle -=0.1;
this.currentAnim.flip.y = false;
}
if(!this.up){
this.currentAnim.angle +=0.1;
this.currentAnim.flip.y = false;
}
if(this.currentAnim.angle <= -1.2){
this.up = false;
}
if(this.currentAnim.angle >=0.5){
this.up = true;
}
}
else if(ig.input.state('left')){
} else {
this.currentAnim.angle =0;
this.up = true;
}
} else {
if(ig.input.state('right')){
this.zIndex = 0;
if(this.up){
this.currentAnim.angle -=0.05;
this.currentAnim.flip.y = false;
}
if(!this.up){
this.currentAnim.angle +=0.05;
this.currentAnim.flip.y = false;
}
if(this.currentAnim.angle <= -0.5){
this.up = false;
}
if(this.currentAnim.angle >= 0.5){
this.up = true;
}
}
else if(ig.input.state('left')){
} else {
this.currentAnim.angle =0;
this.up = true;
this.zIndex = 3;
}
}
if(ig.input.state('down')) this.pos.y = this.pos.y +7;
}
});
EntityPlayerBody = ig.Entity.extend({
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NONE,
zIndex: 2,
minBounceVelocity: 1,
size:{
x: 32
,y: 36
},
animSheet: new ig.AnimationSheet('media/player/body.png', 32, 36),
init: function(x, y, settings) {
this.addAnim('idle', 0.3, [0]);
this.addAnim('side', 0.3, [0]);
this.parent(x, y, settings);
},
update: function() {
this.parent();
this.pos.x = this.target.pos.x;
this.pos.y = this.target.pos.y;
if(ig.input.state('down')) this.pos.y = this.pos.y +3;
if(ig.input.state('right')){
this.flipx = false;
this.currentAnim = this.anims.side;
} else if(ig.input.state('left')){
this.flipx = true;
this.currentAnim = this.anims.side;
} else {
this.currentAnim = this.anims.idle;
}
this.currentAnim.flip.x = this.flipx;
}
});
EntityPlayerFeet = ig.Entity.extend({
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NONE,
zIndex: 1,
minBounceVelocity: 1,
size:{
x: 32
,y: 48
},
up:true,
animSheet: new ig.AnimationSheet('media/player/feet.png', 32, 48),
walk: new ig.Timer(),
init: function(x, y, settings) {
this.addAnim('idle', 0.3, [2]);
this.addAnim('idle2', 0.3, [0]);
this.addAnim('down', 0.3, [1]);
this.parent(x, y, settings);
this.currentAnim.pivot.y = 33;
this.walk.reset();
},
update: function() {
this.parent();
this.pos.x = this.target.pos.x;
this.pos.y = this.target.pos.y;
if(ig.input.pressed('right')){
this.walk.reset();
this.walk.unpause();
}
if(ig.input.state('right')){
this.currentAnim = this.anims.idle;
if(this.walk.delta() <= 0.2){
if(this.currentAnim.angle >= -45) this.currentAnim.angle -=0.1;
} else if(this.walk.delta() <= 0.4){
if(this.currentAnim.angle <= 0) this.currentAnim.angle +=0.1;
} else if(this.walk.delta() <= 0.6){
if(this.currentAnim.angle <= 45) this.currentAnim.angle +=0.1;
} else if(this.walk.delta() <= 0.8){
if(this.currentAnim.angle >= 0) this.currentAnim.angle -=0.1;
} else if(this.walk.delta() <= 1){
this.walk.reset();
}
} else {
this.currentAnim.angle = 0;
this.up = true;
this.walk.pause();
this.currentAnim = this.anims.idle2;
}
}
});
EntityPlayerFeet2 = ig.Entity.extend({
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.NONE,
zIndex: 0,
minBounceVelocity: 1,
size:{
x: 32
,y: 48
},
up:true,
animSheet: new ig.AnimationSheet('media/player/feet.png', 32, 48),
walk: new ig.Timer(),
init: function(x, y, settings) {
this.addAnim('idle', 0.3, [3]);
this.addAnim('idle2', 0.3, [6]);
this.addAnim('down', 0.3, [1]);
this.parent(x, y, settings);
this.currentAnim.pivot.y = 33;
this.walk.reset();
},
update: function() {
this.parent();
this.pos.x = this.target.pos.x;
this.pos.y = this.target.pos.y;
if(ig.input.pressed('right')){
this.walk.reset();
this.walk.unpause();
}
if(ig.input.state('right')){
this.currentAnim = this.anims.idle;
this.flipx = false;
if(this.walk.delta() <= 0.2){
if(this.currentAnim.angle <= 45) this.currentAnim.angle +=0.1;
} else if(this.walk.delta() <= 0.4){
if(this.currentAnim.angle >= 0) this.currentAnim.angle -=0.1;
} else if(this.walk.delta() <= 0.6){
if(this.currentAnim.angle >= -45) this.currentAnim.angle -=0.1;
} else if(this.walk.delta() <= 0.8){
if(this.currentAnim.angle <= 0) this.currentAnim.angle +=0.1;
} else if(this.walk.delta() <= 1){
this.walk.reset();
}
} else {
this.currentAnim.angle = 0;
this.up = true;
this.walk.pause();
this.currentAnim = this.anims.idle2;
}
this.currentAnim.flip.x = this.flipx;
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment