// game Object ********************************************************
//*********************************************************************
const game = {
loop() {
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();
level.start();
}
game.gravity();
Engine.update(engine, game.delta);
game.wipe();
game.textLog();
mech.keyMove();
level.checkZones();
level.checkQuery();
mech.move();
mech.look();
game.fallChecks();
ctx.save();
game.camera();
if (game.testing) {
mech.draw();
game.draw.wireFrame();
game.draw.cons();
game.draw.testing();
game.drawCircle();
ctx.restore();
game.getCoords.out();
game.testingOutput();
} else {
level.drawFillBGs();
level.exit.draw();
level.enter.draw();
game.draw.powerUp();
mobs.draw();
game.draw.cons();
game.draw.body();
mobs.loop();
mech.draw();
mech.hold();
level.drawFills();
game.draw.drawMapPath();
b.draw();
b.fire();
game.drawCircle();
ctx.restore();
}
game.drawCursor();
},
mouse: {
x: canvas.width / 2,
y: canvas.height / 2
},
mouseInGame: {
x: 0,
y: 0
},
levelsCleared: 0,
g: 0.001,
dmgScale: null, //set in levels.setDifficulty
accelScale: null, //set in levels.setDifficulty
CDScale: null, //set in levels.setDifficulty
lookFreqScale: null, //set in levels.setDifficulty
onTitlePage: true,
paused: false,
testing: false, //testing mode: shows wireframe and some variables
cycle: 0, //total cycles, 60 per second
fpsCap: null, //limits frames per second to 144/2=72, on most monitors the fps is capped at 60fps by the hardware
fpsCapDefault: 72, //use to change fpsCap back to normal after a hit from a mob
cyclePaused: 0,
fallHeight: 3000, //below this y position the player dies
lastTimeStamp: 0, //tracks time stamps for measuring delta
delta: 1000 / 60, //speed of game engine //looks like it has to be 16 to match player input
buttonCD: 0,
isBodyDamage: true,
isEasyMode: false,
difficulty: null,
// dropFPS(cap = 40, time = 15) {
// game.fpsCap = cap
// game.fpsInterval = 1000 / game.fpsCap;
// game.defaultFPSCycle = game.cycle + time
// const normalFPS = function () {
// if (game.defaultFPSCycle < game.cycle) {
// game.fpsCap = 72
// game.fpsInterval = 1000 / game.fpsCap;
// } else {
// requestAnimationFrame(normalFPS);
// }
// };
// requestAnimationFrame(normalFPS);
// },
drawCursor() {
const size = 10;
ctx.beginPath();
ctx.moveTo(game.mouse.x - size, game.mouse.y);
ctx.lineTo(game.mouse.x + size, game.mouse.y);
ctx.moveTo(game.mouse.x, game.mouse.y - size);
ctx.lineTo(game.mouse.x, game.mouse.y + size);
ctx.lineWidth = 2;
ctx.strokeStyle = "#000"; //'rgba(0,0,0,0.4)'
ctx.stroke(); // Draw it
},
drawList: [], //so you can draw a first frame of explosions.. I know this is bad
drawTime: 8, //how long circles are drawn. use to push into drawlist.time
mobDmgColor: "rgba(255,0,0,0.7)", //used top push into drawList.color
playerDmgColor: "rgba(0,0,0,0.7)", //used top push into drawList.color
drawCircle() {
//draws a circle for two cycles, used for showing damage mostly
let i = this.drawList.length;
while (i--) {
ctx.beginPath(); //draw circle
ctx.arc(this.drawList[i].x, this.drawList[i].y, this.drawList[i].radius, 0, 2 * Math.PI);
ctx.fillStyle = this.drawList[i].color;
ctx.fill();
if (this.drawList[i].time) {
//remove when timer runs out
this.drawList[i].time--;
} else {
this.drawList.splice(i, 1);
}
}
},
lastLogTime: 0,
lastLogTimeBig: 0,
boldActiveGunHUD() {
if (b.inventory.length > 0) {
for (let i = 0, len = b.inventory.length; i < len; ++i) {
// document.getElementById(b.inventory[i]).style.fontSize = "25px";
document.getElementById(b.inventory[i]).style.opacity = "0.3";
}
// document.getElementById(b.activeGun).style.fontSize = "30px";
if (document.getElementById(b.activeGun)) document.getElementById(b.activeGun).style.opacity = "1";
}
},
updateGunHUD() {
for (let i = 0, len = b.inventory.length; i < len; ++i) {
document.getElementById(b.inventory[i]).innerHTML = b.guns[b.inventory[i]].name + " - " + b.guns[b.inventory[i]].ammo;
}
},
makeGunHUD() {
//remove all nodes
const myNode = document.getElementById("guns");
while (myNode.firstChild) {
myNode.removeChild(myNode.firstChild);
}
//add nodes
for (let i = 0, len = b.inventory.length; i < len; ++i) {
const node = document.createElement("div");
node.setAttribute("id", b.inventory[i]);
let textnode = document.createTextNode(b.guns[b.inventory[i]].name + " - " + b.guns[b.inventory[i]].ammo);
node.appendChild(textnode);
document.getElementById("guns").appendChild(node);
}
game.boldActiveGunHUD();
},
updateModHUD() {
let text = ""
for (let i = 0, len = b.mods.length; i < len; i++) { //add mods
if (b.mods[i].have) {
if (text) text += " " //add a new line, but not on the first line
text += b.mods[i].name
}
}
document.getElementById("mods").innerHTML = text
},
replaceTextLog: true,
//
SVGleftMouse: '',
SVGrightMouse: '',
makeTextLog(text, time = 180) {
if (game.replaceTextLog) {
document.getElementById("text-log").innerHTML = text;
document.getElementById("text-log").style.opacity = 1;
game.lastLogTime = mech.cycle + time;
}
},
textLog() {
if (game.lastLogTime && game.lastLogTime < mech.cycle) {
game.lastLogTime = 0;
game.replaceTextLog = true
// document.getElementById("text-log").innerHTML = " ";
document.getElementById("text-log").style.opacity = 0;
}
},
nextGun() {
if (b.inventory.length > 0) {
b.inventoryGun++;
if (b.inventoryGun > b.inventory.length - 1) b.inventoryGun = 0;
game.switchGun();
}
},
previousGun() {
if (b.inventory.length > 0) {
b.inventoryGun--;
if (b.inventoryGun < 0) b.inventoryGun = b.inventory.length - 1;
game.switchGun();
}
},
switchGun() {
b.activeGun = b.inventory[b.inventoryGun];
game.updateGunHUD();
game.boldActiveGunHUD();
// mech.drop();
},
// tips = [
// "You can throw blocks at dangerous speeds by holding the right mouse or spacebar.",
// "You can use your field to block damage. (right mouse or spacebar)",
// "Explosive weapons, like the grenade are good at clearing groups.",
// "Larger and faster bullets do more damage.",
// "You take more damage from colliding with larger baddies",
// "Holding large blocks slows down the player.",
// "holding blocks prevents the field energy from regenerating.",
// `There are ${mech.fieldUpgrades.length-1} possible field upgrades.`,
// `There are ${b.guns.length} different guns.`,
// "You keep field upgrades after you die.",
// "Unique level bosses always drop a field upgrade if you don't have one.",
// "You jump higher if you hold down the jump button.",
// "Crouching while firing makes bullets go faster, but slows the rate of fire.",
// ]
keyPress() {
if (keys[189]) {
// - key
game.zoomScale /= 0.9;
game.setZoom();
} else if (keys[187]) {
// = key
game.zoomScale *= 0.9;
game.setZoom();
}
//full screen toggle
// if (keys[13]) {
// //enter key
// var doc = window.document;
// var docEl = doc.documentElement;
// var requestFullScreen = docEl.requestFullscreen || docEl.mozRequestFullScreen || docEl.webkitRequestFullScreen || docEl.msRequestFullscreen;
// var cancelFullScreen = doc.exitFullscreen || doc.mozCancelFullScreen || doc.webkitExitFullscreen || doc.msExitFullscreen;
// if (!doc.fullscreenElement && !doc.mozFullScreenElement && !doc.webkitFullscreenElement && !doc.msFullscreenElement) {
// requestFullScreen.call(docEl);
// } else {
// cancelFullScreen.call(doc);
// }
// setupCanvas();
// }
if (keys[69]) {
// e swap to next active gun
game.nextGun();
} else if (keys[81]) {
//q swap to previous active gun
game.previousGun();
}
if (keys[80]) {
//p for pause
if (game.paused) {
game.paused = false;
requestAnimationFrame(cycle);
} else {
game.paused = true;
game.replaceTextLog = true;
game.makeTextLog("
PAUSED
", 1);
// let text = "
PAUSED
"
// //output current mod, field, and gun info when paused
// if (mech.fieldMode !== 0) text += "