2022-03-03 02:12:56 +00:00
|
|
|
import projInfo from "@/data/projInfo.json";
|
2022-01-14 04:25:47 +00:00
|
|
|
import Decimal, { DecimalSource } from "@/util/bignum";
|
|
|
|
import { createNanoEvents } from "nanoevents";
|
2022-01-25 04:23:30 +00:00
|
|
|
import { App, Ref } from "vue";
|
|
|
|
import { GenericLayer } from "./layers";
|
2022-01-14 04:25:47 +00:00
|
|
|
import player from "./player";
|
|
|
|
import settings, { Settings } from "./settings";
|
|
|
|
import state from "./state";
|
|
|
|
|
|
|
|
export interface GlobalEvents {
|
|
|
|
addLayer: (layer: GenericLayer, saveData: Record<string, unknown>) => void;
|
|
|
|
removeLayer: (layer: GenericLayer) => void;
|
|
|
|
update: (diff: Decimal, trueDiff: number) => void;
|
|
|
|
loadSettings: (settings: Partial<Settings>) => void;
|
|
|
|
setupVue: (vue: App) => void;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const globalBus = createNanoEvents<GlobalEvents>();
|
|
|
|
|
|
|
|
let intervalID: number | null = null;
|
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
// Not imported immediately due to dependency cycles
|
|
|
|
// This gets set during startGameLoop(), and will only be used in the update function
|
|
|
|
let hasWon: null | Ref<boolean> = null;
|
|
|
|
|
2022-01-14 04:25:47 +00:00
|
|
|
function update() {
|
|
|
|
const now = Date.now();
|
|
|
|
let diff: DecimalSource = (now - player.time) / 1e3;
|
|
|
|
player.time = now;
|
|
|
|
const trueDiff = diff;
|
|
|
|
|
|
|
|
state.lastTenTicks.push(trueDiff);
|
|
|
|
if (state.lastTenTicks.length > 10) {
|
|
|
|
state.lastTenTicks = state.lastTenTicks.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop here if the game is paused on the win screen
|
2022-01-25 04:23:30 +00:00
|
|
|
if (hasWon?.value && !player.keepGoing) {
|
2022-01-14 04:25:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Stop here if the player had a NaN value
|
|
|
|
if (state.hasNaN) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
diff = new Decimal(diff).max(0);
|
|
|
|
|
2022-02-27 19:49:34 +00:00
|
|
|
if (player.devSpeed === 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-14 04:25:47 +00:00
|
|
|
// Add offline time if any
|
|
|
|
if (player.offlineTime != undefined) {
|
2022-03-03 02:12:56 +00:00
|
|
|
if (Decimal.gt(player.offlineTime, projInfo.offlineLimit * 3600)) {
|
|
|
|
player.offlineTime = new Decimal(projInfo.offlineLimit * 3600);
|
2022-01-14 04:25:47 +00:00
|
|
|
}
|
2022-01-25 04:25:34 +00:00
|
|
|
if (Decimal.gt(player.offlineTime, 0) && player.devSpeed !== 0) {
|
|
|
|
const offlineDiff = Decimal.div(player.offlineTime, 10).max(diff);
|
|
|
|
player.offlineTime = Decimal.sub(player.offlineTime, offlineDiff);
|
2022-01-14 04:25:47 +00:00
|
|
|
diff = diff.add(offlineDiff);
|
|
|
|
} else if (player.devSpeed === 0) {
|
2022-01-25 04:25:34 +00:00
|
|
|
player.offlineTime = Decimal.add(player.offlineTime, diff);
|
2022-01-14 04:25:47 +00:00
|
|
|
}
|
2022-01-25 04:25:34 +00:00
|
|
|
if (!player.offlineProd || Decimal.lt(player.offlineTime, 0)) {
|
2022-01-14 04:25:47 +00:00
|
|
|
player.offlineTime = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cap at max tick length
|
2022-03-03 02:12:56 +00:00
|
|
|
diff = Decimal.min(diff, projInfo.maxTickLength);
|
2022-01-14 04:25:47 +00:00
|
|
|
|
|
|
|
// Apply dev speed
|
|
|
|
if (player.devSpeed != undefined) {
|
|
|
|
diff = diff.times(player.devSpeed);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update
|
|
|
|
if (diff.eq(0)) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-25 04:25:34 +00:00
|
|
|
player.timePlayed = Decimal.add(player.timePlayed, diff);
|
2022-01-14 04:25:47 +00:00
|
|
|
globalBus.emit("update", diff, trueDiff);
|
|
|
|
|
|
|
|
if (settings.unthrottled) {
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
if (intervalID != null) {
|
|
|
|
clearInterval(intervalID);
|
|
|
|
intervalID = null;
|
|
|
|
}
|
|
|
|
} else if (intervalID == null) {
|
|
|
|
intervalID = setInterval(update, 50);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-25 04:23:30 +00:00
|
|
|
export async function startGameLoop() {
|
2022-03-03 02:12:56 +00:00
|
|
|
hasWon = (await import("@/data/projEntry")).hasWon;
|
2022-01-14 04:25:47 +00:00
|
|
|
if (settings.unthrottled) {
|
|
|
|
requestAnimationFrame(update);
|
|
|
|
} else {
|
|
|
|
intervalID = setInterval(update, 50);
|
|
|
|
}
|
|
|
|
}
|