place npcs only in reachable areas
This commit is contained in:
parent
52c976c7f0
commit
5fe266ca9c
6 changed files with 31 additions and 9 deletions
|
@ -14,6 +14,7 @@ import System.Random
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
import Navigation
|
import Navigation
|
||||||
|
import Util
|
||||||
|
|
||||||
import Types.Interior
|
import Types.Interior
|
||||||
import Types.Map
|
import Types.Map
|
||||||
|
@ -139,7 +140,3 @@ insertMat i into (roffs, coffs) =
|
||||||
)
|
)
|
||||||
into
|
into
|
||||||
((,) <$> [1 .. nrows i] <*> [1 .. ncols i])
|
((,) <$> [1 .. nrows i] <*> [1 .. ncols i])
|
||||||
|
|
||||||
inBounds :: V2 Int -> Boundaries Int -> Bool
|
|
||||||
inBounds (V2 r c) (Boundaries (minr, minc) (maxr, maxc)) =
|
|
||||||
(r >= minr && r <= maxr) && (c >= minc && c <= maxc)
|
|
||||||
|
|
22
src/NPC.hs
22
src/NPC.hs
|
@ -13,7 +13,12 @@ import NanoVG hiding (V2(..))
|
||||||
|
|
||||||
-- internal imports
|
-- internal imports
|
||||||
|
|
||||||
|
import Navigation
|
||||||
|
import Util
|
||||||
|
|
||||||
import Types.UserData
|
import Types.UserData
|
||||||
|
import Types.Interior
|
||||||
|
import Types.Map
|
||||||
|
|
||||||
drawNPCs
|
drawNPCs
|
||||||
:: Context
|
:: Context
|
||||||
|
@ -40,8 +45,13 @@ drawNPCs ctx npcposs prow pcol row col = do
|
||||||
)
|
)
|
||||||
fnpcposs
|
fnpcposs
|
||||||
|
|
||||||
placeNPCs :: M.Matrix (Maybe ImgId) -> Int -> Affection UserData [V2 Double]
|
placeNPCs
|
||||||
placeNPCs imgmat count =
|
:: M.Matrix (Maybe ImgId)
|
||||||
|
-> [ReachPoint]
|
||||||
|
-> [Graph]
|
||||||
|
-> Int
|
||||||
|
-> Affection UserData [V2 Double]
|
||||||
|
placeNPCs imgmat rp gr count =
|
||||||
doPlace 1 []
|
doPlace 1 []
|
||||||
where
|
where
|
||||||
doPlace :: Int -> [V2 Double] -> Affection UserData [V2 Double]
|
doPlace :: Int -> [V2 Double] -> Affection UserData [V2 Double]
|
||||||
|
@ -50,8 +60,14 @@ placeNPCs imgmat count =
|
||||||
then do
|
then do
|
||||||
r <- liftIO $ randomRIO (1, M.nrows imgmat)
|
r <- liftIO $ randomRIO (1, M.nrows imgmat)
|
||||||
c <- liftIO $ randomRIO (1, M.ncols imgmat)
|
c <- liftIO $ randomRIO (1, M.ncols imgmat)
|
||||||
if null (imgObstacle $ imgmat M.! (r, c))
|
if null (imgObstacle $ imgmat M.! (r, c)) &&
|
||||||
|
isReachable imgmat [V2 r c] (exits r c)
|
||||||
then doPlace (nr + 1) ((V2 (fromIntegral r) (fromIntegral c)) : acc)
|
then doPlace (nr + 1) ((V2 (fromIntegral r) (fromIntegral c)) : acc)
|
||||||
else doPlace nr acc
|
else doPlace nr acc
|
||||||
else
|
else
|
||||||
return acc
|
return acc
|
||||||
|
applRooms row col =
|
||||||
|
filter (\r -> graphIsRoom r && inBounds (V2 row col) (bounds r)) gr
|
||||||
|
exits row col= concatMap (\b ->
|
||||||
|
filter (\p -> pointType p == RoomExit && inBounds (pointCoord p) b) rp
|
||||||
|
) (map bounds (applRooms row col))
|
||||||
|
|
|
@ -49,8 +49,8 @@ loadMap = do
|
||||||
[]
|
[]
|
||||||
((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat])
|
((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat])
|
||||||
-- liftIO $ A.logIO A.Debug (show exits)
|
-- liftIO $ A.logIO A.Debug (show exits)
|
||||||
(inter, _) <- liftIO $ placeInteriorIO mat imgmat exits gr
|
(inter, rps) <- liftIO $ placeInteriorIO mat imgmat exits gr
|
||||||
npcposs <- placeNPCs inter 10
|
npcposs <- placeNPCs inter rps gr 10
|
||||||
(nws, _) <- yieldSystemT (worldState ud) $ do
|
(nws, _) <- yieldSystemT (worldState ud) $ do
|
||||||
void $ newEntity $ defEntity
|
void $ newEntity $ defEntity
|
||||||
{ pos = Just (V2 20.5 20.5)
|
{ pos = Just (V2 20.5 20.5)
|
||||||
|
|
|
@ -56,5 +56,9 @@ data Graph
|
||||||
}
|
}
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
graphIsRoom :: Graph -> Bool
|
||||||
|
graphIsRoom (GRoom _ _) = True
|
||||||
|
graphIsRoom _ = False
|
||||||
|
|
||||||
class Size a where
|
class Size a where
|
||||||
size :: a -> Double
|
size :: a -> Double
|
||||||
|
|
|
@ -149,6 +149,7 @@ data Entity f = Entity
|
||||||
, obstacle :: Component f 'Field (Boundaries Double)
|
, obstacle :: Component f 'Field (Boundaries Double)
|
||||||
, player :: Component f 'Unique ()
|
, player :: Component f 'Unique ()
|
||||||
, npc :: Component f 'Field ()
|
, npc :: Component f 'Field ()
|
||||||
|
, npcPath :: Component f 'Field (Maybe [V2 Int])
|
||||||
}
|
}
|
||||||
deriving (Generic)
|
deriving (Generic)
|
||||||
|
|
||||||
|
|
|
@ -116,3 +116,7 @@ relativizeMouseCoords (V2 ix iy) = do
|
||||||
dx = fromIntegral rx - hx
|
dx = fromIntegral rx - hx
|
||||||
dy = fromIntegral ry - hy
|
dy = fromIntegral ry - hy
|
||||||
return $ V2 (dx / hx) (dy / hy)
|
return $ V2 (dx / hx) (dy / hy)
|
||||||
|
|
||||||
|
inBounds :: V2 Int -> Boundaries Int -> Bool
|
||||||
|
inBounds (V2 r c) (Boundaries (minr, minc) (maxr, maxc)) =
|
||||||
|
(r >= minr && r <= maxr) && (c >= minc && c <= maxc)
|
||||||
|
|
Loading…
Reference in a new issue