Tweak sound
This commit is contained in:
+23
-7
@@ -15,6 +15,7 @@ import Control.Lens
|
||||
playSoundQueue :: SoundData a -> [Int] -> IO ()
|
||||
playSoundQueue sd ns = forM_ ns $ \n -> playIfFree (_loadedChunks sd IM.! n) Mix.Once
|
||||
|
||||
-- this can be cleaned up
|
||||
playIfFree :: Mix.Chunk -> Mix.Times -> IO (Maybe Mix.Channel)
|
||||
playIfFree c times = do
|
||||
mayChan <- Mix.getAvailable Mix.DefaultGroup
|
||||
@@ -22,18 +23,33 @@ playIfFree c times = do
|
||||
Nothing -> return Nothing
|
||||
Just i -> Just <$> Mix.playOn i times c
|
||||
|
||||
playSoundIfFree :: Mix.Chunk -> Sound -> IO (Maybe Sound)
|
||||
playSoundIfFree c s = case _soundTime s of
|
||||
Just _ -> (playIfFree c Mix.Forever) >>= return . f
|
||||
Nothing -> playIfFree c Mix.Once >>= return . f
|
||||
where
|
||||
f :: Maybe Mix.Channel -> Maybe Sound
|
||||
f = fmap (\chan -> (s & soundChannel .~ Just chan))
|
||||
|
||||
haltMaybe :: Maybe Mix.Channel -> IO (Maybe Sound)
|
||||
haltMaybe (Just x) = Mix.halt x >> return Nothing
|
||||
haltMaybe Nothing = return Nothing
|
||||
|
||||
-- logic: check if sound is playing:
|
||||
-- if so, decrement timers, and/or stop playing and remove
|
||||
-- if not, check if there is free channel:
|
||||
-- if so, start playing
|
||||
-- if not, remove sound
|
||||
playSound :: SoundData a -> Sound -> IO (Maybe Sound)
|
||||
playSound sd s
|
||||
| _soundTime s <= 0 = haltMaybe (_soundChannel s)
|
||||
| otherwise = case _soundChannel s of
|
||||
Just i -> return $ Just $ over soundTime (\t -> t - 1) s
|
||||
Nothing -> do maychan <- playIfFree (_loadedChunks sd IM.! _soundType s) Mix.Forever
|
||||
return $ Just $ s & soundTime -~ 1
|
||||
& soundChannel .~ maychan
|
||||
playSound sd s = case _soundChannel s of
|
||||
Just i -> case _soundTime s of
|
||||
Just t | t > 0 -> return $ Just $ set soundTime (Just $ t - 1) s
|
||||
| otherwise -> haltMaybe (_soundChannel s)
|
||||
Nothing -> Mix.playing i >>= (\b -> if b
|
||||
then return (Just s)
|
||||
else return Nothing
|
||||
)
|
||||
Nothing -> playSoundIfFree (_loadedChunks sd IM.! _soundType s) s
|
||||
|
||||
playSounds :: SoundData a -> IO (SoundData a)
|
||||
playSounds sd = do
|
||||
|
||||
Reference in New Issue
Block a user