This commit is contained in:
PC-20230316NUNE\Administrator
2024-10-22 20:36:46 +08:00
parent 0e94e376fb
commit a3257421ad
26 changed files with 694 additions and 112 deletions

View File

@@ -37,14 +37,5 @@
// private static int _frameRate = 60;
// public static int FrameRate => _frameRate;
/// <summary>
///
/// </summary>
private static int _TimeLineAbilityTickTime = 20;
public static int TimeLineAbilityTickTime => _TimeLineAbilityTickTime;
public static int FrameRateValue => 1000 / _TimeLineAbilityTickTime;
}
}

View File

@@ -12,26 +12,13 @@ namespace GAS.General
bool IsFromPool { get; set; }
}
public class ObjectPool
public class JexGasObjectPool
{
private static ObjectPool _singleton;
public static ObjectPool Instance => _singleton ??= new ObjectPool();
private readonly ConcurrentDictionary<Type, Pool> _objPool = new();
private readonly Func<Type, Pool> _addPoolFunc = type => new Pool(type, 1024);
public static void Init()
{
_singleton = null;
_singleton = new ObjectPool();
}
public static void Destroy()
{
_singleton = null;
}
public T Fetch<T>() where T : class
{
var type = typeof(T);

View File

@@ -1,6 +1,7 @@
using System.Collections.Generic;
using GAS.General;
using JNGame.Runtime.GAS.Runtime;
using UnityEngine;
namespace GAS.Runtime
@@ -68,7 +69,7 @@ namespace GAS.Runtime
}
WorkingContext.TotalRunTime += deltaTime;
var targetFrame = WorkingContext.TotalRunTime / GASTimer.TimeLineAbilityTickTime;
var targetFrame = WorkingContext.TotalRunTime / JexGasManager.TimeLineAbilityTickTime;
while (WorkingContext.CurrentFrame < targetFrame)
{
++WorkingContext.CurrentFrame;

View File

@@ -82,7 +82,7 @@ namespace GAS.Runtime
var modifier = geSpec.Modifiers[i];
if (modifier.attributeName == _processedAttribute.Name)
{
var modifierSpec = ObjectPool.Instance.Fetch<ModifierSpec>();
var modifierSpec = _owner.GetManager().ObjectPool.Fetch<ModifierSpec>();
modifierSpec.Init(geSpec, modifier, i);
_modifierCache.Add(modifierSpec);
TryRegisterAttributeChangedListen(geSpec, modifier);
@@ -120,7 +120,7 @@ namespace GAS.Runtime
foreach (var modifierSpec in _cached)
{
modifierSpec.Release();
ObjectPool.Instance.Recycle(modifierSpec);
_owner.GetManager().ObjectPool.Recycle(modifierSpec);
}
_cached.Clear();

View File

@@ -61,13 +61,6 @@ namespace GAS.Runtime
public static void TickEventAttackAfterRecord(this AbilitySystemComponent asc, AbilitySystemComponent source, AbilitySystemComponent receive, GameplayTag damageType,
DamageOther other, LFloat atkValue,int skillId)
{
#if UNITY_EDITOR
asc.DamageList.Enqueue((receive, atkValue, damageType, other, 0, 0, false, 0, skillId,(long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds));
if (asc.DamageList.Count > 10)
{
asc.DamageList.Dequeue();
}
#endif
// asc.TickEventContainer.TickEventAttackAfter(source, receive, damageType, other, atkValue);
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using GAS.General;
using JNGame.Math;
using JNGame.GAS;
using JNGame.Runtime.GAS.Runtime;
using UnityEngine;
namespace GAS.Runtime
@@ -34,14 +35,6 @@ namespace GAS.Runtime
/// </summary>
public IGASResourceService GASResService { get; protected set; }
#if UNITY_EDITOR
public Queue<(AbilitySystemComponent asc, int atkValue, GameplayTag damageType, DamageOther other, int missRate, int criRate, bool isDouble, int doubleRate,int skillId, long time)> DamageList
{
get;
private set;
} = new();
#endif
private bool _ready;
/// <summary>
@@ -49,12 +42,12 @@ namespace GAS.Runtime
/// </summary>
public LFloat BoundRadius { get; protected set; } = LFloat.One;
protected void OnAwake()
public void OnAwake()
{
Prepare();
enabled = true;
#if UNITY_EDITOR
GameplayAbilitySystem.GAS.Register(this);
JexGasManager.Editor.Register(this);
#endif
}
@@ -72,9 +65,6 @@ namespace GAS.Runtime
AttributeSetContainer.Awake(this);
GameplayTagAggregator.Awake(this);
TickEventContainer.Awake(this);
#if UNITY_EDITOR
DamageList.Clear();
#endif
}
public void SetPreset(IAbilitySystemComponentPreset ascPreset)
@@ -364,12 +354,20 @@ namespace GAS.Runtime
/// <param name="abilitySpec"></param>
public abstract void OnAbilityEnd(AbilitySpec abilitySpec);
public JexGasManager Manager;
public JexGasManager GetManager()
{
#if UNITY_EDITOR
return JexGasManager.Editor;
#else
return Manager;
#endif
}
public virtual void Release()
{
#if UNITY_EDITOR
GameplayAbilitySystem.GAS.Unregister(this);
DamageList.Clear();
JexGasManager.Editor.Unregister(this);
#endif
enabled = default;
preset = default;

View File

@@ -1,4 +1,5 @@
using System;
using JNGame.Runtime.GAS.Runtime;
namespace GAS.Runtime
{
@@ -29,5 +30,11 @@ namespace GAS.Runtime
bool TryActivateAbility(string abilityName, params object[] args);
void TryEndAbility(string abilityName);
T AttrSet<T>() where T : AttributeSet;
/// <summary>
/// 获取管理器
/// </summary>
/// <returns></returns>
JexGasManager GetManager();
}
}

View File

@@ -91,7 +91,7 @@ namespace GAS.Runtime
public GameplayEffectSpec CreateSpec(int skillID, AbilitySystemComponent creator, AbilitySystemComponent owner,
int level = 1)
{
var spec = GameplayEffectSpec.GetItem();
var spec = owner.GetManager().ObjectPool.Fetch<GameplayEffectSpec>();
spec.Init(this, creator, owner, skillID, level);
return spec;
}
@@ -99,7 +99,7 @@ namespace GAS.Runtime
public GameplayEffectSpec CreateSpec(int skillID, GameplayEffectSpec sourceSpec, AbilitySystemComponent creator,
AbilitySystemComponent owner, int level = 1)
{
var spec = GameplayEffectSpec.GetItem();
var spec = owner.GetManager().ObjectPool.Fetch<GameplayEffectSpec>();
spec.Init(this, creator, owner, skillID, level);
spec.SetSourceGESpec(sourceSpec);
return spec;

View File

@@ -19,7 +19,7 @@ namespace GAS.Runtime
public List<GameplayEffectSpec> FetchGameplayEffects()
{
var list = ObjectPool.Instance.Fetch<List<GameplayEffectSpec>>();
var list = _owner.GetManager().ObjectPool.Fetch<List<GameplayEffectSpec>>();
list.AddRange(_gameplayEffectSpecs);
return list;
}
@@ -27,7 +27,7 @@ namespace GAS.Runtime
public void ReturnGameplayEffects(List<GameplayEffectSpec> list)
{
list.Clear();
ObjectPool.Instance.Recycle(list);
_owner.GetManager().ObjectPool.Recycle(list);
}
public void Tick(int deltaTime)
@@ -50,7 +50,7 @@ namespace GAS.Runtime
foreach (var spec in _recycleTask)
{
spec.Release();
GameplayEffectSpec.RecycleItem(spec);
_owner.GetManager().ObjectPool.Recycle(spec);
}
_recycleTask.Clear();
@@ -61,7 +61,7 @@ namespace GAS.Runtime
{
if (tags.Empty) return;
var removeList = ObjectPool.Instance.Fetch<List<GameplayEffectSpec>>();
var removeList = _owner.GetManager().ObjectPool.Fetch<List<GameplayEffectSpec>>();
foreach (var gameplayEffectSpec in _gameplayEffectSpecs)
{
@@ -85,7 +85,7 @@ namespace GAS.Runtime
}
removeList.Clear();
ObjectPool.Instance.Recycle(removeList);
_owner.GetManager().ObjectPool.Recycle(removeList);
}
/// <summary>
@@ -334,7 +334,7 @@ namespace GAS.Runtime
foreach (var spec in _recycleTask)
{
spec.Release();
GameplayEffectSpec.RecycleItem(spec);
_owner.GetManager().ObjectPool.Recycle(spec);
}
_recycleTask.Clear();

View File

@@ -15,17 +15,6 @@ namespace GAS.Runtime
SelfNumber = GameplayEffectSpec.Count;
}
public static GameplayEffectSpec GetItem()
{
var item = ObjectPool.Instance.Fetch<GameplayEffectSpec>();
return item;
}
public static void RecycleItem(GameplayEffectSpec obj)
{
ObjectPool.Instance.Recycle(obj);
}
/// <summary>
/// The execution type of onImmunity is one shot.
/// </summary>

View File

@@ -1,10 +0,0 @@
namespace JNGame.Runtime.GAS.Runtime
{
/// <summary>
/// GAS 管理器
/// </summary>
public class JNGasManager
{
}
}

View File

@@ -0,0 +1,81 @@
using System.Collections.Generic;
using GAS.General;
using GAS.Runtime;
using JNGame.Runtime.Util;
namespace JNGame.Runtime.GAS.Runtime
{
/// <summary>
/// GAS 管理器
/// </summary>
public class JexGasManager
{
#if UNITY_EDITOR
//编辑器专用的单例 用于预览GAS
public static JexGasManager Editor = new JexGasManager();
#endif
//---------------- 全局信息 ------------------------------------------------------------------------------------------
private static int _TimeLineAbilityTickTime = 20;
public static int TimeLineAbilityTickTime => _TimeLineAbilityTickTime;
public static int FrameRateValue => 1000 / _TimeLineAbilityTickTime;
/// <summary>
/// GAS 专用对象池 (只限制当前管理器)
/// </summary>
public JexGasObjectPool ObjectPool { get; private set; }
public List<AbilitySystemComponent> AbilitySystemComponents = new();
public JexGasManager()
{
ObjectPool = new JexGasObjectPool();
#if UNITY_EDITOR
Editor = this;
#endif
}
/// <summary>
/// 委托 AbilitySystemComponent
/// </summary>
public void Register(AbilitySystemComponent abilitySystemComponent)
{
if (AbilitySystemComponents.Contains(abilitySystemComponent)) return;
AbilitySystemComponents.Add(abilitySystemComponent);
abilitySystemComponent.OnAwake();
}
/// <summary>
/// 取消委托 AbilitySystemComponent
/// </summary>
public bool Unregister(AbilitySystemComponent abilitySystemComponent)
{
abilitySystemComponent.Release();
return AbilitySystemComponents.Remove(abilitySystemComponent);
}
//GAS 更新
public void Update(int dt)
{
Profiler.BeginSample($"{nameof(JexGasManager)}::Tick()");
var abilitySystemComponents = ObjectPool.Fetch<List<AbilitySystemComponent>>();
abilitySystemComponents.AddRange(AbilitySystemComponents);
foreach (var abilitySystemComponent in abilitySystemComponents)
{
abilitySystemComponent.Tick(dt);
}
abilitySystemComponents.Clear();
ObjectPool.Recycle(abilitySystemComponents);
Profiler.EndSample();
}
}
}

View File

@@ -141,7 +141,7 @@ namespace GAS.Runtime
}
else
{
var list = ObjectPool.Instance.Fetch<List<object>>();
var list = _owner.GetManager().ObjectPool.Fetch<List<object>>();
list.Add(source);
_dynamicAddedTags.Add(tag, list);
}
@@ -156,7 +156,7 @@ namespace GAS.Runtime
if (_dynamicAddedTags.TryGetValue(tag, out var addedTag))
{
addedTag.Clear();
ObjectPool.Instance.Recycle(addedTag);
_owner.GetManager().ObjectPool.Recycle(addedTag);
dirty = _dynamicAddedTags.Remove(tag);
}
@@ -168,7 +168,7 @@ namespace GAS.Runtime
}
else
{
var list = ObjectPool.Instance.Fetch<List<object>>();
var list = _owner.GetManager().ObjectPool.Fetch<List<object>>();
list.Add(source);
_dynamicRemovedTags.Add(tag, list);
}
@@ -190,7 +190,7 @@ namespace GAS.Runtime
dirty = tagList.Count == 0;
if (dirty)
{
ObjectPool.Instance.Recycle(tagList);
_owner.GetManager().ObjectPool.Recycle(tagList);
dynamicTag.Remove(tag);
}
}

View File

@@ -2,6 +2,7 @@
using System.Collections.Generic;
using GAS.General;
using JNGame.Runtime.GAS.Runtime;
namespace GAS.Runtime
{
@@ -308,7 +309,7 @@ namespace GAS.Runtime
return;
}
_playTotalTime += deltaTime;
var targetFrame = (int)(_playTotalTime / GASTimer.TimeLineAbilityTickTime);
var targetFrame = (int)(_playTotalTime / JexGasManager.TimeLineAbilityTickTime);
// 追帧
while (_currentFrame < targetFrame)