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