CTT/Server/Hotfix/Module/Numeric/NumericWatcher_Level.cs

52 lines
2.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.

namespace ET
{
/// <summary>
/// 监视hp数值变化改变血条值
/// </summary>
[NumericWatcher(NumericType.Level)]
public class NumericWatcher_Level : INumericWatcher
{
public async ETTask Run(Entity entity, float old, float value)
{
if (!entity) return;
Unit unit = entity.As<Unit>();
if (unit.UnitType != UnitType.Player) return;
int oldLevel = MathHelper.RoundToInt(old);
int level = MathHelper.RoundToInt(value);
if (oldLevel == level) return;
NumericComponent num = unit.GetComponent<NumericComponent>();
//!账户信息变化
User user = await UserComponent.Instance.Query(unit.Id);
user.Level = level;
UserComponent.Instance.Save(user).Coroutine();
//!属性变化
PlayerData data = unit.GetComponent<PlayerData>();
CharacterHelper.AddAutoCharacterPoint(num, data, oldLevel, level);
CharacterHelper.AddSkillPoint(data, level);
//!任务
await Game.EventSystem.Publish(new EventType.UpdateTaskState
{
unit = unit,
type = TaskTargetType.LevelTask,
value = level
});
DelaySendSyncAttributeComponent.instance.Add(unit, NumericType.Level, value);
LevelLog levelLog = await DBComponent.Instance.Query<LevelLog>(unit.Id);
if (levelLog == null)
{
levelLog = Entity.Create(typeof(LevelLog), false).As<LevelLog>();
levelLog.Id = unit.Id;
levelLog.levelList = new System.Collections.Generic.LinkedList<int>();
}
levelLog.levelList.AddLast(MathHelper.RoundToInt(value));
if (levelLog.levelList.Count > 20)
{
levelLog.levelList.RemoveFirst();
}
await DBComponent.Instance.Save(levelLog);
await ETTask.CompletedTask;
}
}
}