From 2b7647285577a3c843e39539605f53c2094e3f56 Mon Sep 17 00:00:00 2001 From: SabreML <57483089+SabreML@users.noreply.github.com> Date: Mon, 25 Mar 2024 23:37:28 +0000 Subject: [PATCH] Body zone targeting keybinds refactor and a fix (#5943) # About the pull request Refactors the bodypart selection keybinds to be a bit more "object-oriented", and to fix a bug when it tries to find the UI element. # Explain why it's good for the game Makes the code very slightly cleaner, and a bit less prone to error. # Testing Photographs and Procedure
Screenshots & Videos Put screenshots and videos here with an empty line between the screenshots and the `
` tags.
# Changelog :cl: refactor: Refactored the bodypart selection keybinds. fix: Fixed a bug where selecting a new body zone would sometimes modify the wrong HUD element. /:cl: --- code/_onclick/hud/hud.dm | 2 +- code/_onclick/hud/screen_objects.dm | 2 +- code/datums/keybinding/mob.dm | 119 +++++++++++++--------------- code/modules/mob/mob_helpers.dm | 69 ++-------------- 4 files changed, 66 insertions(+), 126 deletions(-) diff --git a/code/_onclick/hud/hud.dm b/code/_onclick/hud/hud.dm index f5f61424daac..7277c74b2fd3 100644 --- a/code/_onclick/hud/hud.dm +++ b/code/_onclick/hud/hud.dm @@ -46,7 +46,7 @@ var/atom/movable/screen/toggle_burst var/atom/movable/screen/unique_action - var/atom/movable/screen/zone_sel + var/atom/movable/screen/zone_sel/zone_sel var/atom/movable/screen/pull_icon var/atom/movable/screen/throw_icon var/atom/movable/screen/oxygen_icon diff --git a/code/_onclick/hud/screen_objects.dm b/code/_onclick/hud/screen_objects.dm index edf2d44a0714..17f06987cd3b 100644 --- a/code/_onclick/hud/screen_objects.dm +++ b/code/_onclick/hud/screen_objects.dm @@ -148,7 +148,6 @@ /atom/movable/screen/zone_sel/update_icon(mob/living/user) overlays.Cut() overlays += image('icons/mob/hud/zone_sel.dmi', "[selecting]") - user.zone_selected = selecting /atom/movable/screen/zone_sel/clicked(mob/user, list/mods) if (..()) @@ -210,6 +209,7 @@ selecting = "eyes" if(old_selecting != selecting) + user.zone_selected = selecting update_icon(user) return 1 diff --git a/code/datums/keybinding/mob.dm b/code/datums/keybinding/mob.dm index b2bf989a7aaf..100f546ba590 100644 --- a/code/datums/keybinding/mob.dm +++ b/code/datums/keybinding/mob.dm @@ -73,7 +73,22 @@ user.mob.drop_held_item(I) return TRUE -/datum/keybinding/mob/target_head_cycle +// Parent type of the bodypart targeting keybinds +/datum/keybinding/mob/target + +/datum/keybinding/mob/target/down(client/user) + . = ..() + if(. || !user.mob) + return + + user.mob.select_body_zone(get_target_zone(user)) + return TRUE + +/// Returns the body zone which should be targeted when pressing this keybind. +/datum/keybinding/mob/target/proc/get_target_zone(client/user) + return + +/datum/keybinding/mob/target/head_cycle hotkey_keys = list("Numpad8") classic_keys = list("Numpad8") name = "target_head_cycle" @@ -81,14 +96,16 @@ description = "Pressing this key targets the head, and continued presses will cycle to the eyes and mouth. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETCYCLEHEAD_DOWN -/datum/keybinding/mob/target_head_cycle/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("head", user) - return TRUE +/datum/keybinding/mob/target/head_cycle/get_target_zone(client/user) + switch(user.mob.zone_selected) + if("head") + return "eyes" + if("eyes") + return "mouth" + else // including if("mouth") + return "head" -/datum/keybinding/mob/target_r_arm +/datum/keybinding/mob/target/r_arm hotkey_keys = list("Numpad4") classic_keys = list("Numpad4") name = "target_r_arm" @@ -96,14 +113,12 @@ description = "Pressing this key targets the right arm. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETRIGHTARM_DOWN -/datum/keybinding/mob/target_r_arm/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("rarm", user) - return TRUE +/datum/keybinding/mob/target/r_arm/get_target_zone(client/user) + if(user.mob.zone_selected == "r_arm") + return "r_hand" + return "r_arm" -/datum/keybinding/mob/target_body_chest +/datum/keybinding/mob/target/body_chest hotkey_keys = list("Numpad5") classic_keys = list("Numpad5") name = "target_body_chest" @@ -111,14 +126,10 @@ description = "Pressing this key targets the body. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETBODYCHEST_DOWN -/datum/keybinding/mob/target_body_chest/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("chest", user) - return TRUE +/datum/keybinding/mob/target/body_chest/get_target_zone(client/user) + return "chest" -/datum/keybinding/mob/target_left_arm +/datum/keybinding/mob/target/left_arm hotkey_keys = list("Numpad6") classic_keys = list("Numpad6") name = "target_left_arm" @@ -126,14 +137,12 @@ description = "Pressing this key targets the body. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETLEFTARM_DOWN -/datum/keybinding/mob/target_left_arm/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("larm", user) - return TRUE +/datum/keybinding/mob/target/left_arm/get_target_zone(client/user) + if(user.mob.zone_selected == "l_arm") + return "l_hand" + return "l_arm" -/datum/keybinding/mob/target_right_leg +/datum/keybinding/mob/target/right_leg hotkey_keys = list("Numpad1") classic_keys = list("Numpad1") name = "target_right_leg" @@ -141,14 +150,12 @@ description = "Pressing this key targets the right leg. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETRIGHTLEG_DOWN -/datum/keybinding/mob/target_right_leg/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("rleg", user) - return TRUE +/datum/keybinding/mob/target/right_leg/get_target_zone(client/user) + if(user.mob.zone_selected == "r_leg") + return "r_foot" + return "r_leg" -/datum/keybinding/mob/target_body_groin +/datum/keybinding/mob/target/body_groin hotkey_keys = list("Numpad2") classic_keys = list("Numpad2") name = "target_body_groin" @@ -156,14 +163,10 @@ description = "Pressing this key targets the groin. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETBODYGROIN_DOWN -/datum/keybinding/mob/target_body_groin/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("groin", user) - return TRUE +/datum/keybinding/mob/target/body_groin/get_target_zone(client/user) + return "groin" -/datum/keybinding/mob/target_left_leg +/datum/keybinding/mob/target/left_leg hotkey_keys = list("Numpad3") classic_keys = list("Numpad3") name = "target_left_leg" @@ -171,14 +174,12 @@ description = "Pressing this key targets the left leg. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETLEFTLEG_DOWN -/datum/keybinding/mob/target_left_leg/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("lleg", user) - return TRUE +/datum/keybinding/mob/target/left_leg/get_target_zone(client/user) + if(user.mob.zone_selected == "l_leg") + return "l_foot" + return "l_leg" -/datum/keybinding/mob/target_next +/datum/keybinding/mob/target/next hotkey_keys = list("Numpad7") classic_keys = list("Numpad7") name = "target_next" @@ -186,14 +187,10 @@ description = "Pressing this key targets the next body part, cycling forward through all of them. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETNEXT_DOWN -/datum/keybinding/mob/target_next/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("next", user) - return TRUE +/datum/keybinding/mob/target/next/get_target_zone(client/user) + return next_in_list(user.mob.zone_selected, DEFENSE_ZONES_LIVING) -/datum/keybinding/mob/target_prev +/datum/keybinding/mob/target/prev hotkey_keys = list("Numpad9") classic_keys = list("Numpad9") name = "target_prev" @@ -201,12 +198,8 @@ description = "Pressing this key targets the previous body part, cycling backward through all of them. This will impact where you hit people, and can be used for surgery." keybind_signal = COMSIG_KB_MOB_TARGETPREV_DOWN -/datum/keybinding/mob/target_prev/down(client/user) - . = ..() - if(.) - return - user.mob.a_select_zone("prev", user) - return TRUE +/datum/keybinding/mob/target/prev/get_target_zone(client/user) + return prev_in_list(user.mob.zone_selected, DEFENSE_ZONES_LIVING) /datum/keybinding/mob/prevent_movement hotkey_keys = list("Ctrl", "Alt") diff --git a/code/modules/mob/mob_helpers.dm b/code/modules/mob/mob_helpers.dm index 6120b1d3e072..bdb9a2718814 100644 --- a/code/modules/mob/mob_helpers.dm +++ b/code/modules/mob/mob_helpers.dm @@ -386,68 +386,16 @@ GLOBAL_LIST_INIT(limb_types_by_name, list( /mob/proc/get_eye_protection() return EYE_PROTECTION_NONE -/mob/proc/a_select_zone(input, client/user) - var/atom/movable/screen/zone_sel/zone - - for(var/A in user.screen) - if(istype(A, /atom/movable/screen/zone_sel)) - zone = A - - if(!zone) +/// Change the mob's selected body zone to `target_zone`. +/mob/proc/select_body_zone(target_zone) + if(!target_zone || !hud_used?.zone_sel) return - switch(input) - if("head") - switch(usr.zone_selected) - if("head") - zone.selecting = "eyes" - if("eyes") - zone.selecting = "mouth" - if("mouth") - zone.selecting = "head" - else - zone.selecting = "head" - if("chest") - zone.selecting = "chest" - if("groin") - zone.selecting = "groin" - if("rarm") - switch(usr.zone_selected) - if("r_arm") - zone.selecting = "r_hand" - if("r_hand") - zone.selecting = "r_arm" - else - zone.selecting = "r_arm" - if("larm") - switch(usr.zone_selected) - if("l_arm") - zone.selecting = "l_hand" - if("l_hand") - zone.selecting = "l_arm" - else - zone.selecting = "l_arm" - if("rleg") - switch(usr.zone_selected) - if("r_leg") - zone.selecting = "r_foot" - if("r_foot") - zone.selecting = "r_leg" - else - zone.selecting = "r_leg" - if("lleg") - switch(usr.zone_selected) - if("l_leg") - zone.selecting = "l_foot" - if("l_foot") - zone.selecting = "l_leg" - else - zone.selecting = "l_leg" - if("next") - zone.selecting = next_in_list(usr.zone_selected, DEFENSE_ZONES_LIVING) - if("prev") - zone.selecting = prev_in_list(usr.zone_selected, DEFENSE_ZONES_LIVING) - zone.update_icon(usr) + // Update the mob's selected zone. + zone_selected = target_zone + // Update the HUD's selected zone. + hud_used.zone_sel.selecting = target_zone + hud_used.zone_sel.update_icon() #define DURATION_MULTIPLIER_TIER_1 0.75 #define DURATION_MULTIPLIER_TIER_2 0.5 @@ -665,4 +613,3 @@ GLOBAL_LIST_INIT(limb_types_by_name, list( lighting_alpha = LIGHTING_PLANE_ALPHA_VISIBLE sync_lighting_plane_alpha() -