CTT/Unity/Assets/Model/Module/Numeric/NumericComponent.cs

168 lines
5.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System;
using System.Collections.Generic;
namespace ET
{
namespace EventType
{
public struct NumbericChange
{
public Entity Parent;
public NumericType NumericType;
public float Old;
public float New;
}
}
public class NumericComponentAwakeSystem : AwakeSystem<NumericComponent>
{
public override void Awake(NumericComponent self)
{
self.Awake();
}
}
public class NumericComponentDestroySystem : DestroySystem<NumericComponent>
{
public override void Destroy(NumericComponent self)
{
self.NumericDic.Clear();
}
}
public class NumericComponent : Entity, ISerializeToEntity
{
[BsonRepresentation(BsonType.Double, AllowTruncation = true)]
[MongoDB.Bson.Serialization.Attributes.BsonDictionaryOptions(MongoDB.Bson.Serialization.Options.DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<int, float> NumericDic = new Dictionary<int, float>();
public void Awake()
{
// 这里初始化base值
}
public float Get(NumericType numericType)
{
return GetByKey((int)numericType);
}
public float Get(int numericType)
{
return GetByKey(numericType);
}
#if UNITY
public int GetAsInt(NumericType numericType)
{
return UnityEngine.Mathf.RoundToInt(GetByKey((int)numericType));
}
#endif
#if SERVER
public int GetAsInt(NumericType numericType)
{
return MathHelper.RoundToInt(GetByKey((int)numericType));
}
public int GetAsInt(int numericType)
{
return MathHelper.RoundToInt(GetByKey(numericType));
}
public long GetAsLong(int numericType)
{
return MathHelper.RoundToLong(GetByKey(numericType));
}
public long GetAsLong(NumericType numericType)
{
return MathHelper.RoundToLong(GetByKey((int)numericType));
}
#endif
public void Set(NumericType nt, float value, bool canMulti = true)
{
this[nt, canMulti] = value;
}
public void AddSet(NumericType nt, float value, bool canMulti = true)
{
if (value == 0) return;
this[nt, canMulti] += value;
}
public void ReduceSet(NumericType nt, float value, bool canMulti = true)
{
if (value == 0) return;
this[nt, canMulti] -= value;
}
private float this[NumericType numericType, bool isCanMultiSet]
{
get
{
return this.GetByKey((int)numericType);
}
set
{
float v = this.GetByKey((int)numericType);
if (Math.Abs(v - value) < 0.00001f)
{
return;
}
NumericDic[(int)numericType] = value;
Update(numericType, v, isCanMultiSet);
}
}
private float GetByKey(int key)
{
this.NumericDic.TryGetValue(key, out float value);
return value;
}
private void Update(NumericType numericType, float old, bool isCanMultiSet)
{
int final = (int)numericType;
float result = this.GetByKey(final);
//如果不是直接操作最终值,需要发送两次事件,一次是修改的值,一次是最终值
if (numericType > NumericType.Max)
{
final = (int)numericType / 10;
int bas = final * 10 + 1;
int add = final * 10 + 2;
//取得最终值由基础xxx+额外xxx值组成
result = this.GetByKey(bas) + GetByKey(add);
//更新最终值
Set((NumericType)final, result, false);
old = GetByKey(final);
}
//将改变的值以事件的形式发送出去
if (isCanMultiSet)
Game.EventSystem.Publish(new EventType.NumbericChange()
{
Parent = this.Parent,
NumericType = (NumericType)final,
Old = old,
New = result
}).Coroutine();
}
public void ReSetAddNum()
{
List<int> removeList = new List<int>();
foreach (var kv in NumericDic)
{
if (kv.Key > (int)NumericType.Max)
{
int final = kv.Key / 10;
int add = final * 10 + 2;
if (GetByKey(add) != 0)
removeList.Add(add);
}
}
foreach (var key in removeList)
{
Set((NumericType)key, 0, true);
}
}
}
}