PropMgr.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. foreach (var propInfo in list)
  56. {
  57. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  58. }
  59. return list;
  60. }
  61. public List<PropInfo> ListForEquipped()
  62. {
  63. List<PropInfo> list = new List<PropInfo>();
  64. foreach (var propInfo in LoginMgr.myUserInfo.bagList)
  65. {
  66. if (propInfo.inuse) {
  67. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  68. list.Add(propInfo);
  69. }
  70. }
  71. return list;
  72. }
  73. //商店购买接口
  74. public bool isSoldOut(PropConfig propConfig)
  75. {
  76. if (propConfig.type == 1 || propConfig.type == 2)
  77. {
  78. List<PropInfo> myProps = LoginMgr.myUserInfo.bagList;
  79. foreach (var myProp in myProps)
  80. {
  81. if (myProp.id == propConfig.id && myProp.count > 0) return true;
  82. }
  83. }
  84. return false;
  85. }
  86. public bool buyProp(PropConfig propConfig)
  87. {
  88. if (LoginMgr.myUserInfo.diamond >= propConfig.diamond) {
  89. LoginMgr.myUserInfo.diamond -= propConfig.diamond;
  90. PropInfo propInfo = new PropInfo();
  91. propInfo.id = propConfig.id;
  92. propInfo.count = 1;
  93. LoginMgr.myUserInfo.bagList.Add(propInfo);
  94. LoginMgr.myUserInfo.Save();
  95. return true;
  96. }
  97. return false;
  98. }
  99. public bool useProp(PropInfo propInfo)
  100. {
  101. if (!propInfo.inuse) {
  102. if (propInfo.config.type == 1 || propInfo.config.type == 2) {
  103. List<PropInfo> equippeds = ListForEquipped();
  104. foreach (var equipped in equippeds)
  105. {
  106. if (equipped.config.type == propInfo.config.type) {
  107. equipped.inuse = false;
  108. }
  109. }
  110. }
  111. }
  112. propInfo.inuse = !propInfo.inuse;
  113. LoginMgr.myUserInfo.Save();
  114. return propInfo.inuse;
  115. }
  116. public void DebugAddAndUseProp(int id)
  117. {
  118. PropInfo propInfo = new PropInfo();
  119. propInfo.id = id;
  120. propInfo.inuse = true;
  121. LoginMgr.myUserInfo.bagList.Add(propInfo);
  122. }
  123. }
  124. public class PropInfo {
  125. public int id = 0;
  126. public int count = 1;
  127. [JsonIgnore]
  128. public PropConfig config = null;
  129. public bool inuse = false;
  130. }
  131. public class PropConfig {
  132. public int id = 0;
  133. public int type = 0;
  134. public int iconID = 0;
  135. public string[] name = {null};
  136. public string[] detail = {null};
  137. public int difficulty = -1;
  138. public int diamond = 0;
  139. public bool inShop = true;
  140. }
  141. public class PropScaleAim : PropConfig {
  142. public int scaleValue = 0;
  143. public PropScaleAim(int baseID, int scaleValue)
  144. {
  145. this.scaleValue = scaleValue;
  146. if (this.scaleValue == 1) {
  147. this.inShop = false;
  148. }
  149. id = baseID + scaleValue;
  150. type = 1;
  151. iconID = 1000;
  152. name = new string[]{"101000", scaleValue.ToString()};
  153. detail = new string[]{"111000", scaleValue.ToString()};
  154. diamond = scaleValue * 20;
  155. }
  156. }
  157. public class PropScaleShoot : PropConfig {
  158. public int scaleValue = 0;
  159. public PropScaleShoot(int baseID, int scaleValue)
  160. {
  161. this.scaleValue = scaleValue;
  162. if (this.scaleValue == 1) {
  163. this.inShop = false;
  164. }
  165. id = baseID + scaleValue;
  166. type = 2;
  167. iconID = 1001;
  168. name = new string[]{"101001", scaleValue.ToString()};
  169. detail = new string[]{"111001", scaleValue.ToString()};
  170. diamond = scaleValue * 20;
  171. }
  172. }