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