Newer
Older
DungeonShooting / DungeonShooting_Godot / src / role / Role.cs
  1. using System.Collections.Generic;
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 脸的朝向
  6. /// </summary>
  7. public enum FaceDirection
  8. {
  9. Left,
  10. Right,
  11. }
  12.  
  13. /// <summary>
  14. /// 角色基类
  15. /// </summary>
  16. public class Role : KinematicBody2D
  17. {
  18. /// <summary>
  19. /// 重写的纹理
  20. /// </summary>
  21. [Export] public Texture Texture;
  22.  
  23. /// <summary>
  24. /// 移动速度
  25. /// </summary>
  26. [Export] public float MoveSpeed = 150f;
  27.  
  28. /// <summary>
  29. /// 所属阵营
  30. /// </summary>
  31. [Export] public CampEnum Camp;
  32.  
  33. /// <summary>
  34. /// 携带的道具包裹
  35. /// </summary>
  36. public List<object> PropsPack { get; } = new List<object>();
  37.  
  38. /// <summary>
  39. /// 角色携带的枪套
  40. /// </summary>
  41. public Holster Holster { get; private set; }
  42.  
  43. /// <summary>
  44. /// 动画播放器
  45. /// </summary>
  46. public AnimatedSprite AnimatedSprite { get; private set; }
  47. /// <summary>
  48. /// 武器挂载点
  49. /// </summary>
  50. public Position2D MountPoint { get; private set; }
  51. /// <summary>
  52. /// 背后武器的挂载点
  53. /// </summary>
  54. public Position2D BackMountPoint { get; private set; }
  55.  
  56. /// <summary>
  57. /// 脸的朝向
  58. /// </summary>
  59. public FaceDirection Face { get => _face; set => SetFace(value); }
  60. private FaceDirection _face;
  61.  
  62. private Vector2 StartScele;
  63. private readonly List<IProp> InteractiveItemList = new List<IProp>();
  64.  
  65. public override void _Ready()
  66. {
  67. StartScele = Scale;
  68. AnimatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  69. MountPoint = GetNode<Position2D>("MountPoint");
  70. BackMountPoint = GetNode<Position2D>("BackMountPoint");
  71. // 更改纹理
  72. ChangeFrameTexture(AnimatorNames.Idle, AnimatedSprite, Texture);
  73. ChangeFrameTexture(AnimatorNames.Run, AnimatedSprite, Texture);
  74. ChangeFrameTexture(AnimatorNames.ReverseRun, AnimatedSprite, Texture);
  75.  
  76. Holster = new Holster(this);
  77. Face = FaceDirection.Right;
  78. }
  79.  
  80. /// <summary>
  81. /// 拾起一个武器, 并且切换到这个武器
  82. /// </summary>
  83. /// <param name="gun">武器对象</param>
  84. public void PickUpGun(Gun gun)
  85. {
  86. Holster.PickupGun(gun);
  87. }
  88.  
  89. /// <summary>
  90. /// 切换到下一个武器
  91. /// </summary>
  92. public void TriggerExchangeNext()
  93. {
  94. Holster.ExchangeNext();
  95. }
  96.  
  97. /// <summary>
  98. /// 切换到上一个武器
  99. /// </summary>
  100. public void ExchangePrev()
  101. {
  102. Holster.ExchangePrev();
  103. }
  104.  
  105. /// <summary>
  106. /// 扔掉当前使用的武器, 切换到上一个武器
  107. /// </summary>
  108. public void TriggerThrowGun()
  109. {
  110. var gun = Holster.RmoveGun(Holster.ActiveIndex);
  111. //播放抛出效果
  112. if (gun != null)
  113. {
  114. if (Face == FaceDirection.Left)
  115. {
  116. gun.Scale *= new Vector2(1, -1);
  117. gun.RotationDegrees = 180;
  118. }
  119. gun.Position = Vector2.Zero;
  120. var temp = new ThrowGun();
  121. var startPos = GlobalPosition + new Vector2(0, 0);
  122. var startHeight = 6;
  123. var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-20, 20);
  124. var xf = 30;
  125. var yf = MathUtils.RandRangeInt(60, 120);
  126. var rotate = MathUtils.RandRangeInt(-180, 180);
  127. temp.InitThrow(new Vector2(20, 20), startPos, startHeight, direction, xf, yf, rotate, gun, gun.GunSprite);
  128. RoomManager.Current.SortRoot.AddChild(temp);
  129. }
  130. }
  131.  
  132. /// <summary>
  133. /// 返回是否存在可互动的物体
  134. /// </summary>
  135. public bool HasTnteractive()
  136. {
  137. return InteractiveItemList.Count > 0;
  138. }
  139.  
  140. /// <summary>
  141. /// 触发与碰撞的物体互动
  142. /// </summary>
  143. public void TriggerTnteractive()
  144. {
  145. if (HasTnteractive())
  146. {
  147. var item = InteractiveItemList[InteractiveItemList.Count - 1];
  148. item.Tnteractive(this);
  149. }
  150. }
  151.  
  152. /// <summary>
  153. /// 触发换弹
  154. /// </summary>
  155. public void TriggerReload()
  156. {
  157. if (Holster.ActiveGun != null)
  158. {
  159. Holster.ActiveGun._Reload();
  160. }
  161. }
  162.  
  163. /// <summary>
  164. /// 触发攻击
  165. /// </summary>
  166. public void TriggerAttack()
  167. {
  168. if (Holster.ActiveGun != null)
  169. {
  170. Holster.ActiveGun.Trigger();
  171. }
  172. }
  173.  
  174. /// <summary>
  175. /// 设置脸的朝向
  176. /// </summary>
  177. private void SetFace(FaceDirection face)
  178. {
  179. if (_face != face)
  180. {
  181. _face = face;
  182. if (face == FaceDirection.Right)
  183. {
  184. RotationDegrees = 0;
  185. Scale = StartScele;
  186. }
  187. else
  188. {
  189. RotationDegrees = 180;
  190. Scale = new Vector2(StartScele.x, -StartScele.y);
  191. }
  192. }
  193. }
  194.  
  195. /// <summary>
  196. /// 更改指定动画的纹理
  197. /// </summary>
  198. private void ChangeFrameTexture(string anim, AnimatedSprite animatedSprite, Texture texture)
  199. {
  200. SpriteFrames spriteFrames = animatedSprite.Frames as SpriteFrames;
  201. if (spriteFrames != null)
  202. {
  203. int count = spriteFrames.GetFrameCount(anim);
  204. for (int i = 0; i < count; i++)
  205. {
  206. AtlasTexture temp = spriteFrames.GetFrame(anim, i) as AtlasTexture;
  207. temp.Atlas = Texture;
  208. }
  209. }
  210. }
  211.  
  212. /// <summary>
  213. /// 连接信号: InteractiveArea.area_entered
  214. /// </summary>
  215. private void _OnPropsEnter(Area2D other)
  216. {
  217. if (other is Gun gun)
  218. {
  219. if (!InteractiveItemList.Contains(gun))
  220. {
  221. InteractiveItemList.Add(gun);
  222. }
  223. }
  224. }
  225.  
  226. /// <summary>
  227. /// 连接信号: InteractiveArea.area_exited
  228. /// </summary>
  229. private void _OnPropsExit(Area2D other)
  230. {
  231. if (other is Gun gun)
  232. {
  233. if (InteractiveItemList.Contains(gun))
  234. {
  235. InteractiveItemList.Remove(gun);
  236. }
  237. }
  238. }
  239.  
  240. }