SFX_CollisionBasedFxInstancer.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Serialization;
  4. // ReSharper disable once CheckNamespace
  5. namespace QFX.SFX
  6. {
  7. [RequireComponent(typeof(SFX_ICollisionsProvider))]
  8. public class SFX_CollisionBasedFxInstancer : SFX_ControlledObject
  9. {
  10. public SFX_FxObject[] FxObjects;
  11. private bool _wasCollided;
  12. private void Awake()
  13. {
  14. var collisionProviders = GetComponents<SFX_ICollisionsProvider>();
  15. foreach (var collisionsProvider in collisionProviders)
  16. {
  17. collisionsProvider.OnCollision += delegate(SFX_CollisionPoint collisionPoint)
  18. {
  19. if (_wasCollided)
  20. return;
  21. var targetRotation = Vector3.zero;
  22. foreach (var fxObject in FxObjects)
  23. {
  24. switch (fxObject.FxRotation)
  25. {
  26. case SFX_FxRotationType.Default:
  27. break;
  28. case SFX_FxRotationType.Normal:
  29. targetRotation = collisionPoint.Normal;
  30. break;
  31. case SFX_FxRotationType.LookAtEmitter:
  32. targetRotation = GetComponent<SFX_IEmitterKeeper>().EmitterTransform.position;
  33. break;
  34. default:
  35. throw new ArgumentOutOfRangeException();
  36. }
  37. SFX_FxObjectInstancer.InstantiateFx(fxObject, collisionPoint.Point, targetRotation);
  38. }
  39. _wasCollided = true;
  40. };
  41. }
  42. }
  43. }
  44. }