DeviceMgr.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. int[] ids = {1000, 2000};
  30. foreach (var id in ids) {
  31. DeviceInfo deviceInfo = new DeviceInfo();
  32. deviceInfo.id = id;
  33. if (id == 1000 || id == 2000) deviceInfo.inuse = true;
  34. deviceInfos.Add(deviceInfo);
  35. }
  36. LoginMgr.myUserInfo.Save();
  37. }
  38. }
  39. void CacheDeviceConfig<T>() where T : DeviceConfig
  40. {
  41. DeviceConfig deviceConfig = System.Activator.CreateInstance<T>();
  42. deviceConfigs.Add(deviceConfig.id, deviceConfig);
  43. }
  44. void CacheDeviceConfig(int id, DeviceConfig deviceConfig)
  45. {
  46. deviceConfigs.Add(id, deviceConfig);
  47. }
  48. public PropConfig GetDeviceConfig(int id)
  49. {
  50. return (PropConfig) deviceConfigs[id];
  51. }
  52. public List<DeviceInfo> GetMyDeviceList()
  53. {
  54. List<DeviceInfo> list = LoginMgr.myUserInfo.deviceList;
  55. foreach (var deviceInfo in list)
  56. {
  57. deviceInfo.config = (DeviceConfig) deviceConfigs[deviceInfo.id];
  58. }
  59. return list;
  60. }
  61. public void UseDevice(DeviceInfo deviceInfo)
  62. {
  63. List<DeviceInfo> myDevices = GetMyDeviceList();
  64. if (deviceInfo.config.type == 1 || deviceInfo.config.type == 2)
  65. {
  66. foreach (var myDevice in myDevices)
  67. {
  68. if (myDevice.config.type == deviceInfo.config.type)
  69. {
  70. myDevice.inuse = false;
  71. }
  72. }
  73. deviceInfo.inuse = true;
  74. LoginMgr.myUserInfo.Save();
  75. }
  76. }
  77. public (DeviceInfo, DeviceInfo) GetCurrentBowArrowInfo()
  78. {
  79. List<DeviceInfo> myDevices = GetMyDeviceList();
  80. DeviceInfo bow = null;
  81. DeviceInfo arrow = null;
  82. foreach (var myDevice in myDevices)
  83. {
  84. if (myDevice.config.type == 1 && myDevice.inuse)
  85. {
  86. bow = myDevice;
  87. } else if (myDevice.config.type == 2 && myDevice.inuse) {
  88. arrow = myDevice;
  89. }
  90. }
  91. return (bow, arrow);
  92. }
  93. public int GetCurrentBowPound()
  94. {
  95. List<DeviceInfo> myDevices = GetMyDeviceList();
  96. foreach (var myDevice in myDevices)
  97. {
  98. if (myDevice.config.type == 1 && myDevice.inuse)
  99. {
  100. return ((DeviceBowConfig) myDevice.config).pound;
  101. }
  102. }
  103. return 0;
  104. }
  105. }
  106. public class DeviceInfo
  107. {
  108. public int id = 0;
  109. public bool inuse = false;
  110. [JsonIgnore]
  111. public DeviceConfig config = null;
  112. }
  113. public class DeviceConfig
  114. {
  115. public int id = 0;
  116. public int name = 0;
  117. public int detail = 0;
  118. public int difficulty = 0;
  119. public int model = 0;
  120. public int type = 0;
  121. }
  122. public class DeviceBowConfig : DeviceConfig {
  123. public int pound = 0;
  124. }
  125. public class DeviceBow1000 : DeviceBowConfig {
  126. public DeviceBow1000()
  127. {
  128. id = 1000;
  129. name = 201000;
  130. detail = 211000;
  131. difficulty = 3;
  132. model = 1;
  133. type = 1;
  134. pound = 18;
  135. }
  136. }
  137. public class DeviceBow1001 : DeviceBowConfig {
  138. public DeviceBow1001()
  139. {
  140. id = 1001;
  141. name = 201001;
  142. detail = 211001;
  143. difficulty = 5;
  144. model = 1;
  145. type = 1;
  146. pound = 25;
  147. }
  148. }
  149. public class DeviceArrow2000 : DeviceConfig {
  150. public DeviceArrow2000()
  151. {
  152. id = 2000;
  153. name = 201002;
  154. detail = 211002;
  155. difficulty = 3;
  156. model = 2;
  157. type = 2;
  158. }
  159. }