Cleanup, delete hotkeys when deleting item from inventory

This commit is contained in:
2025-01-01 14:04:25 +00:00
parent 0915e04b56
commit 7dd379d4bc
12 changed files with 497 additions and 492 deletions
+29 -36
View File
@@ -2,7 +2,7 @@ module Dodge.Creature.YourControl (
yourControl,
) where
import Dodge.Inventory
import Control.Monad
import Data.Foldable
import qualified Data.Map.Strict as M
import Data.Maybe
@@ -12,6 +12,7 @@ import Dodge.Creature.Impulse.UseItem
import Dodge.Data.World
import Dodge.Hotkey
import Dodge.InputFocus
import Dodge.Inventory
import Dodge.WASD
import Geometry
import LensHelp
@@ -25,8 +26,8 @@ yourControl _ w
| Just NoSubInventory{} <- w ^? hud . hudElement . subInventory =
w
& cWorld . lWorld . creatures . ix 0
%~ (wasdWithAiming w . mouseActionsCr pkeys)
& pressedMBEffectsTopInventory pkeys
%~ (wasdWithAiming w . setCrPosture pkeys)
& tryClickUse pkeys
& handleHotkeys
| otherwise =
w & cWorld . lWorld . creatures . ix 0 %~ wasdWithAiming w
@@ -36,17 +37,17 @@ yourControl _ w
handleHotkeys :: World -> World
handleHotkeys w
| SDL.ScancodeLShift `M.member` _pressedKeys (_input w)
|| SDL.ScancodeRShift `M.member` _pressedKeys (_input w) = fromMaybe w $ do
let hks = mapMaybe scancodeToHotkey $ M.keys $ w ^. input . pressedKeys
invid <- w ^? cWorld . lWorld . creatures . ix 0 . crManipulation . manObject . imSelectedItem
itid <- w ^? cWorld . lWorld . creatures . ix 0 . crInv . ix invid . itID
return $ w & cWorld . lWorld %~ \lw -> foldl' (flip $ assignHotkey itid) lw hks
|| SDL.ScancodeRShift `M.member` _pressedKeys (_input w)
, hks <- mapMaybe scancodeToHotkey $ M.keys $ w ^. input . pressedKeys
, Just invid <- lw ^? creatures . ix 0 . crManipulation . manObject . imSelectedItem
, Just itid <- lw ^? creatures . ix 0 . crInv . ix invid . itID =
w & cWorld . lWorld %~ \lw' -> foldl' (flip $ assignHotkey itid) lw' hks
| SDL.ScancodeLCtrl `M.member` _pressedKeys (_input w)
|| SDL.ScancodeRCtrl `M.member` _pressedKeys (_input w) = fromMaybe w $ do
hk <- listToMaybe . mapMaybe scancodeToHotkey $ M.keys $ w ^. input . pressedKeys
itid <- w ^? cWorld . lWorld . hotkeys . ix hk . unNInt
invid <- w ^? cWorld . lWorld . itemLocations . ix itid . ilInvID
return $ w & augInvDirectSelect (0,invid,mempty)
|| SDL.ScancodeRCtrl `M.member` _pressedKeys (_input w)
, Just hk <- listToMaybe . mapMaybe scancodeToHotkey . M.keys $ w ^. input . pressedKeys
, Just itid <- w ^? cWorld . lWorld . hotkeys . ix hk . unNInt
, Just invid <- w ^? cWorld . lWorld . itemLocations . ix itid . ilInvID =
w & augInvDirectSelect (0, invid, mempty)
| otherwise =
M.foldl'
useHotkey
@@ -54,11 +55,12 @@ handleHotkeys w
(M.intersectionWith (,) thehotkeys (w ^. input . pressedKeys))
where
thehotkeys = M.mapKeys hotkeyToScancode $ w ^. cWorld . lWorld . hotkeys
lw = w ^. cWorld . lWorld
useHotkey :: World -> (NewInt ItmInt, PressType) -> World
useHotkey w (NInt itid, pt) = fromMaybe w $ do
invid <- w ^? cWorld . lWorld . itemLocations . ix itid . ilInvID
return $ useItem invid pt w
useItem invid pt w
hotkeyToScancode :: Hotkey -> SDL.Scancode
hotkeyToScancode x = case x of
@@ -152,34 +154,25 @@ aimTurn a cr = creatureTurnTowardDir a (x * 0.2) cr
{- | Set posture according to mouse presses.
(should you be aiming without a selected item?)
-}
mouseActionsCr :: M.Map SDL.MouseButton Int -> Creature -> Creature
mouseActionsCr pkeys
setCrPosture :: M.Map SDL.MouseButton Int -> Creature -> Creature
setCrPosture pkeys
| SDL.ButtonRight `M.member` pkeys = crStance . posture .~ Aiming
| otherwise = crStance . posture .~ AtEase
-- where
-- noaction = fromMaybe True $ do
-- theaction <- cr ^? crManipulation . manObject . inInventory . iselAction
-- return $ theaction == NoInvSelAction
pressedMBEffectsTopInventory :: M.Map SDL.MouseButton Int -> World -> World
pressedMBEffectsTopInventory pkeys w
| Just ltime <- pkeys ^? ix SDL.ButtonLeft
, Just rtime <- pkeys ^? ix SDL.ButtonRight
, ltime <= rtime && inTopInv =
fromMaybe w $ do
invid <-
w
^? cWorld . lWorld . creatures . ix 0
. crManipulation
. manObject
. imSelectedItem
return $ useItem invid (f ltime) w
| otherwise = w
tryClickUse :: M.Map SDL.MouseButton Int -> World -> World
tryClickUse pkeys w = fromMaybe w $ do
ltime <- pkeys ^? ix SDL.ButtonLeft
rtime <- pkeys ^? ix SDL.ButtonRight
guard $ ltime <= rtime && inTopInv
invid <- w
^? cWorld . lWorld . creatures . ix 0
. crManipulation
. manObject
. imSelectedItem
useItem invid (f ltime) w
where
f 0 = InitialPress
f _ = ShortPress
-- youhammerdown = set (cWorld . lWorld . creatures . ix 0 . crHammerPosition) HammerDown
inTopInv = case w ^. hud . hudElement of
DisplayInventory{_subInventory = NoSubInventory{}} -> True
_ -> False