Profectus-Demo/src/game/player.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-06-05 22:30:40 +00:00
import type { Ref } from "vue";
import { reactive, unref } from "vue";
2022-07-15 05:55:36 +00:00
/** The player save data object. */
export interface Player {
2022-07-15 05:55:36 +00:00
/** The ID of this save. */
2022-01-14 04:25:47 +00:00
id: string;
2022-07-15 05:55:36 +00:00
/** A multiplier for time passing. Set to 0 when the game is paused. */
devSpeed: number | null;
2022-07-15 05:55:36 +00:00
/** The display name of this save. */
2022-01-14 04:25:47 +00:00
name: string;
2022-07-15 05:55:36 +00:00
/** The open tabs. */
2022-01-14 04:25:47 +00:00
tabs: Array<string>;
2022-07-15 05:55:36 +00:00
/** The current time this save was last opened at, in ms since the unix epoch. */
2022-01-14 04:25:47 +00:00
time: number;
2022-07-15 05:55:36 +00:00
/** Whether or not to automatically save every couple of seconds and on tab close. */
2022-01-14 04:25:47 +00:00
autosave: boolean;
2022-07-15 05:55:36 +00:00
/** Whether or not to apply offline time when loading this save. */
2022-01-14 04:25:47 +00:00
offlineProd: boolean;
2022-07-15 05:55:36 +00:00
/** How much offline time has been accumulated and not yet processed. */
offlineTime: number | null;
2022-07-15 05:55:36 +00:00
/** How long, in ms, this game has been played. */
timePlayed: number;
2022-07-15 05:55:36 +00:00
/** Whether or not to continue playing after {@link data/projEntry.hasWon} is true. */
2022-01-14 04:25:47 +00:00
keepGoing: boolean;
2022-07-15 05:55:36 +00:00
/** The ID of this project, to make sure saves aren't imported into the wrong project. */
2022-01-14 04:25:47 +00:00
modID: string;
2022-07-15 05:55:36 +00:00
/** The version of the project this save was created by. Used for upgrading saves for new versions. */
2022-01-14 04:25:47 +00:00
modVersion: string;
2022-07-15 05:55:36 +00:00
/** A dictionary of layer save data. */
2022-06-05 22:30:40 +00:00
layers: Record<string, LayerData<unknown>>;
2022-01-14 04:25:47 +00:00
}
2022-07-15 05:55:36 +00:00
/** A layer's save data. Automatically unwraps refs. */
2022-06-05 22:30:40 +00:00
export type LayerData<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? Record<string, LayerData<U>>
2022-06-05 22:30:40 +00:00
: T[P] extends Record<string, never>
? never
: T[P] extends Ref<infer S>
? S
: T[P] extends object
? LayerData<T[P]>
: T[P];
};
const player = reactive<Player>({
id: "",
2022-01-14 04:25:47 +00:00
devSpeed: null,
name: "",
tabs: [],
time: -1,
autosave: true,
offlineProd: true,
offlineTime: null,
timePlayed: 0,
keepGoing: false,
modID: "",
modVersion: "",
layers: {}
});
export default window.player = player;
2022-07-15 05:55:36 +00:00
/** Convert a player save data object into a JSON string. Unwraps refs. */
export function stringifySave(player: Player): string {
return JSON.stringify(player, (key, value) => unref(value));
2022-01-14 04:25:47 +00:00
}
2022-07-10 03:09:25 +00:00
declare global {
2022-07-10 05:43:52 +00:00
/** Augment the window object so the player can be accessed from the console. */
2022-07-10 03:09:25 +00:00
interface Window {
player: Player;
}
}