zxl
/
CTT
forked from Cal/CTT
1
0
Fork 0
CTT/Server/Model/Game/Helper/SkillHelper.cs

169 lines
5.7 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 Cal;
using System;
using System.Collections.Generic;
using System.IO;
namespace ET
{
public static class SkillHelper
{
public static async ETTask<byte[]> GetSkillConfig()
{
string path = "../Config/Skill/SkillConfig.bytes";
return await File.ReadAllBytesAsync(path);
}
public static async ETTask<byte[]> GetSkillBuffConfig()
{
string path = "../Config/Skill/BuffConfig.bytes";
return await File.ReadAllBytesAsync(path);
}
public static async ETTask<byte[]> GetSkillLogicConfig()
{
string path = "../Config/Skill/SkillLogicConfig.bytes";
return await File.ReadAllBytesAsync(path);
}
/// <summary>
/// 从一个迭代器中获取指定条件,指定数量的元素,返回数组类型
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source">源数据</param>
/// <param name="list">临时列表</param>
/// <param name="indexList">临时索引列表</param>
/// <param name="match">条件表达式</param>
/// <param name="targetCount">需要的数量</param>
/// <returns></returns>
public static T[] SelectTarget<T>(IEnumerable<T> source, List<T> list, List<int> indexList, Func<T, bool> match, int targetCount, T firstT = null)
where T : class
{
if (firstT != null)
{
targetCount--;
}
T[] ret = new T[targetCount];
if (source == null) return null;
list.Clear();
foreach (T item in source)
{
if (item != firstT)
if (match(item))
{
list.Add(item);
}
}
int sourceCount = list.Count;
if (sourceCount == 0)
return null;
if (sourceCount <= targetCount)
{
for (int i = 0; i < list.Count; i++)
{
ret[i] = list[i];
}
return ret;
}
indexList.Clear();
for (int i = 0; i < sourceCount; i++)
indexList.Add(i);
int site = sourceCount;//设置下限
int id;
for (int j = 0; j < targetCount; j++)
{
//返回0到site - 1之中非负的一个随机数
id = RandomHelper.RandomNumber(0, site);
//在随机位置取出一个数,保存到结果数组
ret[j] = list[indexList[id]];
//最后一个数复制到当前位置
indexList[id] = indexList[site - 1];
//位置的下限减少一
site--;
}
return ret;
}
public static bool GetParam(SkillParam skillParam, int skillId, out float value)
{
value = 0;
if (skillParam == null)
return true;
switch (skillParam.skillSourcetype)
{
case SkillSourcetype.Changable:
skillParam.values.TryGetValue(skillId % 100, out value);
return true;
default:
case SkillSourcetype.None:
value = 0;
return false;
case SkillSourcetype.Constage:
value = skillParam.value;
return true;
}
}
//private static readonly List<int> indexList = new List<int>();
//private static readonly List<Unit> tempList = new List<Unit>();
///// <summary>
///// 从sourceList中选取count个随机目标
///// </summary>
//public static void SelectUnits(List<Unit> sourceList, Unit selectUnit, int targetCount)
//{
// if (sourceList == null || sourceList.Count == 0) return;
// var sourceCount = sourceList.Count;
// if (sourceCount <= targetCount)
// {
// return;
// }
// tempList.Clear();
// indexList.Clear();
// tempList.AddRange(sourceList);
// sourceList.Clear();
// if (selectUnit != null)
// {
// targetCount--;
// sourceList.Add(selectUnit);
// }
// for (int i = 0; i < sourceCount; i++)
// indexList.Add(i);
// int site = sourceCount;//设置下限
// for (int j = 0; j < targetCount; j++)
// {
// //返回0到site - 1之中非负的一个随机数
// int id = RandomHelper.RandomNumber(0, site);
// //在随机位置取出一个数,保存到结果数组
// sourceList.Add(tempList[indexList[id]]);
// //最后一个数复制到当前位置
// indexList[id] = indexList[site - 1];
// //位置的下限减少一
// site--;
// }
//}
public static int RandomNumber(int skillId, SkillParam minCount, SkillParam maxCount, bool isRnadom = true)
{
if (!SkillHelper.GetParam(maxCount, skillId, out float max))
{
Log.Error($"数据填写不完整the id of skill is {skillId}");
return 1;
}
float min = 1;
if(isRnadom && !SkillHelper.GetParam(minCount, skillId, out min))
{
Log.Error($"数据填写不完整the id of skill is {skillId}");
}
return isRnadom ? RandomHelper.RandomNumber((int)min, (int)max + 1) : (int)max;
}
}
}