From 573828a6aa3d57568e0e8231a29a80a96235aef9 Mon Sep 17 00:00:00 2001 From: Mestima Date: Mon, 4 Oct 2021 20:47:35 +0300 Subject: [PATCH] added keybinds support --- data/modinfo.lua | 46 +++++++++++++++++ data/modmain.lua | 125 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 163 insertions(+), 8 deletions(-) diff --git a/data/modinfo.lua b/data/modinfo.lua index be5215b..d20bbcf 100644 --- a/data/modinfo.lua +++ b/data/modinfo.lua @@ -23,6 +23,12 @@ priority = 0 server_filter_tags = {"Wanda's bag"} +local keys = { + "None", + "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z", + "LSHIFT","LALT","LCTRL","TAB","BACKSPACE","PERIOD","SLASH","TILDE" +} + configuration_options = { { name = "lang", @@ -55,5 +61,45 @@ configuration_options = { { description = "you can't", data = false } }, default = true + }, + { + name = "key_binds", + label = "Enable Keybinds", + hover = "Enable Wanda's Keybinds or not", + options = { + { description = "enable", data = true }, + { description = "disable", data = false } + }, + default = true + }, + { + name = "ageless_key", + label = "Ageless watch Keybind", + hover = "Use available Ageless Watch with...", + options = {}, + default = "X" + }, + { + name = "backstep_key", + label = "Backstep watch Keybind", + hover = "Use available Backstep Watch with...", + options = {}, + default = "Z" + }, + { + name = "backtrek_key", + label = "Backtrek watch Keybind", + hover = "Use available Backtrek Watch with...", + options = {}, + default = "C" } } + +local function filltable(tbl) + for i=1, #keys do + tbl[i] = {description = keys[i], data = keys[i]} + end +end +filltable(configuration_options[5].options) +filltable(configuration_options[6].options) +filltable(configuration_options[7].options) diff --git a/data/modmain.lua b/data/modmain.lua index 24bee20..f6b2a4e 100644 --- a/data/modmain.lua +++ b/data/modmain.lua @@ -1,14 +1,123 @@ GLOBAL.setmetatable(env,{__index=function(t,k) return GLOBAL.rawget(GLOBAL,k) end}) +local G = GLOBAL -local function GetConfig(name, default) - local opt = GetModConfigData(name) - if (not opt) then - opt = default +local agelesskey = GetModConfigData("ageless_key") +local backstepkey = GetModConfigData("backstep_key") +local backtrekkey = GetModConfigData("backtrek_key") +local keybinds = GetModConfigData("key_binds") + +local function IsInGameplay() + return G.ThePlayer ~= nil and G.TheFrontEnd:GetActiveScreen().name == "HUD" +end + +local function GetWatch(watchType) + if keybinds ~= true then return end + local items = G.ThePlayer.replica.inventory:GetItems() + local backpack = G.ThePlayer.replica.inventory:GetEquippedItem(G.EQUIPSLOTS.BODY) + local watchbag = nil + for k, v in pairs(items) do + if v.prefab == "pocketwatchpack" then + watchbag = v + end end - if (type(opt) == "table") then - opt = opt.option_data + local watchbagitems = watchbag and watchbag.replica.container and watchbag.replica.container:GetItems() or nil + local packitems = backpack and backpack.replica.container and backpack.replica.container:GetItems() or nil + local watch = nil + for k, v in pairs(items) do + if v.prefab == watchType and v:HasTag("pocketwatch_inactive") then + watch = v + break + end + end + if watch ~= nil or (packitems == nil and watchbagitems == nil) then return watch end + if (packitems ~= nil) then + for k, v in pairs(packitems) do + if v.prefab == watchType and v:HasTag("pocketwatch_inactive") then + watch = v + break + end + end + end + if watch ~= nil or watchbagitems == nil then return watch end + for k, v in pairs(watchbagitems) do + if v.prefab == watchType and v:HasTag("pocketwatch_inactive") then + watch = v + break + end + end + return watch +end + +if agelesskey ~= "None" then + if keybinds == true then + local keybind = G["KEY_"..agelesskey] + G.TheInput:AddKeyDownHandler(keybind, function() + if not IsInGameplay() then return end + local pocketwatch = GetWatch("pocketwatch_heal") + if pocketwatch == nil or not G.ThePlayer:HasTag("pocketwatchcaster") then return end + act = G.ACTIONS.CAST_POCKETWATCH + local buffact = G.BufferedAction(G.ThePlayer, target, act, pocketwatch) + if not G.TheWorld.ismastersim then + local function cb() + G.SendRPCToServer(G.RPC.ControllerUseItemOnSelfFromInvTile, act.code, pocketwatch, target) + end + if G.ThePlayer.components.locomotor then + buffact.preview_cb = cb + else + cb() + end + end + G.ThePlayer.components.playercontroller:DoAction(buffact) + end) + end +end + +if backstepkey ~= "None" then + if keybinds == true then + local keybind2 = G["KEY_"..backstepkey] + G.TheInput:AddKeyDownHandler(keybind2, function() + if not IsInGameplay() then return end + local pocketwatch = GetWatch("pocketwatch_warp") + if pocketwatch == nil or not G.ThePlayer:HasTag("pocketwatchcaster") then return end + act = G.ACTIONS.CAST_POCKETWATCH + local buffact = G.BufferedAction(G.ThePlayer, target, act, pocketwatch) + if not G.TheWorld.ismastersim then + local function cb() + G.SendRPCToServer(G.RPC.ControllerUseItemOnSelfFromInvTile, act.code, pocketwatch, target) + end + if G.ThePlayer.components.locomotor then + buffact.preview_cb = cb + else + cb() + end + end + G.ThePlayer.components.playercontroller:DoAction(buffact) + end) + end +end + +if backtrekkey ~= "None" then + if keybinds == true then + local keybind3 = G["KEY_"..backtrekkey] + G.TheInput:AddKeyDownHandler(keybind3, function() + if not IsInGameplay() then return end + local pocketwatch = GetWatch("pocketwatch_recall") + if pocketwatch == nil or not G.ThePlayer:HasTag("pocketwatchcaster") then return end + act = G.ACTIONS.CAST_POCKETWATCH + local buffact = G.BufferedAction(G.ThePlayer, target, act, pocketwatch) + if not G.TheWorld.ismastersim then + local function cb() + G.SendRPCToServer(G.RPC.ControllerUseItemOnSelfFromInvTile, act.code, pocketwatch, target) + end + if G.ThePlayer.components.locomotor then + buffact.preview_cb = cb + else + cb() + end + end + G.ThePlayer.components.playercontroller:DoAction(buffact) + end) end - return opt end local lang = { @@ -34,7 +143,7 @@ local lang = { } } -local language = GetConfig("lang", "eng") +local language = GetModConfigData("lang") PrefabFiles = { "pocketwatchpack" } Assets = {