Tweak bullet bouncing and spawning

This commit is contained in:
2022-07-19 18:21:51 +01:00
parent 3031f2478c
commit 89d397a928
8 changed files with 49 additions and 37 deletions
+17 -16
View File
@@ -17,7 +17,6 @@ import qualified Streaming.Prelude as S
import Data.Maybe
import Dodge.Movement.Turn
import Dodge.WorldEvent.ThingsHit
import Control.Monad.State
updateBullet :: World -> Bullet -> (World,Maybe Bullet)
updateBullet w bu = case _buState bu of
@@ -53,22 +52,12 @@ useAmmoParams it cr w = w & instantBullets .:~ (_amBullet bultype
mvBullet :: Float -> World -> Bullet -> (World, Maybe Bullet)
mvBullet x w bt'
| t <= 0 || magV (_buVel bt) < 1 = (endspawn w,Nothing)
| otherwise = bimap maybebounce (fmap dodrag) $
| otherwise = bimap id (fmap dodrag) $
hiteff bt hitstream w
where
endspawn w' = fromMaybe w' $ do
partspawn <- bulletSpawn bt'
return $ partspawn p w'
maybebounce = fromMaybe id $ do
guard (_buEffect bt' == BounceBullet)
(hp,crwl) <- runIdentity (S.head_ hitstream)
dir <- bounceDir (hp,crwl)
return $ instantBullets .:~ (bt
& buPos .~ hp +.+ normalizeV (_buPos bt' -.- hp)
& buVel %~ reflectIn dir
& buTrajectory .~ BasicBulletTrajectory
& buTimer -~ 1
)
hitstream = thingsHit p (p +.+ vel) w
bt = foldr (\mg b -> _mgField mg mg b) bt' (_magnets w)
& buState .~ NormalBulletState
@@ -86,7 +75,7 @@ mvBullet x w bt'
t = _buTimer bt
bounceDir :: (Point2, Either Creature Wall) -> Maybe Point2
bounceDir (_,Right wl) = Just $ uncurry (-.-) (_wlLine wl)
bounceDir (_,Right wl) | _wlBouncy wl = Just $ uncurry (-.-) (_wlLine wl)
bounceDir (p,Left cr) | crIsArmouredFrom p cr = Just $ vNormal $ p -.- _crPos cr
bounceDir _ = Nothing
@@ -101,11 +90,23 @@ hitEffFromBul :: Float -> Bullet
-> Stream (Of (Point2, Either Creature Wall)) Identity ()
-> World
-> (World,Maybe Bullet)
hitEffFromBul x = expireAndDamage' x setfromtodams
hitEffFromBul x bu hitstream w = case _buEffect bu of
PenetrateBullet -> undefined
BounceBullet -> case runIdentity (S.head_ hitstream) of
Nothing -> (w, moveBullet x bu)
Just (hp,crwl) -> fromMaybe (expireAndDamage' x setfromtodams bu hitstream w) $ do
dir <- bounceDir (hp,crwl)
return $ (w,Just $ bu
& buPos .~ hp +.+ normalizeV (_buPos bu -.- hp)
& buVel %~ reflectIn dir
& buTrajectory .~ BasicBulletTrajectory
& buTimer -~ 1
)
DestroyBullet -> expireAndDamage' x setfromtodams bu hitstream w
where
setfromtodams bu p = map f (_buDamages bu)
setfromtodams bu' p = map f (_buDamages bu')
where
f = (dmFrom .~ _buPos bu) . (dmAt .~ p) . (dmTo .~ _buPos bu +.+ _buVel bu)
f = (dmFrom .~ _buPos bu') . (dmAt .~ p) . (dmTo .~ _buPos bu' +.+ _buVel bu')
expireAndDamage' :: Float -> (Bullet -> Point2 -> [Damage])
-> Bullet