wizard-wipeout/src-server/Server/Util.hs

50 lines
945 B
Haskell
Raw Normal View History

2023-12-09 12:58:59 +00:00
module Server.Util where
import Control.Exception
import qualified Data.Matrix as M
import Linear
2023-12-09 12:58:59 +00:00
import System.Directory
import System.IO.Error
import System.Random
import Library.Types
2023-12-09 13:18:10 +00:00
-- | Remove a file if it exists
removeIfExists
:: FilePath -- File to remove
-> IO ()
2023-12-09 12:58:59 +00:00
removeIfExists fileName = removeFile fileName `catch` handleExists
where handleExists e
| isDoesNotExistError e = return ()
| otherwise = throwIO e
newWizard
:: Position
-> Wizard
newWizard pos =
Wizard
{ wizardWands = []
2023-12-23 10:38:57 +00:00
, 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