using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
#if UNITY_EDITOR
using UnityEditor.SceneManagement;
#endif
public class SpawnManager : MonoBehaviour
{
public List<GameObject> npcPrefabs = new List<GameObject>();
public List<GameObject> itemPrefabs = new List<GameObject>();
public int minNumberOfNPCs = 1;
public int maxNumberOfNPCs = 3;
public float minCompetency = 0f;
public float maxCompetency = 1.0f;
public float minDurability = 0f;
public float maxDurability = 1.0f;
private int decimalPlaces = 1;
public UIManager uiManager;
public List<GameObject> spawnedNPCs = new List<GameObject>();
public List<GameObject> spawnedItems = new List<GameObject>();
public List<EntityData> entityDataList = new List<EntityData>();
private bool allowSpawning = false;
public void SpawnButtonPressed()
{
allowSpawning = true;
SpawnNPCs();
}
public void SpawnNPCs()
{
if (!Application.isPlaying)
{
Debug.LogError("Spawning is only allowed in Play mode.");
return;
}
DestroyAllNPCs();
DestroyAllItems();
int numberOfNPCs = Random.Range(minNumberOfNPCs, maxNumberOfNPCs + 1);
for (int i = 0; i < numberOfNPCs; i++)
{
// NPC Spawning
GameObject npcPrefab = GetRandomPrefab(npcPrefabs);
GameObject newNPC = Instantiate(npcPrefab, transform.position + Random.insideUnitSphere * 5f, Quaternion.identity);
spawnedNPCs.Add(newNPC);
float npcCompetency = Random.Range(minCompetency, maxCompetency);
npcCompetency = Mathf.Round(npcCompetency * Mathf.Pow(10, decimalPlaces)) / Mathf.Pow(10, decimalPlaces);
EntityData npcData = new EntityData(newNPC.name, npcCompetency, 0f, 0f);
entityDataList.Add(npcData);
NPC refToNPC = newNPC.AddComponent<NPC>();
refToNPC.UpdateCompetency(npcCompetency);
}
// Ensure that numberOfItems matches the count of spawnedNPCs
int numberOfItems = spawnedNPCs.Count;
for (int i = 0; i < numberOfItems; i++)
{
// Item Spawning
GameObject itemPrefab = GetRandomPrefab(itemPrefabs);
GameObject newItem = Instantiate(itemPrefab, transform.position + Random.insideUnitSphere * 5f, Quaternion.identity);
spawnedItems.Add(newItem);
Item refToItemScript = newItem.GetComponent<Item>();
float itemDurability = Random.Range(minDurability, maxDurability);
itemDurability = Mathf.Round(itemDurability * Mathf.Pow(10, decimalPlaces)) / Mathf.Pow(10, decimalPlaces);
float itemRepairUrgency = 1 - (Random.Range(minCompetency, maxCompetency) * itemDurability);
EntityData itemData = new EntityData(newItem.name, 0f, itemDurability, itemRepairUrgency);
entityDataList.Add(itemData);
if (refToItemScript != null)
{
refToItemScript.minDurability = minDurability;
refToItemScript.maxDurability = maxDurability;
refToItemScript.GetRandomDurability();
}
}
}
void DestroyAllNPCs()
{
foreach (GameObject npc in spawnedNPCs)
{
DestroyImmediate(npc);
}
spawnedNPCs.Clear(); // Clear the list after destroying NPCs
}
void DestroyAllItems()
{
foreach (GameObject item in spawnedItems)
{
DestroyImmediate(item);
}
spawnedItems.Clear(); // Clear the list after destroying items
}
GameObject GetRandomPrefab(List<GameObject> prefabList)
{
if (prefabList.Count > 0)
{
int randomIndex = Random.Range(0, prefabList.Count);
return prefabList[randomIndex];
}
else
{
Debug.LogError("Prefab list is empty!");
return null;
}
}
void ClearAll()
{
// Do not clear the entityDataList, only destroy the game objects
DestroyAllNPCs();
DestroyAllItems();
// Update the UI after clearing NPCs and items
uiManager.UpdateNPCItemListUI();
}
[ContextMenu("Reset Scene")]
private void ResetScene()
{
#if UNITY_EDITOR
EditorSceneManager.LoadScene(EditorSceneManager.GetActiveScene().buildIndex);
#endif
}
public void ResetAll()
{
if (allowSpawning)
{
allowSpawning = false;
ClearAll();
}
}
}
using UnityEngine;
using TMPro;
using System.Collections.Generic;
public class UIManager : MonoBehaviour
{
public SpawnManager spawnManager;
public TextMeshProUGUI npcItemListText;
// Update is called once per frame
void Update()
{
UpdateNPCItemListUI();
}
public void UpdateNPCItemListUI()
{
if (npcItemListText != null)
{
npcItemListText.text = "NPCs and Items:\\n";
int maxCount = Mathf.Max(spawnManager.spawnedNPCs.Count, spawnManager.spawnedItems.Count);
for (int i = 0; i < maxCount; i++)
{
if (i < spawnManager.spawnedNPCs.Count)
{
npcItemListText.text += $"{spawnManager.spawnedNPCs[i].name} : ";
}
else
{
npcItemListText.text += "None : ";
}
if (i < spawnManager.spawnedItems.Count)
{
npcItemListText.text += $"{spawnManager.spawnedItems[i].name}";
}
else
{
npcItemListText.text += "None";
}
npcItemListText.text += "\\n";
}
}
else
{
Debug.LogWarning("npcItemListText not assigned in UIManager.");
}
}
}