提交GAS 打算做一个帧同步的GAS

This commit is contained in:
DESKTOP-5RP3AKU\Jisol
2024-10-18 03:16:09 +08:00
parent b0a2e4a900
commit d9b0c78827
726 changed files with 76601 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
namespace GAS.Runtime
{
public abstract class AttributeSet
{
protected AbilitySystemComponent _owner;
public abstract AttributeBase this[string key] { get; }
public abstract string[] AttributeNames { get; }
public abstract void SetOwner(AbilitySystemComponent owner);
public void ChangeAttributeBase(string attributeShortName, float value)
{
if (this[attributeShortName] != null)
{
this[attributeShortName].SetBaseValue(value);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: dadd8b95a392468784bd6c5f3347cb2b
timeCreated: 1702375477

View File

@@ -0,0 +1,148 @@
using System;
using System.Collections.Generic;
using GAS.General;
namespace GAS.Runtime
{
public class AttributeSetContainer
{
private readonly AbilitySystemComponent _owner;
private readonly Dictionary<string, AttributeSet> _attributeSets = new();
private readonly Dictionary<AttributeBase, AttributeAggregator> _attributeAggregators = new();
public Dictionary<string, AttributeSet> Sets => _attributeSets;
public AttributeSetContainer(AbilitySystemComponent owner)
{
_owner = owner;
}
public void AddAttributeSet<T>() where T : AttributeSet
{
AddAttributeSet(typeof(T));
}
public void AddAttributeSet(Type attrSetType)
{
if (TryGetAttributeSet(attrSetType, out _)) return;
var setName = AttributeSetUtil.AttributeSetName(attrSetType);
_attributeSets.Add(setName, Activator.CreateInstance(attrSetType) as AttributeSet);
var attrSet = _attributeSets[setName];
foreach (var attr in attrSet.AttributeNames)
{
if (!_attributeAggregators.ContainsKey(attrSet[attr]))
{
var attrAggt = new AttributeAggregator(attrSet[attr], _owner);
if (_owner.enabled) attrAggt.OnEnable();
_attributeAggregators.Add(attrSet[attr], attrAggt);
}
}
attrSet.SetOwner(_owner);
}
/// <summary>
/// Be careful when using this method, it may cause unexpected errors(when using network sync).
/// </summary>
/// <typeparam name="T"></typeparam>
public void RemoveAttributeSet<T>() where T : AttributeSet
{
var setName = AttributeSetUtil.AttributeSetName(typeof(T));
var attrSet = _attributeSets[setName];
foreach (var attr in attrSet.AttributeNames)
{
_attributeAggregators.Remove(attrSet[attr]);
}
_attributeSets.Remove(setName);
}
public bool TryGetAttributeSet<T>(out T attributeSet) where T : AttributeSet
{
if (_attributeSets.TryGetValue(AttributeSetUtil.AttributeSetName(typeof(T)), out var set))
{
attributeSet = (T)set;
return true;
}
attributeSet = null;
return false;
}
bool TryGetAttributeSet(Type attrSetType, out AttributeSet attributeSet)
{
if (_attributeSets.TryGetValue(AttributeSetUtil.AttributeSetName(attrSetType), out var set))
{
attributeSet = set;
return true;
}
attributeSet = null;
return false;
}
public AttributeValue? GetAttributeAttributeValue(string attrSetName, string attrShortName)
{
return _attributeSets.TryGetValue(attrSetName, out var set)
? set[attrShortName].Value
: (AttributeValue?)null;
}
public CalculateMode? GetAttributeCalculateMode(string attrSetName, string attrShortName)
{
return _attributeSets.TryGetValue(attrSetName, out var set)
? set[attrShortName].CalculateMode
: (CalculateMode?)null;
}
public float? GetAttributeBaseValue(string attrSetName, string attrShortName)
{
return _attributeSets.TryGetValue(attrSetName, out var set) ? set[attrShortName].BaseValue : (float?)null;
}
public float? GetAttributeCurrentValue(string attrSetName, string attrShortName)
{
return _attributeSets.TryGetValue(attrSetName, out var set)
? set[attrShortName].CurrentValue
: (float?)null;
}
public Dictionary<string, float> Snapshot()
{
var snapshot = ObjectPool.Instance.Fetch<Dictionary<string, float>>();
foreach (var kv in _attributeSets)
{
var attributeSet = kv.Value;
foreach (var name in attributeSet.AttributeNames)
{
var attr = attributeSet[name];
snapshot.Add(attr.Name, attr.CurrentValue);
}
}
return snapshot;
}
public void OnDisable()
{
foreach (var aggregator in _attributeAggregators)
aggregator.Value.OnDisable();
}
public void OnEnable()
{
foreach (var aggregator in _attributeAggregators)
aggregator.Value.OnEnable();
}
public void OnDestroy()
{
foreach (var aggregator in _attributeAggregators)
aggregator.Value.OnDestroy();
_attributeAggregators.Clear();
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5d1bf754bc834bceb8b87c4d05adb589
timeCreated: 1703063263

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
namespace GAS.Runtime
{
public static class AttributeSetUtil
{
public static IReadOnlyDictionary<Type, string> AttrSetNameCache { get; private set; }
public static void Cache(IReadOnlyDictionary<Type,string> typeToName)
{
AttrSetNameCache = typeToName;
}
public static string AttributeSetName(Type attrSetType)
{
if(AttrSetNameCache==null)
return attrSetType.Name;
return AttrSetNameCache.TryGetValue(attrSetType, out var value) ? value : attrSetType.Name;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 8a521d8eaa934c54bb891bc3bc995db0
timeCreated: 1713693216

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
namespace GAS.Runtime
{
public class CustomAttrSet:AttributeSet
{
Dictionary<string,AttributeBase> _attributes = new Dictionary<string,AttributeBase>();
public void AddAttribute(AttributeBase attribute)
{
if (_attributes.ContainsKey(attribute.Name))
return;
_attributes.Add(attribute.Name, attribute);
}
public void RemoveAttribute(AttributeBase attribute)
{
_attributes.Remove(attribute.Name);
}
public override AttributeBase this[string key] =>
_attributes.TryGetValue(key, value: out var attribute) ? attribute : null;
public override string[] AttributeNames { get; }
public override void SetOwner(AbilitySystemComponent owner)
{
_owner = owner;
foreach (var attribute in _attributes.Values)
attribute.SetOwner(owner);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 417bd32fced046cfa52d9bdbe94468cc
timeCreated: 1702463034