blob: 9dcc02b8e88f0f3490a334125e30e18ff2ee8b81 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
{-# LANGUAGE ViewPatterns #-}
--------------------------------------------------------------------------------
module Xanthous.Data.EntityMap.Graphics
( visiblePositions
, visibleEntities
) where
--------------------------------------------------------------------------------
import Xanthous.Prelude
--------------------------------------------------------------------------------
import Xanthous.Util (takeWhileInclusive)
import Xanthous.Data
import Xanthous.Data.EntityMap
import Xanthous.Entities
import Xanthous.Util.Graphics (circle, line)
--------------------------------------------------------------------------------
visiblePositions :: Position -> Word -> EntityMap SomeEntity -> Set Position
visiblePositions pos radius = setFromList . positions . visibleEntities pos radius
-- | Given a point and a radius of vision, returns a list of all entities that
-- are *visible* (eg, not blocked by an entity that obscures vision) from that
-- point
visibleEntities :: Position -> Word -> EntityMap SomeEntity -> EntityMap SomeEntity
visibleEntities (view _Position -> pos) visionRadius em
= fromEIDsAndPositioned . fold . fold $ sightAdjustedLines
where
-- I love laziness!
radius = circle pos $ fromIntegral visionRadius
linesOfSight = radius <&> line pos
entitiesOnLines = linesOfSight <&> map getPositionedAt
sightAdjustedLines = entitiesOnLines <&> takeWhileInclusive (none $ blocksVision . snd)
getPositionedAt p =
let ppos = _Position # p
in atPositionWithIDs ppos em
|