Simplify bullet movement

This commit is contained in:
2025-01-08 19:05:48 +00:00
parent ef751cbd21
commit bd42f44278
2 changed files with 262 additions and 267 deletions
+23 -33
View File
@@ -22,10 +22,8 @@ import Data.Bifunctor
updateBullet :: World -> Bullet -> (World, Maybe Bullet)
updateBullet w bu
| magV (_buVel bu) < 1 = (endspawn w, Nothing)
| otherwise =
second (fmap updateBulVel)
. hitEffFromBul w
$ applyMagnetsToBul bu w
-- have to be slightly carefull not to accelerate bullets using magnets
| otherwise = second (Just . updateBulVel) . hitEffFromBul w $ applyMagnetsToBul bu w
where
endspawn = fromMaybe id $ do
partspawn <- useBulletPayload bu
@@ -34,18 +32,19 @@ updateBullet w bu
-- do we want this to drain energy from the deflection source?
applyMagnetsToBul :: Bullet -> World -> Bullet
applyMagnetsToBul bu =
foldl' (flip doMagnetBuBu) bu . _oldMagnets . _lWorld . _cWorld
applyMagnetsToBul bu = foldl' doMagnetBuBu bu . _oldMagnets . _lWorld . _cWorld
doMagnetBuBu :: Magnet -> Bullet -> Bullet
doMagnetBuBu mg bu = if isclose
then case _mgField mg of
MagnetAlign -> bu & buVel %~ vecTurnTo (10 * pi / (d+40)) bpos mgalignpos
MagnetDeflect -> bu & buVel %~ vecTurnTo (10 * pi / (d+40)) bpos mgdeflectpos
MagnetAttract -> bu & buVel %~ vecTurnTo (10 * pi / (d+40)) bpos mpos
MagnetRepulse -> bu & buVel %~ vecTurnTo (10 * pi / (d+40)) bpos (2*bpos - mpos)
else bu
doMagnetBuBu :: Bullet -> Magnet -> Bullet
doMagnetBuBu bu mg
| notclose = bu
| otherwise = case _mgField mg of
MagnetAlign -> doturntowards mgalignpos
MagnetDeflect -> doturntowards mgdeflectpos
MagnetAttract -> doturntowards mpos
MagnetRepulse -> doturntowards (2*bpos - mpos)
where
notclose = d > 100
doturntowards p = bu & buVel %~ vecTurnTo (10 * pi / (d+40)) bpos p
bvel = bu ^. buVel
mpos = mg ^. mgPos
bpos = bu ^. buPos
@@ -56,10 +55,9 @@ doMagnetBuBu mg bu = if isclose
| dotV bvel (bpos - mpos) > 0 = bpos - mpos
| otherwise = mpos - bpos
d = dist (bu ^. buPos) (mg ^. mgPos)
isclose = d < 100
updateBulVel :: Bullet -> Bullet
updateBulVel bt = bt & buVel .*.*~ _buDrag bt
updateBulVel bt = bt & buVel .*.*~ _buDrag bt
--case _buTrajectory bt of
-- BasicBulletTrajectory -> bt & buVel .*.*~ _buDrag bt
-- MagnetTrajectory tpos -> bt
@@ -155,10 +153,7 @@ makeFlak bu _ w = w & cWorld . lWorld . bullets .++~ [f x | x <- xs]
]
g x v = v +.+ x *.* normalizeV (vNormal v)
hitEffFromBul ::
World ->
Bullet ->
(World, Maybe Bullet)
hitEffFromBul :: World -> Bullet -> (World, Bullet)
hitEffFromBul w bu = case _buEffect bu of
PenetrateBullet -> movePenBullet bu hitstream w
BounceBullet -> case List.safeHead hitstream of
@@ -167,8 +162,7 @@ hitEffFromBul w bu = case _buEffect bu of
dir <- bounceDir (hp, crwl)
return
( w
, Just $
bu
, bu
& buPos .~ hp +.+ normalizeV (_buPos bu -.- hp)
& buVel %~ reflectIn dir
-- & buTrajectory .~ BasicBulletTrajectory
@@ -196,22 +190,18 @@ expireAndDamage ::
Bullet ->
[(Point2, Either Creature Wall)] ->
World ->
(World, Maybe Bullet)
(World, Bullet)
expireAndDamage bt things w = case List.safeHead things of
Nothing -> (w, moveBullet bt)
Just x -> (damageThingHit bt x w, destroyAt (fst x) bt)
Just x -> (damageThingHit bt x w, stopBulletAt (fst x) bt)
moveBullet :: Bullet -> Maybe Bullet
moveBullet pt =
Just $
pt
moveBullet :: Bullet -> Bullet
moveBullet pt = pt
& buPos %~ (+.+ _buVel pt)
& buOldPos .~ _buPos pt
destroyAt :: Point2 -> Bullet -> Maybe Bullet
destroyAt hitp pt =
Just $
pt
stopBulletAt :: Point2 -> Bullet -> Bullet
stopBulletAt hitp pt = pt
& buPos .~ hitp +.+ normalizeV (p -.- hitp)
& buOldPos .~ p
& buVel .~ 0
@@ -222,7 +212,7 @@ movePenBullet ::
Bullet ->
[(Point2, Either Creature Wall)] ->
World ->
(World, Maybe Bullet)
(World, Bullet)
movePenBullet bu hitstream w = case hitstream of
[] -> (w, moveBullet bu)
((p, crwl) : strm) ->
+239 -234
View File
File diff suppressed because it is too large Load Diff