PropMgr.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. /* 游戏道具管理者 */
  6. public class PropMgr {
  7. static PropMgr _ins = null;
  8. public static PropMgr ins {
  9. get {
  10. if (_ins == null) {
  11. _ins = new PropMgr();
  12. }
  13. return _ins;
  14. }
  15. }
  16. Hashtable propConfigs = new Hashtable();
  17. List<int> propConfigIds = new List<int>();
  18. PropMgr()
  19. {
  20. for (int i = 1; i <= 5; i++)
  21. {
  22. PropScaleAim propScaleAim = new PropScaleAim(10, i);
  23. CachePropConfig(propScaleAim.id, propScaleAim);
  24. }
  25. for (int i = 1; i <= 5; i++)
  26. {
  27. PropScaleShoot propScaleShoot = new PropScaleShoot(20, i);
  28. CachePropConfig(propScaleShoot.id, propScaleShoot);
  29. }
  30. }
  31. void CachePropConfig(int id, PropConfig propConfig)
  32. {
  33. propConfigs.Add(id, propConfig);
  34. propConfigIds.Add(id);
  35. }
  36. public PropConfig GetPropConfig(int id)
  37. {
  38. return (PropConfig) propConfigs[id];
  39. }
  40. public List<PropConfig> ListForShop()
  41. {
  42. List<PropConfig> list = new List<PropConfig>();
  43. foreach (int id in propConfigIds)
  44. {
  45. PropConfig propConfig = (PropConfig) propConfigs[id];
  46. if (propConfig.inShop) {
  47. list.Add(propConfig);
  48. }
  49. }
  50. return list;
  51. }
  52. public List<PropInfo> ListForBag()
  53. {
  54. List<PropInfo> list = LoginMgr.myUserInfo.bagList;
  55. if (CommonConfig.SpecialVersion1) list = GetDebugLocalBagList();
  56. foreach (var propInfo in list)
  57. {
  58. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  59. }
  60. return list;
  61. }
  62. public List<PropInfo> ListForEquipped()
  63. {
  64. List<PropInfo> list = new List<PropInfo>();
  65. List<PropInfo> myProps = LoginMgr.myUserInfo.bagList;
  66. if (CommonConfig.SpecialVersion1) myProps = GetDebugLocalBagList();
  67. foreach (var propInfo in myProps)
  68. {
  69. if (propInfo.inuse) {
  70. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  71. list.Add(propInfo);
  72. }
  73. }
  74. return list;
  75. }
  76. //商店购买接口
  77. public bool isSoldOut(PropConfig propConfig)
  78. {
  79. if (propConfig.type == 1 || propConfig.type == 2)
  80. {
  81. List<PropInfo> myProps = LoginMgr.myUserInfo.bagList;
  82. if (CommonConfig.SpecialVersion1) myProps = GetDebugLocalBagList();
  83. foreach (var myProp in myProps)
  84. {
  85. if (myProp.id == propConfig.id && myProp.count > 0) return true;
  86. }
  87. }
  88. return false;
  89. }
  90. public bool buyProp(PropConfig propConfig)
  91. {
  92. if (LoginMgr.myUserInfo.diamond >= propConfig.diamond) {
  93. LoginMgr.myUserInfo.diamond -= propConfig.diamond;
  94. PropInfo propInfo = new PropInfo();
  95. propInfo.id = propConfig.id;
  96. propInfo.count = 1;
  97. LoginMgr.myUserInfo.bagList.Add(propInfo);
  98. LoginMgr.myUserInfo.Save();
  99. return true;
  100. }
  101. return false;
  102. }
  103. public bool useProp(PropInfo propInfo)
  104. {
  105. if (!propInfo.inuse) {
  106. if (propInfo.config.type == 1 || propInfo.config.type == 2) {
  107. List<PropInfo> equippeds = ListForEquipped();
  108. foreach (var equipped in equippeds)
  109. {
  110. if (equipped.config.type == propInfo.config.type) {
  111. equipped.inuse = false;
  112. }
  113. }
  114. }
  115. }
  116. propInfo.inuse = !propInfo.inuse;
  117. if (CommonConfig.SpecialVersion1) {
  118. SaveDebugLocalBagList();
  119. return propInfo.inuse;
  120. }
  121. LoginMgr.myUserInfo.Save();
  122. return propInfo.inuse;
  123. }
  124. private List<PropInfo> _debugLocalBagList;
  125. private int _debugLocalBagList_uid = -1;
  126. private List<PropInfo> GetDebugLocalBagList() {
  127. if (_debugLocalBagList_uid != LoginMgr.myUserInfo.id) {
  128. _debugLocalBagList_uid = LoginMgr.myUserInfo.id;
  129. _debugLocalBagList = null;
  130. }
  131. if (_debugLocalBagList == null) {
  132. try {
  133. string str = PlayerPrefs.GetString("sv1_baglist," + LoginMgr.myUserInfo.id, null);
  134. _debugLocalBagList = JsonConvert.DeserializeObject<List<PropInfo>>(str);
  135. } catch (System.Exception) {}
  136. if (_debugLocalBagList == null) {
  137. _debugLocalBagList = new List<PropInfo>();
  138. for (int i = 2; i <= 5; i++) {
  139. PropInfo propInfo = new PropInfo();
  140. propInfo.id = 10 + i;
  141. propInfo.count = 1;
  142. _debugLocalBagList.Add(propInfo);
  143. if (propInfo.id == 12) {
  144. propInfo.inuse = true;
  145. }
  146. }
  147. for (int i = 2; i <= 5; i++) {
  148. PropInfo propInfo = new PropInfo();
  149. propInfo.id = 20 + i;
  150. propInfo.count = 1;
  151. _debugLocalBagList.Add(propInfo);
  152. if (propInfo.id == 25) {
  153. propInfo.inuse = true;
  154. }
  155. }
  156. }
  157. }
  158. return _debugLocalBagList;
  159. }
  160. private void SaveDebugLocalBagList() {
  161. if (_debugLocalBagList != null) {
  162. string str = JsonConvert.SerializeObject(_debugLocalBagList);
  163. PlayerPrefs.SetString("sv1_baglist," + LoginMgr.myUserInfo.id, str);
  164. }
  165. }
  166. }
  167. public class PropInfo {
  168. public int id = 0;
  169. public int count = 1;
  170. [JsonIgnore]
  171. public PropConfig config = null;
  172. public bool inuse = false;
  173. }
  174. public class PropConfig {
  175. public int id = 0;
  176. public int type = 0;
  177. public int iconID = 0;
  178. public string[] name = {null};
  179. public string[] detail = {null};
  180. public int difficulty = -1;
  181. public int diamond = 0;
  182. public bool inShop = true;
  183. }
  184. public class PropScaleAim : PropConfig {
  185. public int scaleValue = 0;
  186. public PropScaleAim(int baseID, int scaleValue)
  187. {
  188. this.scaleValue = scaleValue;
  189. if (this.scaleValue == 1) {
  190. this.inShop = false;
  191. }
  192. id = baseID + scaleValue;
  193. type = 1;
  194. iconID = 1000;
  195. name = new string[]{"101000", scaleValue.ToString()};
  196. detail = new string[]{"111000", scaleValue.ToString()};
  197. diamond = scaleValue * 20;
  198. }
  199. }
  200. public class PropScaleShoot : PropConfig {
  201. public int scaleValue = 0;
  202. public PropScaleShoot(int baseID, int scaleValue)
  203. {
  204. this.scaleValue = scaleValue;
  205. if (this.scaleValue == 1) {
  206. this.inShop = false;
  207. }
  208. id = baseID + scaleValue;
  209. type = 2;
  210. iconID = 1001;
  211. name = new string[]{"101001", scaleValue.ToString()};
  212. detail = new string[]{"111001", scaleValue.ToString()};
  213. diamond = scaleValue * 20;
  214. }
  215. }