using System.Collections; using System.Collections.Generic; using UnityEngine; using wildlogicgames; namespace DoomBreakers { [RequireComponent(typeof(SpriteRenderer))] public class BaseFX :MonoBehaviour { protected Transform _transform; protected Vector3 _velocity; protected Animator _animator; protected SpriteRenderer _spriteRenderer; protected int _direction; protected float _targetVelocityX, _targetVelocityY; protected Color _colour; protected float _alpha; protected float _alphaFreq; protected bool _applyAlphaFadeFlag; protected bool _process; private GameObject timerObj; private ITimer _optimizeTimer; //Use this to disable the BaseFX object after specified wait time. //public BaseFX(int direction) => Setup(direction); public virtual void Setup(int direction) { _transform = this.transform; _velocity = Vector3.zero; _direction = direction; _targetVelocityX = 0f; _targetVelocityY = 0f; _spriteRenderer = this.GetComponent(); //_animator = this.GetComponent(); //_animator.Play(""); //FXController.controller _alpha = 1.0f; _alphaFreq = 0.005f; _applyAlphaFadeFlag = true; if (_spriteRenderer != null) { _colour = _spriteRenderer.color; _colour.a = _alpha; _spriteRenderer.color = _colour; } FlipSprite(); timerObj = new GameObject(); timerObj.AddComponent(); _optimizeTimer = timerObj.GetComponent(); //_optimizeTimer = new Timer(); _optimizeTimer.StartTimer(10.0f); } public virtual void SetDirection(int dir) //=> _direction = dir; { Setup(dir); if (!this.gameObject.activeSelf) this.gameObject.SetActive(true); _process = true; } private void OnDisable() { _process = false; } void Awake() { } void Start() { } public virtual void Update() { if (!_process) return; if (_optimizeTimer.HasTimerFinished()) { _process = false; //reset alpha for reuse in object pool _alpha = 1.0f; _colour.a = _alpha; _spriteRenderer.color = _colour; this.gameObject.SetActive(false); return; } ApplyTransparency(); _transform.Translate(_velocity * Time.deltaTime); if (_alpha < 0.05f) { _process = false; //reset alpha for reuse in object pool _alpha = 1.0f; _colour.a = _alpha; _spriteRenderer.color = _colour; this.gameObject.SetActive(false); } } public virtual void FlipSprite() { if (_direction == -1) //Moving Left { if(!_spriteRenderer.flipX) _spriteRenderer.flipX = true; return; } if (_direction == 1) //Moving Right { if (_spriteRenderer.flipX) _spriteRenderer.flipX = false; return; } } public virtual void ApplyTransparency() { if (!_applyAlphaFadeFlag) return; _alpha -= _alphaFreq; _colour.a = _alpha; _spriteRenderer.color = _colour; } } }