Files
loop/src/Dodge/Update/UsingInput.hs
T
2022-03-14 20:39:23 +00:00

50 lines
1.9 KiB
Haskell

{- Functions that affect the world according to what pressed buttons are stored in a Set. -}
module Dodge.Update.UsingInput
( updateUsingInput
) where
import Dodge.Data
import Dodge.Base.You
import Dodge.Creature.Impulse.UseItem
import Geometry
import SDL
import qualified Data.Set as S
import Control.Lens
updateUsingInput :: World -> World
updateUsingInput w = case _hudElement $ _hud w of
DisplayInventory {} -> updatePressedButtons (_mouseButtons w) w
DisplayCarte -> updatePressedButtonsCarte (_mouseButtons w) w
updatePressedButtons :: S.Set MouseButton -> World -> World
updatePressedButtons pkeys w
| lbPressed && rbPressed && inTopInv = useItem (you w) w
| lbPressed &&
(inTopInv || _timeFlow w == RewindingLastFrame)
= useLeftItem (_yourID w) w
| mbPressed = w & clickMousePos .~ _mousePos w
& cameraRot -~ rotation
| otherwise = w
where
inTopInv = _hudElement (_hud w) == DisplayInventory NoSubInventory
lbPressed = ButtonLeft `S.member` pkeys
rbPressed = ButtonRight `S.member` pkeys
mbPressed = ButtonMiddle `S.member` pkeys
rotation = angleBetween (_mousePos w) (_clickMousePos w)
updatePressedButtonsCarte :: S.Set MouseButton -> World -> World
updatePressedButtonsCarte pkeys w
| rbPressed = w & clickMousePos .~ _mousePos w
& hud . carteCenter %~ (-.- trans)
| lbPressed = w & clickMousePos .~ _mousePos w
& hud . carteRot -~ rot
& hud . carteZoom *~ czoom
| otherwise = w
where
lbPressed = ButtonLeft `S.member` pkeys
rbPressed = ButtonRight `S.member` pkeys
--mbPressed = ButtonMiddle `S.member` pkeys
rot = angleBetween (_mousePos w) (_clickMousePos w)
czoom = magV (_mousePos w) / magV (_clickMousePos w)
trans = rotateV (_carteRot (_hud w)) $ (1 / _carteZoom (_hud w)) *.* (_mousePos w -.- _clickMousePos w)