diff --git a/js/bullet.js b/js/bullet.js index ed845d5..56f93ec 100644 --- a/js/bullet.js +++ b/js/bullet.js @@ -47,9 +47,6 @@ class Bullet{ } - // collision(){ - // - // } diff --git a/js/enemy.js b/js/enemy.js index e2d97e4..7ecde31 100644 --- a/js/enemy.js +++ b/js/enemy.js @@ -41,16 +41,10 @@ class Enemy{ if(this.pos.x > canvas.width || this.pos.x < 0 || this.pos.y < 0 || this.pos.y > canvas.height){ this.remove = true; } - - - } shoot(){ bullets.push(new Bullet(this.pos.x,this.pos.y,this.bulletSpeed,this.bulletWidth,this.bulletHeight,this.angle + Math.PI,this.bulletPic,this.bulletType )); - } - - } diff --git a/js/graphicsCommon.js b/js/graphicsCommon.js index cb039a9..b356245 100644 --- a/js/graphicsCommon.js +++ b/js/graphicsCommon.js @@ -20,8 +20,10 @@ function drawBall(centerX,centerY,radius,fillColor) { } function colorText(showWords,textX,textY,fillColor,fontface,textAlign = 'left' ) { + ctx.save(); ctx.textAlign = textAlign; ctx.font = fontface; ctx.fillStyle = fillColor; ctx.fillText(showWords, textX, textY); + ctx.restore(); } diff --git a/js/input.js b/js/input.js index 681941a..f9096f4 100644 --- a/js/input.js +++ b/js/input.js @@ -7,7 +7,7 @@ var rightArrowButtonHold = false; function addInputs(){ document.addEventListener('keydown', function(evt){ - + if(evt.code == "ArrowLeft"){ leftArrowButtonHold = true; @@ -20,9 +20,23 @@ function addInputs(){ //shoot shoot if(evt.code == "KeyX"){ // shootKeyHold = true; - + if(gameOver){ + gameReset(); + } + else{ satelliteOne.shoot(); satelliteTwo.shoot(); + } + } + + if(evt.code == "KeyO"){ + gameOver =!gameOver; + } + + if(evt.code == "KeyD"){ + // shootKeyHold = true; + + debug = !debug; } diff --git a/js/main.js b/js/main.js index a88bc7e..a3f26f4 100644 --- a/js/main.js +++ b/js/main.js @@ -15,7 +15,8 @@ satelliteOne, satelliteTwo, planetHealth, - score; + score, + gameOver,debug = false; window.onload = function() { canvas = document.getElementById('gameCanvas'); @@ -27,6 +28,7 @@ planetDia = 220; planetHealth = 100; score = 0; + gameOver = false; loadImages(); addInputs(); satelliteOne = new Satellite(pic = blueSatellitePic, bulletPic = blueSatelliteShotPic, type='shield'); @@ -35,86 +37,116 @@ function drawEverything() { ctx.drawImage(backgroundPic,0,0); // center, draw - planetAngle += PLANETANGLECHANGE; - drawBitmapCenteredAtLocationWithRotation(planetPic, centerX, centerY, planetAngle); - satelliteOne.move(); - satelliteTwo.move(); - satelliteOne.draw(); - satelliteTwo.draw(); - - - // if(shootKeyHold){ - // if(satelliteOneSelected){ - // satelliteOne.shoot(); - // } - // else{ - // satelliteTwo.shoot(); - // } - // } - // console.log(bullets); - for(var i = 0; i < bullets.length; i++ ){ - // console.log(bullets[i].satObject); - - if(SAT.testPolygonPolygon(bullets[i].satObject, satelliteOne.satObject) && - bullets[i].type == 'enemy'){ - bullets[i].remove = true; - satelliteOne.remove = true; + if(!gameOver){ + planetAngle += PLANETANGLECHANGE; + drawBitmapCenteredAtLocationWithRotation(planetPic, centerX, centerY, planetAngle); + satelliteOne.move(); + satelliteTwo.move(); + satelliteOne.draw(); + satelliteTwo.draw(); - } - if(SAT.testPolygonPolygon(bullets[i].satObject, satelliteTwo.satObject) && - bullets[i].type == 'enemy'){ - bullets[i].remove = true; - satelliteTwo.remove = true; - } + for(var i = 0; i < bullets.length; i++ ){ + // console.log(bullets[i].satObject); + if(SAT.testPolygonPolygon(bullets[i].satObject, satelliteOne.satObject) && + bullets[i].type == 'enemy'){ + bullets[i].remove = true; + satelliteOne.lives--; + } + if(SAT.testPolygonPolygon(bullets[i].satObject, satelliteTwo.satObject) && + bullets[i].type == 'enemy'){ + bullets[i].remove = true; + satelliteTwo.lives--; - for(var j = 0; j < enemies.length; j++){ + } - if(SAT.testPolygonPolygon(bullets[i].satObject, enemies[j].satObject) && bullets[i].type == 'satellite'){ - bullets[i].remove = true; - enemies[j].remove = true; + for(var j = 0; j < enemies.length; j++){ + + if(SAT.testPolygonPolygon(bullets[i].satObject, enemies[j].satObject) && bullets[i].type == 'satellite'){ + bullets[i].remove = true; + enemies[j].remove = true; + score += 20; + } } + } + + for(var j = 0; j < enemies.length; j++){ + + if(SAT.testPolygonPolygon(satelliteOne.satObject, enemies[j].satObject)){ + satelliteOne.lives--; + enemies[j].remove = true; + score += 20; } + if(SAT.testPolygonPolygon(satelliteTwo.satObject, enemies[j].satObject)){ + satelliteTwo.lives--; + enemies[j].remove = true; + score += 20; + } + } + if(satelliteOne.lives<=0){ + satelliteOne.remove = true; + } - } + if(satelliteTwo.lives<=0){ + satelliteTwo.remove = true; + } - for(var i = 0; i < bullets.length; i++ ){ - if(!bullets[i].remove){ - bullets[i].draw(); + if(satelliteTwo.lives<=0 && satelliteOne.lives<=0){ + gameOver = true; } - } - for(var i = 0; i < bullets.length; i++ ){ - if(bullets[i].remove){ - bullets.splice(i,1); + for(var i = 0; i < bullets.length; i++ ){ + if(!bullets[i].remove){ + bullets[i].draw(); + } + } - } + for(var i = 0; i < bullets.length; i++ ){ + if(bullets[i].remove){ + bullets.splice(i,1); + } - for(var i = 0; i < enemies.length; i++){ - if(!enemies[i].remove){ - enemies[i].draw(); } - } + for(var i = 0; i < enemies.length; i++){ + if(!enemies[i].remove){ + enemies[i].draw(); + } - for(var i = 0; i < enemies.length; i++ ){ - if(enemies[i].remove){ - enemies.splice(i,1); } - } + for(var i = 0; i < enemies.length; i++ ){ + if(enemies[i].remove){ + enemies.splice(i,1); + } - colorText("Bullets: " + bullets.length,5,10,"white","Arial"); - colorText("Enemies: " + enemies.length,5,20,"white","Arial"); - colorText("Planet Health: " + planetHealth,5,30,"white","Arial"); - colorText("Score: " + score,5,40,"white","Arial"); + } + colorText("Planet Health: " + planetHealth,5,30,"white","30px Arial"); + colorText("Score: " + score,5,60,"white","20px Arial",); + colorText("Satellite-1 Life: " + satelliteOne.lives,5,90,"white","20px Arial"); + colorText("Satellite-2 Life: " + satelliteTwo.lives,5,120,"white","20px Arial"); + if(debug){ + colorText("Bullets: " + bullets.length,5,150,"white","Arial"); + colorText("Enemies: " + enemies.length,5,180,"white","Arial"); + + } + + if(planetHealth <= 0){ + gameOver = true; + } + + } + else{ + colorText("Final Score - " + score, canvas.width/2 , canvas.height/2 - 60 ,"white"," 50px Arial","center"); + colorText("Game Over . Press X to restart", canvas.width/2 , canvas.height/2,"white"," 40px Arial","center"); + } } @@ -126,20 +158,33 @@ function loadingDoneSoStartGame(){ }, 1000/framesPerSecond); - setInterval(function() { - enemies.push(new Enemy()); + if(!gameOver){ + setInterval(function() { + enemies.push(new Enemy()); - }, 3000); //2000 - setInterval(function() { - for(var i = 0; i < enemies.length; i++ ){ - enemies[i].shoot(); + }, 3000); //2000 + setInterval(function() { + for(var i = 0; i < enemies.length; i++ ){ + enemies[i].shoot(); - } + } - }, 2500); //2500 - setInterval(function() { - score+=10; + }, 2500); //2500 + // setInterval(function() { + // score+=10; + // + // }, 4000); //2500 + } - }, 4000); //2500 } + +function gameReset(){ + planetHealth = 100; + score = 0; + gameOver = false; + satelliteOne = new Satellite(pic = blueSatellitePic, bulletPic = blueSatelliteShotPic, type='shield'); + satelliteTwo = new Satellite(pic = yellowSatellitePic, bulletPic = yellowSatelliteShotPic, type='shooter'); + bullets = []; + enemies = []; +} diff --git a/js/satellite.js b/js/satellite.js index 764dac1..d45ea71 100644 --- a/js/satellite.js +++ b/js/satellite.js @@ -19,6 +19,7 @@ class Satellite{ this.bulletHeight = 10; this.bulletPic = bulletPic; this.bulletType = 'satellite'; + this.lives = 3; //satObject need an initial minimum points //Drawn from bottom. // this.initlalSatObject = new SAT.Box(new SAT.Vector(this.pos.x - this.width/2 , this.pos.y + this.height/2), this.width, this.height).toPolygon(); this.satObject;