zombie_mp/Assets/Scripts/zombies/zombieManager.cs
Sewmina Dilshan 68183e5317 initial
2021-08-23 13:28:33 +05:30

100 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;
public class zombieManager : NetworkBehaviour
{
public float radius;
public bool drawGizmos;
public int maxZombiesAtOnce;
public GameObject[] zombiesToSpawn;
public List<GameObject> currentZombies;
void Start()
{
}
float t = 0;
void Update()
{
if (!isServer) { return; }
//waiting for zombie refresh rate
if (t < 5f)
{
t += Time.deltaTime;
return;
}
foreach(netPlayer player in FindObjectsOfType<netPlayer>())
{
zombieForPlayer(player);
}
}
void zombieForPlayer(netPlayer player)
{
if (player.currentZombies.Count < maxZombiesAtOnce)
{
Vector3 hit;
if (RandomPoint(player.transform.position, radius, out hit))
{
Debug.Log($"Distance to new point is {Vector3.Distance(player.transform.position, hit)}");
// bool notTooClose = (Vector3.Distance(netPlayerStats.localPlayer.transform.position, hit) >= 10);
// Vector3 screenPoint = Camera.main.WorldToScreenPoint(hit);
// bool playerCanSee = (screenPoint.x > 0 && screenPoint.x < Screen.width && screenPoint.y > 0 &&
// screenPoint.y < Screen.height);
GameObject newZombie = Instantiate(zombiesToSpawn[Random.Range(0, zombiesToSpawn.Length - 1)], hit, Quaternion.identity);
player.currentZombies.Add(newZombie);
currentZombies.Add(newZombie);
newZombie.GetComponent<zombieBehaviour>().m_zombieArea = this;
newZombie.GetComponent<zombieBehaviour>().player = player.transform;
NetworkServer.Spawn(newZombie);
t = 0;
}
}
}
public Transform giveMePlayer()
{
netPlayer[] players = FindObjectsOfType<netPlayer>();
if (players.Length <= 0)
{
return null;
}
return players[Random.Range(0, players.Length-1)].transform;
}
bool RandomPoint(Vector3 center, float range, out Vector3 result)
{
for (int i = 0; i < 30; i++)
{
Vector3 randomPoint = center + Random.insideUnitSphere * range;
UnityEngine.AI.NavMeshHit hit;
if (UnityEngine.AI.NavMesh.SamplePosition(randomPoint, out hit, 1.0f, UnityEngine.AI.NavMesh.AllAreas))
{
result = hit.position;
return true;
}
}
result = Vector3.zero;
return false;
}
void OnDrawGizmos()
{
if (drawGizmos)
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere(transform.position, radius);
}
}
}