Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using Controllers;
- using SO;
- using UnityEngine;
- using UnityEngine.UI;
- namespace Effects {
- public static class EffectsController {
- private static EffectsSO _effects = MainDatabase.Effects;
- private static int _id = 0;
- private static Dictionary<int, GameObject> _activeGO = new Dictionary<int, GameObject>();
- private static Dictionary<int, IEffectController> _activeEffects = new Dictionary<int, IEffectController>();
- //////////////////////////// Add effect at point ////////////////////////////
- public static int StartEffect(Vector3 position, EffectHolder effect) {
- return StartEffect(position, Quaternion.identity, effect);
- }
- public static int StartEffect(Vector3 position, Vector3 direction, EffectHolder effect) {
- return StartEffect(position, Quaternion.LookRotation(direction), effect);
- }
- public static int StartEffect(Vector3 position, Quaternion rotation, EffectHolder effect) {
- if (effect == null) return -1;
- var go = _effects.GetEffect(effect).Prefab;
- if (go == null) return -1;
- go = Object.Instantiate(go, position, rotation);
- var iec = go.GetComponent<IEffectController>();
- _activeGO.Add(_id, go);
- _activeEffects.Add(_id, iec);
- _id++;
- return _id;
- }
- //////////////////////////// Add effect to Transform ////////////////////////////
- public static int StartEffect(Transform target, EffectHolder effect) {
- return StartEffect(target, Vector3.zero, Quaternion.identity, effect);
- }
- public static int StartEffect(Transform target, Vector3 offset, Vector3 direction, EffectHolder effect) {
- return StartEffect(target, offset, Quaternion.LookRotation(direction), effect);
- }
- public static int StartEffect(Transform target, Vector3 offset, EffectHolder effect) {
- return StartEffect(target, offset, Quaternion.identity, effect);
- }
- public static int StartEffect(Transform target, Vector3 offset, Quaternion rotation, EffectHolder effect) {
- if (effect == null) return -1;
- var go = _effects.GetEffect(effect).Prefab;
- if (go == null) return -1;
- go = Object.Instantiate(go, target);
- go.transform.localRotation = rotation;
- go.transform.localPosition = offset;
- var iec = go.GetComponent<IEffectController>();
- _activeGO.Add(_id, go);
- _activeEffects.Add(_id, iec);
- _id++;
- return _id;
- }
- //////////////////////////// Stop effect ////////////////////////////
- public static void StopEffect(int id, bool remove = true) {
- if (id == -1) return;
- if (_activeEffects.ContainsKey(id)) {
- if (_activeEffects[id] != null) {
- _activeEffects[id].Stop();
- if (remove) {
- _activeEffects[id].Destroy();
- _activeEffects.Remove(id);
- _activeGO.Remove(id);
- }
- }
- else {
- Object.Destroy(_activeGO[id]);
- _activeGO.Remove(id);
- _activeEffects.Remove(id);
- }
- }
- }
- public static void RemoveAllEffects(bool force = true) {
- foreach (var o in _activeGO) {
- if (!force) StopEffect(o.Key);
- else Object.Destroy(o.Value);
- }
- _activeGO.Clear();
- _activeEffects.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement