Add pausing
This commit is contained in:
parent
0f54f532f0
commit
59d344b678
|
@ -8,6 +8,7 @@ import Control.Monad (guard)
|
||||||
import Control.Monad.Trans.MSF.Maybe
|
import Control.Monad.Trans.MSF.Maybe
|
||||||
import Control.Monad.Trans.MSF.Reader
|
import Control.Monad.Trans.MSF.Reader
|
||||||
import Control.Monad.Trans.MSF.Writer
|
import Control.Monad.Trans.MSF.Writer
|
||||||
|
import Data.Bits (xor)
|
||||||
import Data.Functor (($>))
|
import Data.Functor (($>))
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.MonadicStreamFunction
|
import Data.MonadicStreamFunction
|
||||||
|
@ -67,8 +68,8 @@ tick = feedback initialState $ proc (dir, state) -> do
|
||||||
newstate <- maybeExit -< tickState dir state
|
newstate <- maybeExit -< tickState dir state
|
||||||
returnA -< (newstate, newstate)
|
returnA -< (newstate, newstate)
|
||||||
|
|
||||||
mainSF :: Monad m => MSF (MaybeT (DrawerT m)) () ()
|
gameSF :: Monad m => MSF (MaybeT (DrawerT m)) () GameState
|
||||||
mainSF = proc () -> do
|
gameSF = proc () -> do
|
||||||
-- A "tick" is each frame that the snake advances
|
-- A "tick" is each frame that the snake advances
|
||||||
n <- count -< ()
|
n <- count -< ()
|
||||||
let isTick = n `mod` 8 == 1
|
let isTick = n `mod` 8 == 1
|
||||||
|
@ -77,12 +78,16 @@ mainSF = proc () -> do
|
||||||
dirs <- liftTransS handleEvents -< ()
|
dirs <- liftTransS handleEvents -< ()
|
||||||
dir <- fifoGate -< (dirs, isTick)
|
dir <- fifoGate -< (dirs, isTick)
|
||||||
|
|
||||||
state' <-
|
-- only run `tick` whenever there's a tick
|
||||||
if isTick
|
pauseMSF undefined tick -< (dir, isTick)
|
||||||
then fmap Just tick -< dir
|
|
||||||
else returnA -< Nothing
|
mainSF :: Monad m => MSF (DrawerT m) () ()
|
||||||
-- undefined is safe here because the first frame is guaranteed to be a tick
|
mainSF = proc () -> do
|
||||||
state <- hold undefined -< state'
|
keys <- getKeys -< ()
|
||||||
|
let esc = SpecialKey KeyEsc `elem` keys
|
||||||
|
|
||||||
|
paused <- accumulateWith xor True -< esc
|
||||||
|
state <- pauseMSF undefined (loopMaybe gameSF) -< ((), paused)
|
||||||
|
|
||||||
-- Display the current state
|
-- Display the current state
|
||||||
liftTransS displayState -< state
|
displayState -< state
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
{-# LANGUAGE Arrows #-}
|
||||||
|
|
||||||
module Game.Utils where
|
module Game.Utils where
|
||||||
|
|
||||||
import Control.Monad.Trans.MSF.Maybe
|
import Control.Monad.Trans.MSF.Maybe
|
||||||
|
@ -75,3 +77,11 @@ fifoGate =
|
||||||
|
|
||||||
loopMaybe :: Monad m => MSF (MaybeT m) a b -> MSF m a b
|
loopMaybe :: Monad m => MSF (MaybeT m) a b -> MSF m a b
|
||||||
loopMaybe msf = msf `catchMaybe` loopMaybe msf
|
loopMaybe msf = msf `catchMaybe` loopMaybe msf
|
||||||
|
|
||||||
|
pauseMSF :: Monad m => b -> MSF m a b -> MSF m (a, Bool) b
|
||||||
|
pauseMSF def msf = proc (x, b) -> do
|
||||||
|
y <-
|
||||||
|
if b
|
||||||
|
then fmap Just msf -< x
|
||||||
|
else returnA -< Nothing
|
||||||
|
hold def -< y
|
||||||
|
|
Loading…
Reference in a new issue