CapabilitySystem/Assets/CapabilitySystem/Core/Instigator.cs
2026-04-15 19:47:09 +08:00

71 lines
1.9 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 UnityEngine;
namespace CapabilitySystem
{
/// <summary>
/// 发起者用于追踪谁发起了某个操作如阻塞Tag
/// </summary>
public class Instigator
{
public enum InstigatorType
{
GameObject,
Capability,
String
}
public InstigatorType Type { get; private set; }
public object Source { get; private set; }
private Instigator(InstigatorType type, object source)
{
Type = type;
Source = source;
}
public static Instigator FromGameObject(GameObject obj)
{
return new Instigator(InstigatorType.GameObject, obj);
}
public static Instigator FromCapability(Capability capability)
{
return new Instigator(InstigatorType.Capability, capability);
}
public static Instigator FromString(string name)
{
return new Instigator(InstigatorType.String, name);
}
public override string ToString()
{
switch (Type)
{
case InstigatorType.GameObject:
return $"GameObject: {(Source as GameObject)?.name ?? "null"}";
case InstigatorType.Capability:
return $"Capability: {Source?.GetType().Name ?? "null"}";
case InstigatorType.String:
return $"String: {Source as string ?? "null"}";
default:
return "Unknown";
}
}
public override bool Equals(object obj)
{
if (obj is Instigator other)
{
return Type == other.Type && Equals(Source, other.Source);
}
return false;
}
public override int GetHashCode()
{
return (Type, Source).GetHashCode();
}
}
}