Files
Gen_Hack-and-Slash-Roguelite/Client/Assets/Scripts/Managers/EventManager.cs
2025-08-28 15:07:36 +08:00

75 lines
2.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;
}
}
}