laser now has infinite ammo, but drains field energy
This commit is contained in:
231
js/bullets.js
231
js/bullets.js
@@ -230,28 +230,53 @@ const b = {
|
|||||||
{
|
{
|
||||||
name: "laser",
|
name: "laser",
|
||||||
ammo: 0,
|
ammo: 0,
|
||||||
ammoPack: 350,
|
// ammoPack: 350,
|
||||||
|
ammoPack: Infinity,
|
||||||
have: false,
|
have: false,
|
||||||
fire() {
|
fire() {
|
||||||
//mech.fireCDcycle = game.cycle + 1
|
//mech.fireCDcycle = game.cycle + 1
|
||||||
let best;
|
|
||||||
const color = "#f00";
|
//laser drains energy as well as bullets
|
||||||
const range = 3000;
|
const FIELD_DRAIN = 0.006
|
||||||
const path = [{
|
if (mech.fieldMeter < FIELD_DRAIN) {
|
||||||
x: mech.pos.x + 20 * Math.cos(mech.angle),
|
mech.fireCDcycle = game.cycle + 120; // cool down if out of energy
|
||||||
y: mech.pos.y + 20 * Math.sin(mech.angle)
|
} else {
|
||||||
},
|
mech.fieldMeter -= mech.fieldRegen + FIELD_DRAIN
|
||||||
{
|
let best;
|
||||||
x: mech.pos.x + range * Math.cos(mech.angle),
|
const color = "#f00";
|
||||||
y: mech.pos.y + range * Math.sin(mech.angle)
|
const range = 3000;
|
||||||
}
|
const path = [{
|
||||||
];
|
x: mech.pos.x + 20 * Math.cos(mech.angle),
|
||||||
const vertexCollision = function (v1, v1End, domain) {
|
y: mech.pos.y + 20 * Math.sin(mech.angle)
|
||||||
for (let i = 0; i < domain.length; ++i) {
|
},
|
||||||
let vertices = domain[i].vertices;
|
{
|
||||||
const len = vertices.length - 1;
|
x: mech.pos.x + range * Math.cos(mech.angle),
|
||||||
for (let j = 0; j < len; j++) {
|
y: mech.pos.y + range * Math.sin(mech.angle)
|
||||||
results = game.checkLineIntersection(v1, v1End, vertices[j], vertices[j + 1]);
|
}
|
||||||
|
];
|
||||||
|
const vertexCollision = function (v1, v1End, domain) {
|
||||||
|
for (let i = 0; i < domain.length; ++i) {
|
||||||
|
let vertices = domain[i].vertices;
|
||||||
|
const len = vertices.length - 1;
|
||||||
|
for (let j = 0; j < len; j++) {
|
||||||
|
results = game.checkLineIntersection(v1, v1End, vertices[j], vertices[j + 1]);
|
||||||
|
if (results.onLine1 && results.onLine2) {
|
||||||
|
const dx = v1.x - results.x;
|
||||||
|
const dy = v1.y - results.y;
|
||||||
|
const dist2 = dx * dx + dy * dy;
|
||||||
|
if (dist2 < best.dist2 && (!domain[i].mob || domain[i].alive)) {
|
||||||
|
best = {
|
||||||
|
x: results.x,
|
||||||
|
y: results.y,
|
||||||
|
dist2: dist2,
|
||||||
|
who: domain[i],
|
||||||
|
v1: vertices[j],
|
||||||
|
v2: vertices[j + 1]
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
results = game.checkLineIntersection(v1, v1End, vertices[0], vertices[len]);
|
||||||
if (results.onLine1 && results.onLine2) {
|
if (results.onLine1 && results.onLine2) {
|
||||||
const dx = v1.x - results.x;
|
const dx = v1.x - results.x;
|
||||||
const dy = v1.y - results.y;
|
const dy = v1.y - results.y;
|
||||||
@@ -262,78 +287,48 @@ const b = {
|
|||||||
y: results.y,
|
y: results.y,
|
||||||
dist2: dist2,
|
dist2: dist2,
|
||||||
who: domain[i],
|
who: domain[i],
|
||||||
v1: vertices[j],
|
v1: vertices[0],
|
||||||
v2: vertices[j + 1]
|
v2: vertices[len]
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
results = game.checkLineIntersection(v1, v1End, vertices[0], vertices[len]);
|
};
|
||||||
if (results.onLine1 && results.onLine2) {
|
const checkforCollisions = function () {
|
||||||
const dx = v1.x - results.x;
|
best = {
|
||||||
const dy = v1.y - results.y;
|
x: null,
|
||||||
const dist2 = dx * dx + dy * dy;
|
y: null,
|
||||||
if (dist2 < best.dist2 && (!domain[i].mob || domain[i].alive)) {
|
dist2: Infinity,
|
||||||
best = {
|
who: null,
|
||||||
x: results.x,
|
v1: null,
|
||||||
y: results.y,
|
v2: null
|
||||||
dist2: dist2,
|
};
|
||||||
who: domain[i],
|
vertexCollision(path[path.length - 2], path[path.length - 1], mob);
|
||||||
v1: vertices[0],
|
vertexCollision(path[path.length - 2], path[path.length - 1], map);
|
||||||
v2: vertices[len]
|
vertexCollision(path[path.length - 2], path[path.length - 1], body);
|
||||||
};
|
};
|
||||||
}
|
const laserHitMob = function (dmg) {
|
||||||
|
if (best.who.alive) {
|
||||||
|
dmg *= b.dmgScale * 0.05;
|
||||||
|
best.who.damage(dmg);
|
||||||
|
best.who.locatePlayer();
|
||||||
|
//draw mob damage circle
|
||||||
|
ctx.fillStyle = color;
|
||||||
|
ctx.beginPath();
|
||||||
|
ctx.arc(path[path.length - 1].x, path[path.length - 1].y, Math.sqrt(dmg) * 100, 0, 2 * Math.PI);
|
||||||
|
ctx.fill();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
};
|
|
||||||
const checkforCollisions = function () {
|
|
||||||
best = {
|
|
||||||
x: null,
|
|
||||||
y: null,
|
|
||||||
dist2: Infinity,
|
|
||||||
who: null,
|
|
||||||
v1: null,
|
|
||||||
v2: null
|
|
||||||
};
|
};
|
||||||
vertexCollision(path[path.length - 2], path[path.length - 1], mob);
|
|
||||||
vertexCollision(path[path.length - 2], path[path.length - 1], map);
|
|
||||||
vertexCollision(path[path.length - 2], path[path.length - 1], body);
|
|
||||||
};
|
|
||||||
const laserHitMob = function (dmg) {
|
|
||||||
if (best.who.alive) {
|
|
||||||
dmg *= b.dmgScale * 0.065;
|
|
||||||
best.who.damage(dmg);
|
|
||||||
best.who.locatePlayer();
|
|
||||||
//draw mob damage circle
|
|
||||||
ctx.fillStyle = color;
|
|
||||||
ctx.beginPath();
|
|
||||||
ctx.arc(path[path.length - 1].x, path[path.length - 1].y, Math.sqrt(dmg) * 100, 0, 2 * Math.PI);
|
|
||||||
ctx.fill();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const reflection = function () {
|
const reflection = function () {
|
||||||
// https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
|
// https://math.stackexchange.com/questions/13261/how-to-get-a-reflection-vector
|
||||||
const n = Matter.Vector.perp(Matter.Vector.normalise(Matter.Vector.sub(best.v1, best.v2)));
|
const n = Matter.Vector.perp(Matter.Vector.normalise(Matter.Vector.sub(best.v1, best.v2)));
|
||||||
const d = Matter.Vector.sub(path[path.length - 1], path[path.length - 2]);
|
const d = Matter.Vector.sub(path[path.length - 1], path[path.length - 2]);
|
||||||
const nn = Matter.Vector.mult(n, 2 * Matter.Vector.dot(d, n));
|
const nn = Matter.Vector.mult(n, 2 * Matter.Vector.dot(d, n));
|
||||||
const r = Matter.Vector.normalise(Matter.Vector.sub(d, nn));
|
const r = Matter.Vector.normalise(Matter.Vector.sub(d, nn));
|
||||||
path[path.length] = Matter.Vector.add(Matter.Vector.mult(r, range), path[path.length - 1]);
|
path[path.length] = Matter.Vector.add(Matter.Vector.mult(r, range), path[path.length - 1]);
|
||||||
};
|
|
||||||
//beam before reflection
|
|
||||||
checkforCollisions();
|
|
||||||
if (best.dist2 != Infinity) {
|
|
||||||
//if hitting something
|
|
||||||
path[path.length - 1] = {
|
|
||||||
x: best.x,
|
|
||||||
y: best.y
|
|
||||||
};
|
};
|
||||||
laserHitMob(1);
|
//beam before reflection
|
||||||
|
|
||||||
//1st reflection beam
|
|
||||||
reflection();
|
|
||||||
//ugly bug fix: this stops the reflection on a bug where the beam gets trapped inside a body
|
|
||||||
let who = best.who;
|
|
||||||
checkforCollisions();
|
checkforCollisions();
|
||||||
if (best.dist2 != Infinity) {
|
if (best.dist2 != Infinity) {
|
||||||
//if hitting something
|
//if hitting something
|
||||||
@@ -341,41 +336,55 @@ const b = {
|
|||||||
x: best.x,
|
x: best.x,
|
||||||
y: best.y
|
y: best.y
|
||||||
};
|
};
|
||||||
laserHitMob(0.75);
|
laserHitMob(1);
|
||||||
|
|
||||||
//2nd reflection beam
|
//1st reflection beam
|
||||||
|
reflection();
|
||||||
//ugly bug fix: this stops the reflection on a bug where the beam gets trapped inside a body
|
//ugly bug fix: this stops the reflection on a bug where the beam gets trapped inside a body
|
||||||
if (who !== best.who) {
|
let who = best.who;
|
||||||
reflection();
|
checkforCollisions();
|
||||||
checkforCollisions();
|
if (best.dist2 != Infinity) {
|
||||||
if (best.dist2 != Infinity) {
|
//if hitting something
|
||||||
//if hitting something
|
path[path.length - 1] = {
|
||||||
path[path.length - 1] = {
|
x: best.x,
|
||||||
x: best.x,
|
y: best.y
|
||||||
y: best.y
|
};
|
||||||
};
|
laserHitMob(0.75);
|
||||||
laserHitMob(0.5);
|
|
||||||
|
//2nd reflection beam
|
||||||
|
//ugly bug fix: this stops the reflection on a bug where the beam gets trapped inside a body
|
||||||
|
if (who !== best.who) {
|
||||||
|
reflection();
|
||||||
|
checkforCollisions();
|
||||||
|
if (best.dist2 != Infinity) {
|
||||||
|
//if hitting something
|
||||||
|
path[path.length - 1] = {
|
||||||
|
x: best.x,
|
||||||
|
y: best.y
|
||||||
|
};
|
||||||
|
laserHitMob(0.5);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
ctx.fillStyle = color;
|
||||||
ctx.fillStyle = color;
|
ctx.strokeStyle = color;
|
||||||
ctx.strokeStyle = color;
|
ctx.lineWidth = 2;
|
||||||
ctx.lineWidth = 2;
|
ctx.lineDashOffset = 300 * Math.random()
|
||||||
ctx.lineDashOffset = 300 * Math.random()
|
// ctx.setLineDash([200 * Math.random(), 250 * Math.random()]);
|
||||||
// ctx.setLineDash([200 * Math.random(), 250 * Math.random()]);
|
|
||||||
|
|
||||||
ctx.setLineDash([50 + 120 * Math.random(), 50 * Math.random()]);
|
ctx.setLineDash([50 + 120 * Math.random(), 50 * Math.random()]);
|
||||||
for (let i = 1, len = path.length; i < len; ++i) {
|
for (let i = 1, len = path.length; i < len; ++i) {
|
||||||
ctx.beginPath();
|
ctx.beginPath();
|
||||||
ctx.moveTo(path[i - 1].x, path[i - 1].y);
|
ctx.moveTo(path[i - 1].x, path[i - 1].y);
|
||||||
ctx.lineTo(path[i].x, path[i].y);
|
ctx.lineTo(path[i].x, path[i].y);
|
||||||
ctx.stroke();
|
ctx.stroke();
|
||||||
ctx.globalAlpha *= 0.5; //reflections are less intense
|
ctx.globalAlpha *= 0.5; //reflections are less intense
|
||||||
// ctx.globalAlpha -= 0.1; //reflections are less intense
|
// ctx.globalAlpha -= 0.1; //reflections are less intense
|
||||||
|
}
|
||||||
|
ctx.setLineDash([0, 0]);
|
||||||
|
ctx.globalAlpha = 1;
|
||||||
}
|
}
|
||||||
ctx.setLineDash([0, 0]);
|
|
||||||
ctx.globalAlpha = 1;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -204,6 +204,7 @@ const level = {
|
|||||||
},
|
},
|
||||||
//empty map for testing mobs
|
//empty map for testing mobs
|
||||||
intro() {
|
intro() {
|
||||||
|
b.giveGuns(0, 1000)
|
||||||
game.zoomScale = 1000 //1400 is normal
|
game.zoomScale = 1000 //1400 is normal
|
||||||
game.zoomTransition(1600, 1)
|
game.zoomTransition(1600, 1)
|
||||||
|
|
||||||
|
|||||||
@@ -72,8 +72,14 @@ const powerUps = {
|
|||||||
//ammo given scales as mobs take more hits to kill
|
//ammo given scales as mobs take more hits to kill
|
||||||
const ammo = Math.ceil((target.ammoPack * (0.60 + 0.5 * Math.random())) / b.dmgScale);
|
const ammo = Math.ceil((target.ammoPack * (0.60 + 0.5 * Math.random())) / b.dmgScale);
|
||||||
target.ammo += ammo;
|
target.ammo += ammo;
|
||||||
game.updateGunHUD();
|
if (target.ammo === Infinity) {
|
||||||
game.makeTextLog("+" + ammo + " ammo: " + target.name, 180);
|
mech.fieldMeter = 1;
|
||||||
|
game.makeTextLog("+energy", 180);
|
||||||
|
} else {
|
||||||
|
game.updateGunHUD();
|
||||||
|
game.makeTextLog("+" + ammo + " ammo: " + target.name, 180);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
gun: {
|
gun: {
|
||||||
|
|||||||
Reference in New Issue
Block a user