Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / currency / Gold.cs
  1.  
  2. using Godot;
  3.  
  4. /// <summary>
  5. /// 金币类
  6. /// </summary>
  7. [Tool]
  8. public partial class Gold : ActivityObject, IPoolItem
  9. {
  10. /// <summary>
  11. /// 金币数量
  12. /// </summary>
  13. [Export]
  14. public int GoldCount { get; set; } = 1;
  15. public bool IsRecycled { get; set; }
  16. public string Logotype { get; set; }
  17. private float _maxSpeed = 250;
  18. private float _speed = 0;
  19. private Role _moveTarget;
  20. public override void OnInit()
  21. {
  22. DefaultLayer = RoomLayerEnum.YSortLayer;
  23. }
  24.  
  25. protected override void OnThrowOver()
  26. {
  27. var current = Player.Current;
  28. if (current != null)
  29. {
  30. this.CallDelay(0.3f, () =>
  31. {
  32. _moveTarget = current;
  33. MoveController.Enable = false;
  34. });
  35. }
  36. }
  37.  
  38. protected override void Process(float delta)
  39. {
  40. if (_moveTarget != null && !_moveTarget.IsDestroyed)
  41. {
  42. var position = Position;
  43. var targetPosition = _moveTarget.Position;
  44. if (position.DistanceSquaredTo(targetPosition) < 3 * 3)
  45. {
  46. _moveTarget.AddGold(GoldCount);
  47. ObjectPool.Reclaim(this);
  48. }
  49. else
  50. {
  51. _speed = Mathf.MoveToward(_speed, _maxSpeed, _maxSpeed * delta);
  52. Position = position.MoveToward(targetPosition, _speed * delta);
  53. }
  54. }
  55. }
  56. public void OnReclaim()
  57. {
  58. GetParent().RemoveChild(this);
  59. _moveTarget = null;
  60. }
  61.  
  62. public void OnLeavePool()
  63. {
  64. _speed = 0;
  65. MoveController.Enable = true;
  66. MoveController.ClearForce();
  67. MoveController.SetAllVelocity(Vector2.Zero);
  68. }
  69. }