using System; using System.Collections.Generic; using Data; using Entity; using Prefab; using UnityEngine; using Utils; using Random = System.Random; namespace Managers { class EventManager:Singleton,ILaunchManager { private static Random _random = new(); public EventDef[] EventDefs { get; private set; } public string StepDescription => "正在载入事件"; public void Init() { if(EventDefs!=null) return; EventDefs = DefineManager.Instance.QueryDefinesByType(); } public void Clear() { EventDefs = null; } public static void ExecuteEvent(EventDef eventDef) { if(eventDef == null) return; if (eventDef.hediffEvent != null) { var entityList = EntityManage.Instance.FindEntitiesByFaction(Program.Instance.FocusedDimensionId, eventDef.hediffEvent.affiliation.defName); List filteredEntitiesTraditional = new(); foreach (var prefab in entityList) { var entity = prefab.entity; if (entity is Character || entity is Monster) { filteredEntitiesTraditional.Add(entity); } } var selectedElements = SelectRandomElements_FisherYates(filteredEntitiesTraditional, eventDef.hediffEvent.specificPawnCount); foreach (var selectedElement in selectedElements) { } } } public static List SelectRandomElements_FisherYates(List sourceList, int count) { if (sourceList == null) throw new ArgumentNullException(nameof(sourceList)); if (count < 0) throw new ArgumentOutOfRangeException(nameof(count), "Count cannot be negative."); if (count == 0) return new List(); if (count >= sourceList.Count) return new List(sourceList); // 如果n大于等于列表大小,则返回所有元素副本 List result = new List(count); // 预分配容量 List temp = new List(sourceList); // 创建一个副本以避免修改原始列表 int n = temp.Count; for (int i = 0; i < count; i++) { int k = _random.Next(i, n); result.Add(temp[k]); (temp[k], temp[i]) = (temp[i], temp[k]); } return result; } } }