Newer
Older
DungeonShooting / 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.  
  64. public override void _Ready()
  65. {
  66. StartScele = Scale;
  67. AnimatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  68. MountPoint = GetNode<Position2D>("MountPoint");
  69. BackMountPoint = GetNode<Position2D>("BackMountPoint");
  70. // 更改纹理
  71. ChangeFrameTexture(AnimatorNames.Idle, AnimatedSprite, Texture);
  72. ChangeFrameTexture(AnimatorNames.Run, AnimatedSprite, Texture);
  73. ChangeFrameTexture(AnimatorNames.ReverseRun, AnimatedSprite, Texture);
  74.  
  75. Holster = new Holster(this);
  76. Face = FaceDirection.Right;
  77. }
  78.  
  79. /// <summary>
  80. /// 拾起一个武器, 并且切换到这个武器
  81. /// </summary>
  82. /// <param name="gun">武器对象</param>
  83. public void PickUpGun(Gun gun)
  84. {
  85. Holster.PickupGun(gun);
  86. }
  87.  
  88. /// <summary>
  89. /// 切换到下一个武器
  90. /// </summary>
  91. public void ExchangeNext()
  92. {
  93. Holster.ExchangeNext();
  94. }
  95.  
  96. /// <summary>
  97. /// 切换到上一个武器
  98. /// </summary>
  99. public void ExchangePrev()
  100. {
  101. Holster.ExchangePrev();
  102. }
  103.  
  104. /// <summary>
  105. /// 扔掉当前使用的武器, 切换到上一个武器
  106. /// </summary>
  107. public void ThrowGun()
  108. {
  109. var gun = Holster.RmoveGun(Holster.ActiveIndex);
  110. //播放抛出效果
  111. if (gun != null)
  112. {
  113. if (Face == FaceDirection.Left) {
  114. gun.Scale *= new Vector2(1, -1);
  115. gun.RotationDegrees = 180;
  116. }
  117. gun.Position = Vector2.Zero;
  118. var temp = new ThrowGun();
  119. var startPos = GlobalPosition + new Vector2(0, 0);
  120. var startHeight = 6;
  121. var direction = GlobalRotationDegrees + MathUtils.RandRangeInt(-20, 20);
  122. var xf = 30;
  123. var yf = MathUtils.RandRangeInt(60, 120);
  124. var rotate = MathUtils.RandRangeInt(-180, 180);
  125. temp.InitThrow(new Vector2(16, 7), startPos, startHeight, direction, xf, yf, rotate, gun, gun.GunSprite);
  126. RoomManager.Current.ObjectRoot.AddChild(temp);
  127. }
  128. }
  129.  
  130. private void SetFace(FaceDirection face)
  131. {
  132. if (_face != face)
  133. {
  134. _face = face;
  135. if (face == FaceDirection.Right)
  136. {
  137. RotationDegrees = 0;
  138. Scale = StartScele;
  139. }
  140. else
  141. {
  142. RotationDegrees = 180;
  143. Scale = new Vector2(StartScele.x, -StartScele.y);
  144. }
  145. }
  146. }
  147.  
  148. /// <summary>
  149. /// 更改指定动画的纹理
  150. /// </summary>
  151. private void ChangeFrameTexture(string anim, AnimatedSprite animatedSprite, Texture texture)
  152. {
  153. SpriteFrames spriteFrames = animatedSprite.Frames as SpriteFrames;
  154. if (spriteFrames != null)
  155. {
  156. int count = spriteFrames.GetFrameCount(anim);
  157. for (int i = 0; i < count; i++)
  158. {
  159. AtlasTexture temp = spriteFrames.GetFrame(anim, i) as AtlasTexture;
  160. temp.Atlas = Texture;
  161. }
  162. }
  163. }
  164.  
  165. }