2023-12-22 15:18:08 +00:00
|
|
|
module Client.Graphics where
|
|
|
|
|
2023-12-23 02:19:32 +00:00
|
|
|
import qualified Control.Concurrent.STM as STM
|
|
|
|
|
|
|
|
import Control.Monad.RWS
|
|
|
|
|
2024-12-12 08:09:17 +00:00
|
|
|
import Data.List (foldl')
|
2023-12-23 10:39:34 +00:00
|
|
|
|
2024-12-12 08:09:17 +00:00
|
|
|
import qualified Data.Matrix as M
|
2023-12-23 02:19:32 +00:00
|
|
|
|
|
|
|
import Graphics.Vty
|
|
|
|
|
2024-06-09 05:32:03 +00:00
|
|
|
import Linear hiding (trace)
|
|
|
|
|
2023-12-23 02:19:32 +00:00
|
|
|
-- internal imports
|
|
|
|
|
2023-12-22 15:18:08 +00:00
|
|
|
import Client.Types
|
2023-12-23 02:19:32 +00:00
|
|
|
import Library.Types
|
|
|
|
|
2023-12-23 10:39:34 +00:00
|
|
|
vFOV :: Float
|
|
|
|
vFOV = pi / 2
|
|
|
|
|
|
|
|
hFOV :: Float
|
2024-06-09 05:32:03 +00:00
|
|
|
hFOV = pi / 2
|
2023-12-23 10:39:34 +00:00
|
|
|
|
2023-12-23 02:19:32 +00:00
|
|
|
getPixel :: Float -> Char
|
|
|
|
getPixel l
|
|
|
|
| l <= 1 = '█'
|
|
|
|
| l <= 2 = '▓'
|
|
|
|
| l <= 3 = '▒'
|
|
|
|
| l <= 4 = '░'
|
|
|
|
| otherwise = ' '
|
2023-12-22 15:18:08 +00:00
|
|
|
|
2023-12-23 02:19:32 +00:00
|
|
|
rotVec :: V2 Float -> Float -> V2 Float
|
|
|
|
rotVec (V2 x y) rad = V2 nx ny
|
|
|
|
where
|
|
|
|
nx = x * cos rad + y * sin rad
|
|
|
|
ny = x * sin rad - y * cos rad
|
2024-11-25 02:48:18 +00:00
|
|
|
|
|
|
|
draw :: Game ()
|
|
|
|
draw = do
|
|
|
|
mapSlice <- msViewMap <$> gets scMapSlice
|
|
|
|
wizard <- gets scWizard
|
|
|
|
clientStateVar <- gets scClientState
|
|
|
|
vty <- clientVty <$> liftIO (STM.atomically $ STM.readTMVar clientStateVar)
|
|
|
|
(w, h) <- liftIO $ displayBounds (outputIface vty)
|
|
|
|
let result = map
|
|
|
|
(\y ->
|
|
|
|
map
|
|
|
|
(\x ->
|
2024-12-12 08:09:17 +00:00
|
|
|
getPixel $ foldl'
|
2024-11-25 02:48:18 +00:00
|
|
|
(\acc coord@(row, col) ->
|
|
|
|
if mapSlice M.! coord == Just Wall
|
|
|
|
then
|
|
|
|
let rayLength = castRay
|
|
|
|
wizard
|
|
|
|
(fromIntegral row, fromIntegral col)
|
|
|
|
(fromIntegral x, fromIntegral y)
|
|
|
|
(fromIntegral w, fromIntegral h)
|
|
|
|
in min rayLength acc
|
|
|
|
else
|
|
|
|
acc
|
|
|
|
)
|
|
|
|
5
|
|
|
|
((,) <$> [1 .. 9] <*> [1 .. 9])
|
|
|
|
)
|
|
|
|
[1 .. w]
|
|
|
|
)
|
|
|
|
[1 .. h]
|
2024-12-12 08:09:17 +00:00
|
|
|
image = foldl'
|
2024-11-25 02:48:18 +00:00
|
|
|
(\img line ->
|
|
|
|
img <-> string defAttr line
|
|
|
|
)
|
|
|
|
(string defAttr $ head result)
|
|
|
|
(tail result)
|
|
|
|
liftIO $ do
|
|
|
|
update vty (picForImage image)
|
|
|
|
-- mapM_ putStrLn result
|
|
|
|
|
|
|
|
castRay
|
|
|
|
:: Wizard -- Player
|
|
|
|
-> (Float, Float) -- coordinates of inspected tile
|
|
|
|
-> (Float, Float) -- screen pixel coordinates
|
|
|
|
-> (Float, Float) -- screen dimensions
|
|
|
|
-> Float -- Ray length
|
|
|
|
castRay wizard (row, col) (x, y) (width, height) =
|
2024-12-17 11:00:51 +00:00
|
|
|
let direction = angle (wizardRot wizard - hFOV / 2 + x * hFOV / width)
|
2024-11-25 02:48:18 +00:00
|
|
|
invdir@(V2 invDirCol invDirRow) = (1 /) <$> direction
|
|
|
|
wStep@(V2 wStepCol wStepRow) = wizardPos wizard - (fromIntegral . floor <$> wizardPos wizard)
|
|
|
|
ulCol = col - 1
|
|
|
|
ulRow = row - 1
|
|
|
|
brCol = col
|
|
|
|
brRow = row
|
|
|
|
dMinCol = ((if signum invDirCol <= 0 then brCol else ulCol) - (4 + wStepCol)) * invDirCol
|
|
|
|
dMaxCol = ((if signum invDirCol <= 0 then ulCol else brCol) - (4 + wStepCol)) * invDirCol
|
|
|
|
dMinRow = ((if signum invDirRow <= 0 then brRow else ulRow) - (4 + wStepRow)) * invDirRow
|
|
|
|
dMaxRow = ((if signum invDirRow <= 0 then ulRow else brRow) - (4 + wStepRow)) * invDirRow
|
|
|
|
tMinCol = max dMinCol 0
|
|
|
|
tMaxCol = min dMaxCol (recip 0)
|
|
|
|
tMinRow = max dMinRow 0
|
|
|
|
tMaxRow = min dMaxRow (recip 0)
|
|
|
|
tMin = max 0 $ max dMinCol dMinRow
|
|
|
|
tMax = min (recip 0) $ min dMaxCol dMaxRow
|
|
|
|
in
|
|
|
|
if tMin < tMax
|
|
|
|
then
|
|
|
|
let result = sqrt (quadrance ((tMin *) <$> direction)) * abs ((- vFOV) / 2 + y * vFOV / height)
|
|
|
|
-- in trace ("ray length: " <> show result) result
|
|
|
|
-- in trace ("vector: " <> show (sqrt $ quadrance $ (tMin *) <$> direction)) result
|
|
|
|
-- in trace ("wStep: " <> show wStep) result
|
|
|
|
in result
|
|
|
|
else
|
|
|
|
-- trace (show (tMin, tMax)) 5
|
|
|
|
-- trace (show invdir) 5
|
|
|
|
5
|