Newer
Older
DungeonShooting / src / role / Player.cs
@gamemusicstory gamemusicstory on 12 Jun 2022 4 KB 移动 添加 摩擦力
  1. using Godot;
  2.  
  3. public class Player : Role
  4. {
  5. /// <summary>
  6. /// 移动加速度
  7. /// </summary>
  8. public float Acceleration = 1500f;
  9.  
  10. /// <summary>
  11. /// 移动摩擦力
  12. /// </summary>
  13. public float Friction = 800f;
  14. /// <summary>
  15. /// 移动速度
  16. /// </summary>
  17. public Vector2 Velocity = Vector2.Zero;
  18.  
  19. /// <summary>
  20. /// 当前的枪
  21. /// </summary>
  22. public Gun CurrGun { get; private set; }
  23.  
  24. [Export] public PackedScene GunPrefab;
  25.  
  26. public override void _Ready()
  27. {
  28. base._Ready();
  29.  
  30. //加载枪
  31. var gun = GunPrefab.Instance<Gun>();
  32. MountPoint.AddChild(gun);
  33. CurrGun = gun;
  34.  
  35. var attr = new GunAttribute();
  36. attr.StartFiringSpeed = 480;
  37. attr.StartScatteringRange = 5;
  38. attr.FinalScatteringRange = 60;
  39. attr.ScatteringRangeAddValue = 2f;
  40. attr.ScatteringRangeBackSpeed = 40;
  41. //连发
  42. attr.ContinuousShoot = true;
  43. //扳机检测间隔
  44. attr.TriggerInterval = 0f;
  45. //连发数量
  46. attr.MinContinuousCount = 3;
  47. attr.MaxContinuousCount = 3;
  48. //开火前延时
  49. attr.DelayedTime = 0f;
  50. //攻击距离
  51. attr.MinDistance = 500;
  52. attr.MaxDistance = 600;
  53. //发射子弹数量
  54. attr.MinFireBulletCount = 1;
  55. attr.MaxFireBulletCount = 1;
  56. //抬起角度
  57. attr.UpliftAngle = 10;
  58. //枪身长度
  59. attr.FirePosition = new Vector2(16, 1.5f);
  60. attr.Sprite = "res://resource/sprite/gun/gun4.png";
  61. gun.Init(attr);
  62. gun.FireEvent += FireEvent_Func;
  63.  
  64. RoomManager.Current.Cursor.TargetGun = gun;
  65. }
  66.  
  67. public override void _Process(float delta)
  68. {
  69. base._Process(delta);
  70.  
  71. Vector2 mousePos = GetGlobalMousePosition();
  72. //枪口跟随鼠标
  73. MountPoint.LookAt(mousePos);
  74. //脸的朝向
  75. if (mousePos.x > GlobalPosition.x && Face == FaceDirection.Left)
  76. {
  77. Face = FaceDirection.Right;
  78. }
  79. else if (mousePos.x < GlobalPosition.x && Face == FaceDirection.Right)
  80. {
  81. Face = FaceDirection.Left;
  82. }
  83. //攻击
  84. Attack();
  85. }
  86.  
  87. public override void _PhysicsProcess(float delta)
  88. {
  89. base._PhysicsProcess(delta);
  90. Move(delta);
  91. //播放动画
  92. PlayAnim();
  93. }
  94.  
  95. private void Move(float delta)
  96. {
  97. //角色移动
  98. // var dir = new Vector2(
  99. // Input.GetActionRawStrength("move_right") - Input.GetActionRawStrength("move_left"),
  100. // Input.GetActionRawStrength("move_down") - Input.GetActionRawStrength("move_up")
  101. // ).Normalized();
  102. // if (dir.x * Velocity.x < 0)
  103. // {
  104. // Velocity.x = 0;
  105. // }
  106. // if (dir.y * Velocity.y < 0)
  107. // {
  108. // Velocity.y = 0;
  109. // }
  110. // Velocity = Velocity.MoveToward(dir * MoveSpeed, Acceleration * delta);
  111. // 得到输入的 vector2 getvector方法返回值已经归一化过了noemalized
  112. Vector2 dir = Input.GetVector("move_left", "move_right", "move_up", "move_down");
  113. // 移动. 如果移动的数值接近0(是用 摇杆可能出现 方向 可能会出现浮点),就fricition的值 插值 到 0
  114. // 如果 有输入 就以当前速度,用acceleration 插值到 对应方向 * 最大速度
  115. if (Mathf.IsZeroApprox(dir.x)) Velocity.x = Mathf.MoveToward(Velocity.x, 0, Friction * delta);
  116. else Velocity.x = Mathf.MoveToward(Velocity.x, dir.x * MoveSpeed, Acceleration * delta);
  117.  
  118. if (Mathf.IsZeroApprox(dir.y)) Velocity.y = Mathf.MoveToward(Velocity.y, 0, Friction * delta);
  119. else Velocity.y = Mathf.MoveToward(Velocity.y, dir.y * MoveSpeed, Acceleration * delta);
  120.  
  121. Velocity = MoveAndSlide(Velocity);
  122. }
  123.  
  124. private void Attack()
  125. {
  126. if (Input.IsActionPressed("fire"))
  127. {
  128. CurrGun.Trigger();
  129. }
  130. }
  131.  
  132. // 播放动画
  133. private void PlayAnim()
  134. {
  135. if (Velocity != Vector2.Zero)
  136. {
  137. if ((Face == FaceDirection.Right && Velocity.x >= 0) || Face == FaceDirection.Left && Velocity.x <= 0) //向前走
  138. {
  139. AnimatedSprite.Animation = AnimatorNames.Run;
  140. }
  141. else if ((Face == FaceDirection.Right && Velocity.x < 0) || Face == FaceDirection.Left && Velocity.x > 0) //向后走
  142. {
  143. AnimatedSprite.Animation = AnimatorNames.ReverseRun;
  144. }
  145. }
  146. else
  147. {
  148. AnimatedSprite.Animation = AnimatorNames.Idle;
  149. }
  150. }
  151.  
  152. //开火后
  153. private void FireEvent_Func(Gun gun)
  154. {
  155.  
  156. }
  157. }