123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using Unity.Mathematics;
- using UnityEngine;
- public static class GameTool
- {
- //用于获取多层级Dictionary中的value
- public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
- {
- Dictionary<string, object> current = root;
- int count = 0;
- foreach (string key in path)
- {
- string rawValue = current[key].ToString();
- rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
- if (count == path.Length - 1)
- {
- return rawValue;
- }
- Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
- current = value;
- count++;
- }
- return null;
- }
- // 输入X=100时候,输出一定是1
- // 输入X=0的时候,随机输出 0.5-0.7之间的数值
- // X越大,输出的随机数越靠近1.
- public static bool Random100Check(float x, float thresHold = 0.6f)
- {
- //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
- //// 原始公式 x/(x+99)=threshold
- //if (thresHold <= 0 || thresHold >= 1)
- // throw new ArgumentException("threshold must be in (0, 1)");
- //if (inputValue<=0 || inputValue > 100)
- //{
- // throw new ArgumentException("inputValue must be in (0,100)");
- //}
- //float factor = (99*thresHold)/(1-thresHold);
- //float result = (factor + inputValue) / (factor + 100f);
- //return result > thresHold;
- // 初始化随机数生成器
- var random = new System.Random();
- // 确保X在0-100范围内
- x = Mathf.Clamp(x, 0, 100);
- if (x == 100)
- {
- return true;
- }
- // 计算随机范围
- double minValue = 0.4 + (0.6 * x / 100.0); // 从0.4随X增加线性增长
- double maxValue = 0.8 + (0.2 * x / 100.0); // 从0.8随X增加线性增长
- // 确保最大值不超过1
- maxValue = Math.Min(maxValue, 1.0);
- // 在[minValue, maxValue]范围内生成随机数
- double result = minValue + (random.NextDouble() * (maxValue - minValue));
- return result > thresHold;
- }
- // 检测新的狗名是否符合重名
- public static bool NewDogNameAllowed(string newDogName){
- bool dogNameAllowed = true;
- foreach (var dog in UserProperty.dogs)
- {
- if (newDogName == dog.dog_name){
- dogNameAllowed = false;
- }
- }
- return dogNameAllowed;
- }
- // 恢复游戏时间运行
- public static void ResumeGameTime()
- {
- Time.timeScale = 1;
- //Time.fixedDeltaTime = 0.02f * Time.timeScale;
- }
- // 暂停游戏时间运行
- public static void PauseGameTime()
- {
- Time.timeScale = 0;
- //Time.fixedDeltaTime = 0.02f * Time.timeScale;
- }
- public static bool IntBetween(int min, int max, int value)
- {
- if (value >= min && value < max)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
|