mmorpg/Assets/GameManager.cs

103 lines
3.2 KiB
C#

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);
}
}
[System.Serializable]
public class EnemySpawnEntry{
public Transform spawnParent;
public GameObject prefab;
public int minEnemyLevel=1;
public int maxEnemyLevel = 10;
}