haskelloids/src/Commons.hs

376 lines
13 KiB
Haskell
Raw Normal View History

2017-01-03 16:34:37 +00:00
{-# LANGUAGE RecordWildCards #-}
2016-12-31 16:01:24 +00:00
module Commons where
import Affection
import qualified SDL
import GEGL
import BABL
import qualified Data.Map as M
2017-01-03 16:34:37 +00:00
import Data.List (delete)
import Data.Maybe (catMaybes)
2016-12-31 16:01:24 +00:00
2017-01-03 18:36:01 +00:00
import Control.Monad (foldM, unless)
2016-12-31 16:01:24 +00:00
import System.Random (randomRIO)
import Debug.Trace
import Types
toR :: Double -> Double
toR deg = deg * pi / 180
clean :: UserData -> IO ()
clean ud = do
2017-01-03 18:36:01 +00:00
mapM_ (gegl_node_drop . (\h -> hNodeGraph h M.! "root")) (haskelloids ud)
2016-12-31 16:01:24 +00:00
gegl_node_drop $ nodeGraph ud M.! KeyRoot
load :: SDL.Surface -> IO UserData
load _ = do
traceM "loading"
root <- gegl_node_new
traceM "root node"
bg <- gegl_node_new_child root $ Operation "gegl:rectangle"
[ Property "x" $ PropertyDouble 0
, Property "y" $ PropertyDouble 0
, Property "width" $ PropertyDouble 800
, Property "height" $ PropertyDouble 600
, Property "color" $ PropertyColor $ GEGL.RGBA 0 0 0.1 1
]
2017-01-02 22:49:17 +00:00
shipNode <- gegl_node_new_child root $ Operation "gegl:svg-load"
2016-12-31 16:01:24 +00:00
[ Property "path" $ PropertyString "assets/ship.svg"
, Property "width" $ PropertyInt 50
, Property "height" $ PropertyInt 50
]
pnop <- 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" []
2017-01-03 18:36:01 +00:00
sover <- gegl_node_new_child root defaultOverOperation
hover <- gegl_node_new_child root defaultOverOperation
pover <- gegl_node_new_child root defaultOverOperation
bgover <- gegl_node_new_child root defaultOverOperation
fgover <- gegl_node_new_child root defaultOverOperation
2016-12-31 16:01:24 +00:00
translate <- gegl_node_new_child root $ Operation "gegl:translate"
[ Property "x" $ PropertyDouble 375
, Property "y" $ PropertyDouble 275
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
]
fgtranslate <- gegl_node_new_child root $ Operation "gegl:translate"
[ Property "x" $ PropertyDouble 150
, Property "y" $ PropertyDouble 250
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
]
rotate <- gegl_node_new_child root $ Operation "gegl:rotate"
[ Property "origin-x" $ PropertyDouble 25
, Property "origin-y" $ PropertyDouble 25
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
]
crop <- gegl_node_new_child root $ Operation "gegl:crop"
[ Property "width" $ PropertyDouble 800
, Property "height" $ PropertyDouble 600
]
2017-01-02 22:49:17 +00:00
nbuffer <- gegl_buffer_new (Just $ GeglRectangle 0 0 800 600) =<<
2016-12-31 16:01:24 +00:00
babl_format (PixelFormat BABL.RGBA CFfloat)
sink <- gegl_node_new_child root $ Operation "gegl:copy-buffer"
2017-01-02 22:49:17 +00:00
[ Property "buffer" $ PropertyBuffer nbuffer
2016-12-31 16:01:24 +00:00
]
won <- gegl_node_new_child root $ textOperation
[ Property "string" $ PropertyString "YOU WON!"
, Property "font" $ PropertyString "Modulo"
, Property "size" $ PropertyDouble 100
, Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 1
]
lost <- gegl_node_new_child root $ textOperation
[ Property "string" $ PropertyString "YOU LOST!"
, Property "font" $ PropertyString "Modulo"
, Property "size" $ PropertyDouble 100
, Property "color" $ PropertyColor $ GEGL.RGBA 1 1 1 1
]
vignette <- gegl_node_new_child root $ Operation "gegl:vignette" []
-- pixelize <- gegl_node_new_child root $ Operation "gegl:pixelize"
pixelize <- gegl_node_new_child root $ Operation "gegl:pixelize"
[ Property "size-x" $ PropertyInt 3
, Property "size-y" $ PropertyInt 3
]
-- vdeg <- gegl_node_new_child root $ Operation "gegl:video-degradation"
-- [ Property "pattern" $ PropertyInt 8
-- ]
2017-01-01 21:58:54 +00:00
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\nPress [H] for Highscore"
, 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
]
2017-01-03 18:36:01 +00:00
menuOver <- gegl_node_new_child root defaultOverOperation
2017-01-01 21:58:54 +00:00
gegl_node_link menuHeading menuTranslateHeading
gegl_node_link_many [menuText, menuTranslateText, menuOver]
2017-01-02 22:49:17 +00:00
_ <- gegl_node_connect_to menuTranslateHeading "output" menuOver "aux"
gegl_node_link_many [shipNode, rotate, translate]
2016-12-31 16:01:24 +00:00
gegl_node_link_many [bgover, pover, hover, sover, crop, fgover, pixelize, vignette, sink]
2017-01-01 21:58:54 +00:00
-- _ <- gegl_node_connect_to translate "output" sover "aux"
2016-12-31 16:01:24 +00:00
_ <- gegl_node_connect_to pnop "output" pover "aux"
_ <- gegl_node_connect_to hnop "output" hover "aux"
_ <- gegl_node_connect_to bg "output" bgover "aux"
2017-02-26 01:13:58 +00:00
liftIO $ gegl_node_link fgnop fgtranslate
-- _ <- gegl_node_connect_to fgtranslate "output" fgover "aux"
2017-01-01 21:58:54 +00:00
_ <- gegl_node_connect_to fgnop "output" fgover "aux"
2016-12-31 16:01:24 +00:00
traceM "nodes complete"
myMap <- return $ M.fromList
[ (KeyRoot, root)
2017-01-02 22:49:17 +00:00
, (KeyShip, shipNode)
2017-01-03 16:34:37 +00:00
, (KeyShipTranslate, translate)
, (KeyShipRotate, rotate)
2016-12-31 16:01:24 +00:00
, (KeyPNop, pnop)
, (KeyHNop, hnop)
, (KeyCrop, crop)
, (KeyShipOver, sover)
, (KeySink, sink)
, (KeyWon, won)
, (KeyLost, lost)
, (KeyPixelize, pixelize)
, (KeyFGOver, fgover)
, (KeyFGNop, fgnop)
2017-02-26 01:13:58 +00:00
, (KeyFGTrans, fgtranslate)
2017-01-01 21:58:54 +00:00
, (KeyMenuHeading, menuTranslateHeading)
, (KeyMenuText, menuText)
, (KeyMenuOver, menuOver)
2016-12-31 16:01:24 +00:00
]
2017-01-01 21:58:54 +00:00
-- hs <- catMaybes <$> foldM (\acc _ -> do
-- px <- liftIO $ randomRIO (0, 800)
-- py <- liftIO $ 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) hnop
2017-01-03 18:36:01 +00:00
return UserData
2016-12-31 16:01:24 +00:00
{ nodeGraph = myMap
, ship = Ship
{ sPos = (375, 275)
, sVel = (0, 0)
, sRot = 0
, sFlange = rotate
}
2017-01-02 22:49:17 +00:00
, buffer = nbuffer
, shots = ParticleSystem (ParticleStorage Nothing []) pnop nbuffer
2017-01-01 21:58:54 +00:00
-- , haskelloids = hs
, haskelloids = []
2016-12-31 16:01:24 +00:00
, wonlost = False
, pixelSize = 3
2017-01-01 21:58:54 +00:00
, state = Menu
2017-01-02 22:49:17 +00:00
, fade = FadeIn 1
2017-01-01 21:58:54 +00:00
}
2016-12-31 16:01:24 +00:00
insertHaskelloid :: [Maybe Haskelloid] -> Maybe Int -> (Double, Double) -> IO [Maybe Haskelloid]
insertHaskelloid hasks split (px, py) = do
-- liftIO $ traceIO "inserting haskelloid"
vx <- liftIO $ randomRIO (-10, 10)
vy <- liftIO $ randomRIO (-10, 10)
rdiv <- case split of
Nothing -> liftIO $ randomRIO (1, 2)
Just x -> return $ x + 1
rot <- liftIO $ randomRIO (0, 360)
pitch <- liftIO $ randomRIO (-45, 45)
2017-01-03 18:36:01 +00:00
tempRoot <- liftIO gegl_node_new
tempOver <- liftIO $ gegl_node_new_child tempRoot defaultOverOperation
2016-12-31 16:01:24 +00:00
tempSvg <- gegl_node_new_child tempRoot $ Operation "gegl:svg-load"
[ Property "path" $ PropertyString "assets/haskelloid.svg"
, Property "width" $ PropertyInt (100 `div` rdiv)
, Property "height" $ PropertyInt (100 `div` rdiv)
]
tempTrans <- liftIO $ gegl_node_new_child tempRoot $ Operation "gegl:translate"
[ Property "x" $ PropertyDouble $ px + (100 / fromIntegral rdiv / 2)
, Property "y" $ PropertyDouble $ py + (100 / fromIntegral rdiv / 2)
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
]
tempRot <- liftIO $ gegl_node_new_child tempRoot $ Operation "gegl:rotate"
[ Property "origin-x" $ PropertyDouble (100 / 2 / fromIntegral rdiv)
, Property "origin-y" $ PropertyDouble (100 / 2 / fromIntegral rdiv)
, Property "degrees" $ PropertyDouble rot
, Property "sampler" $ PropertyInt $ fromEnum GeglSamplerCubic
]
liftIO $ gegl_node_link_many [tempSvg, tempRot, tempTrans]
_ <- liftIO $ gegl_node_connect_to tempTrans "output" tempOver "aux"
return $ Just Haskelloid
{ hPos =
( px + (100 / 2 / fromIntegral rdiv)
, py + (100 / 2 / fromIntegral rdiv)
)
, hVel = (vx, vy)
, hRot = rot
, hPitch = pitch
, hDiv = rdiv
, hFlange = tempOver
, hNodeGraph = M.fromList
[ ("root", tempRoot)
, ("over", tempOver)
, ("svg", tempSvg)
, ("trans", tempTrans)
, ("rot", tempRot)
]
} : hasks
2017-01-03 16:34:37 +00:00
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!"
2017-02-26 01:13:58 +00:00
_ <- liftIO $ gegl_node_link
2017-01-03 16:34:37 +00:00
(nodeGraph ud M.! KeyWon)
2017-02-26 01:13:58 +00:00
(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"
2017-01-03 16:34:37 +00:00
putAffection ud
{ wonlost = True
}
2017-02-26 01:14:49 +00:00
ud2 <- getAffection
putAffection ud2
2017-01-03 16:34:37 +00:00
{ haskelloids = newHaskelloids
}
updateHaskelloid :: Double -> Haskelloid -> Affection UserData Haskelloid
updateHaskelloid sec h@Haskelloid{..} = do
2017-01-03 18:36:01 +00:00
let newX = fst hPos + sec * fst hVel
newY = snd hPos + sec * snd hVel
2017-01-03 16:34:37 +00:00
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"
2017-01-03 18:36:01 +00:00
[ Property "x" $ PropertyDouble nnx
, Property "y" $ PropertyDouble nny
2017-01-03 16:34:37 +00:00
]
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
2017-02-26 01:14:49 +00:00
maybe (return ()) (const $ do
lose
putAffection ud {wonlost = True}
) lost
2017-01-03 16:34:37 +00:00
return h
{ hPos = (nnx, nny)
, hRot = newRot
}
wrapAround :: (Ord t, Num t) => (t, t) -> t -> (t, t)
wrapAround (nx, ny) width = (nnx, nny)
where
2017-01-03 18:36:01 +00:00
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
2017-01-03 16:34:37 +00:00
shotsUpd :: Double -> Particle -> Affection UserData Particle
shotsUpd sec part@Particle{..} = do
2017-01-03 18:36:01 +00:00
let newX = fst particlePosition + sec * fromIntegral (fst particleVelocity)
newY = snd particlePosition + sec * fromIntegral (snd particleVelocity)
2017-01-03 16:34:37 +00:00
(nnx, nny) = wrapAround (newX, newY) 4
liftIO $ gegl_node_set (particleNodeGraph M.! "rect") $ Operation "gegl:rectangle"
2017-01-03 18:36:01 +00:00
[ Property "x" $ PropertyDouble nnx
, Property "y" $ PropertyDouble nny
2017-01-03 16:34:37 +00:00
]
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)
2017-01-03 18:36:01 +00:00
unless (null inters) $
2017-01-03 16:34:37 +00:00
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
)
2017-01-03 18:36:01 +00:00
maybe (return ()) (const lose) lost
2017-01-03 16:34:37 +00:00
return part
{ particlePosition = (nnx, nny)
2017-01-03 18:36:01 +00:00
, particleTimeToLive = if not $ null inters then 0 else particleTimeToLive
2017-01-03 16:34:37 +00:00
}
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)
2017-02-26 01:13:58 +00:00
(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"
2017-01-03 16:34:37 +00:00
putAffection ud
{ wonlost = True
}