Profectus-Demo/src/game/events.ts
thepaperpilot 83d41428eb Feature rewrite
- Removed `jsx()` and `JSXFunction`. You can now use `JSX.Element` like any other `Computable` value
- `joinJSX` now always requires a joiner. Just pass the array of elements or wrap them in `<>` and `</>` if there's no joiner
- Removed `coerceComponent`, `computeComponent`, and `computeOptionalComponent`; just use the `render` function now
- It's recommended to now do `<MyComponent />` instead of `<component :is="myComponent" />`
- All features no longer take the options as a type parameter, and all generic forms have been removed as a result
- Fixed `forceHideGoBack` not being respected
- Removed `deepUnref` as now things don't get unreffed before being passed into vue components by default
- Moved MarkNode to new wrapper, and removed existing `mark` properties
- Moved Tooltip to new wrapper, and made it take an options function instead of raw object
- VueFeature component now wraps all vue features, and applies styling, classes, and visibility in the wrapping div. It also adds the Node component so features don't need to
- `mergeAdjacent` now works with grids (perhaps should've used scss to reduce the amount of css this took)
- `CoercableComponent` renamed to `Renderable` since it should be used with `render`
- Replaced `isCoercableComponent` with `isJSXElement`
- Replaced `Computable` and `ProcessedComputable` with the vue built-ins `MaybeRefOrGetter` and `MaybeRef`
- `convertComputable` renamed to `processGetter`
- Also removed `GetComputableTypeWithDefault` and `GetComputableType`, which can similarly be replaced
- `dontMerge` is now a property on rows and columns rather than an undocumented css class you'd have to include on every feature within the row or column
- Fixed saves manager not being imported in addiction warning component
- Created `vueFeatureMixin` for simplifying the vue specific parts of a feature. Passes the component's properties in explicitly and directly from the feature itself
- All features should now return an object that includes props typed to omit the options object and satisfies the feature. This will ensure type correctness and pass-through custom properties. (see existing features for more thorough examples of changes)
- Replaced decorators with mixins, which won't require casting. Bonus amount decorators converted into generic bonus amount mixin. Removed effect decorator
- All `render` functions now return `JSX.Element`. The `JSX` variants (e.g. `renderJSX`) (except `joinJSX`) have been removed
- Moved all features that use the clickable component into the clickable folder
- Removed `small` property from clickable, since its a single css rule (`min-height: unset`) (you could add a small css class and pass small to any vue feature's classes property, though)
- Upgrades now use the clickable component
- Added ConversionType symbol
- Removed setDefault, just use `??=`
- Added isType function that uses a type symbol to check
- General cleanup
2024-11-29 08:02:24 -06:00

66 lines
2.6 KiB
TypeScript

import type { Settings } from "game/settings";
import { createNanoEvents } from "nanoevents";
import type { App } from "vue";
import type { Layer } from "./layers";
import state from "./state";
/** All types of events able to be sent or emitted from the global event bus. */
export interface GlobalEvents {
/**
* Sent whenever a layer is added.
* @param layer The layer being added.
* @param saveData The layer's save data object within player.
*/
addLayer: (layer: Layer, saveData: Record<string, unknown>) => void;
/**
* Sent whenever a layer is removed.
* @param layer The layer being removed.
*/
removeLayer: (layer: Layer) => void;
/**
* 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;
/**
* 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.
*/
loadSettings: (settings: Partial<Settings>) => void;
/**
* Sent when the game has ended.
*/
gameWon: VoidFunction;
/**
* 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.
*/
setupVue: (vue: App) => void;
/**
* Sent whenever a save has finished loading.
* Happens when the page is opened and upon switching saves in the saves manager.
*/
onLoad: VoidFunction;
/**
* Using document.fonts.ready returns too early on firefox, so we use document.fonts.onloadingdone instead, which doesn't accept multiple listeners.
* This event fires when that callback is called.
*/
fontsLoaded: VoidFunction;
}
/** A global event bus for hooking into {@link GlobalEvents}. */
export const globalBus = createNanoEvents<GlobalEvents>();
if ("fonts" in document) {
// This line breaks tests
// JSDom doesn't add document.fonts, and Object.defineProperty doesn't seem to work on document
document.fonts.onloadingdone = () => globalBus.emit("fontsLoaded");
}
document.onmousemove = function () {
state.mouseActivity[state.mouseActivity.length - 1] = true;
};