--移动组件 local ExpSoundSystem = {} local private = {} local timeSoundList = {} local PlaySoundType = { Loop = 1, Queue = 2, } local NO_CATCH_SOUND = "No" local TARGET_ON_MIN_MONSTER_COUNT = 5 --播放注视音效所需最少的怪物数量 function ExpSoundSystem.OnLevelInited() ExpSoundSystem.minionTargetOnSoundValidFrame = 0 ExpSoundSystem.bossTargetOnSoundValidFrame = 0 end function ExpSoundSystem.OnUpdate(deltaTime) for expEntity, soundEntity in pairs(timeSoundList) do local timeScale = FP.tonumberV1(expEntity.Entity.components.timeScale:GetCurTimeScale()) soundEntity.timeSince = soundEntity.timeSince + (deltaTime * timeScale) if soundEntity.type == PlaySoundType.Loop then private.PlayLoop(expEntity,deltaTime,timeScale) else private.PlayQueue(expEntity) end end end function ExpSoundSystem.OnBorn(expEntity) end function ExpSoundSystem.OnActive(expEntity) ExpSoundSystem.InitSound(expEntity) if LogicUpdateManager.IsInReconnectFastPlay() then return end local modelSoundData = expEntity.Entity.components.base.CurModelSoundData if modelSoundData and expEntity.Entity._logicInitData.CharacterType ~= LuaConst.CharacterType.NPC then if not string.isNilOrEmpty(modelSoundData.BornSound) then CTL.Sound.PlayObjSound(modelSoundData.BornSound, expEntity.RenderEntity) end if not string.isNilOrEmpty(modelSoundData.BornBGSound) then CTL.Sound.PlayGameSound(modelSoundData.BornBGSound) end end end function ExpSoundSystem.InitSound(expEntity) if expEntity.Entity.isPickupItem then return end local originEntity = expEntity.Entity:GetOriginEntity() local renderEntity = expEntity.RenderEntity if expEntity.Entity._logicInitData.IsPlayerSelf and expEntity.Entity:IsActive() then CTL.Sound.SetCameraListener(false) end if originEntity._logicInitData.IsPlayerSelf then CTL.Sound.SetSwitch("Player1_Player3", "Player1", renderEntity) else if originEntity:IsBoss() or BattleDataGetter.IsSyncSolo() then CTL.Sound.SetSwitch("Player1_Player3", "Player1", renderEntity) else CTL.Sound.SetSwitch("Player1_Player3", "Player3", renderEntity) end end if expEntity.Entity._logicInitData.CharacterType == LuaConst.CharacterType.Hero then local heroNumData = DataSheetManager.HeroNum:GetDataByID(expEntity.Entity._logicInitData.CharacterID) if heroNumData then CTL.Sound.SetSwitch("Role_label", heroNumData.WwiseSwitchState, renderEntity) end end --海外中文和国内中文声音切换 if GameAppInfo.IsChinesePackage() then CTL.Sound.SetSwitch("Language", "China", renderEntity) else CTL.Sound.SetSwitch("Language", "Foreign", renderEntity) end end function ExpSoundSystem.OnDriveEntity(expEntity ,carEntity) --主角驾驶投石车 切为player1 if expEntity.Entity._logicInitData.IsPlayerSelf then CTL.Sound.SetSwitch("Player1_Player3", "Player1", carEntity.ExpEntity.RenderEntity) else CTL.Sound.SetSwitch("Player1_Player3", "Player3", carEntity.ExpEntity.RenderEntity) end end local TARGET_ON_SOUND_MUTE_FRAME_WHEN_SWITCH = 5 --切将时,锁敌音效需要暂停一小段时间。(否则必然会播锁敌音效,这是不符合预期的) function ExpSoundSystem.OnSwitchHero(fromExpEntity, toExpEntity) if LogicUpdateManager._curEntity then if fromExpEntity.Entity == LogicUpdateManager._curEntity then fromExpEntity:SetSoundListener(false) end if ExpSoundSystem.IsPlaySwitchHero(toExpEntity) then local fightSound local logicInitData = toExpEntity.Entity._logicInitData if logicInitData.HeroAppearanceID and logicInitData.HeroAppearanceID > 0 then fightSound = DataSheetManager.HeroAppearance:GetDataByID(logicInitData.HeroAppearanceID).FightSound end if string.isNilOrEmpty(fightSound) then fightSound = DataSheetManager.HeroNum:GetDataByID(logicInitData.CharacterID).FightSound end CTL.Sound.PlayObjSound(fightSound,toExpEntity.RenderEntity) if LogicUpdateManager._clientHandleFrame + TARGET_ON_SOUND_MUTE_FRAME_WHEN_SWITCH > ExpSoundSystem.minionTargetOnSoundValidFrame then ExpSoundSystem.minionTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + TARGET_ON_SOUND_MUTE_FRAME_WHEN_SWITCH end if LogicUpdateManager._clientHandleFrame + TARGET_ON_SOUND_MUTE_FRAME_WHEN_SWITCH > ExpSoundSystem.bossTargetOnSoundValidFrame then ExpSoundSystem.bossTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + TARGET_ON_SOUND_MUTE_FRAME_WHEN_SWITCH end end ExpSoundSystem.InitSound(toExpEntity) end end function ExpSoundSystem.OnSetCurEntity(expEntity) if expEntity.Entity == LogicUpdateManager._curEntity then expEntity:SetSoundListener(true) end end function ExpSoundSystem.IsPlaySwitchHero(expEntity) --主角 和 非军团战的多人玩法播放出战配音 if expEntity.Entity._logicInitData.IsPlayerSelf then return true end if not BattleDataGetter.IsGuildWarLevel() and not CampManager.IsEnemyEntity(LogicUpdateManager._curEntity,expEntity.Entity) then return true end return false end --causer :谁造成的状态切换,可以为空,是一个Entity function ExpSoundSystem.PlayAinmSound(expEntity, playAnimName, causer) if expEntity.RenderEntity == nil then return end if expEntity.Entity._logicInitData.IsPlayerSelf or expEntity.Entity._logicInitData.CharacterType ~= LuaConst.CharacterType.Hero then ExpSoundSystem.StopAinmSound(expEntity) local isSuccess, data = DataSheetManager.AnimSound:TryGetDataByID(playAnimName) if isSuccess then if timeSoundList[expEntity] == nil then timeSoundList[expEntity] = {} local soundEntity = timeSoundList[expEntity] if data.LoopSound then soundEntity.startTime = data.SoundEventData_Table[1].PlayTime/1000 soundEntity.eventName1 = data.SoundEventData_Table[1].SoundResource soundEntity.isBreak = data.Break local loopData = data.SoundEventData_Table[2] or data.SoundEventData_Table[1] if loopData.PlayTime <= 100 then logging.error(LTAG.LOG," 动作声音间隔太小 ", data.SoundEventData_Table[1].SoundResource ) timeSoundList[expEntity] = nil return end soundEntity.interval = loopData.PlayTime/1000 soundEntity.eventName2 = loopData.SoundResource soundEntity.type = PlaySoundType.Loop soundEntity.timeSince = 0 else soundEntity.soundList = data.SoundEventData_Table soundEntity.isBreak = data.Break soundEntity.type = PlaySoundType.Queue soundEntity.curIndex = 1 --logging.log(LTAG.LOG,"----------------",math.random(60)/100) if data.Random then soundEntity.timeSince = -math.random(50)/100 else soundEntity.timeSince = 0 end end soundEntity.renderEntity = expEntity.RenderEntity soundEntity.time = -1 if causer and not causer:GetOriginEntity()._logicInitData.IsPlayerSelf then CTL.Sound.SetRTPCValue("shout", 0) else CTL.Sound.SetRTPCValue("shout", 100) end end end end end function private.PlayQueue(expEntity) local soundEntity = timeSoundList[expEntity] local curSoundData = soundEntity.soundList[soundEntity.curIndex] if curSoundData == nil then timeSoundList[expEntity] = nil return end if soundEntity.timeSince >= curSoundData.PlayTime/1000 then soundEntity.soundId = CTL.Sound.PlayObjSound(curSoundData.SoundResource,soundEntity.renderEntity) soundEntity.curIndex = soundEntity.curIndex + 1 end end function private.PlayLoop(expEntity,deltaTime,timeScale) --,默认需要循环的声音都是走路声 打开某些界面时关闭走路声音 local soundEntity = timeSoundList[expEntity] if soundEntity.timeSince >= soundEntity.startTime then if soundEntity.time == -1 then soundEntity.soundId = CTL.Sound.PlayFootSound(soundEntity.eventName1,soundEntity.renderEntity) soundEntity.time = 0 return end soundEntity.time = soundEntity.time + deltaTime * timeScale if soundEntity.time >= soundEntity.interval then soundEntity.soundId = CTL.Sound.PlayFootSound(soundEntity.eventName2,soundEntity.renderEntity) soundEntity.time = 0 end end end function ExpSoundSystem.OnAttackEnter(expEntity) local curSkillData = expEntity.Entity.components.skillBase.CurSkillData if curSkillData and curSkillData.SkillSound and string.len(curSkillData.SkillSound)> 0 then CTL.Sound.PlayObjSound(curSkillData.SkillSound,expEntity.RenderEntity) end end function ExpSoundSystem.StopAinmSound(expEntity) local soundEntity = timeSoundList[expEntity] --logging.log(LTAG.LOG," StopAinmSound ",soundEntity) if soundEntity then if soundEntity.soundId and not soundEntity.isBreak then CTL.Sound.StopById(soundEntity.soundId) end timeSoundList[expEntity] = nil end end local MINION_TARGET_ON_SOUND_CD = 200 --小兵锁敌音效CD(帧数) function ExpSoundSystem.OnTargetOnMainEntityMinionNoZero(causerEntity) if LogicUpdateManager._curEntity == nil then -- 当中途进入战场快速追帧的时候 ,LogicUpdateManager._curEntity是nil,这是正常的 return end if ExpSoundSystem.minionTargetOnSoundValidFrame <= LogicUpdateManager._clientHandleFrame then --锁敌音效增加个CD,因为小兵很容易脱战,不加CD很容易播个不停 -- old↓ -- if BattleDataGetter.GetSpdNoZeroMonsterCount() >= TARGET_ON_MIN_MONSTER_COUNT then -- CTL.Sound.PlayObjSound(DataSheetManager.ConstData:GetDataByID("AISeekEnemyNormalMusic").StringValue, LogicUpdateManager._curEntity.ExpEntity.RenderEntity) -- ExpSoundSystem.minionTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + MINION_TARGET_ON_SOUND_CD -- end -- old↑ --new↓ --logging.error(LTAG.ERROR, "HYW : count : ", BattleDataGetter.GetCatchSoundCount(causerEntity.components.base.CurMonsterFeature.CatchSound), " CatchSound : ", causerEntity.components.base.CurMonsterFeature.CatchSound) if BattleDataGetter.GetCatchSoundCount(causerEntity.components.base.CurMonsterFeature.CatchSound) >= TARGET_ON_MIN_MONSTER_COUNT then local catchSound = causerEntity.components.base.CurMonsterFeature.CatchSound if catchSound ~= NO_CATCH_SOUND then catchSound = string.isNilOrEmpty(catchSound) and DataSheetManager.ConstData:GetDataByID("AISeekEnemyNormalMusic").StringValue or catchSound CTL.Sound.PlayObjSound(catchSound, LogicUpdateManager._curEntity.ExpEntity.RenderEntity) ExpSoundSystem.minionTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + MINION_TARGET_ON_SOUND_CD end end --new↑ end end local MINION_TARGET_ON_SOUND_CD = 200 --boss锁敌音效CD(帧数) function ExpSoundSystem.OnTargetOnMainEntityBossNoZero(causerEntity) if LogicUpdateManager._curEntity == nil then -- 当中途进入战场快速追帧的时候 ,LogicUpdateManager._curEntity是nil,这是正常的 return end if ExpSoundSystem.bossTargetOnSoundValidFrame <= LogicUpdateManager._clientHandleFrame then -- old↓ -- if BattleDataGetter.GetSpdNoZeroMonsterCount() >= TARGET_ON_MIN_MONSTER_COUNT then -- CTL.Sound.PlayObjSound(DataSheetManager.ConstData:GetDataByID("AISeekEnemyEliteMusic").StringValue, LogicUpdateManager._curEntity.ExpEntity.RenderEntity) -- ExpSoundSystem.bossTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + MINION_TARGET_ON_SOUND_CD -- end --old↑ --new↓ if BattleDataGetter.GetSpdNoZeroMonsterCount() >= TARGET_ON_MIN_MONSTER_COUNT then local catchSound = causerEntity.components.base.CurMonsterFeature.CatchSound if catchSound ~= NO_CATCH_SOUND then catchSound = string.isNilOrEmpty(catchSound) and DataSheetManager.ConstData:GetDataByID("AISeekEnemyEliteMusic").StringValue or catchSound CTL.Sound.PlayObjSound(catchSound, LogicUpdateManager._curEntity.ExpEntity.RenderEntity) ExpSoundSystem.minionTargetOnSoundValidFrame = LogicUpdateManager._clientHandleFrame + MINION_TARGET_ON_SOUND_CD end end --new↑ end end function ExpSoundSystem.OnDead(expEntity) if not expEntity.Entity.isPickupItem then if expEntity.Entity.components.record.KillerEntity == LogicUpdateManager._curEntity then if expEntity.Entity._logicInitData.CharacterType == LuaConst.CharacterType.Monster then local modelSoundData = expEntity.Entity.components.base.CurModelSoundData CTL.Sound.StopByRenderEntity(expEntity.RenderEntity) if modelSoundData and not string.isNilOrEmpty(modelSoundData.DeadSound) then CTL.Sound.PlayObjSound(modelSoundData.DeadSound,expEntity.RenderEntity) end end end end end function ExpSoundSystem.Unload(expEntity) if expEntity.RenderEntity then CTL.Sound.StopByRenderEntity(expEntity.RenderEntity) end timeSoundList[expEntity] = nil if expEntity.Entity._logicInitData.IsPlayerSelf then CTL.Sound.SetCameraListener(true) end local soundComp = expEntity.components.sound soundComp:Unload() end function ExpSoundSystem.OnChangeModel(expEntity) local soundComp = expEntity.components.sound soundComp:OnModelChange() end function ExpSoundSystem.OnFastPlayOver(expEntity) local soundComp = expEntity.components.sound soundComp:OnFastPlayOver() end function ExpSoundSystem.OnWinBattle(expEntity) local logicInitData = expEntity.Entity._logicInitData local isHero = DataSheetManager.HeroNum:TryGetDataByID(logicInitData.CharacterID) if isHero then local winSound, winPoseSound if logicInitData.HeroAppearanceID and logicInitData.HeroAppearanceID > 0 then winSound = DataSheetManager.HeroAppearance:GetDataByID(logicInitData.HeroAppearanceID).WinSound winPoseSound = DataSheetManager.HeroAppearance:GetDataByID(logicInitData.HeroAppearanceID).WinPoseSound end if string.isNilOrEmpty(winSound) then winSound = DataSheetManager.HeroNum:GetDataByID(logicInitData.CharacterID).WinSound end if string.isNilOrEmpty(winPoseSound) then winPoseSound = DataSheetManager.HeroNum:GetDataByID(logicInitData.CharacterID).WinPoseSound end CTL.Sound.PlayObjSound(winSound,expEntity.RenderEntity) CTL.Sound.PlayObjSound(winPoseSound,expEntity.RenderEntity) end end return ExpSoundSystem