2025-08-28 15:07:36 +08:00
|
|
|
|
using System;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using Data;
|
2025-08-28 15:07:36 +08:00
|
|
|
|
using Entity;
|
|
|
|
|
using Prefab;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
using UnityEngine;
|
2025-08-28 15:07:36 +08:00
|
|
|
|
using Utils;
|
|
|
|
|
using Random = System.Random;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
|
|
|
|
namespace Managers
|
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
class EventManager:Singleton<EventManager>,ILaunchManager
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
private static Random _random = new();
|
|
|
|
|
public EventDef[] EventDefs { get; private set; }
|
|
|
|
|
public string StepDescription => "正在载入事件";
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-08-28 15:07:36 +08:00
|
|
|
|
public void Init()
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
if(EventDefs!=null)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
2025-08-28 15:07:36 +08:00
|
|
|
|
EventDefs = DefineManager.Instance.QueryDefinesByType<EventDef>();
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 15:07:36 +08:00
|
|
|
|
public void Clear()
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
EventDefs = null;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-08-28 15:07:36 +08:00
|
|
|
|
public static void ExecuteEvent(EventDef eventDef)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
if(eventDef == null)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
return;
|
2025-08-28 15:07:36 +08:00
|
|
|
|
if (eventDef.hediffEvent != null)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-28 15:07:36 +08:00
|
|
|
|
public static List<T> SelectRandomElements_FisherYates<T>(List<T> sourceList, int count)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
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大于等于列表大小,则返回所有元素副本
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-08-28 15:07:36 +08:00
|
|
|
|
List<T> result = new List<T>(count); // 预分配容量
|
|
|
|
|
List<T> temp = new List<T>(sourceList); // 创建一个副本以避免修改原始列表
|
2025-08-27 19:56:49 +08:00
|
|
|
|
|
2025-08-28 15:07:36 +08:00
|
|
|
|
int n = temp.Count;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2025-08-27 19:56:49 +08:00
|
|
|
|
{
|
2025-08-28 15:07:36 +08:00
|
|
|
|
int k = _random.Next(i, n);
|
|
|
|
|
result.Add(temp[k]);
|
|
|
|
|
(temp[k], temp[i]) = (temp[i], temp[k]);
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
2025-08-28 15:07:36 +08:00
|
|
|
|
return result;
|
2025-08-27 19:56:49 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|