GameManager.cs 4.4 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. public void onEnemyDeath(enemyScript enemy, Vector3 spawnPos)
  77. {
  78. if (!isServer)
  79. {
  80. Debug.LogError("Server function called on client. This cant happen");
  81. return;
  82. }
  83. string enemyName = enemy.transform.name;
  84. int enemyLevel = enemy.level;
  85. NetworkServer.Destroy(enemy.gameObject);
  86. foreach(EnemySpawnEntry entry in enemySpawns)
  87. {
  88. if (entry.prefab.name.Contains(enemyName.Replace("(Clone)","")))
  89. {
  90. StartCoroutine(SpawnLater(entry.prefab, spawnPos, 30, enemyLevel));
  91. Debug.Log("Found enemy prefab for " + enemyName);
  92. return;
  93. }
  94. }
  95. Debug.LogError("Unable to find matching prefab for " + enemyName);
  96. }
  97. IEnumerator SpawnLater(GameObject go, Vector3 pos, float timer, int level)
  98. {
  99. yield return new WaitForSeconds(timer);
  100. GameObject newGo = Instantiate(go, pos, Quaternion.identity);
  101. NetworkServer.Spawn(newGo);
  102. newGo.GetComponent<enemyScript>().SetLevel(level);
  103. }
  104. }
  105. [System.Serializable]
  106. public class EnemySpawnEntry{
  107. public Transform spawnParent;
  108. public GameObject prefab;
  109. public int minEnemyLevel=1;
  110. public int maxEnemyLevel = 10;
  111. }