Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / bullet / normal / Arrow.cs
@小李xl 小李xl on 20 Feb 2024 3 KB 弓箭功能开发中
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 弓箭
  6. /// </summary>
  7. [Tool]
  8. public partial class Arrow : Bullet, IMountItem
  9. {
  10. [Export, ExportFillNode]
  11. public AnimatedSprite2D HalfSprite { get; set; }
  12.  
  13. public override void InitData(BulletData data, uint attackLayer)
  14. {
  15. base.InitData(data, attackLayer);
  16. SetEnableMovement(true);
  17. EnableVerticalMotion = false;
  18. DefaultLayer = RoomLayerEnum.NormalLayer;
  19. Debug.Log("IsFallOver: ", IsFallOver, ", ", CollisionLayer);
  20. }
  21.  
  22. public override CheckInteractiveResult CheckInteractive(ActivityObject master)
  23. {
  24. if (master is Role role) //如果角色有弓箭武器, 则可以拾起地上的箭
  25. {
  26. var index = role.WeaponPack.FindIndex((weapon, index) =>
  27. {
  28. return weapon.ActivityBase.Id == Ids.Id_weapon0016;
  29. });
  30. if (index >= 0)
  31. {
  32. var weapon = role.WeaponPack.GetItem(index);
  33. weapon.SetResidueAmmo(weapon.ResidueAmmo + 1);
  34. ObjectPool.Reclaim(this);
  35. }
  36. }
  37. return base.CheckInteractive(master);
  38. }
  39.  
  40. public override void OnCollisionTarget(IHurt hurt)
  41. {
  42. base.OnCollisionTarget(hurt);
  43. var activityObject = hurt.GetActivityObject();
  44. if (activityObject != null)
  45. {
  46. CallDeferred(nameof(OnBindTarget), activityObject);
  47. }
  48. }
  49.  
  50. public override void LogicalFinish()
  51. {
  52. if (State == BulletStateEnum.CollisionTarget) //碰撞到目标, 直接冻结
  53. {
  54. SetEnableMovement(false);
  55. var slideCollision = GetLastSlideCollision();
  56. if (slideCollision != null)
  57. {
  58. Position -= slideCollision.GetTravel();
  59. }
  60. }
  61. else if (State == BulletStateEnum.FallToGround) //落地, 啥也不干
  62. {
  63. }
  64. else
  65. {
  66. //Debug.Log("碰撞速度: " + Velocity.Length());
  67. base.LogicalFinish();
  68. }
  69. }
  70.  
  71. //将弓箭挂载到目标物体上
  72. private void OnBindTarget(ActivityObject activityObject)
  73. {
  74. if (activityObject.IsDestroyed)
  75. {
  76. OnUnmount(activityObject);
  77. }
  78. else
  79. {
  80. Altitude = Mathf.Max(1, -activityObject.ToLocal(GlobalPosition).Y);
  81. activityObject.AddMountObject(this);
  82. }
  83. }
  84.  
  85. public void OnMount(ActivityObject target)
  86. {
  87. Reparent(target);
  88. AnimatedSprite.Play(AnimatorNames.HalfEnd);
  89. HalfSprite.Visible = true;
  90. }
  91.  
  92. public void OnUnmount(ActivityObject target)
  93. {
  94. SetOriginCollisionLayerValue(PhysicsLayer.Prop, true);
  95. AnimatedSprite.Play(AnimatorNames.Default);
  96. HalfSprite.Visible = false;
  97. SetEnableMovement(true);
  98. EnableVerticalMotion = true;
  99. MoveController.ClearForce();
  100. MoveController.BasisVelocity = Vector2.Zero;
  101. ShadowOffset = new Vector2(0, 1);
  102. Throw(Mathf.Max(3, Altitude), Utils.Random.RandomRangeInt(50, 80), Vector2.Zero, Utils.Random.RandomRangeInt(-30, 30));
  103. InheritVelocity(target);
  104. }
  105.  
  106. public override void OnLeavePool()
  107. {
  108. SetOriginCollisionLayerValue(PhysicsLayer.Prop, false);
  109. if (Utils.CollisionMaskWithLayer(CollisionLayer, PhysicsLayer.Prop))
  110. {
  111. CollisionLayer ^= PhysicsLayer.Prop;
  112. }
  113. base.OnLeavePool();
  114. }
  115. }