Skip to content
Snippets Groups Projects
Commit 998bca3f authored by akramab's avatar akramab
Browse files

add object pooling for skeleton

parent f3e30765
No related merge requests found
...@@ -39,6 +39,11 @@ public class EnemyHealth : MonoBehaviour ...@@ -39,6 +39,11 @@ public class EnemyHealth : MonoBehaviour
void Update() void Update()
{ {
if (isSinking && startingHealth > 0)
{
// memindahkan object ke bawah
isSinking = false;
}
if (isSinking) if (isSinking)
{ {
// memindahkan object ke bawah // memindahkan object ke bawah
...@@ -104,6 +109,14 @@ public class EnemyHealth : MonoBehaviour ...@@ -104,6 +109,14 @@ public class EnemyHealth : MonoBehaviour
GetComponent<Rigidbody>().isKinematic = true; GetComponent<Rigidbody>().isKinematic = true;
isSinking = true; isSinking = true;
ScoreManager.score += scoreValue; ScoreManager.score += scoreValue;
Destroy(gameObject, 2f);
if (gameObject.GetComponent<RangedEnemyAttack>() != null)
{
startingHealth = 100;
gameObject.SetActive(false);
} else
{
Destroy(gameObject, 2f);
}
} }
} }
...@@ -12,14 +12,20 @@ public class MainMenu : MonoBehaviour ...@@ -12,14 +12,20 @@ public class MainMenu : MonoBehaviour
{ {
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1); //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex+1);
playerName = GameObject.Find("Text").GetComponent<Text>().text; playerName = GameObject.Find("Text").GetComponent<Text>().text;
Debug.Log(GameObject.Find("Text").GetComponent<Text>().text); if (playerName == "")
{
playerName = "<Anonymous>";
}
SceneManager.LoadScene("WaveComplete"); SceneManager.LoadScene("WaveComplete");
} }
public void PlayZenMode() public void PlayZenMode()
{ {
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
playerName = GameObject.Find("Text").GetComponent<Text>().text; playerName = GameObject.Find("Text").GetComponent<Text>().text;
Debug.Log(GameObject.Find("Text").GetComponent<Text>().text); if (playerName == "")
{
playerName = "<Anonymous>";
}
SceneManager.LoadScene("ZenComplete"); SceneManager.LoadScene("ZenComplete");
} }
public void QuitGame() public void QuitGame()
......
...@@ -47,6 +47,16 @@ public class WaveManager : MonoBehaviour ...@@ -47,6 +47,16 @@ public class WaveManager : MonoBehaviour
Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 1, Random.Range(-10.0F, 10.0F)); Vector3 position = new Vector3(Random.Range(-10.0F, 10.0F), 1, Random.Range(-10.0F, 10.0F));
Instantiate (Factory.FactoryMethod(0), position, Quaternion.identity); Instantiate (Factory.FactoryMethod(0), position, Quaternion.identity);
// Object Pooling
GameObject skeleton = ObjectPool.SharedInstance.GetPooledObject();
if (skeleton != null)
{
skeleton.transform.position = position;
skeleton.transform.rotation = Quaternion.identity;
skeleton.SetActive(true);
}
} }
void SpawnBomber() void SpawnBomber()
......
fileFormatVersion: 2
guid: 1832cfdaaf499294db4240a22f22069c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
public static ObjectPool SharedInstance;
public List<GameObject> pooledObjects;
public GameObject objectToPool;
public int amountToPool;
private void Awake()
{
SharedInstance = this;
}
private void Start()
{
pooledObjects = new List<GameObject>();
GameObject tmp;
for(int i = 0; i < amountToPool; i++)
{
tmp = Instantiate(objectToPool);
tmp.SetActive(false);
pooledObjects.Add(tmp);
}
}
public GameObject GetPooledObject()
{
for(int i = 0; i < amountToPool; i++)
{
if (!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
return null;
}
}
fileFormatVersion: 2
guid: b7784cea58a4d584281884f8a12e7671
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -30,10 +30,10 @@ EditorUserSettings: ...@@ -30,10 +30,10 @@ EditorUserSettings:
value: 22424703114646680e0b0227036c6515183417273c2423242867083debf42d value: 22424703114646680e0b0227036c6515183417273c2423242867083debf42d
flags: 0 flags: 0
RecentlyUsedScenePath-8: RecentlyUsedScenePath-8:
value: 22424703114646680e0b0227036c681100123b2521382a35392c5326ece92021 value: 22424703114646680e0b0227036c72111f19352f223d68252320092a
flags: 0 flags: 0
RecentlyUsedScenePath-9: RecentlyUsedScenePath-9:
value: 22424703114646680e0b0227036c72111f19352f223d68252320092a value: 22424703114646680e0b0227036c681100123b2521382a35392c5326ece92021
flags: 0 flags: 0
vcSharedLogLevel: vcSharedLogLevel:
value: 0d5e400f0650 value: 0d5e400f0650
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment