wizard-wipeout/src-client/Client/Events.hs

74 lines
1.6 KiB
Haskell
Raw Normal View History

module Client.Events where
import Control.Concurrent.STM (atomically, writeTQueue, flushTQueue)
import Control.Monad.IO.Class (liftIO)
2024-11-03 14:22:28 +00:00
import Control.Monad.RWS (asks, get)
import Graphics.Vty
-- internal imports
2024-11-03 14:22:28 +00:00
import Client.Communication
2024-12-17 09:03:05 +00:00
import Client.Log
import Client.Types
2024-12-17 09:03:05 +00:00
import Library.Types
2024-12-17 09:03:05 +00:00
-- | Function for Handling singular input events
handleEvent
2024-12-17 09:03:05 +00:00
:: Event -- A single event
-> Game ()
handleEvent (EvKey KEsc _) = do
2024-11-03 14:22:28 +00:00
st <- get
2024-11-07 16:47:11 +00:00
curLevel <- asks rcLogLevel
liftIO $ gracefulExit curLevel st "Quitting due to user input"
2024-12-17 09:03:05 +00:00
handleEvent (EvKey (KChar c) _) = do
curLevel <- asks rcLogLevel
cId <- asks rcClientUUID
sock <- asks rcSocket
let action = case c of
'w' -> Just StepForward
's' -> Just StepBackward
'a' -> Just TurnLeft
'd' -> Just TurnRight
'q' -> Just StrifeLeft
'e' -> Just StrifeRight
_ -> Nothing
maybe
(logPrint Verbose $ "Unmapped input:" <> [c])
(\act -> do
let msg = ClientMessage
cId
(ClientAction act)
liftIO $ sendMessage curLevel msg sock
)
action
handleEvent _ =
pure ()
2024-12-17 09:03:05 +00:00
-- | Function handling the event list from the queue
handleEvents
:: Game ()
handleEvents = do
eventQueue <- asks rcEventQueue
evs <- liftIO $ atomically $ flushTQueue eventQueue
mapM_
handleEvent
evs
2024-12-17 09:03:05 +00:00
-- | Function for punping all Events from Vty into a queue
pumpEvents
2024-12-17 09:03:05 +00:00
:: Vty
-> Game ()
pumpEvents vty = do
eventQueue <- asks rcEventQueue
mev <- liftIO $ nextEventNonblocking vty
case mev of
Just ev -> do
liftIO $ atomically $ writeTQueue eventQueue ev
pumpEvents vty
Nothing ->
return ()