#3 #2 Player render and movement

This commit is contained in:
Tibo De Peuter 2022-12-20 22:52:06 +01:00
parent de02c7113f
commit fb4bc5bb36
12 changed files with 158 additions and 9 deletions

View file

@ -0,0 +1,63 @@
module RPGEngine.Render.Core where
import Graphics.Gloss ( Picture, translate )
import GHC.IO (unsafePerformIO)
import Graphics.Gloss.Juicy (loadJuicyPNG)
import Data.Maybe (fromJust)
import Graphics.Gloss.Data.Picture (scale)
----------------------------- Constants ------------------------------
-- Default scale
zoom :: Float
zoom = 5.0
-- Resolution of the texture
resolution :: Float
resolution = 16
assetsFolder :: FilePath
assetsFolder = "assets/"
unknownImage :: FilePath
unknownImage = "unkown.png"
allEntities :: [(String, FilePath)]
allEntities = [
("player", "player.png"),
("door", "door.png")
]
allItems :: [(String, FilePath)]
allItems = [
("dagger", "dagger.png"),
("key", "key.png" )
]
-- Map of all renders
library :: [(String, Picture)]
library = unknown:entities ++ environment ++ gui ++ items
where unknown = ("unkown", renderPNG (assetsFolder ++ unknownImage))
entities = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities
environment = []
gui = []
items = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems
----------------------------------------------------------------------
-- Turn a path to a .png file into a Picture.
renderPNG :: FilePath -> Picture
renderPNG path = scale zoom zoom $ fromJust $ unsafePerformIO $ loadJuicyPNG path
-- Retrieve an image from the library. If the library does not contain
-- the requested image, a default is returned.
getRender :: String -> Picture
getRender id = get filtered
where filtered = filter ((== id) . fst) library
get [] = snd $ head library
get ((_, res):_) = res
setRenderPos :: Int -> Int -> Picture -> Picture
setRenderPos x y = translate floatX floatY
where floatX = fromIntegral x * zoom * resolution
floatY = fromIntegral y * zoom * resolution