--阵营管理器,提供阵营管理 local CampManager = {} local LuaConst=LuaConst local ipairs=ipairs local tostring=tostring local table_insert=table.insert local table_sort=table.sort local table_remove=table.remove local private = { campEntityInfoList = {} --campEntityInfo = {campId,entityList} } function CampManager.LogCampList() logging.error(LTAG.ERROR, "HYW : ----------------------------------") for i,campEntityInfo in ipairs(private.campEntityInfoList) do local list = campEntityInfo.entityList if list and #list > 0 then logging.error(LTAG.ERROR, "HYW : 阵营", campEntityInfo.campId) for j,entity in ipairs(list) do logging.error(LTAG.ERROR, " ", entity.EntityID, " , entityCampID : ", entity:GetCampID()) end end end end function CampManager.IsEnemyEntity(entityA,entityB) return entityA.CampID ~= entityB.CampID end function CampManager.IsEnemyCamp(campId1,campId2) return campId1 ~= campId2 end --返回一维数组,可能返回空 function CampManager.GetEntityListOfCampId(campId) local result = nil for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campId == campEntityInfo.campId then result = campEntityInfo.entityList break end end return result end function CampManager.GetEntityListCopyOfCampId(campId) local result = nil for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campId == campEntityInfo.campId then result = {} for j,_entity in ipairs(campEntityInfo.entityList) do result[j] = _entity end break end end return result end function CampManager.GetEntityCountOfCampId(campId) local entityList = CampManager.GetEntityListOfCampId(campId) if entityList then return #entityList else return 0 end end --会返回二维数组,以节省性能 --最多支持两个except,可以拓展。如果不填第2个参数也不影响使用 function CampManager.Get2DEnitityListExceptCampId(campId1,campId2) local list2d = {} for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campEntityInfo.campId ~= campId1 and campEntityInfo.campId ~= campId2 then table_insert(list2d,campEntityInfo.entityList) end end return list2d end --返回副本。会返回二维数组,以节省性能 --最多支持两个except,可以拓展。如果不填第2个参数也不影响使用 function CampManager.Get2DEnitityListCopyExceptCampId(campId1,campId2) local list2d = {} for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campEntityInfo.campId ~= campId1 and campEntityInfo.campId ~= campId2 then list2d[#list2d + 1] = {} for j,_entity in ipairs(campEntityInfo.entityList) do table_insert(list2d, _entity) end end end return list2d end --最多支持两个except,可以拓展。如果不填第2个参数也不影响使用 function CampManager.GetEnitityCountExceptCampId(campId1, campId2) local count = 0 for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campEntityInfo.campId ~= campId1 and campEntityInfo.campId ~= campId2 then count = count + #campEntityInfo.entityList end end return count end --返回非中立和平的阵营的单位的二维数组 function CampManager.Get2DNoPeaceList() local list2d = {} for i,campEntityInfo in ipairs(private.campEntityInfoList) do if campEntityInfo.campId ~= LuaConst.ECampID.NEUTRAL_PEACE then table_insert(list2d,campEntityInfo.entityList) end end return list2d end function CampManager.Get2DEntityListOfAll() return CampManager.Get2DEnitityListExceptCampId(nil) end local function SortCampId(a,b) return a.campId < b.campId end function private.AddCampEntityInfo(campId) local campEntityInfo = {} campEntityInfo.campId = campId campEntityInfo.entityList = {} table_insert(private.campEntityInfoList,campEntityInfo) table_sort(private.campEntityInfoList,SortCampId) return campEntityInfo end function private.IndexOf(list,entity) local index = -1 for i,v in ipairs(list) do if v == entity then index = i break end end return index end local function SortEntityID(a,b) return a.EntityID < b.EntityID end function private.AddEntity(entity,dontSort) local campId = entity:GetCampID() local entityList = CampManager.GetEntityListOfCampId(campId) if not entityList then entityList = private.AddCampEntityInfo(campId).entityList end if private.IndexOf(entityList,entity) <= 0 then table_insert(entityList,entity) if not dontSort then table_sort(entityList,SortEntityID) end end end --不一定会成功Remove,因为有的entity会被RemoveEntity多次,第一remove掉了,第二次就不会成功Remove --因为现在在OnEntityDead和OnEntityUnload都会试图remove掉Entity,后者可能出现在前者的后续堆栈中 --之所以这样是因为SystemManager.OnUnload在SystemManager.OnDead和SystemManager.DeadQuit的后续堆栈里,并且不一定会调用OnUnload,比如boss尸体不消失,就不会OnUnload --并且,某些entity会直接走SystemManager.OnUnload进行清除,某些 --为了保证调用OnUnload、OnDead、Onload其中的任何一个进行清除的entity的引用被清理,3个地方都会尝试清理引用,所以会出现RemoveEntity多次的清空 function private.RemoveEntity(entity) local campId = entity:GetCampID() local list = CampManager.GetEntityListOfCampId(campId) if list then local index = private.IndexOf(list,entity) if index > 0 then table_remove(list,index) end end end function private.RemoveEntities(entities) --预期RemoveEntity要做一些刷新,这里遍历的时候可能需要禁止刷新,在遍历之后一次性刷新 for i,entity in ipairs(entities) do private.RemoveEntity(entity) end end function private.Clear() private.campEntityInfoList = {} end --添加entityList到阵营管理器,会自动去重 function CampManager.AddEntityList(entityList) for i,entity in ipairs(entityList) do private.AddEntity(entity,true) end end -- 是否有阵营中 (NPC和副将没有) function CampManager.IsCampEntity(entity) if entity.isPickupItem then return false else local characterType = entity:GetCharacterType() return characterType == LuaConst.CharacterType.Monster or characterType == LuaConst.CharacterType.Hero or characterType == LuaConst.CharacterType.Illusion end end local function OnEntityCreatedSort(entity) if CampManager.IsCampEntity(entity) then private.AddEntity(entity) --CampManager.LogCampList() end end function CampManager.OnEntityRevive(entity) if CampManager.IsCampEntity(entity) then private.AddEntity(entity) end end function CampManager.OnEntityDead(entity) if CampManager.IsCampEntity(entity) then private.RemoveEntity(entity) end end function CampManager.OnEntityUnload(entity) if CampManager.IsCampEntity(entity) then private.RemoveEntity(entity) end end function CampManager.OnExitLevel() private.Clear() end function CampManager.OnEntityChangeCampId() end CampManager.OnEntityCreated = OnEntityCreatedSort --region 重新向用的代码 ,目前只有肉鸽玩法用。 local function OnEntityCreatedNoSort(entity) if CampManager.IsCampEntity(entity) then private.AddEntity(entity,true) end end function CampManager.SetOnEntityCreatedNotSort() CampManager.OnEntityCreated = OnEntityCreatedNoSort end function CampManager.SetOnEntityCreatedSort() CampManager.OnEntityCreated = OnEntityCreatedSort end function CampManager.IsOnEntityCreatedNotSort() return CampManager.OnEntityCreated == OnEntityCreatedNoSort end --endregion 重新向用的代码 ,目前只有肉鸽玩法用。 return CampManager