place npcs only in reachable areas

This commit is contained in:
nek0 2018-04-14 13:34:28 +02:00
parent 52c976c7f0
commit 5fe266ca9c
6 changed files with 31 additions and 9 deletions

View file

@ -14,6 +14,7 @@ import System.Random
-- internal imports
import Navigation
import Util
import Types.Interior
import Types.Map
@ -139,7 +140,3 @@ insertMat i into (roffs, coffs) =
)
into
((,) <$> [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)

View file

@ -13,7 +13,12 @@ import NanoVG hiding (V2(..))
-- internal imports
import Navigation
import Util
import Types.UserData
import Types.Interior
import Types.Map
drawNPCs
:: Context
@ -40,8 +45,13 @@ drawNPCs ctx npcposs prow pcol row col = do
)
fnpcposs
placeNPCs :: M.Matrix (Maybe ImgId) -> Int -> Affection UserData [V2 Double]
placeNPCs imgmat count =
placeNPCs
:: M.Matrix (Maybe ImgId)
-> [ReachPoint]
-> [Graph]
-> Int
-> Affection UserData [V2 Double]
placeNPCs imgmat rp gr count =
doPlace 1 []
where
doPlace :: Int -> [V2 Double] -> Affection UserData [V2 Double]
@ -50,8 +60,14 @@ placeNPCs imgmat count =
then do
r <- liftIO $ randomRIO (1, M.nrows 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)
else doPlace nr acc
else
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))

View file

@ -49,8 +49,8 @@ loadMap = do
[]
((,) <$> [1 .. nrows mat] <*> [1 .. ncols mat])
-- liftIO $ A.logIO A.Debug (show exits)
(inter, _) <- liftIO $ placeInteriorIO mat imgmat exits gr
npcposs <- placeNPCs inter 10
(inter, rps) <- liftIO $ placeInteriorIO mat imgmat exits gr
npcposs <- placeNPCs inter rps gr 10
(nws, _) <- yieldSystemT (worldState ud) $ do
void $ newEntity $ defEntity
{ pos = Just (V2 20.5 20.5)

View file

@ -56,5 +56,9 @@ data Graph
}
deriving (Show, Eq)
graphIsRoom :: Graph -> Bool
graphIsRoom (GRoom _ _) = True
graphIsRoom _ = False
class Size a where
size :: a -> Double

View file

@ -149,6 +149,7 @@ data Entity f = Entity
, obstacle :: Component f 'Field (Boundaries Double)
, player :: Component f 'Unique ()
, npc :: Component f 'Field ()
, npcPath :: Component f 'Field (Maybe [V2 Int])
}
deriving (Generic)

View file

@ -116,3 +116,7 @@ relativizeMouseCoords (V2 ix iy) = do
dx = fromIntegral rx - hx
dy = fromIntegral ry - 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)