Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / bullet / Bullet.cs
  1. using Godot;
  2.  
  3. /// <summary>
  4. /// 子弹类
  5. /// </summary>
  6. public class Bullet : ActivityObject
  7. {
  8. public Bullet(string scenePath, float maxDistance, Vector2 position, float rotation, uint targetLayer) :
  9. base(scenePath)
  10. {
  11. CollisionArea = GetNode<Area2D>("CollisionArea");
  12. CollisionArea.CollisionMask = targetLayer;
  13. CollisionArea.Connect("body_entered", this, nameof(_BodyEntered));
  14.  
  15. Collision.Disabled = true;
  16.  
  17. MaxDistance = maxDistance;
  18. Position = position;
  19. Rotation = rotation;
  20. ShadowOffset = new Vector2(0, 5);
  21. }
  22.  
  23. public Area2D CollisionArea { get; }
  24.  
  25. // 最大飞行距离
  26. private float MaxDistance;
  27.  
  28. // 子弹飞行速度
  29. private float FlySpeed = 350;
  30.  
  31. //当前子弹已经飞行的距离
  32. private float CurrFlyDistance = 0;
  33.  
  34. public override void _Ready()
  35. {
  36. base._Ready();
  37. //绘制阴影
  38. ShowShadowSprite();
  39. }
  40.  
  41. public override void _PhysicsProcess(float delta)
  42. {
  43. base._PhysicsProcess(delta);
  44. //移动
  45. Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation);
  46. //距离太大, 自动销毁
  47. CurrFlyDistance += FlySpeed * delta;
  48. if (CurrFlyDistance >= MaxDistance)
  49. {
  50. Destroy();
  51. }
  52. }
  53.  
  54. private void _BodyEntered(Node2D other)
  55. {
  56. if (other is Role role)
  57. {
  58. role.Hurt(1);
  59. }
  60.  
  61. //播放受击动画
  62. // Node2D hit = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_Hit_tscn).Instance<Node2D>();
  63. // hit.RotationDegrees = Utils.RandRangeInt(0, 360);
  64. // hit.GlobalPosition = GlobalPosition;
  65. // GameApplication.Instance.Room.GetRoot(true).AddChild(hit);
  66.  
  67. SpecialEffectManager.Play(ResourcePath.resource_effects_Hit_tres, "default", GlobalPosition,
  68. Mathf.Deg2Rad(Utils.RandRangeInt(0, 360)), Vector2.One, new Vector2(1, 11), 0);
  69.  
  70. Destroy();
  71. }
  72. }