Skip to content

Commit

Permalink
[Update] Update fire logic with pooling behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Nov 28, 2019
1 parent 9947399 commit ef69db9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
24 changes: 23 additions & 1 deletion MissileCommander/Assets/_Scripts/Bullet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using UnityEngine;
using System;
using UnityEngine;

namespace MissileCommander
{
Expand All @@ -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<Bullet> 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;
}
}
}
7 changes: 7 additions & 0 deletions MissileCommander/Assets/_Scripts/BulletLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion MissileCommander/Assets/_Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private void Awake()
_launcher = Instantiate(launcherPrefab);

MouseGameController mouseController = gameObject.AddComponent<MouseGameController>();
mouseController.FireButtonPressed += _launcher.OnFireButtonPressed;
mouseController.OnFireButtonPressed += _launcher.OnFireButtonPressed;
}
}
}
4 changes: 2 additions & 2 deletions MissileCommander/Assets/_Scripts/MouseGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace MissileCommander
public class MouseGameController : MonoBehaviour, IGameController
{
private Camera _mainCamera;
public event Action<Vector3> FireButtonPressed = delegate { };
public event Action<Vector3> OnFireButtonPressed = delegate { };

private void Awake()
{
Expand All @@ -17,7 +17,7 @@ private void Update()
{
if (Input.GetMouseButtonDown(0))
{
FireButtonPressed(GetClickPosition(Input.mousePosition));
OnFireButtonPressed(GetClickPosition(Input.mousePosition));
}
}

Expand Down

0 comments on commit ef69db9

Please sign in to comment.