Skip to content

Commit

Permalink
[Update] Add bullet launch delay
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Nov 29, 2019
1 parent ef69db9 commit d30a0cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions MissileCommander/Assets/Game/Core/BulletLauncher.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,4 @@ MonoBehaviour:
bulletPrefab: {fileID: 649290798713983650, guid: f0b0296c96dfd8b46b8397a3a4160464,
type: 3}
firePosition: {fileID: 787914998351899525}
fireDelay: 0.5
20 changes: 20 additions & 0 deletions MissileCommander/Assets/_Scripts/BulletLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class BulletLauncher : MonoBehaviour
{
[SerializeField] private Bullet bulletPrefab;
[SerializeField] private Transform firePosition;
[SerializeField] private float fireDelay = 0.5f;
private float _elapsedFireTime;
private bool _canShoot = true;

private Factory _bulletFactory;

Expand All @@ -15,12 +18,29 @@ private void Awake()
_bulletFactory = new Factory(bulletPrefab);
}

private void Update()
{
if (!_canShoot)
{
_elapsedFireTime += Time.deltaTime;
if (_elapsedFireTime >= fireDelay)
{
_canShoot = true;
_elapsedFireTime = 0f;
}
}
}

public void OnFireButtonPressed(Vector3 mousePosition)
{
if (!_canShoot) { return; }

// Instantiate Bullet
Bullet bullet = _bulletFactory.Get() as Bullet;
bullet.Activate(firePosition.position, mousePosition);
bullet.OnDestroyed += OnBulletDestroyed;

_canShoot = false;
}

public void OnBulletDestroyed(Bullet usedBullet)
Expand Down

0 comments on commit d30a0cc

Please sign in to comment.