diff --git a/src/Game/Engine.hs b/src/Game/Engine.hs index 67d4807..073bc69 100644 --- a/src/Game/Engine.hs +++ b/src/Game/Engine.hs @@ -45,10 +45,11 @@ tick = mainSF :: Monad m => MSF (DrawerT m) () () mainSF = proc () -> do + -- A "tick" is each frame that the snake advances n <- count -< () - let isTick = n `mod` 20 == 1 + let isTick = n `mod` 8 == 1 - -- handle inputs (buffer) + -- Handle inputs (buffer) dirs <- handleEvents -< () dir <- fifoGate -< (dirs, isTick) diff --git a/src/Game/Utils.hs b/src/Game/Utils.hs index 443e29a..8f172aa 100644 --- a/src/Game/Utils.hs +++ b/src/Game/Utils.hs @@ -10,6 +10,11 @@ import Graphics.Gloss.Interface.IO.Game -- | Display an MSF outputting a picture value as a window using Gloss. -- Note that the MSF is not passed a real-time clock. +-- +-- The MSF is always called at a consistent framerate, regardless of when input +-- events are registered. It is passed a buffered list of all events that +-- occured between the last and current frames, with the first event in the +-- list being the latest. playMSF :: Display -> Color -> Int -> MSF IO [Event] Picture -> IO () playMSF display color freq msf = do -- `react` doesn't allow inputs or outputs, so we have to use IORefs @@ -52,6 +57,8 @@ hold = Just x' -> (x', x') ) +-- | Buffers and returns the elements in FIFO order, only allowing elements to +-- shift out whenever the input boolean is true. fifoGate :: Monad m => MSF m ([a], Bool) (Maybe a) fifoGate = mealy