Hi,
and thanks again for giving us the function.
But it does not work. Since You wrote "bullet.js", I ask myself if you thought we have a bullet.js entinity function, since you wrote it in your post.
we don't have a bullet.js
I have no idea how to build it in, I post my whole main.js and (entinity) player.js:
ig.module(
'game.main'
)
.requires(
'impact.game',
'game.levels.strandhuette',
'impact.font',
'impact.debug.debug'
)
.defines(function(){
MyGame = ig.Game.extend({
gravity: 300,
instructText: new ig.Font( 'media/04b03.font.png' ),
statText: new ig.Font( 'media/04b03.font.png' ),
showStats: false,
statMatte: new ig.Image('media/stat-matte.png'),
levelTimer: new ig.Timer(),
levelExit: null,
stats: {time: 0, kills: 0, deaths: 0},
lives: 3,
lifeSprite: new ig.Image('media/life-sprite.png'),
init: function() {
this.loadLevel( LevelStrandhuette );
// Bind keys
ig.input.bind( ig.KEY.LEFT_ARROW, 'left' );
ig.input.bind( ig.KEY.UP_ARROW, 'up' );
ig.input.bind( ig.KEY.DOWN_ARROW, 'down' );
ig.input.bind( ig.KEY.RIGHT_ARROW, 'right' );
ig.input.bind( ig.KEY.X, 'jump' );
ig.input.bind( ig.KEY.C, 'shoot' );
ig.input.bind( ig.KEY.TAB, 'switch' );
ig.input.bind( ig.KEY.SPACE, 'continue' );
ig.music.add( 'media/sounds/Ruf die Polizei (Album Version).*' );
ig.music.volume = 0.5;
ig.music.play();
},
loadLevel: function( data ) {
this.stats = {time: 0, kills: 0, deaths: 0};
this.parent(data);
this.levelTimer.reset();
},
update: function() {
// screen follows the player
var player = this.getEntitiesByType( EntityPlayer )[0];
if( player ) {
this.screen.x = player.pos.x - ig.system.width/2;
this.screen.y = player.pos.y - ig.system.height/2;
if(player.accel.x > 0 && this.instructText)
this.instructText = null;
}
// Update all entities and BackgroundMaps
if(!this.showStats){
this.parent();
}else{
if(ig.input.state('continue')){
this.showStats = false;
this.levelExit.nextLevel();
this.parent();
}
}
},
draw: function() {
// Draw all entities and backgroundMaps
this.parent();
if(this.instructText){
var x = ig.system.width/2,
y = ig.system.height - 10;
this.instructText.draw( 'Left/Right Moves, X Jumps, C Fires & Tab Switches Weapons.', x, y, ig.Font.ALIGN.CENTER );
}
if(this.showStats){
this.statMatte.draw(0,0);
var x = ig.system.width/2;
var y = ig.system.height/2 - 20;
this. statText.draw('Level Complete', x, y, ig.Font.ALIGN.CENTER);
this. statText.draw('Time: '+this.stats.time, x, y+30, ig.Font.ALIGN.CENTER);
this. statText.draw('Kills: '+this.stats.kills, x, y+40, ig.Font.ALIGN.CENTER);
this. statText.draw('Deaths: '+this.stats.deaths, x, y+50, ig.Font.ALIGN.CENTER);
this. statText.draw('Press Spacebar to continue.', x, ig.system.height - 10, ig.Font.ALIGN.CENTER);
}
this.statText.draw("Lives", 5,5);
for(var i=0; i < this.lives; i++)
this.lifeSprite.draw(((this.lifeSprite.width + 2) * i)+5, 15);
},
toggleStats: function(levelExit){
this.showStats = true;
this.stats.time = Math.round(this.levelTimer.delta());
this.levelExit = levelExit;
},
gameOver: function(){
ig.finalStats = ig.game.stats;
ig.system.setGame(GameOverScreen);
}
});
StartScreen = ig.Game.extend({
instructText: new ig.Font( 'media/04b03.font.png' ),
background: new ig.Image('media/screen-bg.png'),
mainCharacter: new ig.Image('media/screen-main-character.png'),
title: new ig.Image('media/game-title.png'),
init: function() {
ig.input.bind( ig.KEY.SPACE, 'start');
},
update: function() {
if(ig.input.pressed ('start')){
ig.system.setGame(MyGame)
}
this.parent();
},
draw: function() {
this.parent();
this.background.draw(0,0);
this.mainCharacter.draw(0,0);
this.title.draw(ig.system.width - this.title.width, 0);
var x = ig.system.width/2,
y = ig.system.height - 10;
this.instructText.draw( 'Press Spacebar To Start', x+40, y, ig.Font.ALIGN.CENTER );
}
});
GameOverScreen = ig.Game.extend({
instructText: new ig.Font( 'media/04b03.font.png' ),
background: new ig.Image('media/screen-bg.png'),
gameOver: new ig.Image('media/game-over.png'),
stats: {},
init: function() {
ig.input.bind( ig.KEY.SPACE, 'start');
this.stats = ig.finalStats;
},
update: function() {
if(ig.input.pressed('start')){
ig.system.setGame(StartScreen)
}
this.parent();
},
draw: function() {
this.parent();
this.background.draw(0,0);
var x = ig.system.width/2;
var y = ig.system.height/2 - 20;
this.gameOver.draw(x - (this.gameOver.width * .5), y - 30);
var score = (this.stats.kills * 100) - (this.stats.deaths * 50);
this.instructText.draw('Total Kills: '+this.stats.kills, x, y+30, ig.Font.ALIGN.CENTER);
this.instructText.draw('Total Deaths: '+this.stats.deaths, x, y+40, ig.Font.ALIGN.CENTER);
this.instructText.draw('Score: '+score, x, y+50, ig.Font.ALIGN.CENTER);
this.instructText.draw('Press Spacebar To Continue.', x, ig.system.height - 10, ig.Font.ALIGN.CENTER);
}
});
if( ig.ua.mobile ) {
// Disable sound for all mobile devices
ig.Sound.enabled = false;
}
// Start the Game with 60fps, a resolution of 320x240, scaled
// up by a factor of 2
ig.main( '#canvas', StartScreen, 60, 320, 240, 2 );
});
player.js
ig.module(
'game.entities.player'
)
.requires(
'impact.entity',
'impact.sound'
)
.defines(function(){
EntityPlayer = ig.Entity.extend({
animSheet: new ig.AnimationSheet( 'media/player.png', 16, 16 ),
size: {x: 8, y:14},
offset: {x: 4, y: 2},
flip: false,
maxVel: {x: 100, y: 150},
friction: {x: 600, y: 0},
accelGround: 400,
accelAir: 200,
jump: 200,
angle: 0,
type: ig.Entity.TYPE.A,
checkAgainst: ig.Entity.TYPE.NONE,
collides: ig.Entity.COLLIDES.PASSIVE,
weapon: 0,
totalWeapons: 2,
activeWeapon: "EntityBullet",
startPosition: null,
invincible: true,
invincibleDelay: 2,
invincibleTimer:null,
_wmDrawBox: true,
_wmBoxColor: 'rgba(255, 0, 0, 0.7)',
jumpSFX: new ig.Sound( 'media/sounds/jump.*' ),
shootSFX: new ig.Sound( 'media/sounds/shoot.*' ),
deathSFX: new ig.Sound( 'media/sounds/death.*' ),
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.setupAnimation(this.weapon);
this.startPosition = {x:x,y:y};
this.invincibleTimer = new ig.Timer();
this.makeInvincible();
},
setupAnimation: function(offset){
offset = offset * 10;
this.addAnim('idle', 1, [0+offset]);
this.addAnim('run', .07, [0+offset,1+offset,2+offset,3+offset,4+offset,5+offset]);
this.addAnim('jump', 1, [9+offset]);
this.addAnim('fall', 0.4, [6+offset,7+offset]);
},
makeInvincible: function(){
this.invincible = true;
this.invincibleTimer.reset();
},
update: function() {
// move left or right
var accel = this.standing ? this.accelGround : this.accelAir;
if( ig.input.state('left') ) {
this.accel.x = -accel;
this.flip = true;
}else if( ig.input.state('right') ) {
this.accel.x = accel;
this.flip = false;
}else if( ig.input.state('up') ) {
this.angle += 13;
ig.log('')
}else if( ig.input.state('down') ) {
this.angle -= 13;
}else{
this.accel.x = 0;
}
this.currentAnim.angle = this.angle*(Math.PI/180);
// jump
if( this.standing && ig.input.pressed('jump') ) {
this.vel.y = -this.jump;
this.jumpSFX.play();
}
// shoot
if( ig.input.pressed('shoot') ) {
ig.game.spawnEntity( this.activeWeapon, this.pos.x, this.pos.y, {flip:this.flip} );
this.shootSFX.play();
}
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 = "EntityGrenade";
break;
}
this.setupAnimation(this.weapon);
}
// set the current animation, based on the player's speed
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;
if( this.invincibleTimer.delta() > this.invincibleDelay ) {
this.invincible = false;
this.currentAnim.alpha = 1;
}
// move!
this.parent();
},
kill: function(){
this.deathSFX.play();
this.parent();
ig.game.respawnPosition = this.startPosition;
ig.game.spawnEntity(EntityDeathExplosion, this.pos.x, this.pos.y, {callBack:this.onDeath} );
},
onDeath: function(){
ig.game.stats.deaths ++;
ig.game.lives --;
if(ig.game.lives < 0){
ig.game.gameOver();
}else{
ig.game.spawnEntity( EntityPlayer, ig.game.respawnPosition.x, ig.game.respawnPosition.y);
}
},
receiveDamage: function(amount, from){
if(this.invincible)
return;
this.parent(amount, from);
},
draw: function(){
if(this.invincible)
this.currentAnim.alpha = this.invincibleTimer.delta()/this.invincibleDelay * 1 ;
this.parent();
}
});
EntityBullet = ig.Entity.extend({
size: {x: 5, y: 3},
animSheet: new ig.AnimationSheet( 'media/bullet.png', 5, 3 ),
accelGround: 40,
accelAir: 20,
angle: 0,
//sinus: math.sin(),
//cosinus: math.cos(),
maxVel: {x: 200, y: 0},
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.PASSIVE,
init: function( x, y, settings ) {
this.parent( x + (settings.flip ? -4 : 8) , y+8, settings );
this.vel.x = this.accel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
ig.log( 'x vel: ', this.vel.x );
this.addAnim( 'idle', 0.2, [0] );
},
handleMovementTrace: function( res ) {
this.parent( res );
if( res.collision.x || res.collision.y ){
this.kill();
}
},
check: function( other ) {
other.receiveDamage( 3, this );
this.kill();
},
update: function() {
var accel = this.standing ? this.accelGround : this.accelAir;
if( ig.input.state('up') ) {
this.angle += 13;
}else if( ig.input.state('down') ) {
this.angle -= 13;
}else{
this.accel.x = 0;
}
this.currentAnim.angle = this.angle*(Math.PI/180);
}
});
EntityGrenade = ig.Entity.extend({
size: {x: 4, y: 4},
offset: {x: 2, y: 2},
animSheet: new ig.AnimationSheet( 'media/grenade.png', 8, 8 ),
type: ig.Entity.TYPE.NONE,
checkAgainst: ig.Entity.TYPE.BOTH,
collides: ig.Entity.COLLIDES.PASSIVE,
maxVel: {x: 200, y: 200},
bounciness: 0.6,
bounceCounter: 0,
init: function( x, y, settings ) {
this.parent( x + (settings.flip ? -4 : 7), y, settings );
this.vel.x = (settings.flip ? -this.maxVel.x : this.maxVel.x);
this.vel.y = -(50 + (Math.random()*100));
this.addAnim( 'idle', 0.2, [0,1] );
},
handleMovementTrace: function( res ) {
this.parent( res );
if( res.collision.x || res.collision.y ) {
// only bounce 3 times
this.bounceCounter++;
if( this.bounceCounter > 3 ) {
this.kill();
}
}
},
check: function( other ) {
other.receiveDamage( 10, this );
this.kill();
},
kill: function(){
for(var i = 0; i < 20; i++)
ig.game.spawnEntity(EntityGrenadeParticle, this.pos.x, this.pos.y);
this.parent();
}
});
EntityDeathExplosion = ig.Entity.extend({
lifetime: 1,
callBack: null,
particles: 25,
init: function( x, y, settings ) {
this.parent( x, y, settings );
for(var i = 0; i < this.particles; i++)
ig.game.spawnEntity(EntityDeathExplosionParticle, x, y, {colorOffset: settings.colorOffset ? settings.colorOffset : 0});
this.idleTimer = new ig.Timer();
},
update: function() {
if( this.idleTimer.delta() > this.lifetime ) {
this.kill();
if(this.callBack)
this.callBack();
return;
}
}
});
EntityDeathExplosionParticle = ig.Entity.extend({
size: {x: 2, y: 2},
maxVel: {x: 160, y: 200},
lifetime: 2,
fadetime: 1,
bounciness: 0,
vel: {x: 100, y: 30},
friction: {x:100, y: 0},
collides: ig.Entity.COLLIDES.LITE,
colorOffset: 0,
totalColors: 7,
animSheet: new ig.AnimationSheet( 'media/blood.png', 2, 2 ),
init: function( x, y, settings ) {
this.parent( x, y, settings );
var frameID = Math.round(Math.random()*this.totalColors) + (this.colorOffset * (this.totalColors+1));
this.addAnim( 'idle', 0.2, [frameID] );
this.vel.x = (Math.random() * 2 - 1) * this.vel.x;
this.vel.y = (Math.random() * 2 - 1) * this.vel.y;
this.idleTimer = new ig.Timer();
},
update: function() {
if( this.idleTimer.delta() > this.lifetime ) {
this.kill();
return;
}
this.currentAnim.alpha = this.idleTimer.delta().map(
this.lifetime - this.fadetime, this.lifetime,
1, 0
);
this.parent();
}
});
EntityGrenadeParticle = ig.Entity.extend({
size: {x: 1, y: 1},
maxVel: {x: 160, y: 200},
lifetime: 1,
fadetime: 1,
bounciness: 0.9,
vel: {x: 40, y: 50},
friction: {x:20, y: 20},
checkAgainst: ig.Entity.TYPE.B,
collides: ig.Entity.COLLIDES.LITE,
animSheet: new ig.AnimationSheet( 'media/explosion.png', 1, 1 ),
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.vel.x = (Math.random() * 4 - 1) * this.vel.x;
this.vel.y = (Math.random() * 10 - 1) * this.vel.y;
this.idleTimer = new ig.Timer();
var frameID = Math.round(Math.random()*7);
this.addAnim( 'idle', 0.2, [frameID] );
},
update: function() {
if( this.idleTimer.delta() > this.lifetime ) {
this.kill();
return;
}
this.currentAnim.alpha = this.idleTimer.delta().map(
this.lifetime - this.fadetime, this.lifetime,
1, 0
);
this.parent();
}
});
});
Cheers
Starri +Phil