local EventSystem = require('Event/EventSystem') local Base = require("Battle/Logic/Level/LevelItemFactory/LevelItem") local ColliderManager = require("Battle/Logic/Level/ColliderManager") local LevelItem_SpawnNpcPoint = class("LevelItem_SpawnNpcPoint", Base) local SystemManager = SystemManager local LogicUpdateManager = LogicUpdateManager local OFFSET_EFFECT_FIGHTING = 3 function LevelItem_SpawnNpcPoint:initialize(data) Base.initialize(self, data) self.attackDistanceSqa = DataSheetManager.ConstData:GetDataByID("RebelStrikeNPCDistance").FloatValue:Square() self._selectEntityId = 0 self._allEntityList = {}-- 所有出生的entity self._allInfoList = {}--entity对应的Info self._allColliderList = {}--每个entity的colliderID end function LevelItem_SpawnNpcPoint:OnDestroy() Base.OnDestroy(self) for _, entity in pairs(self._allEntityList) do local colliderID = self._allColliderList[entity._logicInitData.EntityID] ColliderManager.DestroyCollider(colliderID) SystemManager.OnUnload(entity, true) end self._allEntityList = nil-- 所有出生的entity self._allInfoList = nil self._allColliderList = nil end --region --***************生命周期***************-- function LevelItem_SpawnNpcPoint:OnActive() if Base.OnActive(self) then EventSystem.Regist(EventID.UpdateRebelStrikeSceneInfo, self.OnUpdateRebelStrikeSceneInfo, self) EventSystem.Regist(EventID.OnEnterSpawnNpcPoint, self.OnEnterSpawnNpcPoint, self) EventSystem.Regist(EventID.OnExitSpawnNpcPoint, self.OnExitSpawnNpcPoint, self) EventSystem.Regist(self.LevelItemID .. EventID._EntityDead, self.OnNpcDead, self) if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.RebelStrike) then self:OnUpdateRebelStrikeSceneInfo() end return true end end function LevelItem_SpawnNpcPoint:OnDeactive() if Base.OnDeactive(self) then EventSystem.UnRegist(EventID.UpdateRebelStrikeSceneInfo, self.OnUpdateRebelStrikeSceneInfo, self) EventSystem.UnRegist(self.LevelItemID .. EventID._EntityDead, self.OnNpcDead, self) EventSystem.UnRegist(EventID.OnEnterSpawnNpcPoint, self.OnEnterSpawnNpcPoint, self) EventSystem.UnRegist(EventID.OnExitSpawnNpcPoint, self.OnExitSpawnNpcPoint, self) EventSystem.Dispatch(EventID.UI_UpdateRebelStrike_Select, nil) return true end end --endregion 生命周期 --region --***************事件相关***************-- function LevelItem_SpawnNpcPoint:OnUpdateRebelStrikeSceneInfo() local infoList = MDL.RebelStrike.GetLevelItemInfo(self.LevelItemID) if infoList then --清理已死亡的 for id, v in pairs(self._allInfoList) do local isAlive = false for i, info in ipairs(infoList) do if info.type == v.type and info.index == v.index then isAlive = true break end end if isAlive == false then local entity = self._allEntityList[id] SystemManager.DeadQuit(entity) end end --Born NPC for _, info in ipairs(infoList) do local entity = self:GetNpcEntity(info) if entity == nil then entity = self:BornNpc(info) else --需要刷新状态 self._allInfoList[entity._logicInitData.EntityID] = info end self:OnStateExp(entity, info.state) end else for _, v in pairs(self._allEntityList) do SystemManager.DeadQuit(v) end end end function LevelItem_SpawnNpcPoint:OnNpcDead(entity) self._allEntityList[entity._logicInitData.EntityID] = nil self._allInfoList[entity._logicInitData.EntityID] = nil if entity._logicInitData.EntityID == self._selectEntityId then self._selectEntityId = 0 EventSystem.Dispatch(EventID.UI_UpdateRebelStrike_Select, nil) end local colliderID = self._allColliderList[entity._logicInitData.EntityID] ColliderManager.DestroyCollider(colliderID) end --endregion 事件相关 --region --***************内部方法***************-- local function OnEnterCollider(owner, entity) if entity == LogicUpdateManager._curEntity then local selectEntityId = owner._logicInitData.EntityID EventSystem.Dispatch(EventID.OnEnterSpawnNpcPoint, selectEntityId) end end local function OnExitCollider(owner, entity) if entity == LogicUpdateManager._curEntity then local selectEntityId = owner._logicInitData.EntityID EventSystem.Dispatch(EventID.OnExitSpawnNpcPoint, selectEntityId) end end function LevelItem_SpawnNpcPoint:OnEnterSpawnNpcPoint(entityId) if self._selectEntityId ~= entityId then self:OnSelectExp(false, false) self._selectEntityId = entityId self:OnSelectExp(true, true) end end function LevelItem_SpawnNpcPoint:OnExitSpawnNpcPoint(entityId) if self._selectEntityId == entityId then self:OnSelectExp(false, true) self._selectEntityId = 0 end end function LevelItem_SpawnNpcPoint:GetNpcEntity(info) local entityId = 0 for id, v in pairs(self._allInfoList) do if v.type == info.type and v.index == info.index then entityId = id break end end if entityId > 0 then return self._allEntityList[entityId] end return nil end function LevelItem_SpawnNpcPoint:BornNpc(info) local data if info.type == 0 then data = self.ItemData.NormalNpcPointList[info.index] else data = self.ItemData.EliteNpcPointList[info.index] end local positionV2 = data.Position local forwardV2 = data.Rotation EntityFactory.SetParams("BornItemID", self.LevelItemID) EntityFactory.SetPos_Dir(positionV2, forwardV2) EntityFactory.SetType_ID(LuaConst.CharacterType.NPC, data.NpcID) local entity = EntityFactory.DoCreateEntity() self._allEntityList[entity._logicInitData.EntityID] = entity self._allInfoList[entity._logicInitData.EntityID] = info --添加碰撞 local colliderID = ColliderManager.CreateCircleCollider(positionV2, self.attackDistanceSqa) self._allColliderList[entity._logicInitData.EntityID] = colliderID ColliderManager.AddEnterEvent(colliderID, OnEnterCollider, entity) ColliderManager.AddExitEvent(colliderID, OnExitCollider, entity) ColliderManager.ActiveCollider(colliderID, LogicUpdateManager.ValidSyncEntityIter) return entity end function LevelItem_SpawnNpcPoint:OnSelectExp(visible, isDispatch) local selectEntity = self._allEntityList[self._selectEntityId] if selectEntity then if visible then selectEntity.ExpEntity:PlayEffectByType(LuaConst.EffectType.RebelStrikeSelect) else selectEntity.ExpEntity:StopEffectByType(LuaConst.EffectType.RebelStrikeSelect) end if isDispatch then if visible then local info = self._allInfoList[self._selectEntityId] EventSystem.Dispatch(EventID.UI_UpdateRebelStrike_Select, info) else EventSystem.Dispatch(EventID.UI_UpdateRebelStrike_Select, nil) end end end end function LevelItem_SpawnNpcPoint:OnStateExp(entity, state) local expEntity = entity.ExpEntity if state == 1 then --战斗中特效 expEntity:PlayEffectByType(LuaConst.EffectType.RebelStrikeFighting) expEntity:SetEffectPosByType(LuaConst.EffectType.RebelStrikeFighting, 0, OFFSET_EFFECT_FIGHTING, 0) else expEntity:StopEffectByType(LuaConst.EffectType.RebelStrikeFighting) end end --endregion 内部方法 --region --***************外部数据获取***************-- --小地图显示信息 function LevelItem_SpawnNpcPoint:GetMapPointInfo() local entityId local type = -1 for id, v in pairs(self._allInfoList) do if v.type > type then type = v.type entityId = id end end return self._allEntityList[entityId], type end --endregion return LevelItem_SpawnNpcPoint