Render HP

This commit is contained in:
Tibo De Peuter 2022-12-22 15:32:49 +01:00
parent d0302c3156
commit 1dc8aac4c7
5 changed files with 31 additions and 12 deletions

View file

@ -282,8 +282,6 @@ These submodules are `Config`, `Data`, `Input`, `Parse` & `Render`. They are all
The following assets were used (and modified if specified): The following assets were used (and modified if specified):
- Kyrise's Free 16x16 RPG Icon Pack<sup>[[1]](#1)</sup> - Kyrise's Free 16x16 RPG Icon Pack<sup>[[1]](#1)</sup>
Every needed asset was taken and put into its own `.png`, instead of in the overview.
- 2D Pixel Dungeon Asset Pack by Pixel_Poem<sup>[[2]](#2)</sup> - 2D Pixel Dungeon Asset Pack by Pixel_Poem<sup>[[2]](#2)</sup>

BIN
assets/gui/health.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 237 B

View file

@ -42,14 +42,19 @@ allItems = [
("key", "key.png" ) ("key", "key.png" )
] ]
allGui :: [(String, FilePath)]
allGui = [
("health", "health.png")
]
-- Map of all renders -- Map of all renders
library :: [(String, Picture)] library :: [(String, Picture)]
library = unknown:entities ++ environment ++ gui ++ items library = unknown:entities ++ environment ++ gui ++ items
where unknown = ("unknown", renderPNG (assetsFolder ++ unknownImage)) where unknown = ("unknown", renderPNG (assetsFolder ++ unknownImage))
entities = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities entities = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities
environment = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "environment/" ++ s))) allEnvironment environment = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "environment/" ++ s))) allEnvironment
gui = [] gui = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "gui/" ++ s))) allGui
items = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems items = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems
------------------------------ Exported ------------------------------ ------------------------------ Exported ------------------------------

View file

@ -5,8 +5,9 @@ module RPGEngine.Render.Playing
import RPGEngine.Render.Core (Renderer, getRender, setRenderPos, overlay) import RPGEngine.Render.Core (Renderer, getRender, setRenderPos, overlay)
import RPGEngine.Config (resolution, zoom, uizoom) import RPGEngine.Config (resolution, zoom, uizoom)
import RPGEngine.Data (State(..), Player (..), Game (..), Level (..), Layout, Physical (..), Item (..), Entity (..)) import RPGEngine.Data (State(..), Player (..), Game (..), Level (..), Layout, Physical (..), Item (..), Entity (..), HP)
import Data.Maybe ( fromJust )
import Graphics.Gloss ( pictures, Picture, translate, white ) import Graphics.Gloss ( pictures, Picture, translate, white )
import Graphics.Gloss.Data.Picture ( blank, text, color, scale ) import Graphics.Gloss.Data.Picture ( blank, text, color, scale )
@ -23,9 +24,9 @@ renderPlaying _ = blank
------------------------------- Player ------------------------------- ------------------------------- Player -------------------------------
renderPlayer :: Renderer Player renderPlayer :: Renderer Player
renderPlayer Player{ position = (x, y) } = move picture renderPlayer Player{ position = (x, y), playerHp = playerHp } = move picture
where move = setRenderPos x y where move = setRenderPos x y
picture = getRender "player" picture = withHealthBar playerHp $ getRender "player"
-- Center the player in the middle of the screen. -- Center the player in the middle of the screen.
-- Not in use at the moment, might be useful later. -- Not in use at the moment, might be useful later.
@ -83,8 +84,8 @@ renderEntities :: [Entity] -> Picture
renderEntities list = pictures $ map renderEntity list renderEntities list = pictures $ map renderEntity list
renderEntity :: Entity -> Picture renderEntity :: Entity -> Picture
renderEntity Entity{ entityId = id, entityX = x, entityY = y} = setRenderPos x y image renderEntity Entity{ entityId = id, entityX = x, entityY = y, entityHp = hp} = setRenderPos x y image
where image = getRender id where image = withHealthBar hp $ getRender id
renderInventory :: Player -> Picture renderInventory :: Player -> Picture
renderInventory Player{ showInventory = False } = blank renderInventory Player{ showInventory = False } = blank
@ -92,4 +93,19 @@ renderInventory Player{ inventory = list } = pictures [overlay, title, items]
where title = translate 0 (offset (-1)) $ scale uizoom uizoom $ color white $ text "Inventory" where title = translate 0 (offset (-1)) $ scale uizoom uizoom $ color white $ text "Inventory"
items = pictures $ map move $ zip [0::Int ..] (map (getRender . itemId) list) items = pictures $ map move $ zip [0::Int ..] (map (getRender . itemId) list)
move (i, pic) = translate 0 (offset i) pic move (i, pic) = translate 0 (offset i) pic
offset i = negate (zoom * resolution * fromIntegral i) offset i = negate (zoom * resolution * fromIntegral i)
withHealthBar :: HP -> Picture -> Picture
withHealthBar (Nothing) renderedEntity = renderedEntity
withHealthBar (Just hp) renderedEntity = pictures [renderedEntity, positionedBar]
where positionedBar = scale smaller smaller $ translate left up renderedBar
renderedBar = pictures [heart, counter]
heart = scale by by $ getRender "health"
counter = translate right down $ scale scaler scaler $ color white $ text $ show hp
left = negate $ uizoom * resolution * scaler
right = uizoom * resolution * 0.05
up = uizoom * resolution
down = negate $ resolution * uizoom * 0.15
smaller = resolution * uizoom
by = uizoom * 0.1
scaler = by * 0.5

Binary file not shown.