Newer
Older
DungeonShooting / DungeonShooting_Godot / src / package / Holster.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 角色身上的枪套, 存储角色携带的武器
  5. /// </summary>
  6. public class Holster
  7. {
  8. /// <summary>
  9. /// 插槽类
  10. /// </summary>
  11. public class GunSlot
  12. {
  13. /// <summary>
  14. /// 是否启用
  15. /// </summary>
  16. public bool Enable = false;
  17. /// <summary>
  18. /// 当前插槽存放的武器类型
  19. /// </summary>
  20. public GunWeightType Type = GunWeightType.MainWeapon;
  21. /// <summary>
  22. /// 插槽存放的武器
  23. /// </summary>
  24. public Gun Gun;
  25. }
  26.  
  27. /// <summary>
  28. /// 归属者
  29. /// </summary>
  30. public Role Master { get; }
  31.  
  32. /// <summary>
  33. /// 当前使用的武器对象
  34. /// </summary>
  35. public Gun ActiveGun { get; private set; }
  36.  
  37. /// <summary>
  38. /// 当前使用的武器的索引
  39. /// </summary>
  40. public int ActiveIndex { get; private set; } = 0;
  41.  
  42. public GunSlot[] SlotList { get; } = new GunSlot[4];
  43.  
  44. public Holster(Role master)
  45. {
  46. Master = master;
  47.  
  48. //创建枪的插槽, 默认前两个都是启用的
  49. GunSlot slot1 = new GunSlot();
  50. slot1.Enable = true;
  51. SlotList[0] = slot1;
  52.  
  53. GunSlot slot2 = new GunSlot();
  54. slot2.Enable = true;
  55. slot2.Type = GunWeightType.DeputyWeapon;
  56. SlotList[1] = slot2;
  57.  
  58. GunSlot slot3 = new GunSlot();
  59. SlotList[2] = slot3;
  60.  
  61. GunSlot slot4 = new GunSlot();
  62. slot4.Type = GunWeightType.DeputyWeapon;
  63. SlotList[3] = slot4;
  64. }
  65.  
  66. /// <summary>
  67. /// 根据索引获取武器
  68. /// </summary>
  69. public Gun GetGun(int index) {
  70. if (index >= SlotList.Length)
  71. {
  72. return null;
  73. }
  74. return SlotList[index].Gun;
  75. }
  76.  
  77. /// <summary>
  78. /// 根据武器id查找枪套中该武器所在的位置, 如果没有, 则返回 -1
  79. /// </summary>
  80. /// <param name="id">武器id</param>
  81. public int FindGun(string id)
  82. {
  83. for (int i = 0; i < SlotList.Length; i++)
  84. {
  85. var item = SlotList[i];
  86. if (item.Gun != null && item.Gun.Attribute.Id == id)
  87. {
  88. return i;
  89. }
  90. }
  91. return -1;
  92. }
  93.  
  94. /// <summary>
  95. /// 返回是否能放入武器
  96. /// </summary>
  97. /// <param name="gun">武器对象</param>
  98. public bool CanPickupGun(Gun gun)
  99. {
  100. for (int i = 0; i < SlotList.Length; i++)
  101. {
  102. var item = SlotList[i];
  103. if (item.Enable && gun.Attribute.WeightType == item.Type && item.Gun == null)
  104. {
  105. return true;
  106. }
  107. }
  108. return false;
  109. }
  110.  
  111. /// <summary>
  112. /// 拾起武器, 存入枪套中, 返回存放在枪套的位置, 如果容不下这把武器, 则会返回 -1
  113. /// </summary>
  114. /// <param name="gun">武器对象</param>
  115. public int PickupGun(Gun gun)
  116. {
  117. for (int i = 0; i < SlotList.Length; i++)
  118. {
  119. var item = SlotList[i];
  120. if (item.Enable && gun.Attribute.WeightType == item.Type && item.Gun == null)
  121. {
  122. gun.Pickup();
  123. item.Gun = gun;
  124. ExchangeByIndex(i);
  125. gun._PickUpGun(Master);
  126. return i;
  127. }
  128. }
  129. return -1;
  130. }
  131.  
  132. /// <summary>
  133. /// 移除指定位置的武器, 并返回这个武器对象, 如果移除正在使用的这把武器, 则会自动切换到上一把武器
  134. /// </summary>
  135. /// <param name="index">所在枪套的位置索引</param>
  136. public Gun RmoveGun(int index)
  137. {
  138. if (index < 0 || index >= SlotList.Length)
  139. {
  140. return null;
  141. }
  142. var slot = SlotList[index];
  143. if (slot.Gun == null)
  144. {
  145. return null;
  146. }
  147. var gun = slot.Gun;
  148. gun.GetParent().RemoveChild(gun);
  149. slot.Gun = null;
  150.  
  151. //如果是当前手持的武器, 就需要调用切换武器操作
  152. if (index == ActiveIndex)
  153. {
  154. //没有其他武器了
  155. if (ExchangePrev() == index)
  156. {
  157. ActiveIndex = 0;
  158. ActiveGun = null;
  159. }
  160. }
  161. gun._ThrowOutGun();
  162. return gun;
  163. }
  164.  
  165. /// <summary>
  166. /// 切换到上一个武器
  167. /// </summary>
  168. public int ExchangePrev()
  169. {
  170. var index = ActiveIndex - 1;
  171. do
  172. {
  173. if (index < 0)
  174. {
  175. index = SlotList.Length - 1;
  176. }
  177. if (ExchangeByIndex(index))
  178. {
  179. return index;
  180. }
  181. } while (index-- != ActiveIndex);
  182. return -1;
  183. }
  184.  
  185. /// <summary>
  186. /// 切换到下一个武器,
  187. /// </summary>
  188. public int ExchangeNext()
  189. {
  190. var index = ActiveIndex + 1;
  191. do
  192. {
  193. if (index >= SlotList.Length)
  194. {
  195. index = 0;
  196. }
  197. if (ExchangeByIndex(index))
  198. {
  199. return index;
  200. }
  201. }
  202. while (index++ != ActiveIndex);
  203. return -1;
  204. }
  205.  
  206. /// <summary>
  207. /// 切换到指定索引的武器
  208. /// </summary>
  209. public bool ExchangeByIndex(int index)
  210. {
  211. if (index == ActiveIndex && ActiveGun != null) return true;
  212. if (index < 0 || index > SlotList.Length) return false;
  213. var slot = SlotList[index];
  214. if (slot == null || slot.Gun == null) return false;
  215.  
  216. //将上一把武器放到背后
  217. if (ActiveGun != null)
  218. {
  219. var tempParent = ActiveGun.GetParentOrNull<Node2D>();
  220. if (tempParent != null)
  221. {
  222. tempParent.RemoveChild(ActiveGun);
  223. Master.BackMountPoint.AddChild(ActiveGun);
  224. if (ActiveIndex == 0)
  225. {
  226. ActiveGun.Position = new Vector2(0, 5);
  227. ActiveGun.RotationDegrees = 50;
  228. ActiveGun.Scale = new Vector2(-1, 1);
  229. }
  230. else if (ActiveIndex == 1)
  231. {
  232. ActiveGun.Position = new Vector2(0, 0);
  233. ActiveGun.RotationDegrees = 120;
  234. ActiveGun.Scale = new Vector2(1, -1);
  235. }
  236. else if (ActiveIndex == 2)
  237. {
  238. ActiveGun.Position = new Vector2(0, 5);
  239. ActiveGun.RotationDegrees = 310;
  240. ActiveGun.Scale = new Vector2(1, 1);
  241. }
  242. else if (ActiveIndex == 3)
  243. {
  244. ActiveGun.Position = new Vector2(0, 0);
  245. ActiveGun.RotationDegrees = 60;
  246. ActiveGun.Scale = new Vector2(1, 1);
  247. }
  248. ActiveGun._Conceal();
  249. }
  250. }
  251.  
  252. //更改父节点
  253. var parent = slot.Gun.GetParentOrNull<Node>();
  254. if (parent == null)
  255. {
  256. Master.MountPoint.AddChild(slot.Gun);
  257. }
  258. else if (parent != Master.MountPoint)
  259. {
  260. parent.RemoveChild(slot.Gun);
  261. Master.MountPoint.AddChild(slot.Gun);
  262. }
  263.  
  264. slot.Gun.Position = Vector2.Zero;
  265. slot.Gun.Scale = Vector2.One;
  266. slot.Gun.RotationDegrees = 0;
  267. ActiveGun = slot.Gun;
  268. ActiveIndex = index;
  269. ActiveGun._Active();
  270. return true;
  271. }
  272. }