zombieManager.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. public class zombieManager : NetworkBehaviour
  6. {
  7. public float radius;
  8. public bool drawGizmos;
  9. public int maxZombiesAtOnce;
  10. public GameObject[] zombiesToSpawn;
  11. public List<GameObject> currentZombies;
  12. void Start()
  13. {
  14. }
  15. float t = 0;
  16. void Update()
  17. {
  18. if (!isServer) { return; }
  19. //waiting for zombie refresh rate
  20. if (t < 5f)
  21. {
  22. t += Time.deltaTime;
  23. return;
  24. }
  25. foreach(netPlayer player in FindObjectsOfType<netPlayer>())
  26. {
  27. zombieForPlayer(player);
  28. }
  29. }
  30. void zombieForPlayer(netPlayer player)
  31. {
  32. if (player.currentZombies.Count < maxZombiesAtOnce)
  33. {
  34. Vector3 hit;
  35. if (RandomPoint(player.transform.position, radius, out hit))
  36. {
  37. Debug.Log($"Distance to new point is {Vector3.Distance(player.transform.position, hit)}");
  38. // bool notTooClose = (Vector3.Distance(netPlayerStats.localPlayer.transform.position, hit) >= 10);
  39. // Vector3 screenPoint = Camera.main.WorldToScreenPoint(hit);
  40. // bool playerCanSee = (screenPoint.x > 0 && screenPoint.x < Screen.width && screenPoint.y > 0 &&
  41. // screenPoint.y < Screen.height);
  42. GameObject newZombie = Instantiate(zombiesToSpawn[Random.Range(0, zombiesToSpawn.Length - 1)], hit, Quaternion.identity);
  43. player.currentZombies.Add(newZombie);
  44. currentZombies.Add(newZombie);
  45. newZombie.GetComponent<zombieBehaviour>().m_zombieArea = this;
  46. newZombie.GetComponent<zombieBehaviour>().player = player.transform;
  47. NetworkServer.Spawn(newZombie);
  48. t = 0;
  49. }
  50. }
  51. }
  52. public Transform giveMePlayer()
  53. {
  54. netPlayer[] players = FindObjectsOfType<netPlayer>();
  55. if (players.Length <= 0)
  56. {
  57. return null;
  58. }
  59. return players[Random.Range(0, players.Length-1)].transform;
  60. }
  61. bool RandomPoint(Vector3 center, float range, out Vector3 result)
  62. {
  63. for (int i = 0; i < 30; i++)
  64. {
  65. Vector3 randomPoint = center + Random.insideUnitSphere * range;
  66. UnityEngine.AI.NavMeshHit hit;
  67. if (UnityEngine.AI.NavMesh.SamplePosition(randomPoint, out hit, 1.0f, UnityEngine.AI.NavMesh.AllAreas))
  68. {
  69. result = hit.position;
  70. return true;
  71. }
  72. }
  73. result = Vector3.zero;
  74. return false;
  75. }
  76. void OnDrawGizmos()
  77. {
  78. if (drawGizmos)
  79. {
  80. Gizmos.color = Color.green;
  81. Gizmos.DrawWireSphere(transform.position, radius);
  82. }
  83. }
  84. }