Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / currency / Gold.cs
  1.  
  2. using System.Collections.Generic;
  3. using Godot;
  4.  
  5. /// <summary>
  6. /// 金币类
  7. /// </summary>
  8. [Tool]
  9. public partial class Gold : ActivityObject, IPoolItem
  10. {
  11. /// <summary>
  12. /// 金币数量
  13. /// </summary>
  14. [Export]
  15. public int GoldCount { get; set; } = 1;
  16. public bool IsRecycled { get; set; }
  17. public string Logotype { get; set; }
  18. private float _maxSpeed = 250;
  19. private float _speed = 0;
  20. private Role _moveTarget;
  21. public override void OnInit()
  22. {
  23. DefaultLayer = RoomLayerEnum.YSortLayer;
  24. }
  25.  
  26. protected override void OnThrowOver()
  27. {
  28. var current = World.Player;
  29. if (current != null)
  30. {
  31. this.CallDelay(0.3f, () =>
  32. {
  33. _moveTarget = current;
  34. MoveController.Enable = false;
  35. });
  36. }
  37. }
  38.  
  39. protected override void Process(float delta)
  40. {
  41. if (_moveTarget != null && !_moveTarget.IsDestroyed)
  42. {
  43. var position = Position;
  44. var targetPosition = _moveTarget.Position;
  45. if (position.DistanceSquaredTo(targetPosition) < 3 * 3)
  46. {
  47. _moveTarget.AddGold(GoldCount);
  48. ObjectPool.Reclaim(this);
  49. }
  50. else
  51. {
  52. _speed = Mathf.MoveToward(_speed, _maxSpeed, _maxSpeed * delta);
  53. Position = position.MoveToward(targetPosition, _speed * delta);
  54. }
  55. }
  56. }
  57. public void OnReclaim()
  58. {
  59. GetParent().RemoveChild(this);
  60. _moveTarget = null;
  61. }
  62.  
  63. public void OnLeavePool()
  64. {
  65. _speed = 0;
  66. MoveController.Enable = true;
  67. MoveController.ClearForce();
  68. MoveController.SetAllVelocity(Vector2.Zero);
  69. }
  70. /// <summary>
  71. /// 创建散落的金币
  72. /// </summary>
  73. /// <param name="position">位置</param>
  74. /// <param name="count">金币数量</param>
  75. /// <param name="force">投抛力度</param>
  76. public static List<Gold> CreateGold(Vector2 position, int count, int force = 10)
  77. {
  78. var list = new List<Gold>();
  79. var goldList = Utils.GetGoldList(count);
  80. foreach (var id in goldList)
  81. {
  82. var o = ObjectManager.GetActivityObject<Gold>(id);
  83. o.Position = position;
  84. o.Throw(0,
  85. Utils.Random.RandomRangeInt(5 * force, 11 * force),
  86. new Vector2(Utils.Random.RandomRangeInt(-2 * force, 2 * force), Utils.Random.RandomRangeInt(-2 * force, 2 * force)),
  87. 0
  88. );
  89. list.Add(o);
  90. }
  91.  
  92. return list;
  93. }
  94. }