successful read of map layer

This commit is contained in:
nek0 2020-10-10 01:00:33 +02:00
parent 4ea4de78f6
commit 31a58280ae
6 changed files with 54 additions and 14 deletions

View file

@ -21,6 +21,7 @@ executable pituicat
, Types.Application
, Types.Subsystems
, Types.GameMap
, Map
-- other-extensions:
build-depends: base ^>=4.13.0.0
, affection
@ -29,6 +30,8 @@ executable pituicat
, containers
, linear
, text
, elerea
, aeson
, vector
, JuicyPixels
hs-source-dirs: src
default-language: Haskell2010

View file

@ -24,16 +24,16 @@ let
license = stdenv.lib.licenses.lgpl3;
}) {};
f = { mkDerivation, base, containers, elerea, linear, stdenv, sdl2, stm
, text}:
f = { mkDerivation, aeson, base, containers, JuicyPixels, linear, stdenv
, sdl2, stm, text, vector}:
mkDerivation {
pname = "pituicat";
version = "0.0.0.0";
src = ./.;
isLibrary = false;
isExecutable = true;
executableHaskellDepends = [ affection base elerea linear containers
sdl2 stm text];
executableHaskellDepends = [ aeson affection base linear containers
JuicyPixels sdl2 stm text vector];
license = stdenv.lib.licenses.gpl3;
};

View file

@ -49,6 +49,7 @@ main = do
init :: IO GameData
init = GameData
<$> newTVarIO Loading
<*> newTVarIO EmptyData
<*> (Subsystems
<$> (SubWindow <$> newTVarIO [])
<*> (SubMouse <$> newTVarIO [])

View file

@ -1,9 +1,43 @@
{-# LANGUAGE OverloadedStrings #-}
module Map where
import Linear
import Codec.Picture
import qualified Data.Vector as V
-- internal imports
import Types
testLevelDesc :: LevelDescriptor
testLevelDesc = LevelDescriptor
[ (0, "res/maps/00_test/00_test.bmp")
]
0
"res/tiles/00_test/00_test.png"
(3, 3)
-- constructMap :: LevelDescriptor -> IO LevelMap
-- cosntructMap desc = do
readLayer
:: (Word, FilePath) -- | index and path of the layer descriptor image
-> IO (MapLayer, V2 Int)
readLayer (idx, path) = do
img <- either error convertRGBA8 <$> readImage path
let width = imageWidth img
height = imageHeight img
layer = V.map
(\(py, px) -> case (pixelAt img px py) of
(PixelRGBA8 r g b _) -> Tile
(V2 (255 - fromIntegral g) (255 - fromIntegral b))
(case r of
255 -> Solid
254 -> Platform
_ -> Decoration
)
)
(V.fromList ((,) <$> [0 .. height - 1] <*> [0 .. width - 1]))
return (MapLayer layer, V2 width height)

View file

@ -11,11 +11,11 @@ import qualified Data.Map.Strict as M
-- internal imports
import Types.Subsystems
import Types.LevelMap
import Types.GameMap
data GameData = GameData
{ gameState :: TVar State
, gameStateData :: StateData
, gameStateData :: TVar StateData
, gameSubsystems :: Subsystems
, gameActionTranslation :: TVar ActionTranslation
, gameRunning :: TVar Bool
@ -63,7 +63,8 @@ data Level
type ActionTranslation = M.Map SDL.Keycode Action
data GameStateData
= TestData
data StateData
= EmptyData
| TestData
{ testMap :: LevelMap
}

View file

@ -1,4 +1,4 @@
module Types.LevelMap where
module Types.GameMap where
import Data.Vector
@ -19,13 +19,13 @@ newtype MapLayer = MapLayer
deriving (Eq, Show)
data Tile = Tile
{ tileOffset :: V2 Float -- | Offset of this tile on the tile map
{ tileOffset :: V2 Word -- | Offset of this tile on the tile map
, tileType :: TileType -- | Type of tile
}
deriving (Eq, Show)
data TileType
= Wall
= Solid
| Platform
| Decoration
deriving (Enum, Ord, Eq, Show)
@ -35,12 +35,13 @@ data TileMap = TileMap
, tileMapPath :: FilePath -- | Filepath to image
, tileMapIndex :: Word -- | Index on GPU
}
deriving (Eq, Show)
data LevelDescriptor = LevelDescriptor
{ levelLayerPath :: [(Word, FilePath)] -- | Indexed paths aths to the layers
{ 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
, 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