75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
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<EventManager>,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<EventDef>();
|
||
}
|
||
|
||
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<Entity.Entity> 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<T> SelectRandomElements_FisherYates<T>(List<T> 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<T>();
|
||
if (count >= sourceList.Count) return new List<T>(sourceList); // 如果n大于等于列表大小,则返回所有元素副本
|
||
|
||
List<T> result = new List<T>(count); // 预分配容量
|
||
List<T> temp = new List<T>(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;
|
||
}
|
||
|
||
}
|
||
} |