71 lines
1.9 KiB
C#
71 lines
1.9 KiB
C#
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();
|
||
}
|
||
}
|
||
}
|