Skip to content

Instantly share code, notes, and snippets.

@auz1111
Created June 16, 2012 17:56
Show Gist options
  • Save auz1111/2942094 to your computer and use it in GitHub Desktop.
Save auz1111/2942094 to your computer and use it in GitHub Desktop.
ImpactJS Player Entity for 2.5D Scrolling Platformer. Turn gravity off in main.js.
ig.module (
'game.entities.player'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityPlayer = ig.Entity.extend({
animSheet: new ig.AnimationSheet( 'media/cali.png', 88, 132 ),
health: 10,
size: {x: 88, y: 132},
//Not sure how to use this yet
//offset: {x: 0, y: 82},
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.BOTH,
collides: ig.Entity.COLLIDES.PASSIVE,
speed:140,
gravityFactor:0,
//Physics
maxVel: {x: 100, y: 300},
friction: {x: 600, y: 0},
accelGround: 400,
accelAir: 400,
jump: 140,
direction: "right",
flip: false,
isDead: false,
punchDuration: .1,
kickDuration: .5,
punchDamage: 50,
kickDamage: 100,
attacking: false,
isPunching: false,
isKicking: false,
action: "idle",
jumping: false,
move: {x: 0, y: 0},
jumpY: 0,
cache: {x: 0, y: 0},
idle: {
towards: { size: {x: 38, y: 6}, offset: {x: 0, y: 82} },
backwards: { size: {x: 38, y: 6}, offset: {x: 27, y: 82} }
},
run: {
towards: { size: {x: 38, y: 6}, offset: {x: 0, y: 82} },
backwards: { size: {x: 38, y: 6}, offset: {x: 27, y: 82} }
},
punch: {
towards: { size: {x: 60}, offset: {x: 5} },
backwards: { size: {x: 60}, offset: {x: -7} }
},
//http://impactjs.com/forums/help/sound-bug-on-chrome/page/1
jumpSFX: new ig.Sound('media/sounds/jump.*' ),
shootSFX: new ig.Sound('media/sounds/shoot.*' ),
deathSFX: new ig.Sound('media/sounds/death.*' ),
//initialize weapons
weapon: 0,
totalWeapons: 2,
activeWeapon: "EntityBullet",
//For invincibility right after spawn
invincible: true,
invincibleDelay: 2,
invincibleTimer:null,
makeInvincible: function(){
this.invincible = true;
this.invincibleTimer.reset();
},
//Because the player is invincible he is invisible in Weltmeister so put a red box around him
_wmDrawBox: true,
_wmBoxColor: 'rgba(255, 0, 0, 0.7)',
receiveDamage: function(amount, from){
if(this.invincible) return;
this.parent(amount, from);
},
//for multiplayer play
nettimer: 10,
name: "player",
gamename: playername,
init: function( x, y, settings ) {
//For respown
this.startPosition = {x:x,y:y};
this.parent( x, y, settings );
//for multiplayer
console.log("Call to Initialize Player from player.js...");
socket.emit('initializeplayer', this.gamename);
//This does the work when you want the animation to be different for different weapons
this.setupAnimation(this.weapon);
//for invinicibilty
this.invincibleTimer = new ig.Timer();
this.makeInvincible();
},
setupAnimation: function(offset){
offset = offset * 10;
// Add the animations
this.addAnim( 'up', .21, [9,10,11] );
this.addAnim( 'upleft', .21, [3,4,5] );
this.addAnim( 'down', .21, [0,1,2] );
this.addAnim( 'downleft', .21, [3,4,5] );
this.addAnim( 'left', .21, [3,4,5] );
this.addAnim( 'right', .21, [6,7,8] );
this.addAnim( 'idleup', 0.1, [10] );
this.addAnim( 'idledown', 0.1, [1] );
this.addAnim( 'idleleft', 0.1, [4] );
this.addAnim( 'idleright', 0.1, [7] );
this.addAnim('jump', 1, [9+offset]);
this.addAnim('fall', 0.4, [6+offset,7+offset]);
this.currentAnim = this.anims.idledown;
},
update: function() {
var currentanimation;
var accel = this.standing ? this.accelGround : this.accelAir;
//console.log("Acceleration: " + accel);
//console.log("Velocity X: " + this.vel.x);
//console.log("Velocity Y: " + this.vel.y);
//////////////////////////////////////////////
// Keep the player bound to narrow Y plane
////////////////////////////////////////////////
if (this.pos.y <= 440){
this.pos.y = 440; // set the player to the top most possible value
}
if (this.pos.y >= 680){
this.pos.y = 680; // set the player to the lowest most current value
}
if (ig.input.pressed('punch') && !this.attacking && !this.jumping) {
this.vel.x = this.vel.y = 0;
this.currentAnim = this.anims.punch;
this.currentAnim.rewind();
this.currentAnim.frame = 0;
this.action = "punch";
this.isPunching = true;
this.attacking = true;
ig.game.sounds.bam.play();
// ig.game.sounds.swing.play();
} else if (ig.input.released('punch')) {
this.isPunching = false;
this.attacking = false;
}else{
/////////////////////////////////////////////////////
// JUMP
////////////////////////////////////////////////////
if (ig.input.pressed('jump') && !this.jumping){
this.gravityFactor=1;
this.jumpY=this.pos.y;
console.log("Position Y: " + this.pos.y);
this.vel.y = -this.jump;
this.cache.y = this.pos.y;
this.jumping = true;
console.log("Jump Velocity Y: " + this.vel.y);
console.log("Standing after jump: " + this.standing);
this.currentAnim = this.anims.jump;
this.jumpSFX.volume = 0.02;
this.jumpSFX.play();
//MOVE LEFT
}else if( ig.input.state('left') && ismove != 1 && ismove != 2 && ismove != 4) {
//Check if jumping and if done with jump then quit jumping.
if ( this.jumping && ( this.pos.y > this.jumpY ) ){
console.log("DONE JUMPING WHILE MOVING Velocity Y: " + this.vel.y);
console.log("DONE JUMPING WHILE MOVING Pos Y: " + this.pos.y);
console.log("DONE JUMPING WHILE MOVING Cache Y: " + this.cache.y);
this.jumping = false;
this.standing = true;
this.gravityFactor=0;
this.vel.y = 0;
}
this.vel.x = -this.speed;
ismove = 3;
this.direction = 3;
this.currentAnim = this.anims.left;
//ig.game.messagebox = ig.game.messagebox + "You pressed left \n";
currentanimation = 3;
//MOVE RIGHT
}else if( ig.input.state('right') && ismove != 1 && ismove != 3 && ismove != 4) {
//Check if jumping and if done with jump then quit jumping.
if ( this.jumping && ( this.pos.y > this.jumpY ) ){
console.log("DONE JUMPING WHILE MOVING Velocity Y: " + this.vel.y);
console.log("DONE JUMPING WHILE MOVING Pos Y: " + this.pos.y);
console.log("DONE JUMPING WHILE MOVING Cache Y: " + this.cache.y);
this.jumping = false;
this.standing = true;
this.gravityFactor=0;
this.vel.y = 0;
}
this.vel.x = +this.speed;
ismove = 2;
this.direction = 2;
this.currentAnim = this.anims.right;
//ig.game.messagebox = ig.game.messagebox + "You pressed right \n";
currentanimation = 4;
//////////////////////////////
// MOVE UP
/////////////////////////////
}else if( ig.input.state('up') && ismove != 3 && ismove != 2 && ismove != 4 && !this.jumping) {
this.vel.y = -this.speed;
this.gravityFactor=0;
//console.log("gravityFactor on UP: " + this.gravityFactor);
ismove = 1;
this.direction = 1;
this.currentAnim = this.anims.up;
//track a separate move.y variable and apply it yourself directly to pos.y
this.pos.y += this.move.y * ig.system.tick;
//ig.game.messagebox = ig.game.messagebox + "You pressed up \n";
currentanimation = 3;
//console.log("Input UP Velocity Y: " + this.vel.y);
console.log("UP Pos Y: " + this.pos.y);
}else if( ig.input.state('down') && ismove != 1 && ismove != 2 && ismove != 3 && !this.jumping) {
this.vel.y = +this.speed;
ismove = 4;
this.direction = 4;
this.currentAnim = this.anims.down;
//ig.game.messagebox = ig.game.messagebox + "You pressed down \n";
currentanimation = 3;
console.log("DOWN Pos Y: " + this.pos.y);
}else {
//if falling back down from jump and on ground plane
if (this.jumping ){
console.log("JUMPING Velocity Y: " + this.vel.y);
//this.pos.y = this.cache.y;
//this.vel.y = 0;
if ( ( this.vel.y==0 ) || ( this.pos.y > this.jumpY ) ){
console.log("DONE JUMPING Velocity Y: " + this.vel.y);
console.log("DONE JUMPING Pos Y: " + this.pos.y);
console.log("DONE JUMPING Cache Y: " + this.cache.y);
this.jumping = false;
this.standing = true;
this.gravityFactor=0;
}
//if not jumping and on ground plane
}else if (!this.jumping && ( this.pos.y + this.size.y > 250 ) && (this.pos.y + this.size.y < 300) ){
this.standing = true;
this.vel.y = 0;
console.log("Standing: " + this.standing);
}else{
//////////////////////////////////////////////////////////////////////////////
//Turning this off is allowing the player to jump, but they lose the ability to go up, and the player still slides down the screen until it hits the collision layer drawn in weltmeister due to gravity.
////////////////////////////////////////////////////////////////////////////
//console.log("Velocity Y B: " + this.vel.y);
this.vel.y = 0;
//console.log("Velocity Y A: " + this.vel.y);
}
this.vel.x = 0;
ismove = 0;
if( this.direction == 4 ){
this.currentAnim = this.anims.idledown;
currentanimation = 7;
}
if( this.direction == 3 ){
this.currentAnim = this.anims.idleleft;
currentanimation = 5;
}
if( this.direction == 2 ){
this.currentAnim = this.anims.idleright;
currentanimation = 2;
}
if( this.direction == 1 ){
this.currentAnim = this.anims.idleup;
currentanimation = 6;
}
// throw slime grenade
if( ig.input.pressed('throwGrenade') ) {
ig.game.spawnEntity( EntitySlimeGrenade, this.pos.x, this.pos.y, {flip:this.flip} );
this.shootSFX.volume = 0.04;
this.shootSFX.play();
}
// shoot
if( ig.input.pressed('shoot') ) {
ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
}
if( ig.input.pressed('switch')){
this.weapon ++;
if(this.weapon >= this.totalWeapons) this.weapon = 0;
switch(this.weapon){
case(0): this.activeWeapon = "EntityBullet";
break;
case(1): this.activeWeapon = "EntitySlimeGrenade";
break;
}
this.setupAnimation(this.weapon);
}
//for invincibility
if( this.invincibleTimer.delta() > this.invincibleDelay ) {
this.invincible = false;
this.currentAnim.alpha = 1;
}
}
}
//for multiplayer
if(this.nettimer < 1){
this.nettimer = 5;
//console.log("Current Animation: " + currentanimation);
if(currentanimation==1){
socket.emit('recievedata',this.pos.x,this.pos.y,1,this.gamename);
}else{
socket.emit('recievedata',this.pos.x,this.pos.y,currentanimation,this.gamename);
}
}
this.nettimer = this.nettimer - 1;
this.parent();
},
draw: function(){
if(this.invincible) this.currentAnim.alpha = this.invincibleTimer.delta()/this.invincibleDelay * 1 ;
this.parent();
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment