DeviceMgr.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Newtonsoft.Json;
  6. /* 设备管理者(弓和箭的种类) */
  7. public class DeviceMgr
  8. {
  9. static DeviceMgr _ins = null;
  10. public static DeviceMgr ins {
  11. get {
  12. if (_ins == null) {
  13. _ins = new DeviceMgr();
  14. }
  15. return _ins;
  16. }
  17. }
  18. Hashtable deviceConfigs = new Hashtable();
  19. DeviceMgr()
  20. {
  21. CacheDeviceConfig<DeviceBow1000>();
  22. CacheDeviceConfig<DeviceBow1001>();
  23. CacheDeviceConfig<DeviceArrow2000>();
  24. //用户数据初始化
  25. List<DeviceInfo> deviceInfos = LoginMgr.myUserInfo.deviceList;
  26. if (deviceInfos.Count == 0) {
  27. // 加入初始设备
  28. int[] ids = {1000, 1001, 2000};
  29. foreach (var id in ids) {
  30. DeviceInfo deviceInfo = new DeviceInfo();
  31. deviceInfo.id = id;
  32. if (id == 1000 || id == 2000) deviceInfo.inuse = true;
  33. deviceInfos.Add(deviceInfo);
  34. }
  35. LoginMgr.myUserInfo.Save();
  36. }
  37. }
  38. void CacheDeviceConfig<T>() where T : DeviceConfig
  39. {
  40. DeviceConfig deviceConfig = System.Activator.CreateInstance<T>();
  41. deviceConfigs.Add(deviceConfig.id, deviceConfig);
  42. }
  43. void CacheDeviceConfig(int id, DeviceConfig deviceConfig)
  44. {
  45. deviceConfigs.Add(id, deviceConfig);
  46. }
  47. public PropConfig GetDeviceConfig(int id)
  48. {
  49. return (PropConfig) deviceConfigs[id];
  50. }
  51. public List<DeviceInfo> GetMyDeviceList()
  52. {
  53. List<DeviceInfo> list = LoginMgr.myUserInfo.deviceList;
  54. foreach (var deviceInfo in list)
  55. {
  56. deviceInfo.config = (DeviceConfig) deviceConfigs[deviceInfo.id];
  57. }
  58. return list;
  59. }
  60. public void UseDevice(DeviceInfo deviceInfo)
  61. {
  62. List<DeviceInfo> myDevices = GetMyDeviceList();
  63. if (deviceInfo.config.type == 1 || deviceInfo.config.type == 2)
  64. {
  65. foreach (var myDevice in myDevices)
  66. {
  67. if (myDevice.config.type == deviceInfo.config.type)
  68. {
  69. myDevice.inuse = false;
  70. }
  71. }
  72. deviceInfo.inuse = true;
  73. LoginMgr.myUserInfo.Save();
  74. }
  75. }
  76. public (DeviceInfo, DeviceInfo) GetCurrentBowArrowInfo()
  77. {
  78. List<DeviceInfo> myDevices = GetMyDeviceList();
  79. DeviceInfo bow = null;
  80. DeviceInfo arrow = null;
  81. foreach (var myDevice in myDevices)
  82. {
  83. if (myDevice.config.type == 1 && myDevice.inuse)
  84. {
  85. bow = myDevice;
  86. } else if (myDevice.config.type == 2 && myDevice.inuse) {
  87. arrow = myDevice;
  88. }
  89. }
  90. return (bow, arrow);
  91. }
  92. public int GetCurrentBowPound()
  93. {
  94. List<DeviceInfo> myDevices = GetMyDeviceList();
  95. foreach (var myDevice in myDevices)
  96. {
  97. if (myDevice.config.type == 1 && myDevice.inuse)
  98. {
  99. return ((DeviceBowConfig) myDevice.config).pound;
  100. }
  101. }
  102. return 0;
  103. }
  104. }
  105. public class DeviceInfo
  106. {
  107. public int id = 0;
  108. public bool inuse = false;
  109. [JsonIgnore]
  110. public DeviceConfig config = null;
  111. }
  112. public class DeviceConfig
  113. {
  114. public int id = 0;
  115. public int name = 0;
  116. public int detail = 0;
  117. public int difficulty = 0;
  118. public int model = 0;
  119. public int type = 0;
  120. }
  121. public class DeviceBowConfig : DeviceConfig {
  122. public int pound = 0;
  123. }
  124. public class DeviceBow1000 : DeviceBowConfig {
  125. public DeviceBow1000()
  126. {
  127. id = 1000;
  128. name = 201000;
  129. detail = 211000;
  130. difficulty = 3;
  131. model = 1;
  132. type = 1;
  133. pound = 18;
  134. }
  135. }
  136. public class DeviceBow1001 : DeviceBowConfig {
  137. public DeviceBow1001()
  138. {
  139. id = 1001;
  140. name = 201001;
  141. detail = 211001;
  142. difficulty = 5;
  143. model = 1;
  144. type = 1;
  145. pound = 25;
  146. }
  147. }
  148. public class DeviceArrow2000 : DeviceConfig {
  149. public DeviceArrow2000()
  150. {
  151. id = 2000;
  152. name = 201002;
  153. detail = 211002;
  154. difficulty = 3;
  155. model = 2;
  156. type = 2;
  157. }
  158. }