pituicat/src/Types/Map.hs

138 lines
3.4 KiB
Haskell
Raw Normal View History

{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
2020-12-14 02:27:21 +00:00
module Types.Map where
2020-10-06 02:19:07 +00:00
import qualified Data.Vector as V
2020-10-06 02:19:07 +00:00
import Linear
2020-10-12 04:08:33 +00:00
-- internal imports
import Graphics.Types.Texture
import Graphics.Types.VertexBuffer
import Graphics.Classes.Drawable
import Physics.Classes
2020-10-12 04:08:33 +00:00
2020-10-06 02:19:07 +00:00
data LevelMap = LevelMap
{ mapLayers :: V.Vector Layer -- ^ Layer stack
, mapWalkLayer :: Word -- ^ Collision layer index in stack
, mapDimensions :: V2 Word -- ^ Dimension of map in tiles
, mapTileMap :: TileMap -- ^ The tile map
, mapStartPos :: V2 Word -- ^ Player start position
2020-10-06 02:19:07 +00:00
}
deriving (Eq, Show)
type Layer = V.Vector Tile
instance Drawable Layer where
-- type VertexList Layer = V.Vector
toVertices vt = V.foldl
(\(acci, accv) (multi, a) ->
let (ris, vs) = toVertices a
is = V.map (multi * 4 +) ris
in (acci V.++ is, accv V.++ vs)
)
(V.empty, V.empty)
(V.zip (V.fromList [0 ..]) vt)
2020-10-06 02:19:07 +00:00
data Tile = Tile
{ tilePosition :: V2 Word -- ^
, tileOffset :: (V2 Float, V2 Float) -- ^ Graphics offset on 'TileMap'
, tileType :: TileType -- ^ Type of tile
2021-09-05 08:46:33 +00:00
, tileCollided :: Bool
2020-10-06 02:19:07 +00:00
}
deriving (Eq, Show)
instance Drawable Tile where
-- type VertexList Tile = V.Vector
2021-09-05 08:46:33 +00:00
toVertices (Tile (V2 x y) (V2 u1 v1, V2 u2 v2) _ _) =
( V.fromList [ 0, 1, 2, 2, 3, 0 ]
, V.fromList
[ newVertex
(V3 (fromIntegral x * 32) (fromIntegral y * 32) 0)
2020-12-23 15:39:45 +00:00
(V4 1 1 1 1)
(V2 u1 v1)
2020-12-23 06:47:20 +00:00
0
, newVertex
(V3 (fromIntegral (x + 1) * 32) (fromIntegral y * 32) 0)
2020-12-23 15:39:45 +00:00
(V4 1 1 1 1)
(V2 u2 v1)
2020-12-23 06:47:20 +00:00
0
, newVertex
(V3 (fromIntegral (x + 1) * 32) (fromIntegral (y + 1) * 32) 0)
2020-12-23 15:39:45 +00:00
(V4 1 1 1 1)
(V2 u2 v2)
2020-12-23 06:47:20 +00:00
0
, newVertex
(V3 (fromIntegral x * 32) (fromIntegral (y + 1) * 32) 0)
2020-12-23 15:39:45 +00:00
(V4 1 1 1 1)
(V2 u1 v2)
2020-12-23 06:47:20 +00:00
0
]
)
instance Mass Tile where
2021-09-05 08:46:33 +00:00
forces _ = V2 0 0
2021-09-05 08:46:33 +00:00
forcesUpdater t _ = t
2021-09-05 08:46:33 +00:00
mass _ = recip 0
velocity _ = V2 0 0
velocityUpdater t = const t
position t =
let (V2 x y) = fromIntegral <$> tilePosition t
in
V2 (x * 32 + 16) (y * 32 + 16)
positionUpdater t = const t
instance Collidible Tile where
2021-09-05 08:46:33 +00:00
prevPosition t = position t
impactForces _ = V2 0 0
impactForcesUpdater t _ = t
collisionOccured t = tileCollided t
updateCollisionOccurence t coll = t
{ tileCollided = coll
}
boundary _ =
( V2 (-16) (-16)
, V2 16 16
)
2020-10-06 02:19:07 +00:00
data TileType
2020-10-09 23:00:33 +00:00
= Solid
2020-10-06 02:19:07 +00:00
| Platform
| Decoration
deriving (Enum, Ord, Eq, Show)
data TileMap = TileMap
{ tileMapDimensions :: V2 Word -- ^ Dimensions of tile map image in pixels
, tileMapTexture :: Texture -- ^ Texture object on GPU
2020-10-06 02:19:07 +00:00
}
2020-10-09 23:00:33 +00:00
deriving (Eq, Show)
2020-10-09 03:51:00 +00:00
data LevelDescriptor = LevelDescriptor
{ levelLayerPath :: [(Word, FilePath)] -- ^ Indexed paths to the layers
, levelWalkLayer :: Word -- ^ Index of walk layer
, levelTileMap :: FilePath -- ^ Filepath to tile map
, levelStartPos :: (Word, Word) -- ^ Player start position
-- , levelCollectibles :: [(V2 Word, ())] -- ^ TODO; Collectibles and their tile coords
-- , levelEnemies :: [(V2 Word, ())] -- ^ TODO: Enemies and their tile coords
-- , levelInteractables :: [(V2 Word, ())] -- ^ TODO: Interactables and their coords
2020-10-09 03:51:00 +00:00
}
deriving (Eq, Show)