Refactor, try to limit dependencies

This commit is contained in:
2022-07-28 00:59:56 +01:00
parent 8aa5c17ab9
commit 160560af5f
418 changed files with 15105 additions and 13343 deletions
+54 -53
View File
@@ -1,48 +1,48 @@
{- Functions that affect the world according to what pressed buttons are stored in a Set. -}
module Dodge.Update.UsingInput
( updateUsingInput
) where
import Dodge.Terminal.LeftButton
import Dodge.Data
module Dodge.Update.UsingInput (
updateUsingInput,
) where
import Control.Lens
import qualified Data.Map.Strict as M
import Dodge.Base.You
import Dodge.Creature.Impulse.UseItem
import Dodge.InputFocus
import Dodge.Combine
import Dodge.Creature.Impulse.UseItem
import Dodge.Data.World
import Dodge.InputFocus
import Dodge.Inventory
import Dodge.Inventory.Add
import Dodge.Terminal.LeftButton
import Geometry
import ListHelp
import SDL
--import qualified Data.Set as S
import qualified Data.Map.Strict as M
import Control.Lens
updateUsingInput :: World -> World
updateUsingInput w = case _hudElement $ _hud (_cWorld w) of
DisplayInventory subinv
-> updatePressedButtons subinv (_mouseButtons w) w
DisplayCarte
-> updatePressedButtonsCarte (_mouseButtons w) w
DisplayInventory subinv ->
updatePressedButtons subinv (_mouseButtons w) w
DisplayCarte ->
updatePressedButtonsCarte (_mouseButtons w) w
updatePressedButtons :: SubInventory -> M.Map MouseButton Bool -> World -> World
updatePressedButtons subinv pkeys w = case subinv of
NoSubInventory
| ButtonLeft `M.member` pkeys && w ^?! hammers . ix SubInvHam /= HammerUp
-> w & hammers . ix SubInvHam .~ HammerDown
| ButtonLeft `M.member` pkeys && w ^?! hammers . ix SubInvHam /= HammerUp ->
w & hammers . ix SubInvHam .~ HammerDown
| otherwise -> updatePressedButtons' pkeys w
CombineInventory mi
| pkeys ^? ix ButtonLeft == Just False
-> maybeexitcombine (maybe id doCombine mi w) & hammers . ix SubInvHam .~ HammerDown
| pkeys ^? ix ButtonLeft == Just False ->
maybeexitcombine (maybe id doCombine mi w) & hammers . ix SubInvHam .~ HammerDown
DisplayTerminal tmid
| pkeys ^? ix ButtonLeft == Just False && inTermFocus w
-> doTerminalEffectLB (w ^?! cWorld . terminals . ix tmid) w
& hammers . ix SubInvHam .~ HammerDown
| pkeys ^? ix ButtonLeft == Just False
-> w & cWorld . terminals . ix tmid . tmInput . tiFocus %~ const True
& hammers . ix SubInvHam .~ HammerDown
_ | ButtonLeft `M.member` pkeys
-> w & hammers . ix SubInvHam .~ HammerDown
| pkeys ^? ix ButtonLeft == Just False && inTermFocus w ->
doTerminalEffectLB (w ^?! cWorld . terminals . ix tmid) w
& hammers . ix SubInvHam .~ HammerDown
| pkeys ^? ix ButtonLeft == Just False ->
w & cWorld . terminals . ix tmid . tmInput . tiFocus %~ const True
& hammers . ix SubInvHam .~ HammerDown
_
| ButtonLeft `M.member` pkeys ->
w & hammers . ix SubInvHam .~ HammerDown
_ -> w
where
maybeexitcombine
@@ -50,55 +50,56 @@ updatePressedButtons subinv pkeys w = case subinv of
| otherwise = cWorld . hud . hudElement .~ DisplayInventory NoSubInventory
updatePressedButtons' :: M.Map MouseButton Bool -> World -> World
updatePressedButtons' pkeys w
| isDown ButtonLeft && isDown ButtonRight && inTopInv
= useItem (you w) w & hammers . ix DoubleMouseHam .~ HammerDown
updatePressedButtons' pkeys w
| isDown ButtonLeft && isDown ButtonRight && inTopInv =
useItem (you w) w & hammers . ix DoubleMouseHam .~ HammerDown
| isDown ButtonLeft && (inTopInv || _timeFlow w == RewindingLastFrame)
&& w ^?! hammers . ix DoubleMouseHam == HammerUp
= useLeftItem (_yourID (_cWorld w)) w
| isDown ButtonLeft && (inTopInv || _timeFlow w == RewindingLastFrame)
= w & hammers . ix DoubleMouseHam .~ HammerDown
&& w ^?! hammers . ix DoubleMouseHam == HammerUp =
useLeftItem (_yourID (_cWorld w)) w
| isDown ButtonLeft && (inTopInv || _timeFlow w == RewindingLastFrame) =
w & hammers . ix DoubleMouseHam .~ HammerDown
| isDown ButtonRight && inTopInv = w
| isDown ButtonMiddle
= w & cWorld . cameraRot -~ rotation
& cWorld . clickMousePos .~ _mousePos w
| isDown ButtonMiddle =
w & cWorld . cameraRot -~ rotation
& cWorld . clickMousePos .~ _mousePos w
| isDown ButtonLeft = w & hammers . ix DoubleMouseHam .~ HammerDown
| otherwise = w
where
where
inTopInv = case _hudElement (_hud (_cWorld w)) of
DisplayInventory NoSubInventory -> True
_ -> False
isDown but = but `M.member` pkeys
rotation = angleBetween (_mousePos w) (_clickMousePos (_cWorld w))
-- note "sort" on the inventory indices; otherwise
-- lower items may be shifted up and items below these removed instead
doCombine :: Int -> World -> World
doCombine i w = case combineItemListYou w ^? ix i of
Nothing -> w
Just (is,it) -> -- set (hud . hudElement) (DisplayInventory NoSubInventory)
Just (is, it) ->
-- set (hud . hudElement) (DisplayInventory NoSubInventory)
selectinv --enterCombineInv
. createPutItem it
$ foldr (rmInvItem yid) w (sort is)
. createPutItem it
$ foldr (rmInvItem yid) w (sort is)
where
yid = _yourID (_cWorld w)
selectinv (Just i', w') = w' & cWorld . creatures . ix yid . crInvSel .~ InvSel i' NoInvSelAction
selectinv (Nothing, w') = w'
updatePressedButtonsCarte :: M.Map MouseButton Bool -> World -> World
updatePressedButtonsCarte pkeys w
| isDown ButtonRight = w
& cWorld . clickMousePos .~ _mousePos w
& cWorld . hud . carteCenter %~ (-.- trans)
| isDown ButtonLeft = w
& cWorld . clickMousePos .~ _mousePos w
& cWorld . hud . carteRot -~ rot
& cWorld . hud . carteZoom *~ czoom
updatePressedButtonsCarte pkeys w
| isDown ButtonRight =
w
& cWorld . clickMousePos .~ _mousePos w
& cWorld . hud . carteCenter %~ (-.- trans)
| isDown ButtonLeft =
w
& cWorld . clickMousePos .~ _mousePos w
& cWorld . hud . carteRot -~ rot
& cWorld . hud . carteZoom *~ czoom
| otherwise = w
where
where
isDown but = but `M.member` pkeys
rot = angleBetween (_mousePos w) (_clickMousePos (_cWorld w))
rot = angleBetween (_mousePos w) (_clickMousePos (_cWorld w))
czoom = magV (_mousePos w) / magV (_clickMousePos (_cWorld w))
trans = rotateV (_carteRot (_hud (_cWorld w))) $ 1 / _carteZoom (_hud (_cWorld w)) *.* (_mousePos w -.- _clickMousePos (_cWorld w))