Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / item / weapon / bullet / Bullet.cs
@小李xl 小李xl on 12 Nov 2022 1 KB 添加敌人视野
  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. MaxDistance = maxDistance;
  17. Position = position;
  18. Rotation = rotation;
  19. ShadowOffset = new Vector2(0, 5);
  20. }
  21.  
  22. public Area2D CollisionArea { get; }
  23. // 最大飞行距离
  24. private float MaxDistance;
  25.  
  26. // 子弹飞行速度
  27. private float FlySpeed = 450;
  28.  
  29. //当前子弹已经飞行的距离
  30. private float CurrFlyDistance = 0;
  31.  
  32. public override void _Ready()
  33. {
  34. base._Ready();
  35. //绘制阴影
  36. ShowShadowSprite();
  37. }
  38.  
  39. public override void _PhysicsProcess(float delta)
  40. {
  41. base._PhysicsProcess(delta);
  42. //移动
  43. Position += new Vector2(FlySpeed * delta, 0).Rotated(Rotation);
  44. //距离太大, 自动销毁
  45. CurrFlyDistance += FlySpeed * delta;
  46. if (CurrFlyDistance >= MaxDistance)
  47. {
  48. Destroy();
  49. }
  50. }
  51.  
  52. private void _BodyEntered(Node2D other)
  53. {
  54. if (other is Role role)
  55. {
  56. role.Hit(1);
  57. }
  58.  
  59. //播放受击动画
  60. Node2D hit = ResourceManager.Load<PackedScene>(ResourcePath.prefab_effect_Hit_tscn).Instance<Node2D>();
  61. hit.RotationDegrees = Utils.RandRangeInt(0, 360);
  62. hit.GlobalPosition = GlobalPosition;
  63. GameApplication.Instance.Room.GetRoot(true).AddChild(hit);
  64.  
  65. Destroy();
  66. }
  67. }