Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Managers/EventManager.cs

75 lines
2.6 KiB
C#
Raw Normal View History

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