Newer
Older
DungeonShooting / src / role / Role.cs
@小李xl 小李xl on 17 May 2022 2 KB 第一次提交
  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 AnimatedSprite AnimatedSprite { get; private set; }
  42. /// <summary>
  43. /// 武器挂载点
  44. /// </summary>
  45. public Position2D MountPoint { get; private set; }
  46.  
  47. /// <summary>
  48. /// 脸的朝向
  49. /// </summary>
  50. public FaceDirection Face { get => _face; set => SetFace(value); }
  51. private FaceDirection _face;
  52.  
  53. private Vector2 StartScele;
  54.  
  55. public override void _Ready()
  56. {
  57. StartScele = Scale;
  58. AnimatedSprite = GetNode<AnimatedSprite>("AnimatedSprite");
  59. MountPoint = GetNode<Position2D>("MountPoint");
  60. // 更改纹理
  61. ChangeFrameTexture(AnimatorNames.Idle, AnimatedSprite, Texture);
  62. ChangeFrameTexture(AnimatorNames.Run, AnimatedSprite, Texture);
  63. ChangeFrameTexture(AnimatorNames.ReverseRun, AnimatedSprite, Texture);
  64.  
  65. Face = FaceDirection.Right;
  66. }
  67.  
  68. private void SetFace(FaceDirection face)
  69. {
  70. if (_face != face)
  71. {
  72. _face = face;
  73. if (face == FaceDirection.Right)
  74. {
  75. RotationDegrees = 0;
  76. Scale = StartScele;
  77. }
  78. else
  79. {
  80. RotationDegrees = 180;
  81. Scale = new Vector2(StartScele.x, -StartScele.y);
  82. }
  83. }
  84. }
  85.  
  86. /// <summary>
  87. /// 更改指定动画的纹理
  88. /// </summary>
  89. private void ChangeFrameTexture(string anim, AnimatedSprite animatedSprite, Texture texture)
  90. {
  91. SpriteFrames spriteFrames = animatedSprite.Frames as SpriteFrames;
  92. if (spriteFrames != null)
  93. {
  94. int count = spriteFrames.GetFrameCount(anim);
  95. for (int i = 0; i < count; i++)
  96. {
  97. AtlasTexture temp = spriteFrames.GetFrame(anim, i) as AtlasTexture;
  98. temp.Atlas = Texture;
  99. }
  100. }
  101. }
  102.  
  103. }