From f0a1e0685ae07582d9f2e95080aac182f96acc7c Mon Sep 17 00:00:00 2001 From: justin Date: Tue, 28 Jun 2022 09:43:50 +0100 Subject: [PATCH] Factor out wls/crsHit --- src/Dodge/WorldEvent/ThingsHit.hs | 37 ++++++++++++++----------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/src/Dodge/WorldEvent/ThingsHit.hs b/src/Dodge/WorldEvent/ThingsHit.hs index 65a7fc7a1..65016ff61 100644 --- a/src/Dodge/WorldEvent/ThingsHit.hs +++ b/src/Dodge/WorldEvent/ThingsHit.hs @@ -25,22 +25,22 @@ thingsHit -> Point2 -- ^ Line end point -> World -> Stream (Of (Point2, Either Creature Wall)) Identity () -thingsHit sp ep w - | sp == ep = S.each [] - | otherwise = void $ S.mergeOn (dist sp . fst) crs wls - where - crs :: Stream (Of (Point2, Either Creature Wall)) Identity () - crs = sortStreamOn (dist sp . fst) +thingsHit sp ep w = void $ S.mergeOn (dist sp . fst) + (S.map (second Left) (crsHit sp ep w)) + (S.map (second Right) (wlsHit sp ep w)) + +crsHit :: Point2 -> Point2 -> World -> Stream (Of (Point2, Creature)) Identity () +crsHit sp ep + | sp == ep = const mempty + | otherwise = sortStreamOn (dist sp . fst) . S.mapMaybe - (\cr -> (,Left cr) <$> listToMaybe (intersectCircSeg (_crPos cr) (_crRad cr) sp ep)) + (\cr -> (, cr) <$> listToMaybe (intersectCircSeg (_crPos cr) (_crRad cr) sp ep)) . S.each - $ _creatures w - wls :: Stream (Of (Point2, Either Creature Wall)) Identity () - wls = S.map (second Right) - . sortStreamOn (dist sp . fst) - . overlapSegWalls sp ep $ wallsAlongLine sp ep w + . _creatures + thingHit :: Point2 -> Point2 -> World -> Maybe (Point2, Either Creature Wall) thingHit sp ep = runIdentity . S.head_ . thingsHit sp ep + {- List objects that appear on a line. Can filter out a creature. -} thingsHitExceptCr @@ -55,12 +55,9 @@ thingsHitExceptCr (Just cid) sp ep = S.filter crNotCid . thingsHit sp ep crNotCid (_,Left cr) = _crID cr /= cid crNotCid _ = True -wallsHit - :: Point2 -- ^ Line start point - -> Point2 -- ^ Line end point - -> World - -> Stream (Of (Point2, Wall)) Identity () -wallsHit sp ep w - | sp == ep = S.each [] +wlsHit :: Point2 -> Point2 -> World -> Stream (Of (Point2, Wall)) Identity () +wlsHit sp ep + | sp == ep = const mempty | otherwise = sortStreamOn (dist sp . fst) - . overlapSegWalls sp ep $ wallsAlongLine sp ep w + . overlapSegWalls sp ep + . wallsAlongLine sp ep