local ExpSoundComponent = class("ExpSoundComponent") local EventSystem = require('Event/EventSystem') local tempV1 = FP.newV1(0) function ExpSoundComponent:initialize(expEntity) self.ExpEntity = expEntity self.modelSoundDic = nil--{} -- 字典:保存对应的模型,剩余的事件 。 key = ModelSoundData.ID ,value = true self.isRegistHpEvent = false --标记是否注册了事件 self.lastHpPercent = 1 self:SetCacheData() end function ExpSoundComponent:OnModelChange() self:SetCacheData() end function ExpSoundComponent:SetCacheData() local entity = self.ExpEntity.Entity local modelSoundData = entity.components.base.CurModelSoundData if modelSoundData then -- 有声音数据才注册事件 if not self.isRegistHpEvent and #modelSoundData.HPPercent > 0 then EventSystem.Regist(entity.Event_EntityIDHPChanged, self.OnHPChange, self) self.isRegistHpEvent = true end self.curModelD = entity.components.base.CurModelData.ID self.curModelSoundData = modelSoundData if self.modelSoundDic == nil then self.modelSoundDic = {} end if self.modelSoundDic[self.curModelD] == nil then -- 已经使用过的模型不要改数据 local hpPercent = FP.tonumberV1(entity.components.attr:HitPointPercents(tempV1)) local hpList = {} local soundStrList = {} for i,hp in ipairs(modelSoundData.HPPercent) do if hp * 0.01 <= hpPercent then -- 加这个判断目的:防止变身后播放高血量的声音。如:变身后剩余20%血,变身配置了80%声音,掉血时这个声音就播放了。 table.insert(hpList,hp*0.01) table.insert(soundStrList,modelSoundData.HPSound[i]) end end self.modelSoundDic[self.curModelD] = {hpPercentList = hpList, soundList = soundStrList, isLoop = modelSoundData.HPSoundIsLoop} end end end function ExpSoundComponent:OnHPChange(entity) local hpPercent = FP.tonumberV1(self.ExpEntity.Entity.components.attr:HitPointPercents(tempV1)) local toUnRegist = true for id, info in pairs(self.modelSoundDic) do local hpList = info.hpPercentList local count = #hpList for i = count, 1, -1 do local hp = hpList[i] if hp >= hpPercent and hp < self.lastHpPercent then if self.curModelD == id then -- 只播放当前模型的声音 ,其他模型的只处理数据,不播放 CTL.Sound.PlayObjSound(info.soundList[i],self.ExpEntity.RenderEntity) end if not info.isLoop then table.remove(hpList,i) table.remove(info.soundList,i) end end end if #hpList ~= 0 then toUnRegist = false end end if self.isRegistHpEvent and toUnRegist then self.isRegistHpEvent = false EventSystem.UnRegist(entity.Event_EntityIDHPChanged, self.OnHPChange, self) end self.lastHpPercent = hpPercent end function ExpSoundComponent:OnFastPlayOver() if self.curModelSoundData then local toUnRegist = true local hpPercent = FP.tonumberV1(self.ExpEntity.Entity.components.attr:HitPointPercents(tempV1)) for id, info in pairs(self.modelSoundDic) do if not info.isLoop then local hpList = info.hpPercentList local count = #hpList for i = count, 1, -1 do local hp = hpList[i] if hp >= hpPercent then table.remove(hpList,i) table.remove(info.soundList,i) end end if #hpList ~= 0 then toUnRegist = false end end end if self.isRegistHpEvent and toUnRegist then self.isRegistHpEvent = false EventSystem.UnRegist(self.ExpEntity.Entity.Event_EntityIDHPChanged, self.OnHPChange, self) end end end function ExpSoundComponent:Unload() if self.isRegistHpEvent then EventSystem.UnRegist(self.ExpEntity.Entity.Event_EntityIDHPChanged, self.OnHPChange, self) end self.modelSoundDic = nil end return ExpSoundComponent