more state machining

This commit is contained in:
nek0 2017-01-02 23:29:32 +01:00
parent 8fc717fd8b
commit 0aff3e84ca
4 changed files with 67 additions and 26 deletions

View file

@ -157,7 +157,7 @@ load _ = do
-- ) [] ([0..9] :: [Int]) -- ) [] ([0..9] :: [Int])
-- liftIO $ gegl_node_link_many $ map hFlange hs -- liftIO $ gegl_node_link_many $ map hFlange hs
-- liftIO $ gegl_node_link (last $ map hFlange hs) hnop -- liftIO $ gegl_node_link (last $ map hFlange hs) hnop
loadMenu $ UserData return $ UserData
{ nodeGraph = myMap { nodeGraph = myMap
, ship = Ship , ship = Ship
{ sPos = (375, 275) { sPos = (375, 275)

View file

@ -17,6 +17,7 @@ import Debug.Trace
import Types import Types
import Commons import Commons
import Transitions
import Menu import Menu
import InGame import InGame
@ -26,13 +27,16 @@ main = withAffection $ AffectionConfig
{ initComponents = All { initComponents = All
, windowTitle = "Haskelloids" , windowTitle = "Haskelloids"
, windowConfig = defaultWindow , windowConfig = defaultWindow
, preLoop = transition , preLoop = pre
, drawLoop = draw , drawLoop = draw
, updateLoop = update , updateLoop = update
, loadState = load , loadState = load
, cleanUp = clean , cleanUp = clean
} }
pre :: Affection UserData ()
pre = smLoad Menu
update :: Double -> Affection UserData () update :: Double -> Affection UserData ()
update sec = do update sec = do
-- traceM $ (show $ 1 / sec) ++ " FPS" -- traceM $ (show $ 1 / sec) ++ " FPS"
@ -47,10 +51,11 @@ update sec = do
putAffection pd putAffection pd
{ pixelSize = pixelSize wd -1 { pixelSize = pixelSize wd -1
} }
case state wd of -- case state wd of
Menu -> -- Menu ->
updateMenu sec -- updateMenu sec
_ -> return () -- _ -> return ()
smUpdate (state wd) sec
evs <- SDL.pollEvents evs <- SDL.pollEvents
mapM_ (\e -> mapM_ (\e ->
case state wd of case state wd of

View file

@ -1,3 +1,5 @@
{-# LANGUAGE MultiParamTypeClasses #-}
module Transitions where module Transitions where
import Affection import Affection
@ -5,17 +7,51 @@ import qualified SDL
import GEGL import GEGL
import BABL import BABL
loadMenu :: UserData -> IO UserData import qualified Data.Map as M
loadMenu ud = do import Data.Maybe (catMaybes)
gegl_node_link (nodeGraph ud M.! KeyMenuOver) (nodeGraph ud M.! KeyFGNop)
hs <- catMaybes <$> foldM (\acc _ -> do import Control.Monad (foldM)
import System.Random (randomRIO)
import Types
import Commons
instance StateMachine State UserData where
smLoad Menu = do
ud <- getAffection
liftIO $ gegl_node_link
(nodeGraph ud M.! KeyMenuOver)
(nodeGraph ud M.! KeyFGNop)
hs <- liftIO $ catMaybes <$> foldM (\acc _ -> do
px <- randomRIO (0, 800) px <- randomRIO (0, 800)
py <- randomRIO (0, 600) py <- randomRIO (0, 600)
insertHaskelloid acc Nothing (px, py) insertHaskelloid acc Nothing (px, py)
) [] ([0..9] :: [Int]) ) [] ([0..9] :: [Int])
liftIO $ gegl_node_link_many $ map hFlange hs liftIO $ gegl_node_link_many $ map hFlange hs
liftIO $ gegl_node_link (last $ map hFlange hs) (nodeGraph ud M.! KeyHNop) liftIO $ gegl_node_link (last $ map hFlange hs) (nodeGraph ud M.! KeyHNop)
return ud putAffection ud
{ haskelloids = hs { haskelloids = hs
, fade = FadeIn 1 , fade = FadeIn 1
, state = Menu
}
smUpdate Menu sec = do
ud <- getAffection
case fade ud of
FadeIn ttl -> do
liftIO $ gegl_node_set (nodeGraph ud M.! KeyMenuText) $
Operation "gegl:text"
[ Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 (1.1 - ttl)
]
putAffection ud
{ fade = if (ttl - sec) > 0 then FadeIn (ttl - sec) else FadeOut 1
}
FadeOut ttl -> do
liftIO $ gegl_node_set (nodeGraph ud M.! KeyMenuText) $
Operation "gegl:text"
[ Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 ttl
]
putAffection ud
{ fade = if (ttl - sec) > 0 then FadeOut (ttl - sec) else FadeIn 1
} }

View file

@ -66,13 +66,13 @@ data State
| InGame | InGame
class StateMachine a us where class StateMachine a us where
load :: a -> Affection us () smLoad :: a -> Affection us ()
update :: a -> Affection us () smUpdate :: a -> Double -> Affection us ()
draw :: a -> Affection us () smDraw :: a -> Affection us ()
clean :: a -> Affection us () smClean :: a -> Affection us ()
instance StateMachine State UserData where -- instance StateMachine State UserData where
update Menu = return () -- update Menu = return ()
data MenuFade data MenuFade