Fix bug where new sounds didn't overplay old sounds

This commit is contained in:
2021-08-26 22:56:11 +01:00
parent c97c16777a
commit c4df048d31
7 changed files with 82 additions and 34 deletions
+3 -1
View File
@@ -159,7 +159,9 @@ isFrictionless cr = case cr ^? crStance . carriage of
_ -> False
stepItemUseCooldown :: Creature -> Creature
stepItemUseCooldown cr = over (crInv . ix iSel . itUseTime) decreaseToZero cr
stepItemUseCooldown cr = cr & crInv . ix iSel %~
(itUseTime %~ decreaseToZero) . (wpCurWarmUp %~ decreaseToZero)
--( over (crInv . ix iSel . itUseTime) decreaseToZero cr
where
iSel = _crInvSel cr
+18 -14
View File
@@ -211,23 +211,27 @@ data ItemPos
| OnFloor { _itFlID :: Int }
data Item
= Weapon
{ _itName :: String
, _wpMaxAmmo :: Int
, _wpLoadedAmmo :: Int
, _wpAmmo :: Ammo
, _wpReloadTime :: Int
{ _itName :: String
, _wpMaxAmmo :: Int
, _wpLoadedAmmo :: Int
, _wpAmmo :: Ammo
, _wpReloadTime :: Int
, _wpReloadState :: Int
, _wpReloadType :: ReloadType
, _itUseRate :: Int
, _itUseTime :: Int
, _itUse :: Item -> Creature -> World -> World
, _wpReloadType :: ReloadType
, _wpMaxWarmUp :: Int
, _wpCurWarmUp :: Int
, _wpMaxCoolDown :: Int
, _wpCurCoolDown :: Int
, _itUseRate :: Int
, _itUseTime :: Int
, _itUse :: Item -> Creature -> World -> World
, _itUseModifiers :: [(Item -> Creature -> World -> World) -> Item -> Creature -> World -> World]
, _itLeftClickUse :: Maybe (Creature -> Int -> World -> World)
, _wpSpread :: Float
, _wpRange :: Float
, _itHammer :: HammerPosition
, _itFloorPict :: Picture
, _itMaxStack :: Int
, _wpSpread :: Float
, _wpRange :: Float
, _itHammer :: HammerPosition
, _itFloorPict :: Picture
, _itMaxStack :: Int
, _itAmount :: Int
, _itAimingSpeed :: Float
, _itAimingRange :: Float
+4
View File
@@ -15,6 +15,10 @@ defaultGun = Weapon
, _wpReloadTime = 40
, _wpReloadState = 0
, _wpReloadType = ActiveReload
, _wpMaxWarmUp = 0
, _wpCurWarmUp = 0
, _wpMaxCoolDown = 0
, _wpCurCoolDown = 0
, _itUseRate = 8
, _itUseTime = 0
, _itUse = \_ _ -> id
+7 -2
View File
@@ -291,10 +291,11 @@ ltAutoGun = defaultAutoGun
, _wpReloadState = 0
, _itUseRate = 3
, _itUseTime = 0
, _itUse = \_ -> withSidePush 50 $ withVelWthHiteff (V2 30 0) 2 basicBulletEffect
, _itUse = useAmmoParams
, _itUseModifiers =
[ shootWithSoundI 0
, withRandomDirI 0.3
, withSidePushI 50
, withMuzFlareI
]
, _wpSpread = 0.5
@@ -305,6 +306,7 @@ ltAutoGun = defaultAutoGun
, _itZoom = defaultItZoom
, _itEquipPict = pictureWeaponOnAim ltAutoGunPic
, _itAimStance = OneHand
, _wpAmmo = ltBullet
}
ltAutoGunPic :: Picture
ltAutoGunPic = color green $ pictures
@@ -319,9 +321,12 @@ miniGun = defaultAutoGun
, _wpLoadedAmmo = 150
, _wpReloadTime = 200
, _wpReloadState = 0
, _wpMaxWarmUp = 100
, _itUseRate = 2
, _itUseTime = 0
, _itUse = \_ -> withWarmUp 50 . torqueBefore 0.03 . withSidePush 50 . withRecoil 15
, _itUse = \_ -> withWarmUp 50
. shootWithSoundFor 28 2
. torqueBefore 0.03 . withSidePush 50 . withRecoil 15
. withRandomDir 0.1
. withRandomOffset 9
. withMuzFlare
+7
View File
@@ -12,6 +12,13 @@ basicBullet = BulletAmmo
, _amBulWth = 2
, _amBulVel = V2 50 0
}
ltBullet :: Ammo
ltBullet = BulletAmmo
{ _amString = "LTBULLET"
, _amBulEff = destroyOnImpact bulHitCr bulHitWall' bulHitFF'
, _amBulWth = 2
, _amBulVel = V2 40 0
}
hvBullet :: Ammo
hvBullet = BulletAmmo
{ _amString = "HVBULLET"
+39 -13
View File
@@ -89,25 +89,23 @@ withWarmUp
-> Creature
-> World
-> World
withWarmUp t f cr w
withWarmUp t f cr w
| reloadCondition = fromMaybe w $ startReloadingWeapon cr w
| _wpReloadState item /= 0 = w
| fState == 0 = w
& pointerToItem %~ ( (itUse .~ (\_ -> withWarmUp 100 f)) . (itUseTime .~ 2) )
| t > 2 = w
& pointerToItem %~ ( (itUse .~ (\_ -> withWarmUp (t-1) f)) . (itUseTime .~ 2) )
| curWarmUp < maxWarmUp = w
& pointerToItem . wpCurWarmUp +~ 2
& soundFrom (CrWeaponSound cid) 26 2 0
| t > 0 = w
& pointerToItem %~ ( (itUse .~ (\_ -> withWarmUp (t-1) f)) . (itUseTime .~ 2) )
| otherwise = w
& pointerToItem %~ ( (itUse .~ (\_ -> withWarmUp 1 f)) . (wpLoadedAmmo -~ 1) . (itUseTime .~ 2) )
| otherwise = w
& pointerToItem . wpCurWarmUp .~ maxWarmUp
& f cr
& soundFrom (CrWeaponSound cid) 28 2 0
-- & soundFrom (CrWeaponSound cid) 28 2 0
where
cid = _crID cr
itRef = _crInvSel cr
item = _crInv cr IM.! itRef
pointerToItem = creatures . ix cid . crInv . ix itRef
curWarmUp = _wpCurWarmUp item
maxWarmUp = _wpMaxWarmUp item
fState = _itUseTime item
reloadCondition = _wpLoadedAmmo item == 0
{- | Adds a sound to a creature based world effect.
@@ -133,11 +131,13 @@ withRecoil recoilAmount eff cr = eff cr . over (creatures . ix cid) pushback
Applied before the underlying effect. -}
withSidePushI
:: Float -- ^ Maximal possible side push amount
-> (Creature -> World -> World)
-- ^ Underlying world effect, takes creature id as input
-> (Item -> Creature -> World -> World)
-- ^ Underlying world effect
-> Item
-> Creature
-> World -> World
withSidePushI maxSide eff cr w = eff cr . over (creatures . ix cid) push $ w
withSidePushI maxSide eff item cr w = eff item cr $
w & creatures . ix cid %~ push
where
cid = _crID cr
push = over crPos (+.+ rotateV (_crDir cr) (V2 0 (pushAmount / _crMass cr)))
@@ -170,6 +170,32 @@ withSidePushAfter maxSide eff cr w = over (creatures . ix cid) push . eff cr $ w
push = over crPos (+.+ rotateV (_crDir cr) (V2 0 (pushAmount / _crMass cr)))
(pushAmount, _) = randomR (-maxSide,maxSide) $ _randGen w
{- | Applies a world effect and sound effect after an ammo check. -}
shootWithSoundFor
:: Int -- ^ Sound identifier
-> Int -- ^ Frames to play
-> (Creature -> World -> World)
-- ^ Shoot effect, takes creature id as input
-> Creature
-> World
-> World
shootWithSoundFor soundid playTime f cr w
| fireCondition = over (pointerToItem . wpLoadedAmmo) (\ammo -> ammo-1)
$ soundFromPos (CrWeaponSound cid) cpos soundid playTime 0
$ set (pointerToItem . itUseTime) (_itUseRate item)
$ f cr w
| reloadCondition = fromMaybe w $ startReloadingWeapon cr w
| otherwise = w
where
cid = _crID cr
cpos = _crPos cr
itRef = _crInvSel cr
item = _crInv cr IM.! itRef
pointerToItem = creatures . ix cid . crInv . ix itRef
fireCondition = _wpReloadState item == 0
&& _itUseTime item == 0
&& _wpLoadedAmmo item > 0
reloadCondition = _wpLoadedAmmo item == 0
{- | Applies a world effect and sound effect after an ammo check. -}
shootWithSound
:: Int -- ^ Sound identifier
-> (Creature -> World -> World)
+4 -4
View File
@@ -39,7 +39,7 @@ New sounds with the same keys as
already playing sounds are merged as follows:
* '_soundChannel' is set to the old value
* if the old '_soundStatus' was 'FadingOut', it set to the new value (can restart playback with 'ToStart')
* if the old '_soundStatus' was 'FadingOut', it is set to the new value (can restart playback with 'ToStart')
* other fields are set to the new value.
In the update:
@@ -57,8 +57,10 @@ mergeSound :: Sound -> Sound -> Sound
mergeSound newS oldS
| _soundStatus oldS == FadingOut
= newS & soundChannel .~ _soundChannel oldS
| otherwise = newS & soundChannel .~ _soundChannel oldS
| _soundChunkID newS == _soundChunkID oldS
= newS & soundChannel .~ _soundChannel oldS
& soundStatus .~ _soundStatus oldS
| otherwise = newS & soundChannel .~ _soundChannel oldS
updateSounds :: Ord a => IM.IntMap Mix.Chunk -> M.Map a Sound -> IO (M.Map a Sound)
updateSounds sd ss = do
@@ -156,8 +158,6 @@ setChannelPos :: Int16 -> Mix.Channel -> MaybeT IO Mix.Channel
setChannelPos a i = do
_ <- liftIO $ Mix.effectPosition i a 0
return i
-----------------------------------------------------------------
{- | Set the volume for all sound channels.
Behind the scenes, scales a float [0,1] to an Int [0..128].