Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge master #3

Merged
merged 11 commits into from
May 3, 2024
Prev Previous commit
Next Next commit
Collision improved a bit, score powerup image added
VRamazing committed Dec 31, 2017
commit 81f231bf56266dcf444aefa265a42f28a9dcbb9a
Binary file added images/explosions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/scorePowerup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
<script src = "js/graphicsCommon.js"></script>
<script src = "js/soundAndMusic.js"></script>
<script src = "js/powerup.js"></script>
<script src = "js/explosions.js"></script>
<script src = "js/main.js"></script>
</body>
</html>
2 changes: 1 addition & 1 deletion js/bullet.js
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ class Bullet{
this.velocity = vec2.create(Math.cos(angle - Math.PI/2)* this.speed ,Math.sin(angle - Math.PI/2)* this.speed);
this.type = type;
this.remove = false;
this.satObject = new SAT.Box(new SAT.Vector(x - width/2 , y + height/2), width, height).toPolygon().rotate(angle - Math.PI/2);
this.satObject = new SAT.Box(new SAT.Vector(x - width/2 , y + height/2), width, height).toPolygon().rotate(angle - Math.PI/2 - 0.145);
}

move(){
2 changes: 1 addition & 1 deletion js/enemy.js
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ class Enemy{

move(){
vec2.add(this.pos,this.pos,this.velocity);
this.satObject = new SAT.Box(new SAT.Vector(this.pos.x - this.width/2 , this.pos.y + this.height/2), this.width, this.height).toPolygon().rotate(-this.angle);
this.satObject = new SAT.Box(new SAT.Vector(this.pos.x - this.width/2 , this.pos.y + this.height/2), this.width, this.height).toPolygon().rotate(-this.angle + 0.145);
if(this.pos.x > canvas.width || this.pos.x < 0 || this.pos.y < 0 || this.pos.y > canvas.height){
this.remove = true;
}
276 changes: 276 additions & 0 deletions js/explosions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,276 @@
/**
*
* EXPLOSIONS v2
*
* made for gamkedo by mcfunkypants

* I made the explosions.png sprites by
* capturing a realtime engine to video
* then turning into a spritesheet
* they are CC0 public domain - enjoy
*
*/

var explosions = [];
var explosions_enabled = true;
var explosion_timestamp = (new Date()).getTime();
var explosion_w = 64;
var explosion_h = 64;
var explosion_spritesheet_framecount = 32;
var explosion_FPS = 60;
var explosion_FRAME_MS = 1000/explosion_FPS;
var FAR_AWAY = -999999;
var spritesheet_image = null;
var spritesheet_image_finished_loading = false;
const EXPLOSION_BOOM = 0;
const EXPLOSION_RING = 1;
const EXPLOSION_SMOKE = 2;
const EXPLOSION_SPARKS = 3;

function clearAllExplosions() {
explosions = [];
}

/**
* spawns a spritesheet-based explosion animation at these coordinates
* implements a reuse POOL and only makes new objects when required
*/
function explode(x, y, explosionType, destX, destY, delayFrames, startScale, endScale) {

//console.log('explode ' + x + ',' + y);

if (!explosions_enabled) return;
if (!spritesheet_image_finished_loading) return;
if (!delayFrames) delayFrames = 0; // deal with undefined
if (startScale==undefined) startScale = 1;
if (endScale==undefined) endScale = 1;
if (!explosionType) explosionType = 0;

var exp, pnum, pcount;
for (pnum = 0, pcount = explosions.length; pnum < pcount; pnum++)
{
exp = explosions[pnum];
if (exp && exp.inactive) {
break;
}
}

// we need a new explosion!
if (!exp || !exp.inactive)
{
//console.log('No inactive explosions. Adding explosion #' + pcount);
var explosion = { x : FAR_AWAY, y : FAR_AWAY, inactive : true };
// remember this new explosion in our system and reuse
explosions.push(explosion);
exp = explosion;
}

if (exp && exp.inactive) {
exp.x = x;// + explosion_offsetx; // FIXME: account for scale (eg x4)
exp.y = y;// + explosion_offsety;
exp.explosion_type = explosionType;
exp.delayFrames = delayFrames; // MS3 - can be delayed by a number of frames
exp.inactive = false;
exp.anim_frame = 0;
exp.anim_start_frame = 0;
exp.anim_end_frame = explosion_spritesheet_framecount;
exp.anim_last_tick = explosion_timestamp;
exp.next_frame_timestamp = explosion_timestamp + explosion_FRAME_MS;
exp.anim_sum_tick = 0;
exp.scale = startScale;
exp.endscale = endScale;
exp.scaleSpeed = (endScale - startScale) / explosion_spritesheet_framecount;

// optionally moving explosions
if (destX && destY) {
exp.moving = true;
exp.destX = destX;
exp.destY = destY;
// rotate: lookAt(p, destX, destY);
exp.speedX = (destX - x) / explosion_spritesheet_framecount;
exp.speedY = (destY - y) / explosion_spritesheet_framecount;
} else {
exp.moving = false;
}

}

}

function clearExplosions() {
console.log('clearexplosions');
explosions.forEach(function (exp) {
exp.x = exp.y = FAR_AWAY; // throw offscreen
exp.inactive = true;
});
}

/**
* steps the explosion effects simulation
*/
var active_explosion_count = 0; // how many we updated last frame

function updateExplosions()
{
if (!explosions_enabled) return;

// get the current time
explosion_timestamp = (new Date()).getTime();

active_explosion_count = 0;

// animate the explosions
explosions.forEach(
function (exp) {
if (!exp.inactive) {

active_explosion_count++;

if (exp.delayFrames>0)
{
//log('delaying explosion: ' + p.delayFrames)
exp.delayFrames--;
}
else // non-delayed explosions:
{
//p.anim_last_tick = explosion_timestamp; // not actually used OPTI

exp.scale += exp.scaleSpeed;

// moving explosions
if (exp.moving) {
exp.x += exp.speedX;
exp.y += exp.speedY;
}

if (exp.anim_frame >= exp.anim_end_frame) {
//console.log('explosion anim ended');
exp.x = exp.y = FAR_AWAY; // throw offscreen
exp.inactive = true;
} else {

if (explosion_timestamp >= exp.next_frame_timestamp)
{
exp.next_frame_timestamp = explosion_timestamp + explosion_FRAME_MS;
exp.anim_frame++; // TODO: ping pong anims?
}

}
}
}
});

if ((active_explosion_count >0)
&& (prev_active_explosion_count != active_explosion_count))
{
// console.log('Active explosions: ' + active_explosion_count);
prev_active_explosion_count = active_explosion_count;
}

}
var prev_active_explosion_count = 0;

function drawExplosions(camerax,cameray)
{
//console.log('draw_explosions');
if (!camerax) camerax = 0;
if (!cameray) cameray = 0;

explosions.forEach(
function (p) {
if (!p.inactive) // and visible in screen bbox
{
if (window.canvasContext) // sanity check
{
canvasContext.drawImage(spritesheet_image,
p.anim_frame * explosion_w,
p.explosion_type * explosion_h,
explosion_w, explosion_h,
p.x - camerax + (-1 * Math.round(explosion_w/2) * p.scale), p.y - cameray + (-1 * Math.round(explosion_h/2) * p.scale),
explosion_w * p.scale, explosion_h * p.scale);
}
}
}
);
}

function initExplosions()
{
//console.log('init_explosions...');
spritesheet_image = new Image();
spritesheet_image.src = 'images/explosions.png';
spritesheet_image.onload = function() {
//console.log('explosions.png loaded.');
spritesheet_image_finished_loading = true;
}
spritesheet_image.onerror = function() {
console.log('ERROR: Failed to download explosions.png.');
}
}

// helper function (inclusive: eg 1,10 may include 1 or 10)
function randomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }

/**
* custom effect types for game-specfic animations!
*
*/
function playerHitExplosion(x,y) // player got hurt!
{
for (var multi=0; multi<8; multi++)
{
explode(x+randomInt(-64,64),y+randomInt(-32,32),EXPLOSION_SMOKE,null,null,null,1,Math.random()*4);
explode(x+randomInt(-32,32),y+randomInt(-32,32),EXPLOSION_BOOM,null,null,null,1,Math.random()*4);
}
}

function powerupExplosion(x,y) // bonus item hit
{
explode(x,y,EXPLOSION_RING,null,null,null,0,2);
for (var multi=0; multi<6; multi++)
{
explode(x+randomInt(-16,16),y+randomInt(-16,16),EXPLOSION_SPARKS,null,null,null,1,1);
}
}

function alienHitExplosion(x,y) // paratrooper hit
{
for (var multi=0; multi<4; multi++)
{
explode(x+randomInt(-8,8),y+randomInt(-8,8),EXPLOSION_SMOKE,null,null,null,0,1);
}
}

function enemyHitExplosion(x,y) // enemy hit
{
for (multi=0; multi<6; multi++)
explode(x+randomInt(-20,20),y+randomInt(-20,20),EXPLOSION_BOOM,null,null,null,1,Math.random()*1);

explode(x,y,EXPLOSION_SMOKE,null,null,null,0,2);

//explode(x,y,EXPLOSION_RING,null,null,null,0,2);
}

function satellitefireExplosion(x,y)
{
//explode(x,y,EXPLOSION_SMOKE,null,null,null,0,1);
explode(x,y,EXPLOSION_SPARKS,null,null,null,0,1);
}

function secondaryGunfireExplosion(x,y)
{
explode(x,y,EXPLOSION_SMOKE,null,null,null,0,1);
explode(x,y,EXPLOSION_SPARKS,null,null,null,0,1);
}

function damageSmokeExplosion(x,y)
{
if (Math.random()>0.2) // lots of small smoke
explode(x+randomInt(-8,8),y+randomInt(-8,8),EXPLOSION_SMOKE,null,null,null,0,Math.random() * 0.5);

if (Math.random()>0.9) // rare big sparks
explode(x+randomInt(-8,8),y+randomInt(-8,8),EXPLOSION_SPARKS,null,null,null,0,1);

//if (Math.random()>0.8) // rare randomly sized explosions
// explode(x+randomInt(-8,8),y+randomInt(-8,8),EXPLOSION_BOOM,null,null,null,0,Math.random());
}
2 changes: 1 addition & 1 deletion js/imageLoading.js
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ function loadImages() {
{varName:swirlPowerupPic, theFile:"swirlPowerup.png"},
{varName:shieldPowerupPic, theFile:"shieldPowerup.png"},
{varName:healthPowerupPic, theFile:"healthPowerup.png"},
{varName:scorePowerupPic, theFile:"healthPowerup.png"},//change this
{varName:scorePowerupPic, theFile:"scorePowerup.png"}



21 changes: 20 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@
enemySpawnRate = 3000;
shieldActivated = false;
swirling = false;
initExplosions();



@@ -161,7 +162,6 @@

}

console.log(powerups)
//satellite enemy collisons
for(var j = 0; j < enemies.length; j++){

@@ -172,13 +172,20 @@
satelliteHurtSound.play();
enemyHurtSound.play();

playerHitExplosion(satelliteOne.pos.x, satelliteOne.pos.y);
enemyHitExplosion(enemies[j].pos.x, enemies[j].pos.y);


}
if(SAT.testPolygonPolygon(satelliteTwo.satObject, enemies[j].satObject)){
satelliteTwo.lives--;
enemies[j].remove = true;
score += 15;
satelliteHurtSound.play();
enemyHurtSound.play();
playerHitExplosion(satelliteTwo.pos.x, satelliteTwo.pos.y)
enemyHitExplosion(enemies[j].pos.x, enemies[j].pos.y)

}
}

@@ -203,13 +210,22 @@




if(satelliteOne.lives<=0){
satelliteOne.remove = true;
}
else{
satelliteOne.remove = false;

}


if(satelliteTwo.lives<=0){
satelliteTwo.remove = true;
}
else{
satelliteTwo.remove = false;
}

if(satelliteTwo.lives<=0 && satelliteOne.lives<=0){
gameOver = true;
@@ -257,6 +273,9 @@
}

}
updateExplosions();
drawExplosions();

if(shieldActivated){
ctx.drawImage(shieldPic,centerX - 260/2,centerY -260/2);
}