diff --git a/IF3210-2024-Unity-AFK/Assets/Code/Scripts/EnemyController.cs b/IF3210-2024-Unity-AFK/Assets/Code/Scripts/EnemyController.cs index b0faedce22cc83bbab1f9d43cbee8e584643ebe3..6e33c46ebe8ae942ea1f98d21e670da4d0009c45 100644 --- a/IF3210-2024-Unity-AFK/Assets/Code/Scripts/EnemyController.cs +++ b/IF3210-2024-Unity-AFK/Assets/Code/Scripts/EnemyController.cs @@ -11,6 +11,7 @@ public class EnemyController : MonoBehaviour public GameObject EnemyGeneralRef; public GameObject EnemyKingRef; public GameObject PlayerRef; + public Vector3 defaultSpawnPosition = new Vector3(40f, 84f, 10f); private Transform PlayerTransform; private int kerocoCount = 0; private int kepalaKerocoCount = 0; @@ -31,9 +32,32 @@ public class EnemyController : MonoBehaviour StartCoroutine(KingDrop()); } - Vector3 RandomPosition() + Vector3 RandomPosition(Vector3 owner, int randomRange, Vector3 defaultSpawn) { - return new Vector3(Random.Range(-25, 25), 0, Random.Range(-25, 25)); + //// Maybe buggy because can't get terrain height + //float xPosMobs = PlayerTransform.position.x + Random.Range(-25, 25); + //float zPosMobs = PlayerTransform.position.z + Random.Range(-25, 25); + //float yPosMobs = PlayerTransform.position.y + 3; + + // Get a random position near the player + Vector3 randomDirection = Random.insideUnitSphere * randomRange; + randomDirection += owner; + + // Sample a position on the NavMesh + if (NavMesh.SamplePosition(randomDirection, out NavMeshHit hit, randomRange, NavMesh.AllAreas)) + { + Vector3 finalPosition = hit.position; + + // increase the y position to avoid spawning under the terrain and spawn the enemy from little above the ground + finalPosition.y += 2.7f; + return finalPosition; + } + else + { + // default spawn position + return defaultSpawn; + } + } IEnumerator KerocoDrop() @@ -44,28 +68,10 @@ public class EnemyController : MonoBehaviour if (kerocoCount < kerocoCountMax) { Debug.Log("Create Keroco"); - - // Get a random position near the player - Vector3 randomDirection = Random.insideUnitSphere * 25; - randomDirection += PlayerTransform.position; - - // Sample a position on the NavMesh - if (NavMesh.SamplePosition(randomDirection, out NavMeshHit hit, 25, NavMesh.AllAreas)) - { - Vector3 finalPosition = hit.position; - - // increase the y position to avoid spawning under the terrain and spawn the enemy from little above the ground - finalPosition.y += 2.5f; - - // Spawn the enemy at the sampled position - GameObject newKeroco = Instantiate(EnemyKerocoRef, finalPosition, Quaternion.identity); - newKeroco.SetActive(true); - newKeroco.GetComponent<CombatBehavior>().health = 50; - kerocoCount++; - } - - - // else maybe make a default spawn position + GameObject newKeroco = Instantiate(EnemyKerocoRef, RandomPosition(PlayerTransform.position, 25, defaultSpawnPosition), Quaternion.identity); + newKeroco.SetActive(true); + newKeroco.GetComponent<CombatBehavior>().health = 50; + kerocoCount++; } yield return new WaitForSeconds(0.1f); } @@ -79,12 +85,7 @@ public class EnemyController : MonoBehaviour { if (kepalaKerocoCount < kepalaKerocoMax) { - // Maybe buggy because can't get terrain height - float xPosMobs = PlayerTransform.position.x + Random.Range(-25, 25); - float zPosMobs = PlayerTransform.position.z + Random.Range(-25, 25); - float yPosMobs = PlayerTransform.position.y + 3; - - GameObject newKepalaKeroco = Instantiate(EnemyKepalaKerocoRef, new Vector3(xPosMobs, yPosMobs, zPosMobs), Quaternion.identity); + GameObject newKepalaKeroco = Instantiate(EnemyKepalaKerocoRef, RandomPosition(PlayerTransform.position, 25, defaultSpawnPosition), Quaternion.identity); newKepalaKeroco.SetActive(true); newKepalaKeroco.GetComponent<CombatBehavior>().health = 200; kepalaKerocoCount++; @@ -100,12 +101,7 @@ public class EnemyController : MonoBehaviour { if (generalCount < generalCountMax) { - // Maybe buggy because can't get terrain height - float xPosMobs = PlayerTransform.position.x + Random.Range(-25, 25); - float zPosMobs = PlayerTransform.position.z + Random.Range(-25, 25); - float yPosMobs = PlayerTransform.position.y + 3; - - GameObject newGeneral = Instantiate(EnemyGeneralRef, new Vector3(xPosMobs, yPosMobs, zPosMobs), Quaternion.identity); + GameObject newGeneral = Instantiate(EnemyGeneralRef, RandomPosition(PlayerTransform.position, 25, defaultSpawnPosition), Quaternion.identity); newGeneral.SetActive(true); newGeneral.GetComponent<CombatBehavior>().health = 500; generalCount++; @@ -121,12 +117,7 @@ public class EnemyController : MonoBehaviour { if (kingCount < kingCountMax) { - // Maybe buggy because can't get terrain height - float xPosMobs = PlayerTransform.position.x + Random.Range(-25, 25); - float zPosMobs = PlayerTransform.position.z + Random.Range(-25, 25); - float yPosMobs = PlayerTransform.position.y + 3; - - GameObject newKing = Instantiate(EnemyKingRef, new Vector3(xPosMobs, yPosMobs, zPosMobs), Quaternion.identity); + GameObject newKing = Instantiate(EnemyKingRef, RandomPosition(PlayerTransform.position, 25, defaultSpawnPosition), Quaternion.identity); newKing.SetActive(true); newKing.GetComponent<CombatBehavior>().health = 1000; kingCount++; @@ -137,11 +128,9 @@ public class EnemyController : MonoBehaviour public void KerocoDropByOtherMob(Transform ownerTransform) { - float xPosMobs = ownerTransform.position.x + Random.Range(-5, 5); - float zPosMobs = ownerTransform.position.z + Random.Range(-5, 5); - float yPosMobs = ownerTransform.position.y + 2; - - GameObject newSlaveKeroco = Instantiate(EnemyKerocoRef, new Vector3(xPosMobs, yPosMobs, zPosMobs), Quaternion.identity); + // default spawn is on the front of the owner + Vector3 defaultSpawnKeroco = ownerTransform.position + new Vector3(0, 3, 1); + GameObject newSlaveKeroco = Instantiate(EnemyKerocoRef, RandomPosition(ownerTransform.position, 5, defaultSpawnKeroco), Quaternion.identity); newSlaveKeroco.GetComponent<KerocoBehaviour>().haveOwner = true; newSlaveKeroco.SetActive(true); newSlaveKeroco.GetComponent<CombatBehavior>().health = 50;