{-| Module : Input Description : Handles input events such as keystrokes Copyright : (c) Christopher Howard, 2016 License : GPL-3 Maintainer : ch.howard@zoho.com -} module Input where import Prelude (Bool(..), Maybe(..), return, IO, map) import Graphics.Gloss.Interface.IO.Game ( Event(EventKey), SpecialKey(KeyEnter, KeyLeft, KeyRight, KeySpace, KeyTab, KeyUp), Modifiers(Modifiers, alt, ctrl, shift), KeyState(Down, Up), Key(Char, SpecialKey) ) import Universe import Lance handleInput :: Event -> Universe -> IO Universe handleInput (EventKey (Char '<') Down Modifiers { shift = Down , ctrl = Down , alt = Down } _) u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { inventory = map (\_ -> True) (inventory l) , swClock = 0.0 } } } handleInput (EventKey (Char ':') Down Modifiers { shift = Down , ctrl = Down , alt = Down } _) u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { godMode = True } } } handleInput (EventKey (Char '"') Down Modifiers { shift = Down , ctrl = Down , alt = Down } _) u = return u { skipLevel = True } --- two-hand keys --- handleInput (EventKey (SpecialKey KeyLeft) Down _ _) u = ccwThrusters u handleInput (EventKey (SpecialKey KeyLeft) Up _ _) u = stabilizeThrusters u handleInput (EventKey (SpecialKey KeyRight) Down _ _) u = cwThrusters u handleInput (EventKey (SpecialKey KeyRight) Up _ _) u = stabilizeThrusters u handleInput (EventKey (SpecialKey KeyUp) Down _ _) u = activateForwardThrusters u handleInput (EventKey (SpecialKey KeyUp) Up _ _) u = deactivateForwardThrusters u handleInput (EventKey (SpecialKey KeySpace) Down _ _) u = activateDeflector u handleInput (EventKey (SpecialKey KeySpace) Up _ _) u = deactivateDeflector u handleInput (EventKey (Char 'a') Down _ _) u = setFireTrigger u handleInput (EventKey (Char 'a') Up _ _) u = releaseFireTrigger u handleInput (EventKey (SpecialKey KeyTab) Down _ _) u = switchWeapon u --------------------- keypad keys handleInput (EventKey (Char '4') Down _ _) u = ccwThrusters u handleInput (EventKey (Char '4') Up _ _) u = stabilizeThrusters u handleInput (EventKey (Char '6') Down _ _) u = cwThrusters u handleInput (EventKey (Char '6') Up _ _) u = stabilizeThrusters u handleInput (EventKey (Char '8') Down _ _) u = activateForwardThrusters u handleInput (EventKey (Char '8') Up _ _) u = deactivateForwardThrusters u handleInput (EventKey (Char '0') Down _ _) u = setFireTrigger u handleInput (EventKey (Char '0') Up _ _) u = releaseFireTrigger u handleInput (EventKey (SpecialKey KeyEnter) Down _ _) u = activateDeflector u handleInput (EventKey (SpecialKey KeyEnter) Up _ _) u = deactivateDeflector u handleInput (EventKey (Char '5') Down _ _) u = switchWeapon u handleInput _ u = return u setFireTrigger u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { fireTrigger = True } } } releaseFireTrigger u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { fireTrigger = False } } } activateDeflector u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { deflector = True } } } deactivateDeflector u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { deflector = False } } } ccwThrusters u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { rotationalThrusters = CCW } } } stabilizeThrusters u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { rotationalThrusters = Stable } } } cwThrusters u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { rotationalThrusters = CW } } } activateForwardThrusters u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { linearThrusters = True } } } deactivateForwardThrusters u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just l { linearThrusters = False } } } switchWeapon u = let a = arena u in let mL = lance a in case mL of Nothing -> return u Just l -> return u { arena = a { lance = Just (changeCurrentWeapon l) } }