123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Mirror;
- using Unity.Mathematics;
- public class GameManager : NetworkBehaviour
- {
- //public Transform spawnPointsParent;
- //public GameObject enemyPrefab;
- public List<EnemySpawnEntry> enemySpawns;
- public Transform LootSpawnPointsParent;
- public Transform spawnPointsPlayer;
- public static GameManager instance;
- public List<LootData> lootDatas;
- public InventoryItemsCollection inventoryItems;
- private void Awake() {
- instance = this;
- }
- void Start(){
- if(isServer){
- // for(int i = 0; i< spawnPointsParent.childCount; i++){
- // GameObject newEnemy = Instantiate(enemyPrefab , spawnPointsParent.GetChild(i).position, Quaternion.identity);
- // NetworkServer.Spawn(newEnemy);
- // // newEnemy.transform.position = spawnPointsParent.GetChild(i).position;
- // }
- foreach(EnemySpawnEntry spawnEntry in enemySpawns){
- for(int i = 0; i< spawnEntry.spawnParent.childCount; i++){
- GameObject newEnemy = Instantiate(spawnEntry.prefab , spawnEntry.spawnParent.GetChild(i).position, Quaternion.identity);
- NetworkServer.Spawn(newEnemy);
- int randomLevel = UnityEngine.Random.Range(spawnEntry.minEnemyLevel, spawnEntry.maxEnemyLevel);
- newEnemy.GetComponent<enemyScript>().SetLevel(randomLevel);
- // newEnemy.transform.position = spawnPointsParent.GetChild(i).position;
- }
- }
- for(int i=0; i < LootSpawnPointsParent.childCount; i++){
-
- GameObject newLoot = Instantiate(GetRandomLoot(), LootSpawnPointsParent.GetChild(i).position, Quaternion.identity);
- NetworkServer.Spawn(newLoot);
- }
- }
- }
- public GameObject GetRandomLoot(){
- GameObject selectedLootPrefab = lootDatas[0].prefab;
- foreach(LootData loot in lootDatas){
- int probCal = UnityEngine.Random.Range(0,100);
- if(probCal < loot.spawnProbability){
- selectedLootPrefab = loot.prefab;
- break;
- }
- }
- return selectedLootPrefab;
- }
- public void SpawnPickup(string type, Vector3 position){
- foreach(item i in inventoryItems.items)
- {
- if(i.type == type)
- {
- GameObject go = Instantiate(i.prefab, position, Quaternion.identity);
- NetworkServer.Spawn(go);
- return;
- }
- }
-
- return;
- foreach(LootData loot in lootDatas){
- if(loot.type == type){
- GameObject newLoot = Instantiate(loot.prefab, position, Quaternion.identity);
- NetworkServer.Spawn(newLoot);
- return;
- }
- }
- Debug.LogError("Couldn't find loot data for " + type);
- }
- public static void OnEnemyDeath(enemyScript enemy, Vector3 spawnPos)
- {
- instance.onEnemyDeath(enemy,spawnPos);
- }
- public void onEnemyDeath(enemyScript enemy, Vector3 spawnPos)
- {
- if (!isServer)
- {
- Debug.LogError("Server function called on client. This cant happen");
- return;
- }
- string enemyName = enemy.transform.name;
- int enemyLevel = enemy.level;
- NetworkServer.Destroy(enemy.gameObject);
-
- foreach(EnemySpawnEntry entry in enemySpawns)
- {
- if (entry.prefab.name.Contains(enemyName.Replace("(Clone)","")))
- {
- StartCoroutine(SpawnLater(entry.prefab, spawnPos, 30, enemyLevel));
- Debug.Log("Found enemy prefab for " + enemyName);
- return;
- }
- }
- Debug.LogError("Unable to find matching prefab for " + enemyName);
- }
- IEnumerator SpawnLater(GameObject go, Vector3 pos, float timer, int level)
- {
- yield return new WaitForSeconds(timer);
- GameObject newGo = Instantiate(go, pos, Quaternion.identity);
- NetworkServer.Spawn(newGo);
- newGo.GetComponent<enemyScript>().SetLevel(level);
- }
- }
- [System.Serializable]
- public class EnemySpawnEntry{
- public Transform spawnParent;
- public GameObject prefab;
- public int minEnemyLevel=1;
- public int maxEnemyLevel = 10;
- }
|