Merge branch 'statemachine' of github.com:nek0/haskelloids into statemachine
This commit is contained in:
commit
664dc8f870
9 changed files with 485 additions and 53 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,3 +1,6 @@
|
||||||
.cabal-sandbox/
|
.cabal-sandbox/
|
||||||
cabal.sandbox.config
|
cabal.sandbox.config
|
||||||
dist/
|
dist/
|
||||||
|
*.aux
|
||||||
|
*.hp
|
||||||
|
*.ps
|
||||||
|
|
BIN
assets/ship.png
Normal file
BIN
assets/ship.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.6 KiB |
|
@ -56,6 +56,7 @@ executable haskelloids
|
||||||
other-modules: InGame
|
other-modules: InGame
|
||||||
, Types
|
, Types
|
||||||
, Commons
|
, Commons
|
||||||
|
, StateMachine
|
||||||
|
|
||||||
-- LANGUAGE extensions used by modules in this package.
|
-- LANGUAGE extensions used by modules in this package.
|
||||||
-- other-extensions:
|
-- other-extensions:
|
||||||
|
@ -65,7 +66,7 @@ executable haskelloids
|
||||||
, affection
|
, affection
|
||||||
, gegl
|
, gegl
|
||||||
, babl
|
, babl
|
||||||
, sdl2
|
, sdl2 >= 2.1.3.1
|
||||||
, containers
|
, containers
|
||||||
, random
|
, random
|
||||||
|
|
||||||
|
|
223
src/Commons.hs
223
src/Commons.hs
|
@ -1,3 +1,5 @@
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
|
|
||||||
module Commons where
|
module Commons where
|
||||||
|
|
||||||
import Affection
|
import Affection
|
||||||
|
@ -6,9 +8,10 @@ import GEGL
|
||||||
import BABL
|
import BABL
|
||||||
|
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
import Data.List (delete)
|
||||||
import Data.Maybe (catMaybes)
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
import Control.Monad (foldM)
|
import Control.Monad (foldM, unless)
|
||||||
|
|
||||||
import System.Random (randomRIO)
|
import System.Random (randomRIO)
|
||||||
|
|
||||||
|
@ -21,7 +24,7 @@ toR deg = deg * pi / 180
|
||||||
|
|
||||||
clean :: UserData -> IO ()
|
clean :: UserData -> IO ()
|
||||||
clean ud = do
|
clean ud = do
|
||||||
mapM_ gegl_node_drop $ map (\h -> hNodeGraph h M.! "root") (haskelloids ud)
|
mapM_ (gegl_node_drop . (\h -> hNodeGraph h M.! "root")) (haskelloids ud)
|
||||||
gegl_node_drop $ nodeGraph ud M.! KeyRoot
|
gegl_node_drop $ nodeGraph ud M.! KeyRoot
|
||||||
|
|
||||||
load :: IO UserData
|
load :: IO UserData
|
||||||
|
@ -36,7 +39,7 @@ load = do
|
||||||
, Property "height" $ PropertyDouble 600
|
, Property "height" $ PropertyDouble 600
|
||||||
, Property "color" $ PropertyColor $ GEGL.RGBA 0 0 0.1 1
|
, Property "color" $ PropertyColor $ GEGL.RGBA 0 0 0.1 1
|
||||||
]
|
]
|
||||||
ship <- gegl_node_new_child root $ Operation "gegl:svg-load"
|
shipNode <- gegl_node_new_child root $ Operation "gegl:svg-load"
|
||||||
[ Property "path" $ PropertyString "assets/ship.svg"
|
[ Property "path" $ PropertyString "assets/ship.svg"
|
||||||
, Property "width" $ PropertyInt 50
|
, Property "width" $ PropertyInt 50
|
||||||
, Property "height" $ PropertyInt 50
|
, Property "height" $ PropertyInt 50
|
||||||
|
@ -44,11 +47,11 @@ load = do
|
||||||
pnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
pnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
||||||
hnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
hnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
||||||
fgnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
fgnop <- gegl_node_new_child root $ Operation "gegl:nop" []
|
||||||
sover <- gegl_node_new_child root $ defaultOverOperation
|
sover <- gegl_node_new_child root defaultOverOperation
|
||||||
hover <- gegl_node_new_child root $ defaultOverOperation
|
hover <- gegl_node_new_child root defaultOverOperation
|
||||||
pover <- gegl_node_new_child root $ defaultOverOperation
|
pover <- gegl_node_new_child root defaultOverOperation
|
||||||
bgover <- gegl_node_new_child root $ defaultOverOperation
|
bgover <- gegl_node_new_child root defaultOverOperation
|
||||||
fgover <- gegl_node_new_child root $ defaultOverOperation
|
fgover <- gegl_node_new_child root defaultOverOperation
|
||||||
translate <- gegl_node_new_child root $ Operation "gegl:translate"
|
translate <- gegl_node_new_child root $ Operation "gegl:translate"
|
||||||
[ Property "x" $ PropertyDouble 375
|
[ Property "x" $ PropertyDouble 375
|
||||||
, Property "y" $ PropertyDouble 275
|
, Property "y" $ PropertyDouble 275
|
||||||
|
@ -68,10 +71,10 @@ load = do
|
||||||
[ Property "width" $ PropertyDouble 800
|
[ Property "width" $ PropertyDouble 800
|
||||||
, Property "height" $ PropertyDouble 600
|
, Property "height" $ PropertyDouble 600
|
||||||
]
|
]
|
||||||
buffer <- gegl_buffer_new (Just $ GeglRectangle 0 0 800 600) =<<
|
nbuffer <- gegl_buffer_new (Just $ GeglRectangle 0 0 800 600) =<<
|
||||||
babl_format (PixelFormat BABL.RGBA CFfloat)
|
babl_format (PixelFormat BABL.RGBA CFfloat)
|
||||||
sink <- gegl_node_new_child root $ Operation "gegl:copy-buffer"
|
sink <- gegl_node_new_child root $ Operation "gegl:copy-buffer"
|
||||||
[ Property "buffer" $ PropertyBuffer buffer
|
[ Property "buffer" $ PropertyBuffer nbuffer
|
||||||
]
|
]
|
||||||
won <- gegl_node_new_child root $ textOperation
|
won <- gegl_node_new_child root $ textOperation
|
||||||
[ Property "string" $ PropertyString "YOU WON!"
|
[ Property "string" $ PropertyString "YOU WON!"
|
||||||
|
@ -93,20 +96,48 @@ load = do
|
||||||
-- vdeg <- gegl_node_new_child root $ Operation "gegl:video-degradation"
|
-- vdeg <- gegl_node_new_child root $ Operation "gegl:video-degradation"
|
||||||
-- [ Property "pattern" $ PropertyInt 8
|
-- [ Property "pattern" $ PropertyInt 8
|
||||||
-- ]
|
-- ]
|
||||||
gegl_node_link_many [ship, rotate, translate]
|
menuHeading <- gegl_node_new_child root $ textOperation
|
||||||
|
[ Property "string" $ PropertyString "Haskelloids"
|
||||||
|
, Property "font" $ PropertyString "Modulo"
|
||||||
|
, Property "size" $ PropertyDouble 100
|
||||||
|
, Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 1
|
||||||
|
]
|
||||||
|
menuText <- gegl_node_new_child root $ textOperation
|
||||||
|
[ Property "string" $ PropertyString "Press [Space] to start"
|
||||||
|
, Property "font" $ PropertyString "Modulo"
|
||||||
|
, Property "size" $ PropertyDouble 50
|
||||||
|
, Property "alignment" $ PropertyInt 1
|
||||||
|
, Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 1
|
||||||
|
]
|
||||||
|
menuTranslateHeading <- gegl_node_new_child root $ Operation "gegl:translate"
|
||||||
|
[ Property "x" $ PropertyDouble 150
|
||||||
|
, Property "y" $ PropertyDouble 100
|
||||||
|
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
|
||||||
|
]
|
||||||
|
menuTranslateText <- gegl_node_new_child root $ Operation "gegl:translate"
|
||||||
|
[ Property "x" $ PropertyDouble 130
|
||||||
|
, Property "y" $ PropertyDouble 300
|
||||||
|
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
|
||||||
|
]
|
||||||
|
menuOver <- gegl_node_new_child root defaultOverOperation
|
||||||
|
gegl_node_link menuHeading menuTranslateHeading
|
||||||
|
gegl_node_link_many [menuText, menuTranslateText, menuOver]
|
||||||
|
_ <- gegl_node_connect_to menuTranslateHeading "output" menuOver "aux"
|
||||||
|
gegl_node_link_many [shipNode, rotate, translate]
|
||||||
gegl_node_link_many [bgover, pover, hover, sover, crop, fgover, pixelize, vignette, sink]
|
gegl_node_link_many [bgover, pover, hover, sover, crop, fgover, pixelize, vignette, sink]
|
||||||
_ <- gegl_node_connect_to translate "output" sover "aux"
|
-- _ <- gegl_node_connect_to translate "output" sover "aux"
|
||||||
_ <- gegl_node_connect_to pnop "output" pover "aux"
|
_ <- gegl_node_connect_to pnop "output" pover "aux"
|
||||||
_ <- gegl_node_connect_to hnop "output" hover "aux"
|
_ <- gegl_node_connect_to hnop "output" hover "aux"
|
||||||
_ <- gegl_node_connect_to bg "output" bgover "aux"
|
_ <- gegl_node_connect_to bg "output" bgover "aux"
|
||||||
liftIO $ gegl_node_link fgnop fgtranslate
|
liftIO $ gegl_node_link fgnop fgtranslate
|
||||||
_ <- gegl_node_connect_to fgtranslate "output" fgover "aux"
|
-- _ <- gegl_node_connect_to fgtranslate "output" fgover "aux"
|
||||||
|
_ <- gegl_node_connect_to fgnop "output" fgover "aux"
|
||||||
traceM "nodes complete"
|
traceM "nodes complete"
|
||||||
myMap <- return $ M.fromList
|
myMap <- return $ M.fromList
|
||||||
[ (KeyRoot, root)
|
[ (KeyRoot, root)
|
||||||
, (KeyTranslate, translate)
|
, (KeyShip, shipNode)
|
||||||
, (KeyRotate, rotate)
|
, (KeyShipTranslate, translate)
|
||||||
, (KeyShip, ship)
|
, (KeyShipRotate, rotate)
|
||||||
, (KeyPNop, pnop)
|
, (KeyPNop, pnop)
|
||||||
, (KeyHNop, hnop)
|
, (KeyHNop, hnop)
|
||||||
, (KeyCrop, crop)
|
, (KeyCrop, crop)
|
||||||
|
@ -117,6 +148,10 @@ load = do
|
||||||
, (KeyPixelize, pixelize)
|
, (KeyPixelize, pixelize)
|
||||||
, (KeyFGOver, fgover)
|
, (KeyFGOver, fgover)
|
||||||
, (KeyFGNop, fgnop)
|
, (KeyFGNop, fgnop)
|
||||||
|
, (KeyFGTrans, fgtranslate)
|
||||||
|
, (KeyMenuHeading, menuTranslateHeading)
|
||||||
|
, (KeyMenuText, menuText)
|
||||||
|
, (KeyMenuOver, menuOver)
|
||||||
]
|
]
|
||||||
traceM "built map"
|
traceM "built map"
|
||||||
hs <- catMaybes <$> foldM (\acc _ -> do
|
hs <- catMaybes <$> foldM (\acc _ -> do
|
||||||
|
@ -136,12 +171,14 @@ load = do
|
||||||
, sRot = 0
|
, sRot = 0
|
||||||
, sFlange = rotate
|
, sFlange = rotate
|
||||||
}
|
}
|
||||||
, buffer = buffer
|
, buffer = nbuffer
|
||||||
, shots = ParticleSystem (ParticleStorage Nothing []) pnop buffer
|
, shots = ParticleSystem (ParticleStorage Nothing []) pnop nbuffer
|
||||||
, haskelloids = hs
|
-- , haskelloids = hs
|
||||||
|
, haskelloids = []
|
||||||
, wonlost = False
|
, wonlost = False
|
||||||
, pixelSize = 3
|
, pixelSize = 3
|
||||||
, state = InGame
|
, state = Menu
|
||||||
|
, fade = FadeIn 1
|
||||||
}
|
}
|
||||||
|
|
||||||
insertHaskelloid :: [Maybe Haskelloid] -> Maybe Int -> (Double, Double) -> IO [Maybe Haskelloid]
|
insertHaskelloid :: [Maybe Haskelloid] -> Maybe Int -> (Double, Double) -> IO [Maybe Haskelloid]
|
||||||
|
@ -199,3 +236,149 @@ insertHaskelloid hasks split (px, py) = do
|
||||||
, ("rot", tempRot)
|
, ("rot", tempRot)
|
||||||
]
|
]
|
||||||
} : hasks
|
} : hasks
|
||||||
|
|
||||||
|
haskelloidShotDown :: Haskelloid -> Affection UserData ()
|
||||||
|
haskelloidShotDown h = do
|
||||||
|
ud <- getAffection
|
||||||
|
-- liftIO $ traceIO $ show $ length $ haskelloids ud
|
||||||
|
let redHaskelloids = delete h (haskelloids ud)
|
||||||
|
newHaskelloids <- catMaybes <$> foldM
|
||||||
|
(\acc _ ->
|
||||||
|
if hDiv h < 4
|
||||||
|
then
|
||||||
|
liftIO $ insertHaskelloid acc (Just $ hDiv h) $ hPos h
|
||||||
|
else
|
||||||
|
return $ Nothing : acc
|
||||||
|
)
|
||||||
|
(map Just redHaskelloids) ([0..1] :: [Int])
|
||||||
|
liftIO $ gegl_node_drop $ hNodeGraph h M.! "root"
|
||||||
|
liftIO $ gegl_node_link_many $ map hFlange newHaskelloids
|
||||||
|
if not $ null newHaskelloids
|
||||||
|
then
|
||||||
|
liftIO $ gegl_node_link
|
||||||
|
(last $ map hFlange newHaskelloids)
|
||||||
|
(nodeGraph ud M.! KeyHNop)
|
||||||
|
else do
|
||||||
|
liftIO $ traceIO "YOU WON!"
|
||||||
|
_ <- liftIO $ gegl_node_link
|
||||||
|
(nodeGraph ud M.! KeyWon)
|
||||||
|
(nodeGraph ud M.! KeyFGTrans)
|
||||||
|
_ <- liftIO $ gegl_node_connect_to
|
||||||
|
(nodeGraph ud M.! KeyFGTrans)
|
||||||
|
"output"
|
||||||
|
(nodeGraph ud M.! KeyFGOver)
|
||||||
|
"aux"
|
||||||
|
_ <- liftIO $ gegl_node_disconnect (nodeGraph ud M.! KeyShipOver) "aux"
|
||||||
|
putAffection ud
|
||||||
|
{ wonlost = True
|
||||||
|
}
|
||||||
|
ud2 <- getAffection
|
||||||
|
putAffection ud2
|
||||||
|
{ haskelloids = newHaskelloids
|
||||||
|
}
|
||||||
|
|
||||||
|
updateHaskelloid :: Double -> Haskelloid -> Affection UserData Haskelloid
|
||||||
|
updateHaskelloid sec h@Haskelloid{..} = do
|
||||||
|
let newX = fst hPos + sec * fst hVel
|
||||||
|
newY = snd hPos + sec * snd hVel
|
||||||
|
newRot = hRot + hPitch * sec
|
||||||
|
(nnx, nny) = wrapAround (newX, newY) (100 / fromIntegral hDiv)
|
||||||
|
-- liftIO $ traceIO $ "moving to: " ++ show nnx ++ " " ++ show nny
|
||||||
|
liftIO $ gegl_node_set (hNodeGraph M.! "trans") $ Operation "gegl:translate"
|
||||||
|
[ Property "x" $ PropertyDouble nnx
|
||||||
|
, Property "y" $ PropertyDouble nny
|
||||||
|
]
|
||||||
|
liftIO $ gegl_node_set (hNodeGraph M.! "rot") $ Operation "gegl:rotate"
|
||||||
|
[ Property "degrees" $ PropertyDouble newRot
|
||||||
|
]
|
||||||
|
ud <- getAffection
|
||||||
|
lost <-
|
||||||
|
case state ud of
|
||||||
|
InGame -> liftIO $ gegl_rectangle_intersect
|
||||||
|
(GeglRectangle (floor nnx) (floor nny) (100 `div` hDiv) (100 `div` hDiv))
|
||||||
|
(GeglRectangle
|
||||||
|
(floor $ fst $ sPos $ ship ud)
|
||||||
|
(floor $ snd $ sPos $ ship ud)
|
||||||
|
50
|
||||||
|
50
|
||||||
|
)
|
||||||
|
_ -> return Nothing
|
||||||
|
maybe (return ()) (const $ do
|
||||||
|
lose
|
||||||
|
putAffection ud {wonlost = True}
|
||||||
|
) lost
|
||||||
|
return h
|
||||||
|
{ hPos = (nnx, nny)
|
||||||
|
, hRot = newRot
|
||||||
|
}
|
||||||
|
|
||||||
|
wrapAround :: (Ord t, Num t) => (t, t) -> t -> (t, t)
|
||||||
|
wrapAround (nx, ny) width = (nnx, nny)
|
||||||
|
where
|
||||||
|
nnx
|
||||||
|
| nx > 800 = nx - (800 + width)
|
||||||
|
| nx < -width = nx + 800 + width
|
||||||
|
| otherwise = nx
|
||||||
|
nny
|
||||||
|
| ny > 600 = ny - (600 + width)
|
||||||
|
| ny < -width = ny + 600 + width
|
||||||
|
| otherwise = ny
|
||||||
|
|
||||||
|
shotsUpd :: Double -> Particle -> Affection UserData Particle
|
||||||
|
shotsUpd sec part@Particle{..} = do
|
||||||
|
let newX = fst particlePosition + sec * fromIntegral (fst particleVelocity)
|
||||||
|
newY = snd particlePosition + sec * fromIntegral (snd particleVelocity)
|
||||||
|
(nnx, nny) = wrapAround (newX, newY) 4
|
||||||
|
liftIO $ gegl_node_set (particleNodeGraph M.! "rect") $ Operation "gegl:rectangle"
|
||||||
|
[ Property "x" $ PropertyDouble nnx
|
||||||
|
, Property "y" $ PropertyDouble nny
|
||||||
|
]
|
||||||
|
ud <- getAffection
|
||||||
|
inters <- catMaybes <$> mapM (\h -> do
|
||||||
|
col <- liftIO $ gegl_rectangle_intersect
|
||||||
|
(GeglRectangle (floor nnx) (floor nny) 4 4)
|
||||||
|
(GeglRectangle
|
||||||
|
(floor $ fst $ hPos h)
|
||||||
|
(floor $ snd $ hPos h)
|
||||||
|
(100 `div` hDiv h)
|
||||||
|
(100 `div` hDiv h)
|
||||||
|
)
|
||||||
|
case col of
|
||||||
|
Just _ -> return $ Just h
|
||||||
|
Nothing -> return Nothing
|
||||||
|
) (haskelloids ud)
|
||||||
|
unless (null inters) $
|
||||||
|
haskelloidShotDown $ head inters
|
||||||
|
lost <- liftIO $ gegl_rectangle_intersect
|
||||||
|
(GeglRectangle (floor nnx) (floor nny) 4 4)
|
||||||
|
(GeglRectangle
|
||||||
|
(floor $ fst $ sPos $ ship ud)
|
||||||
|
(floor $ snd $ sPos $ ship ud)
|
||||||
|
50
|
||||||
|
50
|
||||||
|
)
|
||||||
|
maybe (return ()) (const lose) lost
|
||||||
|
return part
|
||||||
|
{ particlePosition = (nnx, nny)
|
||||||
|
, particleTimeToLive = if not $ null inters then 0 else particleTimeToLive
|
||||||
|
}
|
||||||
|
|
||||||
|
shotsDraw :: GeglBuffer -> GeglNode -> Particle -> Affection UserData ()
|
||||||
|
shotsDraw _ _ _ = return ()
|
||||||
|
|
||||||
|
lose :: Affection UserData ()
|
||||||
|
lose = do
|
||||||
|
ud <- getAffection
|
||||||
|
liftIO $ traceIO "YOU LOST!"
|
||||||
|
_ <- liftIO $ gegl_node_link
|
||||||
|
(nodeGraph ud M.! KeyLost)
|
||||||
|
(nodeGraph ud M.! KeyFGTrans)
|
||||||
|
_ <- liftIO $ gegl_node_connect_to
|
||||||
|
(nodeGraph ud M.! KeyFGTrans)
|
||||||
|
"output"
|
||||||
|
(nodeGraph ud M.! KeyFGOver)
|
||||||
|
"aux"
|
||||||
|
_ <- liftIO $ gegl_node_disconnect (nodeGraph ud M.! KeyShipOver) "aux"
|
||||||
|
putAffection ud
|
||||||
|
{ wonlost = True
|
||||||
|
}
|
||||||
|
|
138
src/InGame.hs
138
src/InGame.hs
|
@ -5,13 +5,125 @@ import qualified SDL
|
||||||
import GEGL
|
import GEGL
|
||||||
|
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
import Control.Monad (when)
|
import Control.Monad (when, foldM)
|
||||||
|
|
||||||
|
import System.Random (randomRIO)
|
||||||
|
|
||||||
import Debug.Trace
|
import Debug.Trace
|
||||||
|
|
||||||
import Types
|
import Types
|
||||||
import Commons
|
import Commons
|
||||||
|
import Menu
|
||||||
|
|
||||||
|
loadGame :: Affection UserData ()
|
||||||
|
loadGame = do
|
||||||
|
liftIO $ traceIO "loading game"
|
||||||
|
ud <- getAffection
|
||||||
|
_ <- liftIO $ gegl_node_connect_to
|
||||||
|
(nodeGraph ud M.! KeyShipTranslate)
|
||||||
|
"output"
|
||||||
|
(nodeGraph ud M.! KeyShipOver)
|
||||||
|
"aux"
|
||||||
|
liftIO $ traceIO "nodes connected"
|
||||||
|
hs <- liftIO $ catMaybes <$> foldM (\acc _ -> do
|
||||||
|
coords <- liftIO excludeShip
|
||||||
|
insertHaskelloid acc Nothing coords
|
||||||
|
) [] ([0..9] :: [Int])
|
||||||
|
liftIO $ traceIO "inserted haskelloids"
|
||||||
|
liftIO $ gegl_node_link_many $ map hFlange hs
|
||||||
|
liftIO $ gegl_node_link (last $ map hFlange hs) (nodeGraph ud M.! KeyHNop)
|
||||||
|
liftIO $ gegl_node_disconnect
|
||||||
|
(nodeGraph ud M.! KeyFGOver)
|
||||||
|
"aux"
|
||||||
|
liftIO $ traceIO "nodes linked"
|
||||||
|
putAffection ud
|
||||||
|
{ haskelloids = hs
|
||||||
|
, wonlost = False
|
||||||
|
, shots = ParticleSystem
|
||||||
|
(ParticleStorage Nothing [])
|
||||||
|
(nodeGraph ud M.! KeyPNop)
|
||||||
|
(buffer ud)
|
||||||
|
, ship = Ship
|
||||||
|
{ sPos = (375, 275)
|
||||||
|
, sVel = (0, 0)
|
||||||
|
, sRot = 0
|
||||||
|
, sFlange = nodeGraph ud M.! KeyShipRotate
|
||||||
|
}
|
||||||
|
, pixelSize = 3
|
||||||
|
, state = InGame
|
||||||
|
}
|
||||||
|
liftIO $ traceIO "game loaded"
|
||||||
|
present
|
||||||
|
(GeglRectangle 0 0 800 600)
|
||||||
|
(buffer ud)
|
||||||
|
True
|
||||||
|
|
||||||
|
excludeShip :: IO (Double, Double)
|
||||||
|
excludeShip = do
|
||||||
|
px <- randomRIO (0, 800)
|
||||||
|
py <- randomRIO (0, 600)
|
||||||
|
inter <- gegl_rectangle_intersect
|
||||||
|
(GeglRectangle px py 100 100)
|
||||||
|
(GeglRectangle 350 250 100 100) -- Ship's starting position and size
|
||||||
|
case inter of
|
||||||
|
Just _ ->
|
||||||
|
excludeShip
|
||||||
|
Nothing ->
|
||||||
|
return (fromIntegral px, fromIntegral py)
|
||||||
|
|
||||||
|
updateGame :: Double -> Affection UserData ()
|
||||||
|
updateGame sec = do
|
||||||
|
ad <- get
|
||||||
|
ud <- getAffection
|
||||||
|
when (((floor $ elapsedTime ad :: Int) * 100) `mod` 10 < 2 && pixelSize ud > 3) $ do
|
||||||
|
pd <- getAffection
|
||||||
|
liftIO $ gegl_node_set (nodeGraph pd M.! KeyPixelize) $ Operation "gegl:pixelize"
|
||||||
|
[ Property "size-x" $ PropertyInt $ pixelSize pd - 1
|
||||||
|
, Property "size-y" $ PropertyInt $ pixelSize pd - 1
|
||||||
|
]
|
||||||
|
putAffection ud
|
||||||
|
{ pixelSize = pixelSize ud -1
|
||||||
|
}
|
||||||
|
let nx = fst (sPos $ ship ud) + fst (sVel $ ship ud) * sec
|
||||||
|
ny = snd (sPos $ ship ud) + snd (sVel $ ship ud) * sec
|
||||||
|
(nnx, nny) = wrapAround (nx, ny) 50
|
||||||
|
liftIO $ gegl_node_set (nodeGraph ud M.! KeyShipTranslate) $ Operation "gegl:translate"
|
||||||
|
[ Property "x" $ PropertyDouble nnx
|
||||||
|
, Property "y" $ PropertyDouble nny
|
||||||
|
]
|
||||||
|
liftIO $ gegl_node_set (nodeGraph ud M.! KeyShipRotate) $ Operation "gegl:rotate"
|
||||||
|
[ Property "degrees" $ PropertyDouble $ sRot $ ship ud
|
||||||
|
]
|
||||||
|
td <- getAffection
|
||||||
|
putAffection td
|
||||||
|
{ ship = (ship ud)
|
||||||
|
{ sPos = (nnx, nny)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ud2 <- getAffection
|
||||||
|
nhs <- mapM (updateHaskelloid sec) (haskelloids ud2)
|
||||||
|
ud3 <- getAffection
|
||||||
|
putAffection ud3
|
||||||
|
{ haskelloids = nhs
|
||||||
|
}
|
||||||
|
-- liftIO $ traceIO $ show $ length nhs
|
||||||
|
ud3 <- getAffection
|
||||||
|
ups <- updateParticleSystem (shots ud3) sec shotsUpd shotsDraw
|
||||||
|
ud4 <- getAffection
|
||||||
|
putAffection ud4
|
||||||
|
{ shots = ups
|
||||||
|
}
|
||||||
|
|
||||||
|
drawGame :: Affection UserData ()
|
||||||
|
drawGame = do
|
||||||
|
ud <- getAffection
|
||||||
|
liftIO $ gegl_node_process $ nodeGraph ud M.! KeySink
|
||||||
|
present
|
||||||
|
(GeglRectangle 0 0 800 600)
|
||||||
|
(buffer ud)
|
||||||
|
True
|
||||||
|
|
||||||
handleGameEvent :: SDL.EventPayload -> Affection UserData ()
|
handleGameEvent :: SDL.EventPayload -> Affection UserData ()
|
||||||
handleGameEvent e = do
|
handleGameEvent e = do
|
||||||
|
@ -20,7 +132,7 @@ handleGameEvent e = do
|
||||||
sec <- getDelta
|
sec <- getDelta
|
||||||
case e of
|
case e of
|
||||||
SDL.KeyboardEvent dat ->
|
SDL.KeyboardEvent dat ->
|
||||||
case (SDL.keysymKeycode $ SDL.keyboardEventKeysym dat) of
|
case SDL.keysymKeycode $ SDL.keyboardEventKeysym dat of
|
||||||
SDL.KeycodeLeft -> do
|
SDL.KeycodeLeft -> do
|
||||||
ud <- getAffection
|
ud <- getAffection
|
||||||
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed && not (wonlost ud)) $
|
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed && not (wonlost ud)) $
|
||||||
|
@ -40,8 +152,8 @@ handleGameEvent e = do
|
||||||
SDL.KeycodeUp ->
|
SDL.KeycodeUp ->
|
||||||
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed && not (wonlost wd)) $ do
|
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed && not (wonlost wd)) $ do
|
||||||
ud <- getAffection
|
ud <- getAffection
|
||||||
let vx = -10 * (sin (toR $ (sRot $ ship ud))) + fst (sVel $ ship ud)
|
let vx = -10 * sin (toR $ sRot $ ship ud) + fst (sVel $ ship ud)
|
||||||
vy = -10 * (cos (toR $ (sRot $ ship ud))) + snd (sVel $ ship ud)
|
vy = -10 * cos (toR $ sRot $ ship ud) + snd (sVel $ ship ud)
|
||||||
putAffection ud
|
putAffection ud
|
||||||
{ ship = (ship ud)
|
{ ship = (ship ud)
|
||||||
{ sVel = (vx, vy)
|
{ sVel = (vx, vy)
|
||||||
|
@ -56,19 +168,19 @@ handleGameEvent e = do
|
||||||
, Property "size-y" $ PropertyInt 8
|
, Property "size-y" $ PropertyInt 8
|
||||||
]
|
]
|
||||||
-- ad <- get
|
-- ad <- get
|
||||||
let posX = (fst $ sPos $ ship ud) + 23 - 35 * sin (toR $ sRot $ ship ud)
|
let posX = fst (sPos $ ship ud) + 23 - 35 * sin (toR $ sRot $ ship ud)
|
||||||
posY = (snd $ sPos $ ship ud) + 23 - 35 * cos (toR $ sRot $ ship ud)
|
posY = snd (sPos $ ship ud) + 23 - 35 * cos (toR $ sRot $ ship ud)
|
||||||
tempRoot <- liftIO $ gegl_node_new
|
tempRoot <- liftIO gegl_node_new
|
||||||
tempRect <- liftIO $ gegl_node_new_child tempRoot $ Operation "gegl:rectangle"
|
tempRect <- liftIO $ gegl_node_new_child tempRoot $ Operation "gegl:rectangle"
|
||||||
[ Property "x" $ PropertyDouble $ posX
|
[ Property "x" $ PropertyDouble posX
|
||||||
, Property "y" $ PropertyDouble $ posY
|
, Property "y" $ PropertyDouble posY
|
||||||
, Property "width" $ PropertyDouble 4
|
, Property "width" $ PropertyDouble 4
|
||||||
, Property "height" $ PropertyDouble 4
|
, Property "height" $ PropertyDouble 4
|
||||||
, Property "color" $ PropertyColor $ (GEGL.RGBA 1 1 1 1)
|
, Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 1
|
||||||
]
|
]
|
||||||
tempOver <- liftIO $ gegl_node_new_child tempRoot $ defaultOverOperation
|
tempOver <- liftIO $ gegl_node_new_child tempRoot defaultOverOperation
|
||||||
_ <- liftIO $ gegl_node_connect_to tempRect "output" tempOver "aux"
|
_ <- liftIO $ gegl_node_connect_to tempRect "output" tempOver "aux"
|
||||||
ips <- insertParticle (shots ud) $
|
ips <- insertParticle (shots ud)
|
||||||
Particle
|
Particle
|
||||||
{ particleTimeToLive = 5
|
{ particleTimeToLive = 5
|
||||||
, particleCreation = elapsedTime ad
|
, particleCreation = elapsedTime ad
|
||||||
|
@ -93,7 +205,7 @@ handleGameEvent e = do
|
||||||
, pixelSize = 8
|
, pixelSize = 8
|
||||||
}
|
}
|
||||||
SDL.KeycodeR ->
|
SDL.KeycodeR ->
|
||||||
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed) $ do
|
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed && wonlost wd) $ do
|
||||||
liftIO $ traceIO "reloading"
|
liftIO $ traceIO "reloading"
|
||||||
liftIO $ clean wd
|
liftIO $ clean wd
|
||||||
nd <- liftIO $ load
|
nd <- liftIO $ load
|
||||||
|
|
25
src/Main.hs
25
src/Main.hs
|
@ -1,4 +1,4 @@
|
||||||
{-# LANGUAGE OverloadedStrings, RecordWildCards #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import Affection
|
import Affection
|
||||||
|
@ -16,11 +16,14 @@ import Debug.Trace
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
import Types
|
import Types
|
||||||
import InGame
|
|
||||||
import Commons
|
import Commons
|
||||||
|
import StateMachine
|
||||||
|
|
||||||
|
import Menu
|
||||||
|
import InGame
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = withAffection $ AffectionConfig
|
main = withAffection AffectionConfig
|
||||||
{ initComponents = All
|
{ initComponents = All
|
||||||
, windowTitle = "Haskelloids"
|
, windowTitle = "Haskelloids"
|
||||||
, windowConfig = defaultWindow
|
, windowConfig = defaultWindow
|
||||||
|
@ -81,17 +84,11 @@ update sec = do
|
||||||
, shots = ups
|
, shots = ups
|
||||||
}
|
}
|
||||||
|
|
||||||
wrapAround :: (Ord t, Num t) => (t, t) -> t -> (t, t)
|
update :: Double -> [SDL.Event] -> Affection UserData ()
|
||||||
wrapAround (nx, ny) width = (nnx, nny)
|
update sec evs = do
|
||||||
where
|
wd <- getAffection
|
||||||
nnx =
|
smUpdate (state wd) sec
|
||||||
if nx > 800
|
mapM_ (smEvent (state wd) sec) evs
|
||||||
then nx - (800 + width)
|
|
||||||
else if nx < -width then nx + 800 + width else nx
|
|
||||||
nny =
|
|
||||||
if ny > 600
|
|
||||||
then ny - (600 + width)
|
|
||||||
else if ny < -width then ny + 600 + width else ny
|
|
||||||
|
|
||||||
draw :: Affection UserData ()
|
draw :: Affection UserData ()
|
||||||
draw = do
|
draw = do
|
||||||
|
|
19
src/Menu.hs
Normal file
19
src/Menu.hs
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
module Menu where
|
||||||
|
|
||||||
|
import Affection
|
||||||
|
import qualified SDL
|
||||||
|
|
||||||
|
import Debug.Trace
|
||||||
|
|
||||||
|
import Types
|
||||||
|
|
||||||
|
handleMenuEvent :: Double -> SDL.Event -> Affection UserData ()
|
||||||
|
handleMenuEvent sec e = do
|
||||||
|
ad <- get
|
||||||
|
case SDL.eventPayload e of
|
||||||
|
SDL.WindowClosedEvent _ -> do
|
||||||
|
traceM "seeya!"
|
||||||
|
put ad
|
||||||
|
{ quitEvent = True
|
||||||
|
}
|
||||||
|
_ -> return ()
|
105
src/StateMachine.hs
Normal file
105
src/StateMachine.hs
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
|
|
||||||
|
module StateMachine where
|
||||||
|
|
||||||
|
import Affection
|
||||||
|
import qualified SDL
|
||||||
|
import GEGL
|
||||||
|
|
||||||
|
import qualified Data.Map as M
|
||||||
|
import Data.Maybe (catMaybes)
|
||||||
|
|
||||||
|
import Control.Monad (foldM, when)
|
||||||
|
|
||||||
|
import System.Random (randomRIO)
|
||||||
|
|
||||||
|
import Types
|
||||||
|
import Commons
|
||||||
|
import InGame
|
||||||
|
|
||||||
|
class StateMachine a us where
|
||||||
|
smLoad :: a -> Affection us ()
|
||||||
|
smUpdate :: a -> Double -> Affection us ()
|
||||||
|
smEvent :: a -> Double -> SDL.Event -> Affection us ()
|
||||||
|
smDraw :: a -> Affection us ()
|
||||||
|
smClean :: a -> Affection us ()
|
||||||
|
|
||||||
|
instance StateMachine State UserData where
|
||||||
|
smLoad Menu = do
|
||||||
|
ud <- getAffection
|
||||||
|
liftIO $ gegl_node_connect_to
|
||||||
|
(nodeGraph ud M.! KeyMenuOver)
|
||||||
|
"output"
|
||||||
|
(nodeGraph ud M.! KeyFGOver)
|
||||||
|
"aux"
|
||||||
|
hs <- liftIO $ catMaybes <$> foldM (\acc _ -> do
|
||||||
|
px <- randomRIO (0, 800)
|
||||||
|
py <- randomRIO (0, 600)
|
||||||
|
insertHaskelloid acc Nothing (px, py)
|
||||||
|
) [] ([0..9] :: [Int])
|
||||||
|
liftIO $ gegl_node_link_many $ map hFlange hs
|
||||||
|
liftIO $ gegl_node_link (last $ map hFlange hs) (nodeGraph ud M.! KeyHNop)
|
||||||
|
liftIO $ gegl_node_disconnect (nodeGraph ud M.! KeyPNop) "input"
|
||||||
|
putAffection ud
|
||||||
|
{ haskelloids = hs
|
||||||
|
, fade = FadeIn 1
|
||||||
|
, state = Menu
|
||||||
|
, shots = (shots ud)
|
||||||
|
{ partSysParts = ParticleStorage Nothing [] }
|
||||||
|
}
|
||||||
|
|
||||||
|
smLoad InGame = loadGame
|
||||||
|
|
||||||
|
smUpdate Menu sec = do
|
||||||
|
ud <- getAffection
|
||||||
|
nhs <- mapM (updateHaskelloid sec) (haskelloids ud)
|
||||||
|
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
|
||||||
|
, haskelloids = nhs
|
||||||
|
}
|
||||||
|
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
|
||||||
|
, haskelloids = nhs
|
||||||
|
}
|
||||||
|
|
||||||
|
smUpdate InGame sec = updateGame sec
|
||||||
|
|
||||||
|
smEvent Menu _ e = do
|
||||||
|
ad <- get
|
||||||
|
case SDL.eventPayload e of
|
||||||
|
SDL.KeyboardEvent dat ->
|
||||||
|
case SDL.keysymKeycode $ SDL.keyboardEventKeysym dat of
|
||||||
|
SDL.KeycodeSpace ->
|
||||||
|
when (SDL.keyboardEventKeyMotion dat == SDL.Pressed) $ do
|
||||||
|
ud <- getAffection
|
||||||
|
liftIO $ gegl_node_disconnect (nodeGraph ud M.! KeyFGNop) "input"
|
||||||
|
smLoad InGame
|
||||||
|
_ -> return ()
|
||||||
|
SDL.WindowClosedEvent _ ->
|
||||||
|
put ad
|
||||||
|
{ quitEvent = True
|
||||||
|
}
|
||||||
|
_ -> return ()
|
||||||
|
|
||||||
|
smEvent InGame sec e = handleGameEvent (smLoad Menu) sec e
|
||||||
|
|
||||||
|
smDraw Menu = do
|
||||||
|
ud <- getAffection
|
||||||
|
liftIO $ gegl_node_process $ nodeGraph ud M.! KeySink
|
||||||
|
present
|
||||||
|
(GeglRectangle 0 0 800 600)
|
||||||
|
(buffer ud)
|
||||||
|
True
|
||||||
|
|
||||||
|
smDraw InGame = drawGame
|
22
src/Types.hs
22
src/Types.hs
|
@ -1,9 +1,10 @@
|
||||||
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
||||||
|
|
||||||
module Types where
|
module Types where
|
||||||
|
|
||||||
import Affection hiding (StateMachine)
|
import Affection hiding (StateMachine)
|
||||||
import qualified SDL
|
import qualified SDL
|
||||||
import GEGL
|
import GEGL
|
||||||
import BABL
|
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
|
||||||
data UserData = UserData
|
data UserData = UserData
|
||||||
|
@ -15,7 +16,8 @@ data UserData = UserData
|
||||||
-- , debris :: ParticleSystem
|
-- , debris :: ParticleSystem
|
||||||
, wonlost :: Bool
|
, wonlost :: Bool
|
||||||
, pixelSize :: Int
|
, pixelSize :: Int
|
||||||
, state :: StateMachine
|
, state :: State
|
||||||
|
, fade :: MenuFade
|
||||||
}
|
}
|
||||||
|
|
||||||
data Ship = Ship
|
data Ship = Ship
|
||||||
|
@ -37,9 +39,9 @@ data Haskelloid = Haskelloid
|
||||||
|
|
||||||
data NodeKey
|
data NodeKey
|
||||||
= KeyRoot
|
= KeyRoot
|
||||||
| KeyTranslate
|
|
||||||
| KeyRotate
|
|
||||||
| KeyShip
|
| KeyShip
|
||||||
|
| KeyShipTranslate
|
||||||
|
| KeyShipRotate
|
||||||
| KeyPNop
|
| KeyPNop
|
||||||
| KeyHNop
|
| KeyHNop
|
||||||
| KeyCrop
|
| KeyCrop
|
||||||
|
@ -50,9 +52,19 @@ data NodeKey
|
||||||
| KeyPixelize
|
| KeyPixelize
|
||||||
| KeyFGOver
|
| KeyFGOver
|
||||||
| KeyFGNop
|
| KeyFGNop
|
||||||
|
| KeyFGTrans
|
||||||
|
| KeyMenuHeading
|
||||||
|
| KeyMenuText
|
||||||
|
| KeyMenuStart
|
||||||
|
| KeyMenuHighscore
|
||||||
|
| KeyMenuOver
|
||||||
deriving (Ord, Eq)
|
deriving (Ord, Eq)
|
||||||
|
|
||||||
data StateMachine
|
data State
|
||||||
= Menu
|
= Menu
|
||||||
| HighScore
|
| HighScore
|
||||||
| InGame
|
| InGame
|
||||||
|
|
||||||
|
data MenuFade
|
||||||
|
= FadeIn Double
|
||||||
|
| FadeOut Double
|
||||||
|
|
Loading…
Reference in a new issue