2024-10-28 11:20:04 +00:00
|
|
|
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)
|
2024-10-28 11:20:04 +00:00
|
|
|
|
|
|
|
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
|
2024-10-28 11:20:04 +00:00
|
|
|
import Client.Types
|
2024-12-17 09:03:05 +00:00
|
|
|
import Library.Types
|
2024-10-28 11:20:04 +00:00
|
|
|
|
2024-12-17 09:03:05 +00:00
|
|
|
-- | Function for Handling singular input events
|
2024-10-28 11:20:04 +00:00
|
|
|
handleEvent
|
2024-12-17 09:03:05 +00:00
|
|
|
:: Event -- A single event
|
2024-10-28 11:20:04 +00:00
|
|
|
-> 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
|
2024-10-28 11:20:04 +00:00
|
|
|
handleEvent _ =
|
|
|
|
pure ()
|
|
|
|
|
2024-12-17 09:03:05 +00:00
|
|
|
-- | Function handling the event list from the queue
|
2024-10-28 11:20:04 +00:00
|
|
|
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
|
2024-10-28 11:20:04 +00:00
|
|
|
pumpEvents
|
2024-12-17 09:03:05 +00:00
|
|
|
:: Vty
|
2024-10-28 11:20:04 +00:00
|
|
|
-> 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 ()
|