From 9c45653b8bd6f995d7089333bf6b37ebd9678894 Mon Sep 17 00:00:00 2001 From: thepaperpilot Date: Fri, 11 Mar 2022 10:02:28 -0600 Subject: [PATCH] Add setupAutoClick --- src/features/clickables/clickable.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) 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?.(); + } + }); +}