Files
Fishing2/Assets/Scripts/Model/Module/Numeric/NumericComponent.cs
2025-09-14 16:26:41 +08:00

138 lines
4.2 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 System.Collections.Generic;
using NBC;
using NBC.Entitas;
namespace NBF.Fishing2
{
/// <summary>
/// 数据组件
/// </summary>
public class NumericComponent : Entity
{
public Dictionary<int, long> NumericDic = new Dictionary<int, long>();
public long this[int numericType]
{
get { return this.GetByKey(numericType); }
set { this.Insert(numericType, value); }
}
}
public struct NumericChange
{
public MapUnit MapUnit;
public int NumericType;
public long Old;
public long New;
}
public static class NumericComponentSystem
{
public static float GetAsFloat(this NumericComponent self, int numericType)
{
return (float)self.GetByKey(numericType) / 10000;
}
public static int GetAsInt(this NumericComponent self, int numericType)
{
return (int)self.GetByKey(numericType);
}
public static long GetAsLong(this NumericComponent self, int numericType)
{
return self.GetByKey(numericType);
}
public static void Set(this NumericComponent self, int nt, float value)
{
self[nt] = (long)(value * 10000);
}
public static void Set(this NumericComponent self, int nt, int value)
{
self[nt] = value;
}
public static void Set(this NumericComponent self, int nt, long value)
{
self[nt] = value;
}
public static void Add(this NumericComponent self, int nt, float value)
{
self[nt] += (long)(value * 10000);
}
public static void Add(this NumericComponent self, int nt, int value)
{
self[nt] += value;
}
public static void Add(this NumericComponent self, int nt, long value)
{
self[nt] += value;
}
public static void SetNoEvent(this NumericComponent self, int numericType, long value)
{
self.Insert(numericType, value, false);
}
public static void Insert(this NumericComponent self, int numericType, long value, bool isPublicEvent = true)
{
long oldValue = self.GetByKey(numericType);
if (oldValue == value)
{
return;
}
self.NumericDic[numericType] = value;
if (numericType >= NumericType.Max)
{
self.Update(numericType, isPublicEvent);
return;
}
if (isPublicEvent)
{
App.Main.EventComponent.Publish(new NumericChange()
{ MapUnit = self.GetParent<MapUnit>(), New = value, Old = oldValue, NumericType = numericType });
}
}
public static long GetByKey(this NumericComponent self, int key)
{
long value = 0;
self.NumericDic.TryGetValue(key, out value);
return value;
}
public static void Update(this NumericComponent self, int numericType, bool isPublicEvent)
{
int final = numericType / 10;
int bas = final * 10 + 1;
int add = final * 10 + 2;
int pct = final * 10 + 3;
int finalAdd = final * 10 + 4;
int finalPct = final * 10 + 5;
// 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100也有些buff增加10%速度所以一个值可以由5个值进行控制其最终结果
// final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
long result = (long)(((self.GetByKey(bas) + self.GetByKey(add)) * (100 + self.GetAsFloat(pct)) / 100f +
self.GetByKey(finalAdd)) *
(100 + self.GetAsFloat(finalPct)) / 100f);
self.Insert(final, result, isPublicEvent);
}
public static Dictionary<int, long> CopyDict(this NumericComponent self)
{
return new Dictionary<int, long>(self.NumericDic);
}
public static void Clear(this NumericComponent self)
{
self.NumericDic.Clear();
}
}
}