Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implementation of Core Elements of Bullet Pool #9

Open
wants to merge 3 commits into
base: Project-Setup
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Assets/Scripts/Bullets/BulletController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public BulletController(BulletView bulletViewPrefab, BulletScriptableObject bull

public void ConfigureBullet(Transform spawnTransform)
{
bulletView.gameObject.SetActive(true);
bulletView.transform.position = spawnTransform.position;
bulletView.transform.rotation = spawnTransform.rotation;
}
Expand All @@ -31,7 +32,8 @@ public void OnBulletEnteredTrigger(GameObject collidedGameObject)
collidedGameObject.GetComponent<IDamageable>().TakeDamage(bulletScriptableObject.damage);
GameService.Instance.GetSoundService().PlaySoundEffects(SoundType.BulletHit);
GameService.Instance.GetVFXService().PlayVFXAtPosition(VFXType.BulletHitExplosion, bulletView.transform.position);
Object.Destroy(bulletView.gameObject);
GameService.Instance.GetPlayerService().ReturnBulletToPool(this);
bulletView.gameObject.SetActive(false);
}
}
}
Expand Down
60 changes: 60 additions & 0 deletions Assets/Scripts/Bullets/BulletPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace CosmicCuration.Bullets
{
//create class of a pooledBullet
//create this pool via PlayerService
//create constructor of pool
//GetBullet Functionality to pool
// ReturnBullet To pool

public class BulletPool
{
private BulletView bulletView;
private BulletScriptableObject bulletScriptableObject;
private List<PooledBullet> pooledBullets = new List<PooledBullet>();

public BulletPool(BulletView bulletView, BulletScriptableObject bulletScriptableObject)
{
this.bulletView = bulletView;
this.bulletScriptableObject = bulletScriptableObject;
}

public BulletController GetBullet()
{
if (pooledBullets.Count > 0)
{
PooledBullet pooledBullet = pooledBullets.Find(item => !item.isUsed);

if (pooledBullet != null)
{
pooledBullet.isUsed = true;
return pooledBullet.Bullet;
}
}
return CreateNewPooledBullet();
}
public void ReturnToBulletPool(BulletController retunToBullet)
{
PooledBullet pooledBullet = pooledBullets.Find(item => item.Bullet.Equals(retunToBullet));
pooledBullet.isUsed = false;
}

private BulletController CreateNewPooledBullet()
{
PooledBullet pooledBullet = new PooledBullet();
pooledBullet.Bullet = new BulletController(bulletView, bulletScriptableObject);
pooledBullet.isUsed = true;
pooledBullets.Add(pooledBullet);
return pooledBullet.Bullet;
}

public class PooledBullet
{
public BulletController Bullet;
public bool isUsed;
}
}
}
11 changes: 11 additions & 0 deletions Assets/Scripts/Bullets/BulletPool.cs.meta

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

Loading