tracer/src/Test.hs

279 lines
7.9 KiB
Haskell
Raw Normal View History

module Test where
2018-02-18 02:11:41 +00:00
import Affection as A hiding (get)
2018-02-18 02:11:41 +00:00
import SDL (get, ($=))
import qualified SDL
import qualified Graphics.Rendering.OpenGL as GL hiding (get)
2018-03-02 01:10:35 +00:00
import Control.Monad (when, unless, void)
import Control.Monad.IO.Class (liftIO)
2018-02-25 09:30:13 +00:00
import Data.Map.Strict as Map
2018-03-01 22:33:08 +00:00
import qualified Data.Set as S
import qualified Data.Text as T
2018-03-03 10:06:38 +00:00
import Data.Matrix as M
2018-02-18 04:31:34 +00:00
import Data.Ecstasy as E
2018-03-04 21:24:30 +00:00
import Data.Maybe
2018-02-18 02:11:41 +00:00
import NanoVG hiding (V2(..))
import Types
import Floorplan
2018-02-18 02:11:41 +00:00
import Linear
import Foreign.C.Types (CFloat(..))
2018-02-18 02:11:41 +00:00
import Debug.Trace
2018-03-02 01:10:35 +00:00
-- internal imports
2018-03-03 16:03:17 +00:00
import Interior
2018-03-02 01:10:35 +00:00
import Util
loadMap :: Affection UserData ()
loadMap = do
ud <- getAffection
2018-03-03 16:42:24 +00:00
let fc = FloorConfig
(20, 20)
[] -- [(5,5), (35, 35)]
(50,75)
2018-02-18 02:11:41 +00:00
(Subsystems _ m) = subsystems ud
2018-03-03 16:03:17 +00:00
(matrix, gr) <- liftIO $ buildHallFloorIO fc
2018-03-03 16:42:24 +00:00
inter <- liftIO $ placeInteriorIO matrix (convertTileToImg matrix) gr
2018-02-25 01:03:25 +00:00
(nws, _) <- yieldSystemT (worldState ud) $ do
2018-02-18 04:31:34 +00:00
void $ newEntity $ defEntity
2018-02-25 01:03:46 +00:00
{ pos = Just (V2 20.5 20.5)
2018-02-18 04:31:34 +00:00
, vel = Just (V2 0 0)
, player = Just True
}
2018-02-18 02:11:41 +00:00
uu <- partSubscribe m movePlayer
putAffection ud
2018-02-25 01:03:25 +00:00
{ worldState = nws
2018-02-18 04:31:34 +00:00
, stateData = MenuData
{ mapMat = matrix
2018-03-03 16:42:24 +00:00
, imgMat = inter
2018-02-23 12:07:24 +00:00
, initCoords = (0, 500)
}
2018-02-18 02:11:41 +00:00
, uuid = [uu]
}
2018-02-18 04:31:34 +00:00
mouseToPlayer :: V2 Int32 -> Affection UserData ()
mouseToPlayer mv2 = do
2018-02-18 02:11:41 +00:00
ud <- getAffection
rela@(V2 rx ry) <- liftIO $ relativizeMouseCoords mv2
2018-02-24 22:15:16 +00:00
let dr = (ry / sin (atan (1/2)) / 2) + rx
dc = rx - (ry / sin (atan (1/2)) / 2)
2018-02-25 01:03:25 +00:00
(nws, _) <- yieldSystemT (worldState ud) $ do
2018-02-18 04:31:34 +00:00
emap $ do
with player
2018-02-25 09:30:13 +00:00
pos' <- E.get pos
2018-02-18 04:31:34 +00:00
pure $ defEntity'
{ vel = Set $ V2 dr dc
}
2018-02-18 02:11:41 +00:00
putAffection ud
2018-02-25 01:03:25 +00:00
{ worldState = nws
2018-02-18 04:31:34 +00:00
}
movePlayer :: MouseMessage -> Affection UserData ()
movePlayer (MsgMouseMotion _ _ _ [SDL.ButtonLeft] m _) = mouseToPlayer m
movePlayer (MsgMouseButton _ _ SDL.Pressed _ SDL.ButtonLeft _ m) =
mouseToPlayer m
movePlayer (MsgMouseButton _ _ SDL.Released _ SDL.ButtonLeft _ m) = do
2018-02-18 04:31:34 +00:00
ud <- getAffection
2018-02-25 01:03:25 +00:00
(nws, _) <- yieldSystemT (worldState ud) $ do
2018-02-18 04:31:34 +00:00
emap $ do
with player
pure $ defEntity'
{ vel = Set $ V2 0 0
}
putAffection ud
2018-02-25 01:03:25 +00:00
{ worldState = nws
2018-02-18 02:11:41 +00:00
}
movePlayer _ = return ()
drawMap :: Affection UserData ()
drawMap = do
ud <- getAffection
2018-03-01 22:33:08 +00:00
dt <- getDelta
2018-03-03 10:06:38 +00:00
(_, playerPos) <- yieldSystemT (worldState ud) $ do
efor $ \_ -> do
with player
pos' <- E.get pos
pure pos'
let V2 pr pc = head playerPos
matrix = imgMat (stateData ud)
ctx = nano ud
cols = fromIntegral (ncols matrix)
rows = fromIntegral (nrows matrix)
tileWidth = 64 :: Double
tileHeight = 32 :: Double
x = realToFrac $ 640 + ((1 - pc) + (1 - pr)) * (tileWidth / 2)
y = realToFrac $ 360 + ((1 - pr) - (1 - pc)) * (tileHeight / 2)
2018-03-01 22:33:08 +00:00
liftIO $ do
2018-03-03 10:06:38 +00:00
beginPath ctx
moveTo ctx x y
lineTo ctx
(x + cols * (realToFrac tileWidth / 2))
(y - realToFrac tileHeight / 2 * cols)
lineTo ctx
(x + realToFrac tileWidth / 2 * (cols + rows))
(y + (rows - cols) * (realToFrac tileHeight / 2))
lineTo ctx
(x + realToFrac tileWidth / 2 * rows)
(y + realToFrac tileHeight / 2 * rows)
closePath ctx
fillColor ctx (rgb 255 255 255)
fill ctx
mapM_ (\(i, ls) -> mapM_
(\(j, t) -> drawTile
2018-03-03 16:03:17 +00:00
(assetImages ud) ctx pr pc i j t)
2018-03-03 10:06:38 +00:00
(reverse $ zip [1..] ls))
(zip [1..] (toLists matrix))
2018-03-01 22:33:08 +00:00
fontSize ctx 20
fontFace ctx (assetFonts ud Map.! FontBedstead)
textAlign ctx (S.fromList [AlignCenter,AlignTop])
fillColor ctx (rgb 255 128 0)
textBox ctx 0 0 200 ("FPS: " `T.append` (T.pack $ Prelude.take 5 $ show (1/dt)))
2018-02-18 04:31:34 +00:00
updateMap :: Double -> Affection UserData ()
2018-02-24 21:24:48 +00:00
updateMap dt = do
2018-02-18 04:31:34 +00:00
ud <- getAffection
2018-02-25 01:03:25 +00:00
let matrix = mapMat $ stateData ud
(nws, _) <- yieldSystemT (worldState ud) $ do
2018-02-18 04:31:34 +00:00
emap $ do
2018-02-25 01:03:25 +00:00
with vel
with pos
pos'@(V2 or oc) <- E.get pos
2018-02-18 04:31:34 +00:00
vel' <- E.get vel
2018-02-25 01:03:25 +00:00
let npos@(V2 nr nc) = pos' + fmap (* (4 * dt)) vel'
2018-03-05 20:11:38 +00:00
let ent = defEntity'
{ pos = Set $ Prelude.foldl (checkBoundsCollision pos')
npos
(imgObstacle (imgMat (stateData ud) M.! (floor nr, floor nc)))
}
2018-02-25 09:30:13 +00:00
return ent
2018-02-18 04:31:34 +00:00
putAffection ud
2018-02-25 01:03:25 +00:00
{ worldState = nws
2018-02-18 04:31:34 +00:00
}
2018-03-03 16:03:17 +00:00
drawTile
:: Map ImgId Image
-> Context
-> Double
-> Double
-> Int
-> Int
-> Maybe ImgId
-> IO ()
2018-03-04 21:24:30 +00:00
drawTile ai ctx pr pc row col img =
do
save ctx
if (isNothing img)
then drawPlayer
else do
2018-03-05 20:11:38 +00:00
if (Prelude.null mb)
2018-03-04 21:24:30 +00:00
then do
drawImage
drawPlayer
else do
2018-03-05 20:11:38 +00:00
if any (\minr -> pr <= fromIntegral (floor pr) + minr) minrs &&
any (\minc -> pc >= fromIntegral (floor pc) + minc) mincs
2018-03-04 21:24:30 +00:00
then do
drawPlayer
drawImage
else do
drawImage
drawPlayer
restore ctx
where
tileWidth = 64 :: Double
tileHeight = 32 :: Double
2018-03-05 20:11:38 +00:00
minrs = Prelude.map (fst . matmin) mb
maxrs = Prelude.map (fst . matmax) mb
mincs = Prelude.map (snd . matmin) mb
maxcs = Prelude.map (snd . matmax) mb
2018-03-04 21:24:30 +00:00
x = realToFrac $ 640 + ((fromIntegral col - pc) +
(fromIntegral row - pr)) * (tileWidth / 2)
y = realToFrac $ 360 - (tileHeight / 2) + ((fromIntegral row - pr) -
(fromIntegral col - pc)) * (tileHeight / 2)
dist = distance (V2 (fromIntegral row) (fromIntegral col))
(V2 (realToFrac pr - 1) (realToFrac pc)) / 4
fact =
if (pr <= fromIntegral row + maximum maxrs &&
2018-03-05 20:11:38 +00:00
pc >= fromIntegral col + minimum mincs) &&
isWall (fromJust img)
2018-03-04 21:24:30 +00:00
then min 1 dist
else 1
mb = imgObstacle img
drawImage = do
beginPath ctx
paint <- imagePattern
ctx x (y - (74 * fact - realToFrac tileHeight))
(realToFrac tileWidth) 74
0
(ai Map.! fromJust img)
fact
if fact < 1
then do
moveTo ctx x (y + (realToFrac tileHeight - 74 * fact))
lineTo ctx
(x + realToFrac tileWidth)
(y + (realToFrac tileHeight - 74 * fact))
lineTo ctx (x + realToFrac tileWidth) (y + realToFrac tileHeight / 2)
lineTo ctx (x + realToFrac tileWidth / 2) (y + realToFrac tileHeight)
lineTo ctx x (y + realToFrac tileHeight / 2)
closePath ctx
else
rect ctx x (y - (74 - realToFrac tileHeight)) (realToFrac tileWidth) 74
fillPaint ctx paint
fill ctx
drawPlayer = do
when (floor pr == row && floor pc == col) $ do
beginPath ctx
circle ctx 640 360 5
closePath ctx
fillColor ctx (rgba 0 255 255 255)
fill ctx
2018-03-05 20:11:38 +00:00
checkBoundsCollision
:: V2 Double
-> V2 Double
-> Boundaries Double
-> V2 Double
checkBoundsCollision
pos'@(V2 or oc) acc@(V2 fr fc) (Boundaries (minr, minc) (maxr, maxc))
| (fr - fromIntegral (floor fr) >= minr &&
fr - fromIntegral (floor fr) <= maxr) &&
(fc - fromIntegral (floor fc) >= minc &&
fc - fromIntegral (floor fc) <= maxc) &&
(or - fromIntegral (floor or) < minr ||
or - fromIntegral (floor or) > maxr) =
(V2 or fc)
| (fr - fromIntegral (floor fr) >= minr &&
fr - fromIntegral (floor fr) <= maxr) &&
(fc - fromIntegral (floor fc) >= minc &&
fc - fromIntegral (floor fc) <= maxc) &&
(oc - fromIntegral (floor oc) < minc ||
oc - fromIntegral (floor oc) > maxc) =
(V2 fr oc)
| (fr - fromIntegral (floor fr) < minr ||
fr - fromIntegral (floor fr) > maxr) &&
(fc - fromIntegral (floor fc) < minc ||
fc - fromIntegral (floor fc) > maxc) =
acc
| (fr - fromIntegral (floor fr) < minr ||
fr - fromIntegral (floor fr) > maxr) &&
(fc - fromIntegral (floor fc) >= minc &&
fc - fromIntegral (floor fc) <= maxc) =
acc
| (fr - fromIntegral (floor fr) >= minr &&
fr - fromIntegral (floor fr) <= maxr) &&
(fc - fromIntegral (floor fc) < minc ||
fc - fromIntegral (floor fc) > maxc) =
acc
| otherwise =
pos'