Advertisement
KpoKec

Untitled

Jul 25th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using Controllers;
  3. using SO;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. namespace Effects {
  8.     public static class EffectsController {
  9.         private static EffectsSO _effects = MainDatabase.Effects;
  10.  
  11.         private static int                                _id            = 0;
  12.         private static Dictionary<int, GameObject>        _activeGO      = new Dictionary<int, GameObject>();
  13.         private static Dictionary<int, IEffectController> _activeEffects = new Dictionary<int, IEffectController>();
  14.  
  15. //////////////////////////// Add effect at point ////////////////////////////
  16.  
  17.         public static int StartEffect(Vector3 position, EffectHolder effect) {
  18.             return StartEffect(position, Quaternion.identity, effect);
  19.         }
  20.  
  21.         public static int StartEffect(Vector3 position, Vector3 direction, EffectHolder effect) {
  22.             return StartEffect(position, Quaternion.LookRotation(direction), effect);
  23.         }
  24.  
  25.         public static int StartEffect(Vector3 position, Quaternion rotation, EffectHolder effect) {
  26.             if (effect == null) return -1;
  27.             var go = _effects.GetEffect(effect).Prefab;
  28.             if (go == null) return -1;
  29.             go = Object.Instantiate(go, position, rotation);
  30.             var iec = go.GetComponent<IEffectController>();
  31.             _activeGO.Add(_id, go);
  32.             _activeEffects.Add(_id, iec);
  33.             _id++;
  34.             return _id;
  35.         }
  36.  
  37. //////////////////////////// Add effect to Transform ////////////////////////////
  38.         public static int StartEffect(Transform target, EffectHolder effect) {
  39.             return StartEffect(target, Vector3.zero, Quaternion.identity, effect);
  40.         }
  41.  
  42.         public static int StartEffect(Transform target, Vector3 offset, Vector3 direction, EffectHolder effect) {
  43.             return StartEffect(target, offset, Quaternion.LookRotation(direction), effect);
  44.         }
  45.  
  46.         public static int StartEffect(Transform target, Vector3 offset, EffectHolder effect) {
  47.             return StartEffect(target, offset, Quaternion.identity, effect);
  48.         }
  49.  
  50.         public static int StartEffect(Transform target, Vector3 offset, Quaternion rotation, EffectHolder effect) {
  51.             if (effect == null) return -1;
  52.             var go = _effects.GetEffect(effect).Prefab;
  53.             if (go == null) return -1;
  54.             go                         = Object.Instantiate(go, target);
  55.             go.transform.localRotation = rotation;
  56.             go.transform.localPosition = offset;
  57.             var iec = go.GetComponent<IEffectController>();
  58.             _activeGO.Add(_id, go);
  59.             _activeEffects.Add(_id, iec);
  60.             _id++;
  61.             return _id;
  62.         }
  63.  
  64. //////////////////////////// Stop effect ////////////////////////////
  65.         public static void StopEffect(int id, bool remove = true) {
  66.             if (id == -1) return;
  67.             if (_activeEffects.ContainsKey(id)) {
  68.                 if (_activeEffects[id] != null) {
  69.                     _activeEffects[id].Stop();
  70.                     if (remove) {
  71.                         _activeEffects[id].Destroy();
  72.                         _activeEffects.Remove(id);
  73.                         _activeGO.Remove(id);
  74.                     }
  75.                 }
  76.                 else {
  77.                     Object.Destroy(_activeGO[id]);
  78.                     _activeGO.Remove(id);
  79.                     _activeEffects.Remove(id);
  80.                 }
  81.             }
  82.         }
  83.  
  84.         public static void RemoveAllEffects(bool force = true) {
  85.             foreach (var o in _activeGO) {
  86.                 if (!force) StopEffect(o.Key);
  87.                 else Object.Destroy(o.Value);
  88.             }
  89.             _activeGO.Clear();
  90.             _activeEffects.Clear();
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement