From 963c9301205de312d298d2d8159435517082e481 Mon Sep 17 00:00:00 2001 From: landgreen Date: Wed, 16 Oct 2019 06:51:35 -0700 Subject: [PATCH] time stop field added, extra mech.cycle --- js/bullets.js | 176 ++++++++++++++++++++++++++------------------------ js/engine.js | 4 +- js/game.js | 14 ++-- js/level.js | 37 +++-------- js/mobs.js | 126 +++++++++++++++++++----------------- js/player.js | 161 ++++++++++++++++++++++++++++----------------- js/spawn.js | 111 ++++++++++++++++--------------- 7 files changed, 338 insertions(+), 291 deletions(-) diff --git a/js/bullets.js b/js/bullets.js index 8aca658..38f0d4a 100644 --- a/js/bullets.js +++ b/js/bullets.js @@ -23,7 +23,7 @@ const b = { game.makeGunHUD(); }, fireProps(cd, speed, dir, me) { - mech.fireCDcycle = game.cycle + cd; // cool down + mech.fireCDcycle = mech.cycle + cd; // cool down Matter.Body.setVelocity(bullet[me], { x: mech.Vx / 2 + speed * Math.cos(dir), y: mech.Vy / 2 + speed * Math.sin(dir) @@ -191,7 +191,7 @@ const b = { // sub = Matter.Vector.sub(bullet[me].position, bullet[i].position); // dist = Matter.Vector.magnitude(sub); // if (dist < bullet[me].explodeRad) { - // bullet[i].endCycle = game.cycle; + // bullet[i].endCycle = mech.cycle; // } // } // } @@ -247,11 +247,11 @@ const b = { ammoPack: Infinity, have: false, fire() { - // mech.fireCDcycle = game.cycle + 1 + // mech.fireCDcycle = mech.cycle + 1 //laser drains energy as well as bullets const FIELD_DRAIN = 0.003 if (mech.fieldMeter < FIELD_DRAIN) { - mech.fireCDcycle = game.cycle + 100; // cool down if out of energy + mech.fireCDcycle = mech.cycle + 100; // cool down if out of energy } else { mech.fieldMeter -= mech.fieldRegen + FIELD_DRAIN let best; @@ -406,11 +406,11 @@ const b = { // ammoPack: Infinity, // have: false, // fire() { - // // mech.fireCDcycle = game.cycle + 1 + // // mech.fireCDcycle = mech.cycle + 1 // //laser drains energy as well as bullets // const FIELD_DRAIN = 0.0001 //should be 0.001 // if (mech.fieldMeter < FIELD_DRAIN) { - // mech.fireCDcycle = game.cycle + 100; // cool down if out of energy + // mech.fireCDcycle = mech.cycle + 100; // cool down if out of energy // } else { // mech.fieldMeter -= mech.fieldRegen + FIELD_DRAIN // let best; @@ -508,7 +508,7 @@ const b = { // x: path[i].x - path[i - 1].x, // y: path[i].y - path[i - 1].y // } - // const a = game.cycle * 5 + // const a = mech.cycle * 5 // p1 = { // x: d.x / 2 * Math.cos(a) - d.y / 2 * Math.sin(a), // y: d.x / 2 * Math.sin(a) + d.y / 2 * Math.cos(a), @@ -592,19 +592,17 @@ const b = { onDmg() {}, onEnd() {}, do() { - //wiggle - this.cycle++ - const THRUST = wiggleMag * Math.cos(this.cycle * 0.3) - this.force = Matter.Vector.mult(Matter.Vector.normalise(this.direction), this.mass * THRUST) + if (!mech.isBodiesAsleep) { + this.cycle++ + const THRUST = wiggleMag * Math.cos(this.cycle * 0.3) + this.force = Matter.Vector.mult(Matter.Vector.normalise(this.direction), this.mass * THRUST) //wiggle - //shrink - if (this.cycle > 0 && !(Math.floor(this.cycle) % 6)) { - Matter.Body.scale(this, SCALE, SCALE); + if (this.cycle > 0 && !(Math.floor(this.cycle) % 6)) Matter.Body.scale(this, SCALE, SCALE); //shrink } } }); World.add(engine.world, bullet[me]); //add bullet to world - mech.fireCDcycle = game.cycle + (mech.crouch ? 8 : 4); // cool down + mech.fireCDcycle = mech.cycle + (mech.crouch ? 8 : 4); // cool down const SPEED = mech.crouch ? 5.2 : 4.5; Matter.Body.setVelocity(bullet[me], { x: SPEED * Math.cos(DIR), @@ -726,70 +724,80 @@ const b = { }; bullet[me].lockedOn = null; bullet[me].do = function () { - if (!(game.cycle % this.lookFrequency)) { - this.close = null; - this.lockedOn = null; - let closeDist = Infinity; - for (let i = 0, len = mob.length; i < len; ++i) { - if ( - mob[i].alive && - mob[i].dropPowerUp && - Matter.Query.ray(map, this.position, mob[i].position).length === 0 && - Matter.Query.ray(body, this.position, mob[i].position).length === 0 - ) { - const dist = Matter.Vector.magnitude(Matter.Vector.sub(this.position, mob[i].position)); - if (dist < closeDist) { - this.close = mob[i].position; - closeDist = dist; - this.lockedOn = mob[i]; + if (!mech.isBodiesAsleep) { + if (!(mech.cycle % this.lookFrequency)) { + this.closestTarget = null; + this.lockedOn = null; + let closeDist = Infinity; + + //look for targets + for (let i = 0, len = mob.length; i < len; ++i) { + if ( + mob[i].alive && + mob[i].dropPowerUp && + Matter.Query.ray(map, this.position, mob[i].position).length === 0 && + Matter.Query.ray(body, this.position, mob[i].position).length === 0 + ) { + const dist = Matter.Vector.magnitude(Matter.Vector.sub(this.position, mob[i].position)); + if (dist < closeDist) { + this.closestTarget = mob[i].position; + closeDist = dist; + this.lockedOn = mob[i]; + } + } + } + //explode when bullet is close enough to target + if (this.closestTarget && closeDist < this.explodeRad * 0.7) { + this.endCycle = 0; //bullet ends cycle after doing damage //this also triggers explosion + } + + if (this.lockedOn) { + this.frictionAir = 0.04; //extra friction + + //draw locked on targeting + ctx.beginPath(); + const vertices = this.lockedOn.vertices; + ctx.moveTo(this.position.x, this.position.y); + const mod = Math.floor((game.cycle / 3) % vertices.length); + ctx.lineTo(vertices[mod].x, vertices[mod].y); + ctx.strokeStyle = "rgba(0,0,155,0.35)"; //"#2f6"; + ctx.lineWidth = 1; + ctx.stroke(); + } + } + + //rotate missile towards the target + if (this.closestTarget) { + const face = { + x: Math.cos(this.angle), + y: Math.sin(this.angle) + }; + const target = Matter.Vector.normalise(Matter.Vector.sub(this.position, this.closestTarget)); + if (Matter.Vector.dot(target, face) > -0.98) { + if (Matter.Vector.cross(target, face) > 0) { + Matter.Body.rotate(this, 0.08); + } else { + Matter.Body.rotate(this, -0.08); } } } - //explode when bullet is close enough to target - if (this.close && closeDist < this.explodeRad * 0.7) { - this.endCycle = 0; //bullet ends cycle after doing damage //this also triggers explosion - } + //accelerate in direction bullet is facing + const dir = this.angle; // + (Math.random() - 0.5); + this.force.x += Math.cos(dir) * thrust; + this.force.y += Math.sin(dir) * thrust; - if (this.lockedOn) { - this.frictionAir = 0.04; //extra friction - - //draw locked on targeting - ctx.beginPath(); - const vertices = this.lockedOn.vertices; - ctx.moveTo(this.position.x, this.position.y); - const mod = Math.floor((game.cycle / 3) % vertices.length); - ctx.lineTo(vertices[mod].x, vertices[mod].y); - ctx.strokeStyle = "rgba(0,0,155,0.35)"; //"#2f6"; - ctx.lineWidth = 1; - ctx.stroke(); - } + //draw rocket + ctx.beginPath(); + ctx.arc(this.position.x - Math.cos(this.angle) * 27 + (Math.random() - 0.5) * 4, this.position.y - Math.sin(this.angle) * 27 + (Math.random() - 0.5) * 4, 11, 0, 2 * Math.PI); + ctx.fillStyle = "rgba(255,155,0,0.5)"; + ctx.fill(); + } else { + //draw rocket with time stop + ctx.beginPath(); + ctx.arc(this.position.x - Math.cos(this.angle) * 27, this.position.y - Math.sin(this.angle) * 27, 11, 0, 2 * Math.PI); + ctx.fillStyle = "rgba(255,155,0,0.5)"; + ctx.fill(); } - - //rotate missile towards the target - if (this.close) { - const face = { - x: Math.cos(this.angle), - y: Math.sin(this.angle) - }; - const target = Matter.Vector.normalise(Matter.Vector.sub(this.position, this.close)); - if (Matter.Vector.dot(target, face) > -0.98) { - if (Matter.Vector.cross(target, face) > 0) { - Matter.Body.rotate(this, 0.08); - } else { - Matter.Body.rotate(this, -0.08); - } - } - } - //accelerate in direction bullet is facing - const dir = this.angle; // + (Math.random() - 0.5); - this.force.x += Math.cos(dir) * thrust; - this.force.y += Math.sin(dir) * thrust; - - //draw rocket - ctx.beginPath(); - ctx.arc(this.position.x - Math.cos(this.angle) * 27 + (Math.random() - 0.5) * 4, this.position.y - Math.sin(this.angle) * 27 + (Math.random() - 0.5) * 4, 11, 0, 2 * Math.PI); - ctx.fillStyle = "rgba(255,155,0,0.5)"; - ctx.fill(); } } }, @@ -934,9 +942,11 @@ const b = { bullet[me].minDmgSpeed = 0; bullet[me].onDmg = function () {}; bullet[me].do = function () { - const SCALE = 1.017 - Matter.Body.scale(this, SCALE, SCALE); - this.frictionAir += 0.00023; + if (!mech.isBodiesAsleep) { + const SCALE = 1.017 + Matter.Body.scale(this, SCALE, SCALE); + this.frictionAir += 0.00023; + } this.force.y += this.mass * 0.00045; @@ -978,7 +988,7 @@ const b = { //find mob targets if (!(game.cycle % this.lookFrequency)) { - this.close = null; + this.closestTarget = null; this.lockedOn = null; let closeDist = Infinity; for (let i = 0, len = mob.length; i < len; ++i) { @@ -987,7 +997,7 @@ const b = { const targetVector = Matter.Vector.sub(this.position, mob[i].position) const dist = Matter.Vector.magnitude(targetVector); if (dist < closeDist) { - this.close = mob[i].position; + this.closestTarget = mob[i].position; closeDist = dist; this.lockedOn = Matter.Vector.normalise(targetVector); if (0.3 > Math.random()) break //doesn't always target the closest mob @@ -1110,13 +1120,13 @@ const b = { }, ], fire() { - if (game.mouseDown && mech.fireCDcycle < game.cycle && !(keys[32] || game.mouseDownRight) && b.inventory.length) { + if (game.mouseDown && mech.fireCDcycle < mech.cycle && !(keys[32] || game.mouseDownRight) && b.inventory.length) { if (b.guns[this.activeGun].ammo > 0) { b.guns[this.activeGun].fire(); b.guns[this.activeGun].ammo--; game.updateGunHUD(); } else { - mech.fireCDcycle = game.cycle + 30; //cooldown + mech.fireCDcycle = mech.cycle + 30; //cooldown // game.makeTextLog("
NO AMMO
E / Q", 200); game.makeTextLog("
NO AMMO

Q, E, and mouse wheel change weapons

", 200); } @@ -1126,13 +1136,13 @@ const b = { } }, gamepadFire() { - if (game.gamepad.rightTrigger && mech.fireCDcycle < game.cycle && !(keys[32] || game.gamepad.leftTrigger) && !mech.isHolding && b.inventory.length) { + if (game.gamepad.rightTrigger && mech.fireCDcycle < mech.cycle && !(keys[32] || game.gamepad.leftTrigger) && !mech.isHolding && b.inventory.length) { if (b.guns[this.activeGun].ammo > 0) { b.guns[this.activeGun].fire(); b.guns[this.activeGun].ammo--; game.updateGunHUD(); } else { - mech.fireCDcycle = game.cycle + 30; //cooldown + mech.fireCDcycle = mech.cycle + 30; //cooldown game.makeTextLog("
NO AMMO

Q, E, and mouse wheel change weapons

", 200); } } diff --git a/js/engine.js b/js/engine.js index b3e2a3b..64185c6 100644 --- a/js/engine.js +++ b/js/engine.js @@ -89,9 +89,9 @@ function mobCollisionChecks(event) { function collide(obj) { //player and mob collision if (obj === playerBody || obj === playerHead) { - if (mech.damageImmune < game.cycle) { + if (mech.damageImmune < mech.cycle) { //player is immune to mob collision damage for 30 cycles - mech.damageImmune = game.cycle + 30; + mech.damageImmune = mech.cycle + 30; mob[k].foundPlayer(); let dmg = Math.min(Math.max(0.025 * Math.sqrt(mob[k].mass), 0.05), 0.3) * game.dmgScale; //player damage is capped at 0.3*dmgScale of 1.0 mech.damage(dmg); diff --git a/js/game.js b/js/game.js index 48aa96b..e833e84 100644 --- a/js/game.js +++ b/js/game.js @@ -4,6 +4,7 @@ const game = { loop() {}, mouseLoop() { game.cycle++; //tracks game cycles + mech.cycle++; //tracks player cycles //used to alow time to stop for everything, but the player if (game.clearNow) { game.clearNow = false; game.clearMap(); @@ -52,6 +53,7 @@ const game = { }, gamepadLoop() { game.cycle++; //tracks game cycles + mech.cycle++; //tracks player cycles //used to alow time to stop for everything, but the player // game.polGamepad(); if (game.clearNow) { game.clearNow = false; @@ -313,10 +315,10 @@ const game = { makeTextLog(text, time = 180) { document.getElementById("text-log").innerHTML = text; document.getElementById("text-log").style.opacity = 1; - game.lastLogTime = game.cycle + time; + game.lastLogTime = mech.cycle + time; }, textLog() { - if (game.lastLogTime && game.lastLogTime < game.cycle) { + if (game.lastLogTime && game.lastLogTime < mech.cycle) { game.lastLogTime = 0; // document.getElementById("text-log").innerHTML = " "; document.getElementById("text-log").style.opacity = 0; @@ -745,7 +747,7 @@ const game = { // if 4000px deep if (mech.pos.y > game.fallHeight) mech.death(); - if (!(game.cycle % 420)) { + if (!(mech.cycle % 420)) { remove = function (who) { let i = who.length; while (i--) { @@ -778,6 +780,8 @@ const game = { ctx.fillText("cycle: " + game.cycle, x, line); line += 20; + ctx.fillText("player cycle: " + mech.cycle, x, line); + line += 20; ctx.fillText("x: " + player.position.x.toFixed(0), x, line); line += 20; ctx.fillText("y: " + player.position.y.toFixed(0), x, line); @@ -814,7 +818,7 @@ const game = { draw: { powerUp() { // draw power up - // ctx.globalAlpha = 0.4 * Math.sin(game.cycle * 0.15) + 0.6; + // ctx.globalAlpha = 0.4 * Math.sin(mech.cycle * 0.15) + 0.6; // for (let i = 0, len = powerUp.length; i < len; ++i) { // let vertices = powerUp[i].vertices; // ctx.beginPath(); @@ -827,7 +831,7 @@ const game = { // ctx.fill(); // } // ctx.globalAlpha = 1; - ctx.globalAlpha = 0.4 * Math.sin(game.cycle * 0.15) + 0.6; + ctx.globalAlpha = 0.4 * Math.sin(mech.cycle * 0.15) + 0.6; for (let i = 0, len = powerUp.length; i < len; ++i) { ctx.beginPath(); ctx.arc(powerUp[i].position.x, powerUp[i].position.y, powerUp[i].size, 0, 2 * Math.PI); diff --git a/js/level.js b/js/level.js index c4e60cf..5953fd0 100644 --- a/js/level.js +++ b/js/level.js @@ -16,13 +16,13 @@ const level = { // game.levelsCleared = 4; //for testing to simulate possible mobs spawns // b.giveGuns(1) // set a starting gun for testing // b.giveGuns("all", 1000) - // mech.fieldUpgrades[5]() //give a field power up for testing - this.intro(); //starting level + mech.fieldUpgrades[1]() //give a field power up for testing + // this.intro(); //starting level // this.testingMap(); // this.bosses(); // this.aerie(); // this.rooftops(); - // this.warehouse(); + this.warehouse(); // this.highrise(); // this.office(); } else { @@ -44,8 +44,9 @@ const level = { //****************************************************************************************************************** //****************************************************************************************************************** testingMap() { + //start with all guns + b.giveGuns("all", 1000) game.zoomScale = 1400 //1400 is normal - game.zoomTransition(1400) spawn.setSpawnList(); game.levelsCleared = 3; //for testing to simulate all possible mobs spawns for (let i = 0; i < game.levelsCleared; i++) { @@ -58,9 +59,9 @@ const level = { level.exit.x = 3500; level.exit.y = -870; this.addZone(level.exit.x, level.exit.y, 100, 30, "nextLevel"); + document.body.style.backgroundColor = "#dcdcde"; + - //start with all guns - b.giveGuns("all", 1000) // this.addZone(250, -1000, 500, 1500, "laser"); @@ -68,13 +69,6 @@ const level = { // setTimeout(function() { // document.body.style.backgroundColor = "#eee"; // }, 1); - document.body.style.backgroundColor = "#fff"; - // document.body.style.backgroundColor = "#fafcff"; - // document.body.style.backgroundColor = "#bbb"; - // document.body.style.backgroundColor = "#eee4e4"; - // document.body.style.backgroundColor = "#dcdcde"; - // document.body.style.backgroundColor = "#e0e5e0"; - // this.addQueryRegion(550, -25, 100, 50, "bounce", { Vx: 0, Vy: -25 }); // level.fillBG.push({ x: 550, y: -25, width: 100, height: 50, color: "#ff0" }); @@ -123,20 +117,9 @@ const level = { // spawn.bodyRect(-45, -100, 40, 50); // spawn.focuser(800, -1150); // spawn.groupBoss(-600, -550); - // for (let i = 0; i < 1; ++i) { - spawn.shooter(800, -150, 10); - spawn.shooter(800, -150, 10); - spawn.shooter(800, -100, 10); - spawn.shooter(800, -50, 10); - spawn.shooter(800, -150, 10); - spawn.shooter(800, -150, 10); - spawn.shooter(800, -100, 10); - spawn.shooter(800, -50, 10); - spawn.shooter(800, -150, 10); - spawn.shooter(800, -150, 10); - spawn.shooter(800, -100, 10); - spawn.shooter(800, -50, 10); - // } + spawn.starter(800, -150, 100); + // spawn.grower(800, -250); + // spawn.blinker(800, -250, 40); // spawn.groupBoss(900, -1070); // for (let i = 0; i < 20; i++) { // spawn.randomBoss(-100, -1470); diff --git a/js/mobs.js b/js/mobs.js index 90b44b4..f896b39 100644 --- a/js/mobs.js +++ b/js/mobs.js @@ -681,20 +681,22 @@ const mobs = { // } }, grow() { - if (this.seePlayer.recall) { - if (this.radius < 80) { - const scale = 1.01; - Matter.Body.scale(this, scale, scale); - this.radius *= scale; - // this.torque = -0.00002 * this.inertia; - this.fill = `hsl(144, ${this.radius}%, 50%)`; - } - } else { - if (this.radius > 15) { - const scale = 0.99; - Matter.Body.scale(this, scale, scale); - this.radius *= scale; - this.fill = `hsl(144, ${this.radius}%, 50%)`; + if (!mech.isBodiesAsleep) { + if (this.seePlayer.recall) { + if (this.radius < 80) { + const scale = 1.01; + Matter.Body.scale(this, scale, scale); + this.radius *= scale; + // this.torque = -0.00002 * this.inertia; + this.fill = `hsl(144, ${this.radius}%, 50%)`; + } + } else { + if (this.radius > 15) { + const scale = 0.99; + Matter.Body.scale(this, scale, scale); + this.radius *= scale; + this.fill = `hsl(144, ${this.radius}%, 50%)`; + } } } }, @@ -813,50 +815,52 @@ const mobs = { } }, fire() { - const setNoseShape = () => { - const mag = this.radius + this.radius * this.noseLength; - this.vertices[1].x = this.position.x + Math.cos(this.angle) * mag; - this.vertices[1].y = this.position.y + Math.sin(this.angle) * mag; - }; - //throw a mob/bullet at player - if (this.seePlayer.recall) { - //set direction to turn to fire - if (!(game.cycle % this.seePlayerFreq)) { - this.fireDir = Matter.Vector.normalise(Matter.Vector.sub(this.seePlayer.position, this.position)); - this.fireDir.y -= Math.abs(this.seePlayer.position.x - this.position.x) / 1600; //gives the bullet an arc - this.fireAngle = Math.atan2(this.fireDir.y, this.fireDir.x); + if (!mech.isBodiesAsleep) { + const setNoseShape = () => { + const mag = this.radius + this.radius * this.noseLength; + this.vertices[1].x = this.position.x + Math.cos(this.angle) * mag; + this.vertices[1].y = this.position.y + Math.sin(this.angle) * mag; + }; + //throw a mob/bullet at player + if (this.seePlayer.recall) { + //set direction to turn to fire + if (!(game.cycle % this.seePlayerFreq)) { + this.fireDir = Matter.Vector.normalise(Matter.Vector.sub(this.seePlayer.position, this.position)); + this.fireDir.y -= Math.abs(this.seePlayer.position.x - this.position.x) / 1600; //gives the bullet an arc + this.fireAngle = Math.atan2(this.fireDir.y, this.fireDir.x); + } + //rotate towards fireAngle + const angle = this.angle + Math.PI / 2; + c = Math.cos(angle) * this.fireDir.x + Math.sin(angle) * this.fireDir.y; + const threshold = 0.1; + if (c > threshold) { + this.torque += 0.000004 * this.inertia; + } else if (c < -threshold) { + this.torque -= 0.000004 * this.inertia; + } else if (this.noseLength > 1.5) { + //fire + spawn.bullet(this.vertices[1].x, this.vertices[1].y, 5 + Math.ceil(this.radius / 15), 5); + const v = 15; + Matter.Body.setVelocity(mob[mob.length - 1], { + x: this.velocity.x + this.fireDir.x * v + Math.random(), + y: this.velocity.y + this.fireDir.y * v + Math.random() + }); + this.noseLength = 0; + // recoil + this.force.x -= 0.005 * this.fireDir.x * this.mass; + this.force.y -= 0.005 * this.fireDir.y * this.mass; + } + if (this.noseLength < 1.5) this.noseLength += this.fireFreq; + setNoseShape(); + } else if (this.noseLength > 0.1) { + this.noseLength -= this.fireFreq / 2; + setNoseShape(); } - //rotate towards fireAngle - const angle = this.angle + Math.PI / 2; - c = Math.cos(angle) * this.fireDir.x + Math.sin(angle) * this.fireDir.y; - const threshold = 0.1; - if (c > threshold) { - this.torque += 0.000004 * this.inertia; - } else if (c < -threshold) { - this.torque -= 0.000004 * this.inertia; - } else if (this.noseLength > 1.5) { - //fire - spawn.bullet(this.vertices[1].x, this.vertices[1].y, 5 + Math.ceil(this.radius / 15), 5); - const v = 15; - Matter.Body.setVelocity(mob[mob.length - 1], { - x: this.velocity.x + this.fireDir.x * v + Math.random(), - y: this.velocity.y + this.fireDir.y * v + Math.random() - }); - this.noseLength = 0; - // recoil - this.force.x -= 0.005 * this.fireDir.x * this.mass; - this.force.y -= 0.005 * this.fireDir.y * this.mass; - } - if (this.noseLength < 1.5) this.noseLength += this.fireFreq; - setNoseShape(); - } else if (this.noseLength > 0.1) { - this.noseLength -= this.fireFreq / 2; - setNoseShape(); + // else if (this.noseLength < -0.1) { + // this.noseLength += this.fireFreq / 4; + // setNoseShape(); + // } } - // else if (this.noseLength < -0.1) { - // this.noseLength += this.fireFreq / 4; - // setNoseShape(); - // } }, turnToFacePlayer() { //turn to face player @@ -887,10 +891,12 @@ const mobs = { this.death(); //death with no power up or body }, timeLimit() { - this.timeLeft--; - if (this.timeLeft < 0) { - this.dropPowerUp = false; - this.death(); //death with no power up + if (!mech.isBodiesAsleep) { + this.timeLeft--; + if (this.timeLeft < 0) { + this.dropPowerUp = false; + this.death(); //death with no power up + } } }, healthBar() { diff --git a/js/player.js b/js/player.js index 2a57a4f..97fc3e8 100644 --- a/js/player.js +++ b/js/player.js @@ -52,6 +52,7 @@ const mech = { }); World.add(engine.world, mech.holdConstraint); }, + cycle: 0, width: 50, radius: 30, fillColor: "#fff", @@ -218,7 +219,7 @@ const mech = { if (momentum > 100) { this.doCrouch(); this.yOff = this.yOffWhen.jump; - this.hardLandCD = game.cycle + Math.min(momentum / 6 - 6, 40) + this.hardLandCD = mech.cycle + Math.min(momentum / 6 - 6, 40) } else { this.yOffGoal = this.yOffWhen.stand; } @@ -228,11 +229,11 @@ const mech = { keyMove() { if (this.onGround) { //on ground ********************** if (this.crouch) { - if (!(keys[83] || keys[40]) && this.isHeadClear && this.hardLandCD < game.cycle) this.undoCrouch(); - } else if (keys[83] || keys[40] || this.hardLandCD > game.cycle) { + if (!(keys[83] || keys[40]) && this.isHeadClear && this.hardLandCD < mech.cycle) this.undoCrouch(); + } else if (keys[83] || keys[40] || this.hardLandCD > mech.cycle) { this.doCrouch(); //on ground && not crouched and pressing s or down - } else if ((keys[87] || keys[38]) && this.buttonCD_jump + 20 < game.cycle && this.yOffWhen.stand > 23) { - this.buttonCD_jump = game.cycle; //can't jump again until 20 cycles pass + } else if ((keys[87] || keys[38]) && this.buttonCD_jump + 20 < mech.cycle && this.yOffWhen.stand > 23) { + this.buttonCD_jump = mech.cycle; //can't jump again until 20 cycles pass //apply a fraction of the jump force to the body the player is jumping off of Matter.Body.applyForce(mech.standingOn, mech.pos, { @@ -274,7 +275,7 @@ const mech = { } else { // in air ********************************** //check for short jumps if ( - this.buttonCD_jump + 60 > game.cycle && //just pressed jump + this.buttonCD_jump + 60 > mech.cycle && //just pressed jump !(keys[87] || keys[38]) && //but not pressing jump key this.Vy < 0 //moving up ) { @@ -298,11 +299,11 @@ const mech = { gamepadMove() { if (this.onGround) { //on ground ********************** if (this.crouch) { - if (game.gamepad.leftAxis.y !== -1 && this.isHeadClear && this.hardLandCD < game.cycle) this.undoCrouch(); - } else if (game.gamepad.leftAxis.y === -1 || this.hardLandCD > game.cycle) { + if (game.gamepad.leftAxis.y !== -1 && this.isHeadClear && this.hardLandCD < mech.cycle) this.undoCrouch(); + } else if (game.gamepad.leftAxis.y === -1 || this.hardLandCD > mech.cycle) { this.doCrouch(); //on ground && not crouched and pressing s or down - } else if (game.gamepad.jump && this.buttonCD_jump + 20 < game.cycle && this.yOffWhen.stand > 23) { - this.buttonCD_jump = game.cycle; //can't jump again until 20 cycles pass + } else if (game.gamepad.jump && this.buttonCD_jump + 20 < mech.cycle && this.yOffWhen.stand > 23) { + this.buttonCD_jump = mech.cycle; //can't jump again until 20 cycles pass //apply a fraction of the jump force to the body the player is jumping off of Matter.Body.applyForce(mech.standingOn, mech.pos, { @@ -344,7 +345,7 @@ const mech = { } else { // in air ********************************** //check for short jumps if ( - this.buttonCD_jump + 60 > game.cycle && //just pressed jump + this.buttonCD_jump + 60 > mech.cycle && //just pressed jump !game.gamepad.jump && //but not pressing jump key this.Vy < 0 //moving up ) { @@ -381,7 +382,7 @@ const mech = { }, health: 0, // regen() { - // if (this.health < 1 && game.cycle % 15 === 0) { + // if (this.health < 1 && mech.cycle % 15 === 0) { // this.addHealth(0.01); // } // }, @@ -439,10 +440,10 @@ const mech = { game.fpsCap = game.fpsCapDefault game.fpsInterval = 1000 / game.fpsCap; } - mech.defaultFPSCycle = game.cycle + mech.defaultFPSCycle = mech.cycle const normalFPS = function () { - if (mech.defaultFPSCycle < game.cycle) { //back to default values + if (mech.defaultFPSCycle < mech.cycle) { //back to default values game.fpsCap = game.fpsCapDefault game.fpsInterval = 1000 / game.fpsCap; document.getElementById("dmg").style.transition = "opacity 1s"; @@ -511,6 +512,8 @@ const mech = { this.Fx = 0.015; //run Force on ground this.FxAir = 0.015; //run Force in Air this.gravity = 0.0019; + mech.isBodiesAsleep = true; + mech.wakeCheck(); // this.phaseBlocks(0x011111) }, drawFieldMeter(range = 60) { @@ -617,7 +620,7 @@ const mech = { } } else if (this.throwCharge > 0) { //throw the body - this.fireCDcycle = game.cycle + this.fieldFireCD; + this.fireCDcycle = mech.cycle + this.fieldFireCD; this.isHolding = false; //bullet-like collisions this.holdingTarget.collisionFilter.category = 0x000100; @@ -675,7 +678,7 @@ const mech = { }, grabPowerUp() { //look for power ups to grab - if (mech.fieldCDcycle < game.cycle) { + if (mech.fieldCDcycle < mech.cycle) { const grabPowerUpRange2 = (this.grabRange + 200) * (this.grabRange + 200) for (let i = 0, len = powerUp.length; i < len; ++i) { const dxP = mech.pos.x - powerUp[i].position.x; @@ -699,7 +702,7 @@ const mech = { y: player.velocity.y + ((powerUp[i].velocity.y * powerUp[i].mass) / player.mass) * 0.2 }); mech.usePowerUp(i); - // this.fireCDcycle = game.cycle + 10; //cool down + // this.fireCDcycle = mech.cycle + 10; //cool down return; } // return; @@ -822,6 +825,32 @@ const mech = { this.holdingTarget.collisionFilter.category = 0x000000; this.holdingTarget.collisionFilter.mask = 0x000000; }, + wakeCheck() { + if (mech.isBodiesAsleep) { + mech.isBodiesAsleep = false; + + function wake(who) { + for (let i = 0, len = who.length; i < len; ++i) { + Matter.Sleeping.set(who[i], false) + if (who[i].storeVelocity) { + Matter.Body.setVelocity(who[i], { + x: who[i].storeVelocity.x, + y: who[i].storeVelocity.y + }) + } + } + } + wake(mob); + wake(body); + wake(bullet); + for (let i = 0, len = cons.length; i < len; i++) { + if (cons[i].stiffness === 0) { + cons[i].stiffness = cons[i].storeStiffness + } + } + // wake(powerUp); + } + }, hold() {}, fieldUpgrades: [ () => { @@ -838,7 +867,7 @@ const mech = { mech.grabPowerUp(); mech.pushMobs(); mech.lookForPickUp(); - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released mech.pickUp(); } else { mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) @@ -848,59 +877,69 @@ const mech = { }, () => { mech.fieldMode = 1; - game.makeTextLog("Inertia Negation Field
(right mouse or space bar)

field slows objects in range
decreased field shielding efficiency

", 1200); + game.makeTextLog("Time Dilation Field
(right mouse or space bar)

field stops all objects except player

", 1200); //
field does not shield player mech.setHoldDefaults(); - mech.grabRange = 900; + // mech.grabRange = 900; mech.fieldShieldingScale = 12; // mech.fieldArc = 1; //field covers full 360 degrees // mech.calculateFieldThreshold(); //run after setting fieldArc, used for powerUp grab and mobPush with lookingAt(mob) + mech.isBodiesAsleep = false; mech.hold = function () { + + function sleep(who) { + for (let i = 0, len = who.length; i < len; ++i) { + if (!who[i].isSleeping) who[i].storeVelocity = who[i].velocity + Matter.Sleeping.set(who[i], true) + } + } + if (mech.isHolding) { + mech.wakeCheck(); mech.drawHold(mech.holdingTarget); mech.holding(); mech.throw(); - } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < game.cycle) { - const DRAIN = 0.001 //mech.fieldRegen = 0.0015 + } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < mech.cycle) { + const DRAIN = 0.002 //mech.fieldRegen = 0.0015 if (mech.fieldMeter > DRAIN) { mech.fieldMeter -= DRAIN; mech.grabPowerUp(); - // mech.pushMobs(); - mech.pushMobs360(180); - mech.lookForPickUp(160); + mech.lookForPickUp(180); - //draw slow field - ctx.beginPath(); - ctx.arc(mech.pos.x, mech.pos.y + 15, mech.grabRange, 0, 2 * Math.PI); + //draw field everywhere ctx.fillStyle = "rgba(255,255,255," + (0.5 + 0.17 * Math.random()) + ")"; - ctx.fill(); + ctx.fillRect(-100000, -100000, 200000, 200000) - function slow(who, friction = 0) { - for (let i = 0, len = who.length; i < len; ++i) { - dist = Matter.Vector.magnitude(Matter.Vector.sub(who[i].position, mech.pos)) - if (dist < mech.grabRange) { - Matter.Body.setAngularVelocity(who[i], who[i].angularVelocity * friction) - Matter.Body.setVelocity(who[i], { - x: who[i].velocity.x * friction, - y: who[i].velocity.y * friction - }); - } + //stop time + mech.isBodiesAsleep = true; + sleep(mob); + sleep(body); + sleep(bullet); + for (let i = 0, len = cons.length; i < len; i++) { + if (cons[i].stiffness !== 0) { + cons[i].storeStiffness = cons[i].stiffness; + cons[i].stiffness = 0; } } - slow(mob); - slow(body); - slow(bullet); - slow(powerUp); + game.cycle--; //pause all functions that depend on game cycle increasing } else { - mech.fieldCDcycle = game.cycle + 120; + mech.wakeCheck(); + mech.fieldCDcycle = mech.cycle + 120; } - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + mech.wakeCheck(); mech.pickUp(); } else { + mech.wakeCheck(); mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) } mech.drawFieldMeter() + if (mech.fieldMode !== 1) { + //wake up if this is no longer the current field mode, like after a new power up + mech.wakeCheck(); + + } } }, () => { @@ -945,7 +984,7 @@ const mech = { mech.grabPowerUp(); mech.pushMobs(); mech.lookForPickUp(); - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released mech.pickUp(); } else { mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) @@ -970,7 +1009,7 @@ const mech = { mech.drawHold(mech.holdingTarget); mech.holding(); mech.throw(); - } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < game.cycle) { //push away + } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < mech.cycle) { //push away const DRAIN = 0.001 //mech.fieldRegen = 0.0015 if (mech.fieldMeter > DRAIN) { mech.fieldMeter -= DRAIN; @@ -1028,9 +1067,9 @@ const mech = { } else { //trigger cool down - mech.fieldCDcycle = game.cycle + 120; + mech.fieldCDcycle = mech.cycle + 120; } - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released mech.pickUp(); mech.grabRange = 0 } else { @@ -1057,15 +1096,15 @@ const mech = { } else if ((keys[32] || game.mouseDownRight && mech.fieldMeter > 0.1)) { //not hold but field button is pressed mech.grabPowerUp(); mech.lookForPickUp(180); - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle) { //holding, but field button is released mech.pickUp(); } else { mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) } if (mech.fieldMeter > 0.1) { - const grabRange1 = 80 + 80 * Math.sin(game.cycle / 23) - const grabRange2 = 80 + 80 * Math.sin(game.cycle / 37) - const grabRange3 = 80 + 80 * Math.sin(game.cycle / 47) + const grabRange1 = 80 + 80 * Math.sin(mech.cycle / 23) + const grabRange2 = 80 + 80 * Math.sin(mech.cycle / 37) + const grabRange3 = 80 + 80 * Math.sin(mech.cycle / 47) const netGrabRange = Math.max(grabRange1, grabRange2, grabRange3) ctx.fillStyle = "rgba(110,170,200," + (0.15 + 0.15 * Math.random()) + ")"; ctx.beginPath(); @@ -1104,7 +1143,7 @@ const mech = { mech.drawField(); mech.grabPowerUp(); mech.lookForPickUp(); - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released mech.pickUp(); } else { mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) @@ -1125,7 +1164,7 @@ const mech = { mech.drawHold(mech.holdingTarget); mech.holding(); mech.throw(); - } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < game.cycle) { + } else if ((keys[32] || game.mouseDownRight) && mech.fieldCDcycle < mech.cycle) { const DRAIN = 0.0035 //mech.fieldRegen = 0.0015 if (mech.fieldMeter > DRAIN) { mech.fieldMeter -= DRAIN; @@ -1148,9 +1187,9 @@ const mech = { mech.grabPowerUp(); mech.lookForPickUp(110); } else { - mech.fieldCDcycle = game.cycle + 120; + mech.fieldCDcycle = mech.cycle + 120; } - } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released mech.pickUp(); } else { mech.holdingTarget = null; //clears holding target (this is so you only pick up right after the field button is released and a hold target exists) @@ -1169,7 +1208,7 @@ const mech = { // mech.drawHold(mech.holdingTarget); // mech.holding(); // mech.throw(); - // } else if ((keys[32] || game.mouseDownRight && mech.fieldCDcycle < game.cycle)) { //not hold but field button is pressed + // } else if ((keys[32] || game.mouseDownRight && mech.fieldCDcycle < mech.cycle)) { //not hold but field button is pressed // mech.grabPowerUp(); // mech.lookForPickUp(Math.max(180, mech.grabRange)); // mech.pushMobs360(140); @@ -1180,7 +1219,7 @@ const mech = { // mech.fieldMeter -= DRAIN; // mech.damage(0.00005 + 0.00000012 * mech.grabRange) // //draw damage field - // mech.grabRange = mech.grabRange * 0.997 + (1350 + 150 * Math.cos(game.cycle / 30)) * 0.003 + // mech.grabRange = mech.grabRange * 0.997 + (1350 + 150 * Math.cos(mech.cycle / 30)) * 0.003 // let gradient = ctx.createRadialGradient(this.pos.x, this.pos.y, 0, this.pos.x, this.pos.y, mech.grabRange); // gradient.addColorStop(0, 'rgba(255,255,255,0.7)'); // gradient.addColorStop(1, 'rgba(255,0,50,' + (0.6 + 0.2 * Math.random()) + ')'); @@ -1204,7 +1243,7 @@ const mech = { // } // } // } else { - // mech.fieldCDcycle = game.cycle + 120; + // mech.fieldCDcycle = mech.cycle + 120; // } // } else { // mech.grabRange = 180; @@ -1212,7 +1251,7 @@ const mech = { // mech.grabPowerUp(); // mech.lookForPickUp(); // } - // } else if (mech.holdingTarget && mech.fireCDcycle < game.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released + // } else if (mech.holdingTarget && mech.fireCDcycle < mech.cycle && mech.fieldMeter > 0.05) { //holding, but field button is released // mech.grabRange = 0 // mech.pickUp(); // } else { diff --git a/js/spawn.js b/js/spawn.js index 03f71bf..dec9ffe 100644 --- a/js/spawn.js +++ b/js/spawn.js @@ -602,58 +602,60 @@ const spawn = { // if (Math.random() < Math.min(0.2 + game.levelsCleared * 0.1, 0.7)) spawn.shield(me, x, y); me.do = function () { this.healthBar(); - this.seePlayerByLookingAt(); - const dist2 = this.distanceToPlayer2(); - //laser Tracking - if (this.seePlayer.yes && dist2 < 4000000 && !mech.isStealth) { - this.attraction(); - const rangeWidth = 2000; //this is sqrt of 4000000 from above if() - //targeting laser will slowly move from the mob to the player's position - this.laserPos = Matter.Vector.add(this.laserPos, Matter.Vector.mult(Matter.Vector.sub(player.position, this.laserPos), 0.1)); - let targetDist = Matter.Vector.magnitude(Matter.Vector.sub(this.laserPos, mech.pos)); - const r = 10; + if (!mech.isBodiesAsleep) { + this.seePlayerByLookingAt(); + const dist2 = this.distanceToPlayer2(); + //laser Tracking + if (this.seePlayer.yes && dist2 < 4000000 && !mech.isStealth) { + this.attraction(); + const rangeWidth = 2000; //this is sqrt of 4000000 from above if() + //targeting laser will slowly move from the mob to the player's position + this.laserPos = Matter.Vector.add(this.laserPos, Matter.Vector.mult(Matter.Vector.sub(player.position, this.laserPos), 0.1)); + let targetDist = Matter.Vector.magnitude(Matter.Vector.sub(this.laserPos, mech.pos)); + const r = 10; - // ctx.setLineDash([15, 200]); - // ctx.lineDashOffset = 20*(game.cycle % 215); - ctx.beginPath(); - ctx.moveTo(this.position.x, this.position.y); - if (targetDist < r + 15) { - // || dist2 < 80000 - targetDist = r + 10; - //charge at player - const forceMag = this.accelMag * 40 * this.mass; - const angle = Math.atan2(this.seePlayer.position.y - this.position.y, this.seePlayer.position.x - this.position.x); - this.force.x += forceMag * Math.cos(angle); - this.force.y += forceMag * Math.sin(angle); + // ctx.setLineDash([15, 200]); + // ctx.lineDashOffset = 20*(game.cycle % 215); + ctx.beginPath(); + ctx.moveTo(this.position.x, this.position.y); + if (targetDist < r + 15) { + // || dist2 < 80000 + targetDist = r + 10; + //charge at player + const forceMag = this.accelMag * 40 * this.mass; + const angle = Math.atan2(this.seePlayer.position.y - this.position.y, this.seePlayer.position.x - this.position.x); + this.force.x += forceMag * Math.cos(angle); + this.force.y += forceMag * Math.sin(angle); + } else { + //high friction if can't lock onto player + Matter.Body.setVelocity(this, { + x: this.velocity.x * 0.96, + y: this.velocity.y * 0.96 + }); + } + if (dist2 > 80000) { + const laserWidth = 0.002; + let laserOffR = Matter.Vector.rotateAbout(this.laserPos, (targetDist - r) * laserWidth, this.position); + let sub = Matter.Vector.normalise(Matter.Vector.sub(laserOffR, this.position)); + laserOffR = Matter.Vector.add(laserOffR, Matter.Vector.mult(sub, rangeWidth)); + ctx.lineTo(laserOffR.x, laserOffR.y); + + let laserOffL = Matter.Vector.rotateAbout(this.laserPos, (targetDist - r) * -laserWidth, this.position); + sub = Matter.Vector.normalise(Matter.Vector.sub(laserOffL, this.position)); + laserOffL = Matter.Vector.add(laserOffL, Matter.Vector.mult(sub, rangeWidth)); + ctx.lineTo(laserOffL.x, laserOffL.y); + // ctx.fillStyle = "rgba(0,0,255,0.15)"; + var gradient = ctx.createRadialGradient(this.position.x, this.position.y, 0, this.position.x, this.position.y, rangeWidth); + gradient.addColorStop(0, `rgba(0,0,255,${((r + 5) * (r + 5)) / (targetDist * targetDist)})`); + gradient.addColorStop(1, "transparent"); + ctx.fillStyle = gradient; + ctx.fill(); + } } else { - //high friction if can't lock onto player - Matter.Body.setVelocity(this, { - x: this.velocity.x * 0.96, - y: this.velocity.y * 0.96 - }); + this.laserPos = this.position; } - if (dist2 > 80000) { - const laserWidth = 0.002; - let laserOffR = Matter.Vector.rotateAbout(this.laserPos, (targetDist - r) * laserWidth, this.position); - let sub = Matter.Vector.normalise(Matter.Vector.sub(laserOffR, this.position)); - laserOffR = Matter.Vector.add(laserOffR, Matter.Vector.mult(sub, rangeWidth)); - ctx.lineTo(laserOffR.x, laserOffR.y); - - let laserOffL = Matter.Vector.rotateAbout(this.laserPos, (targetDist - r) * -laserWidth, this.position); - sub = Matter.Vector.normalise(Matter.Vector.sub(laserOffL, this.position)); - laserOffL = Matter.Vector.add(laserOffL, Matter.Vector.mult(sub, rangeWidth)); - ctx.lineTo(laserOffL.x, laserOffL.y); - // ctx.fillStyle = "rgba(0,0,255,0.15)"; - var gradient = ctx.createRadialGradient(this.position.x, this.position.y, 0, this.position.x, this.position.y, rangeWidth); - gradient.addColorStop(0, `rgba(0,0,255,${((r + 5) * (r + 5)) / (targetDist * targetDist)})`); - gradient.addColorStop(1, "transparent"); - ctx.fillStyle = gradient; - ctx.fill(); - } - } else { - this.laserPos = this.position; - } - }; + }; + } }, laser(x, y, radius = 30) { //only on level 1 @@ -706,14 +708,17 @@ const spawn = { me.collisionFilter.mask = 0x010111; //can't touch player // me.memory = 420; me.do = function () { + this.seePlayerCheck(); this.attraction(); this.gravity(); //draw - if (this.seePlayer.yes) { - if (this.alpha < 1) this.alpha += 0.01; - } else { - if (this.alpha > 0) this.alpha -= 0.03; + if (!mech.isBodiesAsleep) { + if (this.seePlayer.yes) { + if (this.alpha < 1) this.alpha += 0.01; + } else { + if (this.alpha > 0) this.alpha -= 0.03; + } } if (this.alpha > 0) { if (this.alpha > 0.95) {