diff --git a/Survival Shooter/Assets/Prefabs/Bullet.prefab b/Survival Shooter/Assets/Prefabs/Bullet.prefab index 5f3894babddeda62e76ba2a2b1915d5ef079fcda..61ea4d3be2bb867024f534d3d9b7887736da3691 100644 --- a/Survival Shooter/Assets/Prefabs/Bullet.prefab +++ b/Survival Shooter/Assets/Prefabs/Bullet.prefab @@ -67,7 +67,7 @@ MonoBehaviour: isRotating: 1 isFloating: 0 isScaling: 0 - rotationAngle: {x: 0, y: 0, z: 10} + rotationAngle: {x: 10, y: 0, z: 10} rotationSpeed: 5 floatSpeed: 0 floatRate: 0 diff --git a/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab new file mode 100644 index 0000000000000000000000000000000000000000..a4c0ed6e1dd8e68a56de8c978df8e21b689f1cd3 --- /dev/null +++ b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab @@ -0,0 +1,49 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &1663985618794767950 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1663985618794767948} + - component: {fileID: 1663985618794767951} + m_Layer: 0 + m_Name: ObjectPooler + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &1663985618794767948 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1663985618794767950} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1663985618794767951 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1663985618794767950} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5cbfe0fa2edd3034d9f950788afa7c83, type: 3} + m_Name: + m_EditorClassIdentifier: + pools: + - tag: Bullet + prefab: {fileID: 8946891882775703378, guid: d6c526ea9d76cdd468b8425ad07a3ded, type: 3} + size: 35 diff --git a/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta new file mode 100644 index 0000000000000000000000000000000000000000..0a90950b3f3708f1fc66058d526c78dc982f66c0 --- /dev/null +++ b/Survival Shooter/Assets/Prefabs/ObjectPooler.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c19968325c6af3d44b67fc4746e2355e +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scenes/WaveMode.unity b/Survival Shooter/Assets/Scenes/WaveMode.unity index 7d9371f06120a3f3ceaf7aa89ffdc3a665730f29..9d5ddd391b5306a85f4587db2d1d6f01c8cff31c 100644 --- a/Survival Shooter/Assets/Scenes/WaveMode.unity +++ b/Survival Shooter/Assets/Scenes/WaveMode.unity @@ -3204,6 +3204,63 @@ PrefabInstance: objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: 533adfa42ba500c43a62a71ebc8f0c99, type: 3} +--- !u!1001 &1663985618332078390 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_RootOrder + value: 15 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767950, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_Name + value: ObjectPooler + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} --- !u!4 &2719544848253408655 stripped Transform: m_CorrespondingSourceObject: {fileID: 1293407273377250347, guid: 6e3d49b904179974ab53cbb0d368a260, type: 3} diff --git a/Survival Shooter/Assets/Scenes/ZenMode.unity b/Survival Shooter/Assets/Scenes/ZenMode.unity index 1c3d33488b030387b36c9357417426a34beca679..0eac9b71635e1795ec1b607eaacf675a67fd7330 100644 --- a/Survival Shooter/Assets/Scenes/ZenMode.unity +++ b/Survival Shooter/Assets/Scenes/ZenMode.unity @@ -604,6 +604,63 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: playerHealth: {fileID: 875225643} +--- !u!1001 &305081429 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 0} + m_Modifications: + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_RootOrder + value: 15 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalPosition.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalRotation.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767948, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 1663985618794767950, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} + propertyPath: m_Name + value: ObjectPooler + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: c19968325c6af3d44b67fc4746e2355e, type: 3} --- !u!1 &529888204 GameObject: m_ObjectHideFlags: 0 diff --git a/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs b/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs index f33c47aa8d5e968a8495aba4357b5e2452daae19..2a7cb7a28a683d6287bd4883963375b25704f4a5 100644 --- a/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs +++ b/Survival Shooter/Assets/Scripts/Bullet/Bullet.cs @@ -2,7 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; -public class Bullet : MonoBehaviour +public class Bullet : MonoBehaviour, IPooledObject { public float speed = 100.0f; public int damage = 20; @@ -26,7 +26,7 @@ public class Bullet : MonoBehaviour bool isCrit = false; // Start is called before the first frame update - void Start() + public void OnObjectSpawn() { player = GameObject.FindGameObjectWithTag ("Player"); playerShooting = player.GetComponentInChildren <PlayerShooting> (); @@ -36,6 +36,8 @@ public class Bullet : MonoBehaviour gunAudioArr = GetComponents<AudioSource>(); critAudio = gunAudioArr[0]; bulletCritChance = PlayerShooting.critChance; + hasHit = false; + timer = 0; } // Update is called once per frame @@ -50,7 +52,8 @@ public class Bullet : MonoBehaviour // Schedule for destruction if bullet never hits anything. if (timer >= life) { - Destroy(gameObject); + // Destroy(gameObject); + gameObject.SetActive(false); } velocity = transform.forward; @@ -117,10 +120,12 @@ public class Bullet : MonoBehaviour { envHit.ShowHit(hit.point); } - Destroy(gameObject); + // Destroy(gameObject); + gameObject.SetActive(false); } void DelayedDestroy() { - Destroy(gameObject, 0.2f); + // Destroy(gameObject, 0.2f); + gameObject.SetActive(false); } } diff --git a/Survival Shooter/Assets/Scripts/IPooledObject.cs b/Survival Shooter/Assets/Scripts/IPooledObject.cs new file mode 100644 index 0000000000000000000000000000000000000000..bd8bdc290024abb04d095374b56199781e4b2dd8 --- /dev/null +++ b/Survival Shooter/Assets/Scripts/IPooledObject.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +public interface IPooledObject +{ + void OnObjectSpawn(); +} diff --git a/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta b/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..9377760a12a4e6a0667e2ac612359e7e5343184c --- /dev/null +++ b/Survival Shooter/Assets/Scripts/IPooledObject.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 817e955d67ecbb144a2f776b2dc484ec +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs b/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs index 0b6efd21ab13b1f7d03018fcd00626777453457a..7b4240754e2a118098ee725050a23c2bbae817ee 100644 --- a/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs +++ b/Survival Shooter/Assets/Scripts/Managers/PowerManager.cs @@ -15,7 +15,7 @@ public class PowerManager : MonoBehaviour // player = GameObject.FindGameObjectWithTag("Player"); // playerShooting = player.GetComponent<PlayerShooting>(); text = GetComponent<Text>(); - power = 0; + power = 1; } // Update is called once per frame diff --git a/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs b/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs index f56416803e20fafe942652dda2dfe7a2f4942b79..54018e77af29475d2937a7afe814c334c0fcea29 100644 --- a/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs +++ b/Survival Shooter/Assets/Scripts/Managers/SpeedManager.cs @@ -11,7 +11,7 @@ public class SpeedManager : MonoBehaviour void Start() { text = GetComponent<Text>(); - speed = 0f; + speed = 1f; } // Update is called once per frame diff --git a/Survival Shooter/Assets/Scripts/ObjectPooler.cs b/Survival Shooter/Assets/Scripts/ObjectPooler.cs new file mode 100644 index 0000000000000000000000000000000000000000..e15a717746dbfae65e3e8f4539c1774d75da1edc --- /dev/null +++ b/Survival Shooter/Assets/Scripts/ObjectPooler.cs @@ -0,0 +1,68 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class ObjectPooler : MonoBehaviour +{ + [System.Serializable] + public class Pool{ + public string tag; + public GameObject prefab; + public int size; + } + #region Singleton + public static ObjectPooler Instance; + + private void Awake() + { + Instance = this; + } + #endregion + public List<Pool> pools; + public Dictionary<string, Queue<GameObject>> poolDictionary; + // Start is called before the first frame update + void Start() + { + poolDictionary = new Dictionary<string, Queue<GameObject>>(); + + foreach (Pool pool in pools) + { + Queue<GameObject> objectPool = new Queue<GameObject>(); + + for (int i = 0; i < pool.size; i++) + { + GameObject obj = Instantiate(pool.prefab); + obj.SetActive(false); + objectPool.Enqueue(obj); + } + + poolDictionary.Add(pool.tag, objectPool); + } + } + + public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation){ + + if (!poolDictionary.ContainsKey(tag)) + { + Debug.LogWarning("Pool with tag " + tag + " doesn't exist"); + return null; + } + + GameObject objectToSpawn = poolDictionary[tag].Dequeue(); + objectToSpawn.SetActive(true); + objectToSpawn.transform.position = position; + objectToSpawn.transform.rotation = rotation; + + IPooledObject pooledObj = objectToSpawn.GetComponent<IPooledObject>(); + + if (pooledObj != null) + { + pooledObj.OnObjectSpawn(); + } + + poolDictionary[tag].Enqueue(objectToSpawn); + + return objectToSpawn; + } + +} diff --git a/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta b/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta new file mode 100644 index 0000000000000000000000000000000000000000..11523ae368b39990f23de5bda46dc84111aff1e3 --- /dev/null +++ b/Survival Shooter/Assets/Scripts/ObjectPooler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5cbfe0fa2edd3034d9f950788afa7c83 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs b/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs index 13c1f366f0c254cd00cd6450d7fcfedf3dab72a4..4d0f14c0d91ec0c6d1da1d99c40403bd6a01161d 100644 --- a/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs +++ b/Survival Shooter/Assets/Scripts/Player/PlayerMovement.cs @@ -55,7 +55,7 @@ public class PlayerMovement : MonoBehaviour public void speedOrb(){ if (speed < 10f){ speed += 0.5f; - SpeedManager.speed += 0.5f; + SpeedManager.speed += 1f; } } diff --git a/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs b/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs index 9f07e7b454997a4566855a9fe0746c4f0520123f..36510988026d90ad63b071a053db04631fced548 100644 --- a/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs +++ b/Survival Shooter/Assets/Scripts/Player/PlayerShooting.cs @@ -7,9 +7,9 @@ public class PlayerShooting : MonoBehaviour public float range = 100f; public static int critChance = 0; - public static int bulletCount = 5; + public static int bulletCount = 1; - public GameObject bullets; + // public GameObject bullets; float timer; Ray shootRay; RaycastHit shootHit; @@ -20,7 +20,12 @@ public class PlayerShooting : MonoBehaviour AudioSource gunAudio; Light gunLight; float effectsDisplayTime = 0.2f; + ObjectPooler objectPooler; + + private void Start(){ + objectPooler = ObjectPooler.Instance; + } void Awake() { shootableMask = LayerMask.GetMask("Shootable"); @@ -79,7 +84,8 @@ public class PlayerShooting : MonoBehaviour int index = bullet==0 ? 0 : bullet*2; // gunLine.SetPosition(index, transform.position); - Instantiate(bullets, transform.position, q * transform.rotation); + ObjectPooler.Instance.SpawnFromPool("Bullet", transform.position, q * transform.rotation); + // Instantiate(bullets, transform.position, q * transform.rotation); // if (Physics.Raycast(shootRay.origin,shootRay.direction, out shootHit, range, shootableMask)) // {