I cannot show you all of the code due to the licence agreement with impactjs, but here are the main and entity files.
ig.module(
'game.main'
)
.requires(
'impact.game',
'impact.font',
'game.levels.level1',
'game.entities.player',
'game.entities.otherplayer',
'game.entities.bullet',
'game.entities.netbullet',
'game.entities.enemy'
)
.defines(function(){
MyGame = ig.Game.extend({
//global variables
font: new ig.Font( 'media/font.png' ),
smallfont: new ig.Font( 'media/smallfont.png' ),
stats:{kills:1, wave:1, lastwave:0, rocks:0, prevrocks:0, deaths:0, bullets:0, fired:0, hits:0, time:0},
lives:3,
levelTimer:new ig.Timer(),
showStats:false,
start: new ig.Image( 'media/start.png' ),
gameover: new ig.Image('media/gameover.png'),
livesSprite: new ig.Image('media/spaceshipred.png'),
HUD: new ig.Image('media/stat-matte.png'),
bullets: 100,
init: function() {
// Initialize bind keys etc.
ig.input.bind(ig.KEY.MOUSE1, 'move');
ig.input.bind(ig.KEY.SPACE, 'minorbull');
this.loadLevel (LevelLevel1);
ig.game.levelTimer.reset(); //starts the timer to see how long it takes to complete the level
},
gameOver: function(){
//shows new screen and submits score when user runs out of lives
ig.game.stats.time = Math.round(ig.game.levelTimer.delta());
ig.game.levelTimer= new ig.Timer();
ig.finalStats=ig.game.stats;
ig.system.setGame(GameOverScreen);
},
update: function() {
// Update all entities and backgroundMaps
this.parent();
// Add your own, additional update code here
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 (ig.game.stats.wave > ig.game.stats.lastwave)
{
//each new wave the user gets 10 more bullets for each boulder
ig.game.bullets = ig.game.bullets + (ig.game.stats.wave * 150);
ig.game.stats.lastwave = ig.game.stats.wave;
ig.game.stats.rocks = 15*ig.game.stats.wave;
for (var i=0; i< ig.game.stats.rocks;i++){
var randomx = Math.floor(Math.random() * (896 - 64 + 1)) + 576;
var randomy = Math.floor(Math.random() * (704 - 64 + 1)) + 704;
ig.game.spawnEntity(EntityEnemy, randomx, randomy);
//make the player invincible for 3 seconds at the start of each wave
player.invincible = true;
player.makeInvincible;
player.invincibleTimer = new ig.Timer();
}
}
//once all the boulders are destroyed move onto the next wave
if(ig.game.stats.kills == ig.game.stats.rocks + ig.game.stats.prevrocks)
{
ig.game.stats.wave = ig.game.stats.wave + 1;
ig.game.stats.prevrocks = ig.game.stats.rocks + ig.game.stats.prevrocks;
}
},
draw: function() {
// Draw all entities, backgrounds and HUD
this.parent();
ig.game.HUD.draw(0,0);
ig.game.font.draw("Lives: ", 25,5);
for(var i=0; i < this.lives; i++){
this.livesSprite.draw(((this.livesSprite.width + 2) * i)+140, +11);
}
ig.game.font.draw("Wave: "+ig.game.stats.wave, 470,5);
//bottom hud
ig.game.HUD.draw(0,422);
ig.game.font.draw("Bullets remaining: "+ig.game.bullets,25,427);
}
});
//instruction screen
StartScreen=ig.Game.extend({
background:new ig.Image('media/start.png'),
init: function(){
ig.input.bind(ig.KEY.MOUSE1, 'start');
},
update: function(){
if(ig.input.pressed('start')){
ig.system.setGame(MyGame)
}
this.parent();
},
draw: function(){
this.parent();
this.background.draw(0,0);
}
});
//game over screen
GameOverScreen = ig.Game.extend({
background:new ig.Image('media/gameover.png'),
font: new ig.Font( 'media/font.png' ),
stats:{},
score:0,
name:"",
init:function(){
//restart level
ig.input.bind(ig.KEY.MOUSE1, 'start');
this.stats = ig.finalStats;
//calculates score here - can't show you how ;)
##########################
##########SECRET##########
##########################
},
update: function(){
//submits score here - can't show you how ;)
##########################
##########SECRET##########
##########################
},
draw: function(){
//display stats
this.parent();
var x = ig.system.width/2;
var y = ig.system.height/2-20;
this.background.draw(0,0);
this.font.draw("Seconds Survived: "+this.stats.time, x,y-50,ig.Font.ALIGN.CENTER);
this.font.draw('Score: '+this.score,x,y-10,ig.Font.ALIGN.CENTER);
this.font.draw('Wave Reached: '+this.stats.wave,x,y+30,ig.Font.ALIGN.CENTER);
this.font.draw('Kills: '+this.stats.kills,x,y+70,ig.Font.ALIGN.CENTER);
this.font.draw('Deaths: '+this.stats.deaths,x,y+110,ig.Font.ALIGN.CENTER);
}
});
// Start the Game with 60fps, a resolution of 320x240, scaled up by a factor of 2
ig.main( '#canvas', StartScreen, 60, 640, 480, 1 );
});
ig.module (
'game.entities.player'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityPlayer = ig.Entity.extend({
size: {x: 26, y: 32}, //size of character
direction: 0,
messagebox: "", //sets message box to empty (this displays user logged in or diconnect)
type: ig.Entity.TYPE.A, //friendly type (enemies type B)
nettimer: 5,
movementspeed:400, //speed of character doesnt work
//gamename:playername, //need to disable gamename line to run weltmeister
destinationx:99999999,
destinationy:99999999,
messageboxtimer: 500, //timer before message box dissapears
checkAgainst: ig.Entity.TYPE.NONE, //how it interacts with other entities
collides: ig.Entity.COLLIDES.PASSIVE, //if something hits it goes through it
animSheet: new ig.AnimationSheet( 'media/spaceshipred.png', 26, 32 ), //link to the sprite sheet
startPosition:null,
invincible: true, //makes player invincible when it respawns
invincibleDelay:3,
invincibleTimer:null,
bulletDelay:0.5,
bulletTimer:null,
init: function( x, y, settings ) {
this.parent( x, y, settings );
this.startPosition = {x:x,y:y}; //gets the respawn position from the start
this.invincibleTimer = new ig.Timer();
this.makeInvincible;
this.maxVel.x = this.maxVel.y = this.movementspeed;
//only has one state
this.addAnim( 'idle', 1, [0] );
},
kill:function(){
this.parent(this.startPosition.x.y);
ig.game.respawnPosition = this.startPosition;
ig.game.stats.deaths = ig.game.stats.deaths + 1;
ig.game.lives = ig.game.lives -1;
if (ig.game.lives == 0)
{
ig.game.gameOver();
}
else
{
this.onDeath();
}
ig.game.bullets = ig.game.bullets + 100; //gives the user 100 bullets each time the user dies
},
makeInvincible: function(){ //allows us call makeInvincible() at any time
this.invincible = true;
this.invincibleTimer.reset();
},
receiveDamage: function(amount, from){ //only takes damage from the player if its not invincible
if(this.invincible)
return;
this.parent(amount,from);
},
draw:function(){ //slowly remove the transparent effect from the player while invisibility lasts
if(this.invincible)
this.currentAnim.alpha = this.invincibleTimer.delta()/this.invincibleDelay * 1; //slowly fade into game
this.parent();
},
onDeath:function(){
ig.game.spawnEntity(EntityPlayer, ig.game.respawnPosition.x,ig.game.respawnPosition.y); //repsawn player at default position
},
update: function() {
//grabs the mouse position
var mx = ig.input.mouse.x + ig.game.screen.x;
var my = ig.input.mouse.y + ig.game.screen.y;
//sets calculates the angle
var mouseAngle = Math.atan2(
my - (this.pos.y + this.size.y/2),
mx - (this.pos.x + this.size.x/2)
);
//sets the player to face the mouse
this.anims.idle.angle = mouseAngle;
if (ig.input.pressed('move')){
this.nettimer = this.nettimer - 1;
this.destinationx = ig.input.mouse.x + ig.game.screen.x;
this.destinationy = ig.input.mouse.y + ig.game.screen.y;
}
if(this.destinationx < 99999999 && this.destinationy < 9999999){
//removes the height and width of the character from the position
this.distancetotargetx = this.destinationx - this.pos.x;
this.distancetotargety = this.destinationy - this.pos.y;
//if the character isnt at the clicked position move it there
if (Math.abs(this.distancetotargetx) > 5 || Math.abs(this.distancetotargety) > 5){
if (Math.abs(this.distancetotargetx) > Math.abs(this.distancetotargety)) {
if (this.distancetotargetx > 5){
//moves right if user clicks in the right segment (X) <==
this.vel.x = this.movementspeed;
this.xydivision = this.distancetotargety / this.distancetotargetx;
this.vel.y = this.xydivision * this.movementspeed;
}
else
{
//moves right if user clicks in the right segment ==>(X)
this.vel.x = -this.movementspeed;
this.xydivision = this.distancetotargety / Math.abs( this.distancetotargetx);
this.vel.y = this.xydivision * this.movementspeed;
}
}
else
{
if(this.distancetotargety > 5)
{
//move down
this.vel.y = this.movementspeed;
this.xydivision = this.distancetotargetx / this.distancetotargety;
this.vel.x = this.xydivision * this.movementspeed;
}
else
{
//move up
this.vel.y = -this.movementspeed;
this.xydivision = this.distancetotargetx / Math.abs (this.distancetotargety);
this.vel.x = this.xydivision * this.movementspeed;
}
}
}
//if the character is at the clicked position set position to idle
else
{
this.vel.y = 0;
this.vel.x = 0;
this.destinationx = 99999999;
this.destinationy = 99999999;
}
}
//if the user presses the space bar and there are bullets remaining
if(ig.input.state('minorbull') && ig.game.bullets > 0){
ig.game.spawnEntity(EntityBullet, this.pos.x, this.pos.y, {flip:this.flip,bullettype:1});
ig.game.bullets = ig.game.bullets - 1;
//shoots bullet towards position of mouse
}
if(this.invincibleTimer.delta() > this.invincibleDelay){ //if not invincible set character alpha full
this.invincible = false;
this.currentAnim.alpha = 1;
}
this.parent();
},
handleMovementTrace: function( res ) {
this.parent( res );
// collision with outer vortex kills player
if( res.collision.x ||res.collision.y ) {
this.kill();
}
}
});
});
ig.module (
'game.entities.bullet'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityBullet = ig.Entity.extend({
size: {x: 8, y:8},
offset: {x:-11 , y:-8},
maxVel: {x:1800, y:1800},
bullettype:1,
type: ig.Entity.TYPE.B,
checkAgainst: ig.Entity.TYPE.B, //attacks the enemy
collides: ig.Entity.COLLIDES.PASSIVE,
animSheet: new ig.AnimationSheet( 'media/bullet.png', 8, 8 ), //gotta find a pic
init: function( x, y, settings ) {
this.parent( x, y, settings );
// Add the animations
this.addAnim( 'idle', 1, [0]);
ig.game.stats.fired = ig.game.stats.fired + 1;
//gets the position of the mous
var mx = ig.input.mouse.x + ig.game.screen.x;
var my = ig.input.mouse.y + ig.game.screen.y;
var angle = Math.atan2(
my - (this.pos.y + this.size.y/2),
mx - (this.pos.x + this.size.x/2)
);
this.vel.x = Math.cos(angle)*600;
this.vel.y = Math.sin(angle)*600;
},
handleMovementTrace:function(res){
this.parent(res);
//destroys when it hits the houter vortex or boulder
if(res.collision.x || res.collision.y){
this.kill();
}
},
check:function(other){
//takes 5 health off the enemy which kills it
other.receiveDamage(5,this);
ig.game.stats.hits = ig.game.stats.hits + 1;
this.kill();
}
});
});
ig.module (
'game.entities.enemy'
)
.requires(
'impact.entity'
)
.defines(function(){
EntityEnemy = ig.Entity.extend({
size: { x: 32, y: 32 },
friction: { x: 0, y: 0 },
speed: 300,
bounciness: 1,
minBounceVelocity: 0,
type: ig.Entity.TYPE.B, //enemy group
checkAgainst: ig.Entity.TYPE.A, //collides with bullets and the player
collides: ig.Entity.COLLIDES.PASSIVE,
animSheet: new ig.AnimationSheet('media/asteroid.png', 32, 32),
init: function( x, y, settings ) {
this.parent( x, y, settings );
// Add the animations
this.addAnim( 'idle', 0.1, [0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,52,54,56,58,60,62] );
this.maxVel.x = this.maxVel.y = this.speed;
// Generate a random angle between -180 and 180 degrees, in radians.
this.angle = (Math.ceil(Math.random()*340) - 160) * (Math.PI/180);
// Set initial velocity.
this.setVelocityByAngle(this.angle, this.speed);
},
handleMovementTrace: function( res ) {
this.parent( res );
// makes them change direction when they reach the end
if( res.collision.x ) {
//this.kill();
}
},
check:function(other){
other.receiveDamage(10, this); //health is 10 by default so hitting boulder kills player
},
receiveDamage:function(value){
this.parent(value);
},
kill:function(){
//kills the boulder
this.parent();
ig.game.stats.kills = ig.game.stats.kills + 1 ;
},
setVelocityByAngle: function(angle, velocity) {
//set the speed and direction of the boulder
this.vel.x = Math.cos(angle) * velocity;
this.vel.y = Math.sin(angle) * velocity;
}
});
});