144 lines
4.4 KiB
C#
Executable File
144 lines
4.4 KiB
C#
Executable File
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;
|
|
} |