Online Book Reader

Home Category

HTML5 Canvas [165]

By Root 6376 0
newSaucerMissile = {};

newSaucerMissile.x = saucer.x+.5*saucer.width;

newSaucerMissile.y = saucer.y+.5*saucer.height;

newSaucerMissile.width = 2;

newSaucerMissile.height = 2;

newSaucerMissile.speed = saucer.missileSpeed;

//ConsoleLog.log("saucer fire");

//fire at player from small saucer

var diffx = player.x-saucer.x;

var diffy = player.y-saucer.y;

var radians = Math.atan2(diffy, diffx);

var degrees = 360 * radians / (2 * Math.PI);

newSaucerMissile.dx = saucer.missileSpeed*Math.cos(Math.PI*(degrees)/180);

newSaucerMissile.dy = saucer.missileSpeed*Math.sin(Math.PI*(degrees)/180);

newSaucerMissile.life = 160;

newSaucerMissile.lifeCtr = 0;

saucerMissiles.push(newSaucerMissile);

}

function playerDie() {

ConsoleLog.log("player die");

createExplode(player.x+player.halfWidth, player.y+player.halfWidth,50);

switchGameState(GAME_STATE_PLAYER_DIE);

}

function createExplode(x,y,num) {

//create 10 particles

for (var partCtr=0;partCtrvar newParticle = new Object();

newParticle.dx = Math.random()*3;

if (Math.random()<.5){

newParticle.dx*=-1;

}

newParticle.dy = Math.random()*3;

if (Math.random()<.5){

newParticle.dy*=-1;

}

newParticle.life = Math.floor(Math.random()*30+30);

newParticle.lifeCtr = 0;

newParticle.x = x;

newParticle.y = y;

//ConsoleLog.log("newParticle.life=" + newParticle.life);

particles.push(newParticle);

}

}

function boundingBoxCollide(object1, object2) {

var left1 = object1.x;

var left2 = object2.x;

var right1 = object1.x + object1.width;

var right2 = object2.x + object2.width;

var top1 = object1.y;

var top2 = object2.y;

var bottom1 = object1.y + object1.height;

var bottom2 = object2.y + object2.height;

if (bottom1 < top2) return(false);

if (top1 > bottom2) return(false);

if (right1 < left2) return(false);

if (left1 > right2) return(false);

return(true);

};

function splitRock(scale,x,y){

for (var newRockctr=0;newRockctr<2;newRockctr++){

var newRock = {};

//ConsoleLog.log("split rock");

if (scale==2){

newRock.scoreValue = medRockScore;

newRock.width = 25;

newRock.height = 25;

newRock.halfWidth = 12.5;

newRock.halfHeight = 12.5;

}else {

newRock.scoreValue = smlRockScore;

newRock.width = 16;

newRock.height = 16;

newRock.halfWidth = 8;

newRock.halfHeight = 8;

}

newRock.scale = scale;

newRock.x = x;

newRock.y = y;

newRock.dx = Math.random()*3;

if (Math.random()<.5){

newRock.dx*=-1;

}

newRock.dy = Math.random()*3;

if (Math.random()<.5){

newRock.dy*=-1;

}

newRock.rotationInc = (Math.random()*5)+1;

if (Math.random()<.5){

newRock.rotationInc*=-1;

}

newRock.rotation = 0;

ConsoleLog.log("new rock scale"+(newRock.scale));

rocks.push(newRock);

}

}

function addToScore(value){

score+=value;

}

document.onkeydown = function(e){

e = e?e:window.event;

//ConsoleLog.log(e.keyCode + "down");

keyPressList[e.keyCode] = true;

}

document.onkeyup = function(e){

//document.body.onkeyup = function(e){

e = e?e:window.event;

//ConsoleLog.log(e.keyCode + "up");

keyPressList[e.keyCode] = false;

};

//*** application start

switchGameState(GAME_STATE_TITLE);

frameRateCounter = new FrameRateCounter();

//**** application loop

const FRAME_RATE = 40;

var intervalTime = 1000/FRAME_RATE;

setInterval(runGame, intervalTime );

}

//***** object prototypes *****

//*** consoleLog util object

//create constructor

function ConsoleLog(){

}

//create function that will be added to the class

console_log = function(message) {

if(typeof(console) !== 'undefined' && console != null) {

console.log(message);

}

}

//add class/static function to class by assignment

ConsoleLog.log = console_log;

//*** end console log object

//*** FrameRateCounter object prototype

function FrameRateCounter() {

this.lastFrameCount = 0;

var dateTemp = new Date();

this.frameLast = dateTemp.getTime();

delete dateTemp;

this.frameCtr = 0;

}

FrameRateCounter.prototype.countFrames = function() {

var dateTemp = new Date();

this.frameCtr++;

if (dateTemp.getTime() >=this.frameLast+1000)

Return Main Page Previous Page Next Page

®Online Book Reader