This commit is contained in:
CortexCore
2025-04-14 15:39:24 +08:00
parent 5fceb6f885
commit 3f9d9f19ce
50 changed files with 683 additions and 41 deletions

39
Src/Buff/IBuff.cs Normal file
View File

@@ -0,0 +1,39 @@
using System;
using System.Collections;
using System.Collections.Generic;
using BITKit;
namespace Net.Project.B.Buff
{
public interface IBuff
{
float Value { get; set; }
}
[Serializable]
public struct AddHp : IBuff
{
public float Value { get; set; }
}
[Serializable]
public struct InfiniteAp : IBuff
{
public float Value { get; set; }
}
[Serializable]
public struct Hunger : IBuff
{
public float Value { get; set; }
}
[Serializable]
public struct Thirsty : IBuff
{
public float Value { get; set; }
}
[Serializable]
public struct Bleeding:IBuff
{
public float Value { get; set; }
}
}

11
Src/Buff/IBuff.cs.meta Normal file
View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0e2b4f90e2e2d174094c68bf35656160
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
using System;
using System.Buffers;
using System.Collections.Generic;
namespace Net.Project.B.Buff
{
public interface IBuffComponent
{
Memory<IBuff> Buffs { get; }
IReadOnlyDictionary<IBuff, float> Durations { get; }
public event Action<IBuff> OnBuffAdded;
public event Action<IBuff, float> OnBuffUpdated;
public event Action<IBuff> OnBuffRemoved;
public void AddBuff(IBuff buff = default, float duration = 0);
public void RemoveBuff<T>() where T : IBuff;
public void RemoveBuff(ref IBuff buff);
}
public class BuffComponent:IBuffComponent
{
private readonly ArrayPool<int> _pool=ArrayPool<int>.Create();
public Memory<IBuff> Buffs=>_buff.AsMemory(0, _count);
public IReadOnlyDictionary<IBuff, float> Durations => _durations;
private readonly IBuff[] _buff = new IBuff[32];
public event Action<IBuff> OnBuffAdded;
public event Action<IBuff, float> OnBuffUpdated;
public event Action<IBuff> OnBuffRemoved;
private readonly Dictionary<IBuff, float> _durations = new();
private readonly Queue<IBuff> _removeQueue = new();
private int _count;
public void AddBuff(IBuff buff, float duration = 0)
{
_buff[_count++] = buff;
OnBuffAdded?.Invoke(buff);
if (duration > 0)
{
if (_durations.TryGetValue(buff, out var currentDuration))
{
currentDuration += duration;
}
else
{
currentDuration = duration;
}
_durations[buff] = currentDuration;
}
}
public void RemoveBuff<T>() where T : IBuff
{
for (var index = 0; index < Buffs.Span.Length; index++)
{
ref var buff =ref Buffs.Span[index];
if (buff is T)
{
_removeQueue.Enqueue(buff);
}
}
while (_removeQueue.TryDequeue(out var remove))
{
RemoveBuff(ref remove);
}
}
public void RemoveBuff(ref IBuff buff)
{
var prev = false;
for (var i = 0; i < _count; i++)
{
ref var b = ref _buff[i];
if (b == buff)
{
prev = true;
continue;
}
if (!prev) continue;
ref var p = ref _buff[i - 1];
p = b;
}
if (prev)
{
OnBuffRemoved?.Invoke(buff);
}
_count--;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ab511dc98184e9641b6aac16c6da1c10
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
{
"name": "Net.Project.B.Buff",
"rootNamespace": "",
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:677cd05ca06c46b4395470200b1acdad"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": true,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": true
}

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 46b59e80b22f9f04dbd080f812276bc4
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: