GameTool.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text.RegularExpressions;
  5. using Unity.Mathematics;
  6. using UnityEngine;
  7. public static class GameTool
  8. {
  9. //用于获取多层级Dictionary中的value
  10. public static string GetValueAtPath(Dictionary<string, object> root, string[] path)
  11. {
  12. Dictionary<string, object> current = root;
  13. int count = 0;
  14. foreach (string key in path)
  15. {
  16. string rawValue = current[key].ToString();
  17. rawValue = Regex.Replace(rawValue, @"[\r\n]", "");
  18. if (count == path.Length - 1)
  19. {
  20. return rawValue;
  21. }
  22. Dictionary<string, object> value = JsonConvert.DeserializeObject<Dictionary<string, object>>(rawValue);
  23. current = value;
  24. count++;
  25. }
  26. return null;
  27. }
  28. // 输入X=100时候,输出一定是1
  29. // 输入X=0的时候,随机输出 0.5-0.7之间的数值
  30. // X越大,输出的随机数越靠近1.
  31. public static bool Random100Check(float x, float thresHold = 0.6f)
  32. {
  33. //// 输入数值为1的时候,保底概率为threshold值。计算结果大于threshold返回true,否则返回false
  34. //// 原始公式 x/(x+99)=threshold
  35. //if (thresHold <= 0 || thresHold >= 1)
  36. // throw new ArgumentException("threshold must be in (0, 1)");
  37. //if (inputValue<=0 || inputValue > 100)
  38. //{
  39. // throw new ArgumentException("inputValue must be in (0,100)");
  40. //}
  41. //float factor = (99*thresHold)/(1-thresHold);
  42. //float result = (factor + inputValue) / (factor + 100f);
  43. //return result > thresHold;
  44. // 初始化随机数生成器
  45. var random = new System.Random();
  46. // 确保X在0-100范围内
  47. x = Mathf.Clamp(x, 0, 100);
  48. if (x == 100)
  49. {
  50. return true;
  51. }
  52. // 计算随机范围
  53. double minValue = 0.4 + (0.6 * x / 100.0); // 从0.4随X增加线性增长
  54. double maxValue = 0.8 + (0.2 * x / 100.0); // 从0.8随X增加线性增长
  55. // 确保最大值不超过1
  56. maxValue = Math.Min(maxValue, 1.0);
  57. // 在[minValue, maxValue]范围内生成随机数
  58. double result = minValue + (random.NextDouble() * (maxValue - minValue));
  59. return result > thresHold;
  60. }
  61. // 检测新的狗名是否符合重名
  62. public static bool NewDogNameAllowed(string newDogName){
  63. bool dogNameAllowed = true;
  64. foreach (var dog in UserProperty.dogs)
  65. {
  66. if (newDogName == dog.dog_name){
  67. dogNameAllowed = false;
  68. }
  69. }
  70. return dogNameAllowed;
  71. }
  72. // 恢复游戏时间运行
  73. public static void ResumeGameTime()
  74. {
  75. Time.timeScale = 1;
  76. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  77. }
  78. // 暂停游戏时间运行
  79. public static void PauseGameTime()
  80. {
  81. Time.timeScale = 0;
  82. //Time.fixedDeltaTime = 0.02f * Time.timeScale;
  83. }
  84. }