PropMgr.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Newtonsoft.Json;
  5. public class PropMgr {
  6. static PropMgr _ins = null;
  7. public static PropMgr ins {
  8. get {
  9. if (_ins == null) {
  10. _ins = new PropMgr();
  11. }
  12. return _ins;
  13. }
  14. }
  15. Hashtable propConfigs = new Hashtable();
  16. List<int> propConfigIds = new List<int>();
  17. PropMgr()
  18. {
  19. for (int i = 1; i <= 5; i++)
  20. {
  21. PropScaleAim propScaleAim = new PropScaleAim(10, i);
  22. CachePropConfig(propScaleAim.id, propScaleAim);
  23. }
  24. for (int i = 1; i <= 5; i++)
  25. {
  26. PropScaleShoot propScaleShoot = new PropScaleShoot(20, i);
  27. CachePropConfig(propScaleShoot.id, propScaleShoot);
  28. }
  29. }
  30. void CachePropConfig(int id, PropConfig propConfig)
  31. {
  32. propConfigs.Add(id, propConfig);
  33. propConfigIds.Add(id);
  34. }
  35. public PropConfig GetPropConfig(int id)
  36. {
  37. return (PropConfig) propConfigs[id];
  38. }
  39. public List<PropConfig> ListForShop()
  40. {
  41. List<PropConfig> list = new List<PropConfig>();
  42. foreach (int id in propConfigIds)
  43. {
  44. list.Add((PropConfig) propConfigs[id]);
  45. }
  46. return list;
  47. }
  48. public List<PropInfo> ListForBag()
  49. {
  50. List<PropInfo> list = LoginMgr.myUserInfo.bagList;
  51. foreach (var propInfo in list)
  52. {
  53. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  54. }
  55. return list;
  56. }
  57. public List<PropInfo> ListForEquipped()
  58. {
  59. List<PropInfo> list = new List<PropInfo>();
  60. foreach (var propInfo in LoginMgr.myUserInfo.bagList)
  61. {
  62. if (propInfo.inuse) {
  63. propInfo.config = (PropConfig) propConfigs[propInfo.id];
  64. list.Add(propInfo);
  65. }
  66. }
  67. return list;
  68. }
  69. //商店购买接口
  70. public bool isSoldOut(PropConfig propConfig)
  71. {
  72. if (propConfig.type == 1 || propConfig.type == 2)
  73. {
  74. List<PropInfo> myProps = LoginMgr.myUserInfo.bagList;
  75. foreach (var myProp in myProps)
  76. {
  77. if (myProp.id == propConfig.id && myProp.count > 0) return true;
  78. }
  79. }
  80. return false;
  81. }
  82. public bool buyProp(PropConfig propConfig)
  83. {
  84. if (LoginMgr.myUserInfo.diamond >= propConfig.diamond) {
  85. LoginMgr.myUserInfo.diamond -= propConfig.diamond;
  86. PropInfo propInfo = new PropInfo();
  87. propInfo.id = propConfig.id;
  88. propInfo.count = 1;
  89. LoginMgr.myUserInfo.bagList.Add(propInfo);
  90. LoginMgr.myUserInfo.Save();
  91. return true;
  92. }
  93. return false;
  94. }
  95. public bool useProp(PropInfo propInfo)
  96. {
  97. if (!propInfo.inuse) {
  98. if (propInfo.config.type == 1 || propInfo.config.type == 2) {
  99. List<PropInfo> equippeds = ListForEquipped();
  100. foreach (var equipped in equippeds)
  101. {
  102. if (equipped.config.type == propInfo.config.type) {
  103. equipped.inuse = false;
  104. }
  105. }
  106. }
  107. }
  108. propInfo.inuse = !propInfo.inuse;
  109. LoginMgr.myUserInfo.Save();
  110. return propInfo.inuse;
  111. }
  112. public void DebugAddAndUseProp(int id)
  113. {
  114. PropInfo propInfo = new PropInfo();
  115. propInfo.id = id;
  116. propInfo.inuse = true;
  117. LoginMgr.myUserInfo.bagList.Add(propInfo);
  118. }
  119. }
  120. public class PropInfo {
  121. public int id = 0;
  122. public int count = 1;
  123. [JsonIgnore]
  124. public PropConfig config = null;
  125. public bool inuse = false;
  126. }
  127. public class PropConfig {
  128. public int id = 0;
  129. public int type = 0;
  130. public int iconID = 0;
  131. public string[] name = {null};
  132. public string[] detail = {null};
  133. public int difficulty = -1;
  134. public int diamond = 0;
  135. }
  136. public class PropScaleAim : PropConfig {
  137. public int scaleValue = 0;
  138. public PropScaleAim(int baseID, int scaleValue)
  139. {
  140. this.scaleValue = scaleValue;
  141. id = baseID + scaleValue;
  142. type = 1;
  143. iconID = 1000;
  144. name = new string[]{"101000", scaleValue.ToString()};
  145. detail = new string[]{"111000", scaleValue.ToString()};
  146. diamond = scaleValue * 20;
  147. }
  148. }
  149. public class PropScaleShoot : PropConfig {
  150. public int scaleValue = 0;
  151. public PropScaleShoot(int baseID, int scaleValue)
  152. {
  153. this.scaleValue = scaleValue;
  154. id = baseID + scaleValue;
  155. type = 2;
  156. iconID = 1001;
  157. name = new string[]{"101001", scaleValue.ToString()};
  158. detail = new string[]{"111001", scaleValue.ToString()};
  159. diamond = scaleValue * 20;
  160. }
  161. }