diff --git a/src/features/clickables/clickable.ts b/src/features/clickables/clickable.ts index 7747150..dfd4b08 100644 --- a/src/features/clickables/clickable.ts +++ b/src/features/clickables/clickable.ts @@ -9,6 +9,8 @@ import { StyleValue, Visibility } from "features/feature"; +import { GenericLayer } from "game/layers"; +import { Unsubscribe } from "nanoevents"; import { Computable, GetComputableType, @@ -17,7 +19,7 @@ import { ProcessedComputable } from "util/computed"; import { createLazyProxy } from "util/proxies"; -import { unref } from "vue"; +import { computed, unref } from "vue"; export const ClickableType = Symbol("Clickable"); @@ -131,3 +133,16 @@ export function createClickable( return clickable as unknown as Clickable; }); } + +export function setupAutoClick( + layer: GenericLayer, + clickable: GenericClickable, + autoActive: Computable = true +): Unsubscribe { + const isActive = typeof autoActive === "function" ? computed(autoActive) : autoActive; + return layer.on("update", () => { + if (unref(isActive) && unref(clickable.canClick)) { + clickable.onClick?.(); + } + }); +}