SFX_HealingAreaController.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. // ReSharper disable once CheckNamespace
  5. namespace QFX.SFX
  6. {
  7. public class SFX_HealingAreaController : SFX_ControlledObject
  8. {
  9. public GameObject HealingAreaFx;
  10. public GameObject HealingFx;
  11. public SFX_ObjectFinder ObjectsFinder;
  12. private readonly Dictionary<GameObject, ParticleSystem>
  13. _healableObjectsInArea = new Dictionary<GameObject, ParticleSystem>();
  14. public override void Run()
  15. {
  16. base.Run();
  17. var healingAreaGo = Instantiate(HealingAreaFx, transform.position, transform.rotation);
  18. healingAreaGo.transform.parent = transform;
  19. }
  20. private void FixedUpdate()
  21. {
  22. if (!IsRunning)
  23. return;
  24. var healableObjects = ObjectsFinder.FindObjects(transform.position);
  25. foreach (var healable in healableObjects)
  26. {
  27. var healableGo = healable.gameObject;
  28. if (!_healableObjectsInArea.ContainsKey(healableGo))
  29. {
  30. var offset = new Vector3(0, healable.bounds.center.y, 0);
  31. var targetPosition = healable.transform.position + offset;
  32. var healingGo = Instantiate(HealingFx, targetPosition, Quaternion.identity);
  33. healingGo.transform.parent = healable.transform;
  34. var healingPs = healingGo.GetComponent<ParticleSystem>();
  35. _healableObjectsInArea[healableGo] = healingPs;
  36. SFX_InvokeUtil.RunLater(this, delegate
  37. {
  38. healingPs.Stop();
  39. Destroy(healingPs.gameObject, 1f);
  40. _healableObjectsInArea.Remove(healableGo);
  41. }, healingPs.main.duration);
  42. }
  43. }
  44. }
  45. }
  46. }