enable collision detection for NPCs
This commit is contained in:
parent
371a54cbd7
commit
bbd57c8ed1
1 changed files with 45 additions and 8 deletions
|
@ -791,6 +791,12 @@ updateMap dt = do
|
||||||
return $ unchanged
|
return $ unchanged
|
||||||
{ anim = Set nstate
|
{ anim = Set nstate
|
||||||
}
|
}
|
||||||
|
obstacleBounds <- efor allEnts $ do
|
||||||
|
with obstacle
|
||||||
|
with pos
|
||||||
|
b <- query obstacle
|
||||||
|
pos' <- query pos
|
||||||
|
return (pos', b)
|
||||||
emap allEnts $ do
|
emap allEnts $ do
|
||||||
without player
|
without player
|
||||||
with vel
|
with vel
|
||||||
|
@ -798,13 +804,50 @@ updateMap dt = do
|
||||||
with pos
|
with pos
|
||||||
with rot
|
with rot
|
||||||
with anim
|
with anim
|
||||||
pos' <- query pos
|
pos'@(V2 pr pc) <- query pos
|
||||||
vel' <- query vel
|
vel' <- query vel
|
||||||
rot' <- query rot
|
rot' <- query rot
|
||||||
fact' <- query velFact
|
fact' <- query velFact
|
||||||
stat <- query anim
|
stat <- query anim
|
||||||
let npos = pos' + fmap (* (dt * fact')) vel'
|
let npos = pos' + fmap (* (dt * fact')) vel'
|
||||||
|
dpos@(V2 dpr dpc) = npos - pos'
|
||||||
aId = asId stat
|
aId = asId stat
|
||||||
|
lll = (,)
|
||||||
|
<$> (
|
||||||
|
if dpr < 0
|
||||||
|
then [(floor dpr :: Int) .. 0]
|
||||||
|
else [0 .. (ceiling dpr :: Int)])
|
||||||
|
<*> (
|
||||||
|
if dpc < 0
|
||||||
|
then [(floor dpc :: Int) .. 0]
|
||||||
|
else [0 .. (ceiling dpc :: Int)])
|
||||||
|
colldpos = dpos * Prelude.foldl
|
||||||
|
(\acc a ->
|
||||||
|
let ret = checkBoundsCollision2 pos' npos dt acc a
|
||||||
|
in A.log A.Verbose (show ret) ret)
|
||||||
|
(V2 1 1)
|
||||||
|
(
|
||||||
|
concatMap
|
||||||
|
(\(dr, dc) ->
|
||||||
|
let bs = (++)
|
||||||
|
(maybe [] collisionObstacle (fromMaybe Nothing $ M.safeGet
|
||||||
|
(fromIntegral $ floor pr + dr)
|
||||||
|
(fromIntegral $ floor pc + dc)
|
||||||
|
(imgMat (stateData ud))))
|
||||||
|
(Prelude.map snd $ Prelude.filter
|
||||||
|
(\((V2 br bc), _) ->
|
||||||
|
floor pr + dr == floor br &&
|
||||||
|
floor pc + dc == floor bc
|
||||||
|
)
|
||||||
|
obstacleBounds)
|
||||||
|
in Prelude.map (\(Boundaries (minr, minc) (maxr, maxc))->
|
||||||
|
Boundaries
|
||||||
|
(minr + fromIntegral dr, minc + fromIntegral dc)
|
||||||
|
(maxr + fromIntegral dr, maxc + fromIntegral dc)
|
||||||
|
) bs
|
||||||
|
)
|
||||||
|
lll -- (A.log A.Verbose (show lll ++ " " ++ show len) lll)
|
||||||
|
)
|
||||||
nstat = case aiName aId of
|
nstat = case aiName aId of
|
||||||
"walking"
|
"walking"
|
||||||
| sqrt (vel' `dot` vel') > 0 ->
|
| sqrt (vel' `dot` vel') > 0 ->
|
||||||
|
@ -838,17 +881,11 @@ updateMap dt = do
|
||||||
}
|
}
|
||||||
x -> error ("unknown animation name" ++ x)
|
x -> error ("unknown animation name" ++ x)
|
||||||
ent = unchanged
|
ent = unchanged
|
||||||
{ pos = Set npos
|
{ pos = Set $ pos' + colldpos
|
||||||
, rot = Set $ fromMaybe rot' (direction vel')
|
, rot = Set $ fromMaybe rot' (direction vel')
|
||||||
, anim = Set nstat
|
, anim = Set nstat
|
||||||
}
|
}
|
||||||
return ent
|
return ent
|
||||||
obstacleBounds <- efor allEnts $ do
|
|
||||||
with obstacle
|
|
||||||
with pos
|
|
||||||
b <- query obstacle
|
|
||||||
pos' <- query pos
|
|
||||||
return (pos', b)
|
|
||||||
emap allEnts $ do
|
emap allEnts $ do
|
||||||
with player
|
with player
|
||||||
with vel
|
with vel
|
||||||
|
|
Loading…
Reference in a new issue