GameManager.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. using Unity.Mathematics;
  6. public class GameManager : NetworkBehaviour
  7. {
  8. //public Transform spawnPointsParent;
  9. //public GameObject enemyPrefab;
  10. public List<EnemySpawnEntry> enemySpawns;
  11. public Transform LootSpawnPointsParent;
  12. public Transform spawnPointsPlayer;
  13. public static GameManager instance;
  14. public List<LootData> lootDatas;
  15. public InventoryItemsCollection inventoryItems;
  16. private void Awake() {
  17. instance = this;
  18. }
  19. void Start(){
  20. if(isServer){
  21. // for(int i = 0; i< spawnPointsParent.childCount; i++){
  22. // GameObject newEnemy = Instantiate(enemyPrefab , spawnPointsParent.GetChild(i).position, Quaternion.identity);
  23. // NetworkServer.Spawn(newEnemy);
  24. // // newEnemy.transform.position = spawnPointsParent.GetChild(i).position;
  25. // }
  26. foreach(EnemySpawnEntry spawnEntry in enemySpawns){
  27. for(int i = 0; i< spawnEntry.spawnParent.childCount; i++){
  28. GameObject newEnemy = Instantiate(spawnEntry.prefab , spawnEntry.spawnParent.GetChild(i).position, Quaternion.identity);
  29. NetworkServer.Spawn(newEnemy);
  30. int randomLevel = UnityEngine.Random.Range(spawnEntry.minEnemyLevel, spawnEntry.maxEnemyLevel);
  31. newEnemy.GetComponent<enemyScript>().SetLevel(randomLevel);
  32. // newEnemy.transform.position = spawnPointsParent.GetChild(i).position;
  33. }
  34. }
  35. for(int i=0; i < LootSpawnPointsParent.childCount; i++){
  36. GameObject newLoot = Instantiate(GetRandomLoot(), LootSpawnPointsParent.GetChild(i).position, Quaternion.identity);
  37. NetworkServer.Spawn(newLoot);
  38. }
  39. }
  40. }
  41. public GameObject GetRandomLoot(){
  42. GameObject selectedLootPrefab = lootDatas[0].prefab;
  43. foreach(LootData loot in lootDatas){
  44. int probCal = UnityEngine.Random.Range(0,100);
  45. if(probCal < loot.spawnProbability){
  46. selectedLootPrefab = loot.prefab;
  47. break;
  48. }
  49. }
  50. return selectedLootPrefab;
  51. }
  52. public void SpawnPickup(string type, Vector3 position){
  53. foreach(item i in inventoryItems.items)
  54. {
  55. if(i.type == type)
  56. {
  57. GameObject go = Instantiate(i.prefab, position, Quaternion.identity);
  58. NetworkServer.Spawn(go);
  59. return;
  60. }
  61. }
  62. return;
  63. foreach(LootData loot in lootDatas){
  64. if(loot.type == type){
  65. GameObject newLoot = Instantiate(loot.prefab, position, Quaternion.identity);
  66. NetworkServer.Spawn(newLoot);
  67. return;
  68. }
  69. }
  70. Debug.LogError("Couldn't find loot data for " + type);
  71. }
  72. public static void OnEnemyDeath(enemyScript enemy, Vector3 spawnPos)
  73. {
  74. instance.onEnemyDeath(enemy,spawnPos);
  75. }
  76. [SerializeField]private float monsterspawnDelay = 45f;
  77. public void onEnemyDeath(enemyScript enemy, Vector3 spawnPos)
  78. {
  79. if (!isServer)
  80. {
  81. Debug.LogError("Server function called on client. This cant happen");
  82. return;
  83. }
  84. string enemyName = enemy.transform.name;
  85. int enemyLevel = enemy.level;
  86. NetworkServer.Destroy(enemy.gameObject);
  87. foreach(EnemySpawnEntry entry in enemySpawns)
  88. {
  89. if (entry.prefab.name.Contains(enemyName.Replace("(Clone)","")))
  90. {
  91. StartCoroutine(SpawnLater(entry.prefab, spawnPos, monsterspawnDelay, enemyLevel));
  92. Debug.Log("Found enemy prefab for " + enemyName);
  93. return;
  94. }
  95. }
  96. Debug.LogError("Unable to find matching prefab for " + enemyName);
  97. }
  98. IEnumerator SpawnLater(GameObject go, Vector3 pos, float timer, int level)
  99. {
  100. yield return new WaitForSeconds(timer);
  101. GameObject newGo = Instantiate(go, pos, Quaternion.identity);
  102. NetworkServer.Spawn(newGo);
  103. newGo.GetComponent<enemyScript>().SetLevel(level);
  104. }
  105. }
  106. [System.Serializable]
  107. public class EnemySpawnEntry{
  108. public Transform spawnParent;
  109. public GameObject prefab;
  110. public int minEnemyLevel=1;
  111. public int maxEnemyLevel = 10;
  112. }