25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

192 lines
4.7 KiB

  1. using System;
  2. using CIG.Translation;
  3. using SUISSEngine;
  4. using UnityEngine;
  5. public static class CIGUtilities
  6. {
  7. public static bool HasGold(Currencies currency)
  8. {
  9. return currency.ContainsApproximate("Gold");
  10. }
  11. public static bool HasCash(Currencies currency)
  12. {
  13. return currency.ContainsApproximate("Cash");
  14. }
  15. public static string QuantityKey(float quantity, string baseKey)
  16. {
  17. if (CIGUtilities.IsSingular(quantity))
  18. {
  19. return baseKey + "$1";
  20. }
  21. return baseKey + "$n";
  22. }
  23. public static bool IsSingular(float value)
  24. {
  25. return Mathf.Approximately(value, 0f) || Mathf.Approximately(value, 1f) || (value > 0f && value < 1f);
  26. }
  27. public static bool IsSingular(int value)
  28. {
  29. return CIGUtilities.IsSingular((float)value);
  30. }
  31. public static bool IsPlural(float value)
  32. {
  33. return !CIGUtilities.IsSingular(value);
  34. }
  35. public static bool IsPlural(int value)
  36. {
  37. return !CIGUtilities.IsSingular(value);
  38. }
  39. public static ILocalizedString LocalizedString(this Currencies rawCurrencies)
  40. {
  41. Currencies currencies = rawCurrencies.Round();
  42. ILocalizedString localizedString = Localization.EmptyLocalizedString;
  43. bool flag = false;
  44. if (currencies.ContainsApproximate("Cash"))
  45. {
  46. localizedString = Localization.Concat(new ILocalizedString[]
  47. {
  48. localizedString,
  49. Localization.Integer(currencies.GetValue("Cash")),
  50. Localization.LiteralWhiteSpace,
  51. Localization.Key("cash")
  52. });
  53. flag = true;
  54. }
  55. if (currencies.ContainsApproximate("Gold"))
  56. {
  57. ILocalizedString localizedString2 = (!flag) ? Localization.EmptyLocalizedString : Localization.Literal(", ");
  58. localizedString = Localization.Concat(new ILocalizedString[]
  59. {
  60. localizedString,
  61. localizedString2,
  62. Localization.Integer(currencies.GetValue("Gold")),
  63. Localization.LiteralWhiteSpace,
  64. Localization.Key("gold")
  65. });
  66. flag = true;
  67. }
  68. if (currencies.ContainsApproximate("XP"))
  69. {
  70. ILocalizedString localizedString3 = (!flag) ? Localization.EmptyLocalizedString : Localization.Literal(", ");
  71. localizedString = Localization.Concat(new ILocalizedString[]
  72. {
  73. localizedString,
  74. localizedString3,
  75. Localization.Integer(currencies.GetValue("XP")),
  76. Localization.LiteralWhiteSpace,
  77. Localization.Key("experience")
  78. });
  79. flag = true;
  80. }
  81. return (!flag) ? Localization.Integer(0) : localizedString;
  82. }
  83. public static ILocalizedString LocalizedAmountString(this Currencies rawCurrencies)
  84. {
  85. Currencies currencies = rawCurrencies.Round();
  86. if (currencies.ContainsApproximate("Cash"))
  87. {
  88. return Localization.Integer(currencies.GetValue("Cash"));
  89. }
  90. if (currencies.ContainsApproximate("Gold"))
  91. {
  92. return Localization.Integer(currencies.GetValue("Gold"));
  93. }
  94. if (currencies.ContainsApproximate("XP"))
  95. {
  96. return Localization.Integer(currencies.GetValue("XP"));
  97. }
  98. return Localization.EmptyLocalizedString;
  99. }
  100. public static ILocalizedString SizeToString(long size)
  101. {
  102. int num = 0;
  103. while (num < CIGUtilities.SizeSuffix.Length - 1 && size > 800000L)
  104. {
  105. num++;
  106. size /= 1024L;
  107. }
  108. return Localization.Concat(new ILocalizedString[]
  109. {
  110. Localization.Float((double)size / 1024.0, 1, false),
  111. Localization.Literal(CIGUtilities.SizeSuffix[num])
  112. });
  113. }
  114. public static Currencies WithoutXPCurrency(this Currencies currencies)
  115. {
  116. Currencies c = new Currencies(currencies);
  117. return c - new Currencies("XP", currencies.GetValue("XP"));
  118. }
  119. public static Rect ScaleSprite(Rect bounds, Sprite sprite)
  120. {
  121. return CIGUtilities.KeepRectInsideBoundsWhileKeepingAspectRatio(bounds, sprite.rect);
  122. }
  123. public static Rect KeepRectInsideBoundsWhileKeepingAspectRatio(Rect bounds, Rect r)
  124. {
  125. float width = r.width;
  126. float height = r.height;
  127. float num = bounds.width / width;
  128. float num2 = bounds.height / height;
  129. float num3;
  130. if (num2 < num)
  131. {
  132. num3 = num2;
  133. }
  134. else
  135. {
  136. num3 = num;
  137. }
  138. float width2 = width * num3;
  139. float height2 = height * num3;
  140. return new Rect(bounds.x, bounds.y, width2, height2);
  141. }
  142. public static int SortByUnlockASC(CIGBuilding a, CIGBuilding b)
  143. {
  144. if (a.unlockLevels[0] > b.unlockLevels[0])
  145. {
  146. return 1;
  147. }
  148. if (a.unlockLevels[0] < b.unlockLevels[0])
  149. {
  150. return -1;
  151. }
  152. int num = Mathf.FloorToInt((float)a.PurchasePrice.GetValue("Gold"));
  153. int num2 = Mathf.FloorToInt((float)b.PurchasePrice.GetValue("Gold"));
  154. if (num < num2)
  155. {
  156. return 1;
  157. }
  158. if (num > num2)
  159. {
  160. return -1;
  161. }
  162. return a.name.CompareTo(b.name);
  163. }
  164. public static Comparison<T> reverse<T>(Comparison<T> compare)
  165. {
  166. return (T a, T b) => -compare(a, b);
  167. }
  168. private static readonly string[] SizeSuffix = new string[]
  169. {
  170. "kb",
  171. "mb",
  172. "gb",
  173. "tb"
  174. };
  175. }