Profectus-Demo/src/game/events.ts

155 lines
4.9 KiB
TypeScript
Raw Normal View History

2022-03-04 03:39:48 +00:00
import projInfo from "data/projInfo.json";
2022-06-27 00:17:22 +00:00
import player from "game/player";
import type { Settings } from "game/settings";
import settings from "game/settings";
import state from "game/state";
2022-01-14 04:25:47 +00:00
import { createNanoEvents } from "nanoevents";
2022-06-27 00:17:22 +00:00
import Decimal from "util/bignum";
import type { App, Ref } from "vue";
import { watch } from "vue";
import type { GenericLayer } from "./layers";
2022-01-14 04:25:47 +00:00
2022-07-15 05:55:36 +00:00
/** All types of events able to be sent or emitted from the global event bus. */
2022-01-14 04:25:47 +00:00
export interface GlobalEvents {
2022-07-15 05:55:36 +00:00
/**
* Sent whenever a layer is added.
* @param layer The layer being added.
* @param saveData The layer's save data object within player.
*/
2022-01-14 04:25:47 +00:00
addLayer: (layer: GenericLayer, saveData: Record<string, unknown>) => void;
2022-07-15 05:55:36 +00:00
/**
* Sent whenever a layer is removed.
* @param layer The layer being removed.
*/
2022-01-14 04:25:47 +00:00
removeLayer: (layer: GenericLayer) => void;
2022-07-15 05:55:36 +00:00
/**
* Sent every game tick. Runs the life cycle of the project.
* @param diff The delta time since last tick, in ms.
* @param trueDiff The delta time since last tick, in ms. Unaffected by time modifiers like {@link game/player.Player.devSpeed} or {@link game/player.Player.offlineTime}. Intended for things like updating animations.
*/
update: (diff: number, trueDiff: number) => void;
2022-07-15 05:55:36 +00:00
/**
* Sent when constructing the {@link Settings} object.
* Use it to add default values for custom properties to the object.
* @param settings The settings object being constructed.
* @see {@link features/features.setDefault} for setting default values.
*/
2022-01-14 04:25:47 +00:00
loadSettings: (settings: Partial<Settings>) => void;
2022-07-15 05:55:36 +00:00
/**
* Sent when the game has ended.
*/
2022-03-11 23:26:39 +00:00
gameWon: VoidFunction;
2022-07-15 05:55:36 +00:00
/**
* Sent when setting up the Vue Application instance.
* Use it to register global components or otherwise set up things that should affect Vue globally.
* @param vue The Vue App being constructed.
*/
2022-01-14 04:25:47 +00:00
setupVue: (vue: App) => void;
2022-07-22 00:23:33 +00:00
/**
* Sent whenever a save has finished loading.
* Happens when the page is opened and upon switching saves in the saves manager.
*/
onLoad: VoidFunction;
2022-01-14 04:25:47 +00:00
}
2022-07-15 05:55:36 +00:00
/** A global event bus for hooking into {@link GlobalEvents}. */
2022-01-14 04:25:47 +00:00
export const globalBus = createNanoEvents<GlobalEvents>();
let intervalID: NodeJS.Timer | null = null;
2022-01-14 04:25:47 +00:00
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 = (now - player.time) / 1e3;
2022-01-14 04:25:47 +00:00
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 = Math.max(diff, 0);
2022-01-14 04:25:47 +00:00
if (player.devSpeed === 0) {
return;
}
2022-01-14 04:25:47 +00:00
// Add offline time if any
if (player.offlineTime != undefined) {
if (Decimal.gt(player.offlineTime, projInfo.offlineLimit * 3600)) {
player.offlineTime = 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 = Math.max(player.offlineTime / 10, diff);
player.offlineTime = player.offlineTime - offlineDiff;
diff += offlineDiff;
2022-01-14 04:25:47 +00:00
} else if (player.devSpeed === 0) {
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
diff = Math.min(diff, projInfo.maxTickLength);
2022-01-14 04:25:47 +00:00
// Apply dev speed
if (player.devSpeed != undefined) {
diff *= player.devSpeed;
}
if (!Number.isFinite(diff)) {
diff = 1e308;
2022-01-14 04:25:47 +00:00
}
// Update
if (Decimal.eq(diff, 0)) {
2022-01-14 04:25:47 +00:00
return;
}
player.timePlayed += diff;
if (!Number.isFinite(player.timePlayed)) {
player.timePlayed = 1e308;
}
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-07-15 05:55:36 +00:00
/** Starts the game loop for the project, which updates the game in ticks. */
2022-01-25 04:23:30 +00:00
export async function startGameLoop() {
2022-03-04 03:39:48 +00:00
hasWon = (await import("data/projEntry")).hasWon;
2022-03-11 23:26:39 +00:00
watch(hasWon, hasWon => {
if (hasWon) {
globalBus.emit("gameWon");
}
});
2022-01-14 04:25:47 +00:00
if (settings.unthrottled) {
requestAnimationFrame(update);
} else {
intervalID = setInterval(update, 50);
}
}