wizard-wipeout/src-server/Server/Util.hs
2024-12-17 12:00:55 +01:00

50 lines
960 B
Haskell

module Server.Util where
import Control.Exception
import qualified Data.Matrix as M
import Linear
import System.Directory
import System.IO.Error
import System.Random
import Library.Types
-- | Remove a file if it exists
removeIfExists
:: FilePath -- File to remove
-> IO ()
removeIfExists fileName = removeFile fileName `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e
newWizard
:: Position
-> Float
-> Wizard
newWizard pos rot =
Wizard
{ wizardWands = []
, wizardRot = 0
, wizardPos = pos
, wizardMana = 100
, wizardHealth = 100
, wizardEffects = []
}
rollInitPosition
:: ServerMap
-> IO Position
rollInitPosition arena = do
r <- randomRIO (1, M.nrows arena)
c <- randomRIO (1, M.ncols arena)
if arena M.! (r, c) == Air
then
pure $ (fromIntegral <$> V2 r c) + V2 0.5 0.5
else
rollInitPosition arena