diff --git a/MissileCommander/Assets/_Scripts/Bullet.cs b/MissileCommander/Assets/_Scripts/Bullet.cs index b4f7ead..ed0ba96 100644 --- a/MissileCommander/Assets/_Scripts/Bullet.cs +++ b/MissileCommander/Assets/_Scripts/Bullet.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System; +using UnityEngine; namespace MissileCommander { @@ -8,17 +9,38 @@ public class Bullet : RecyclableObject private Transform _cachedTransform; private Transform CachedTransform => _cachedTransform ? _cachedTransform : (_cachedTransform = transform); + private Vector3 _targetPosition; + [SerializeField] private float sqrApproximateOffset = 0.01f; + private bool _isActivated = false; + public event Action OnDestroyed = delegate { }; private void Update() { + if (!_isActivated) { return; } + CachedTransform.position += Time.deltaTime * moveSpeed * transform.up; + + if (IsArrivedToTarget()) + { + _isActivated = false; + OnDestroyed(this); + } } public void Activate(Vector3 startPos, Vector3 targetPos) { + this._targetPosition = targetPos; + Vector3 direction = (targetPos - startPos).normalized; CachedTransform.SetPositionAndRotation(startPos, Quaternion.LookRotation(CachedTransform.forward, direction)); + _isActivated = true; + } + + private bool IsArrivedToTarget() + { + float sqrDist = (_targetPosition - CachedTransform.position).sqrMagnitude; + return sqrDist < sqrApproximateOffset; } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/BulletLauncher.cs b/MissileCommander/Assets/_Scripts/BulletLauncher.cs index 3dd2007..904e9ed 100644 --- a/MissileCommander/Assets/_Scripts/BulletLauncher.cs +++ b/MissileCommander/Assets/_Scripts/BulletLauncher.cs @@ -20,6 +20,13 @@ public void OnFireButtonPressed(Vector3 mousePosition) // Instantiate Bullet Bullet bullet = _bulletFactory.Get() as Bullet; bullet.Activate(firePosition.position, mousePosition); + bullet.OnDestroyed += OnBulletDestroyed; + } + + public void OnBulletDestroyed(Bullet usedBullet) + { + usedBullet.OnDestroyed -= OnBulletDestroyed; + _bulletFactory.ReturnToPool(usedBullet); } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/GameManager.cs b/MissileCommander/Assets/_Scripts/GameManager.cs index a4eb905..6671188 100644 --- a/MissileCommander/Assets/_Scripts/GameManager.cs +++ b/MissileCommander/Assets/_Scripts/GameManager.cs @@ -19,7 +19,7 @@ private void Awake() _launcher = Instantiate(launcherPrefab); MouseGameController mouseController = gameObject.AddComponent(); - mouseController.FireButtonPressed += _launcher.OnFireButtonPressed; + mouseController.OnFireButtonPressed += _launcher.OnFireButtonPressed; } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/MouseGameController.cs b/MissileCommander/Assets/_Scripts/MouseGameController.cs index af43136..6167999 100644 --- a/MissileCommander/Assets/_Scripts/MouseGameController.cs +++ b/MissileCommander/Assets/_Scripts/MouseGameController.cs @@ -6,7 +6,7 @@ namespace MissileCommander public class MouseGameController : MonoBehaviour, IGameController { private Camera _mainCamera; - public event Action FireButtonPressed = delegate { }; + public event Action OnFireButtonPressed = delegate { }; private void Awake() { @@ -17,7 +17,7 @@ private void Update() { if (Input.GetMouseButtonDown(0)) { - FireButtonPressed(GetClickPosition(Input.mousePosition)); + OnFireButtonPressed(GetClickPosition(Input.mousePosition)); } }