遊戲金幣單位換算管理類
using System.Collections; using System.Collections.Generic; using UnityEngine;
//金幣單位 public enum GoldUnit { N = 0, K, M, B, T, AA, BB, CC, DD, EE, FF, GG, HH, II, JJ, KK, LL, MM, NN, OO, PP, QQ, RR, SS, TT, UU, VV, WW, XX, YY, ZZ, }
/// <summary> /// 金幣數量 /// </summary> public class GoldNum { //建構函式 private string goldCountText; public string GoldCountText { get { if (goldUnit == 0) { goldCountText = ((int)goldValue).ToString(); } else { float tempValue = (float)decimal.Round((decimal)goldValue, 2); goldCountText = tempValue + ((GoldUnit)goldUnit).ToString(); } return goldCountText; } set { goldCountText = value; } }
internal int goldUnit; internal float goldValue; public GoldNum(string value) { value = value.Trim(); //刪除字串首部和尾部的空格 bool isSingleDigit = true; for (int i = 30; i >= 0; i--) { if (value.EndsWith(((GoldUnit)i).ToString())) { value = value.Trim(((GoldUnit)i).ToString().ToCharArray()); isSingleDigit = false; goldUnit = i; goldValue = float.Parse(value); break; } }
if (isSingleDigit) { goldUnit = 0; goldValue = float.Parse(value); }
while (goldValue > 500 && goldUnit < 30) { goldUnit++; goldValue /= 1000; } while (goldValue > 0 && goldValue < 1 && goldUnit > 0) { goldUnit--; goldValue *= 1000; } }
//運算子重寫 /*operator是C#、C++和pascal的關鍵字,它和運算子一起使用,表示一個運算子函式,理解時應將operator和運算子整體上視為一個函式名。 過載運算子的函式一般格式如下
函式型別 operator 運算子名稱 (形參表列)
{對運算子的過載處理}
例如,想將“+”用於Complex(複數)的加法運算,函式的原型可以是這樣的:
Complex operator + (Complex & c1,Complex &c2);
operator+函式表示對運算子+過載。 其中,operator是關鍵字,專門用於定義過載運算子的函式的,運算子名稱就是C++提供給使用者的預定運算子。
注意:函式名是由operator和運算子組成。 */
public static GoldNum operator +(GoldNum A, GoldNum B) { int limit = 4; int tempUnit = 0; float tempValue = 0;
if (A.goldUnit == B.goldUnit) { tempUnit = A.goldUnit; tempValue = A.goldValue + B.goldValue; } else if (A.goldUnit > B.goldUnit) { if (A.goldUnit - B.goldUnit <= limit) { tempUnit = A.goldUnit; tempValue = A.goldValue + B.goldValue / Mathf.Pow(1000, A.goldUnit - B.goldUnit);
} else if (A.goldUnit - B.goldUnit > limit) { tempUnit = A.goldUnit; tempValue = A.goldValue; } } else if (A.goldUnit < B.goldUnit) { if (B.goldUnit - A.goldUnit <= limit) { tempUnit = B.goldUnit; tempValue = A.goldValue / Mathf.Pow(1000, B.goldUnit - A.goldUnit) + B.goldValue; } else if (B.goldUnit - A.goldUnit > limit) { tempUnit = B.goldUnit; tempValue = B.goldValue; } }
GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString()); return result; } public static GoldNum operator -(GoldNum A, GoldNum B) { int limit = 4; int tempUnit = 0; float tempValue = 0;
if (A.goldUnit == B.goldUnit) { tempUnit = A.goldUnit; tempValue = A.goldValue - B.goldValue; } else if (A.goldUnit > B.goldUnit) { if (A.goldUnit - B.goldUnit <= limit) { tempUnit = A.goldUnit; tempValue = A.goldValue - B.goldValue / Mathf.Pow(1000, A.goldUnit - B.goldUnit); } else if (A.goldUnit - B.goldUnit > limit) { tempUnit = A.goldUnit; tempValue = A.goldValue; } } else if (A.goldUnit < B.goldUnit) { if (B.goldUnit - A.goldUnit <= limit) { tempUnit = B.goldUnit; tempValue = A.goldValue / Mathf.Pow(1000, B.goldUnit - A.goldUnit) - B.goldValue; } else if (B.goldUnit - A.goldUnit > limit) { tempUnit = B.goldUnit; tempValue = -B.goldValue; } }
GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString()); return result; } public static bool operator >(GoldNum A, GoldNum B) { GoldNum result = B - A; return result.GoldCountText.StartsWith("-"); } public static bool operator <(GoldNum A, GoldNum B) { GoldNum result = A - B; return result.GoldCountText.StartsWith("-"); } public static bool operator >=(GoldNum A, GoldNum B) { GoldNum result = A - B; return !result.GoldCountText.StartsWith("-") || result.GoldCountText == "0"; } public static bool operator <=(GoldNum A, GoldNum B) { GoldNum result = A - B; return result.GoldCountText.StartsWith("-") || result.GoldCountText == "0"; } public static GoldNum operator *(GoldNum A, float B) { int tempUnit = A.goldUnit; float tempValue = A.goldValue * B; GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString()); return result; } public static GoldNum operator /(GoldNum A, float B) { int tempUnit = A.goldUnit; float tempValue = A.goldValue / B; GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString()); return result; } public static GoldNum operator *(GoldNum A, GoldNum B) { int tempUnit = A.goldUnit + B.goldUnit; float tempValue = A.goldValue * B.goldValue; GoldNum result = new GoldNum(tempValue + ((GoldUnit)tempUnit).ToString()); return result; } }