| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using UnityEngine;
- [CreateAssetMenu(fileName = "GamePrefabs", menuName = "Gatherer of Souls/Game Prefabs")]
- public class GamePrefabs : ScriptableObject
- {
- [Header("Core Prefabs")]
- public GameObject villagerPrefab;
- public GameObject townHallPrefab;
- [Header("Resource Nodes")]
- public GameObject treePrefab;
- public GameObject stonePrefab;
- public GameObject farmPrefab;
- [Header("Buildings")]
- public GameObject storagePrefab;
- public GameObject lumberMillPrefab;
- public GameObject quarryPrefab;
- public GameObject housePrefab;
- [Header("Materials")]
- public Material villagerMaterial;
- public Material ghostMaterial;
- public Material selectionMaterial;
- [Header("UI")]
- public GameObject uiCanvasPrefab;
- }
- public class PrefabManager : MonoBehaviour
- {
- public static PrefabManager Instance { get; private set; }
- [Header("Prefab Data")]
- public GamePrefabs gamePrefabs;
- void Awake()
- {
- if (Instance != null && Instance != this)
- {
- Destroy(gameObject);
- return;
- }
- Instance = this;
- DontDestroyOnLoad(gameObject);
- }
- public GameObject CreateVillager(Vector3 position, string villagerName = "")
- {
- GameObject villagerGO;
- if (gamePrefabs?.villagerPrefab != null)
- {
- villagerGO = Instantiate(gamePrefabs.villagerPrefab, position, Quaternion.identity);
- }
- else
- {
- // Fallback creation
- villagerGO = CreateBasicVillager(position);
- }
- Villager villager = villagerGO.GetComponent<Villager>();
- if (villager != null && !string.IsNullOrEmpty(villagerName))
- {
- villager.villagerName = villagerName;
- }
- return villagerGO;
- }
- GameObject CreateBasicVillager(Vector3 position)
- {
- GameObject villagerGO = GameObject.CreatePrimitive(PrimitiveType.Capsule);
- villagerGO.name = "Villager";
- villagerGO.tag = "Villager";
- villagerGO.transform.position = position;
- // Add Villager component
- Villager villager = villagerGO.AddComponent<Villager>();
- // Apply material if available
- if (gamePrefabs?.villagerMaterial != null)
- {
- villagerGO.GetComponent<Renderer>().material = gamePrefabs.villagerMaterial;
- }
- // Create selection indicator
- GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- indicator.transform.SetParent(villagerGO.transform);
- indicator.transform.localPosition = Vector3.down * 0.6f;
- indicator.transform.localScale = Vector3.one * 1.2f;
- indicator.GetComponent<Renderer>().material.color = Color.yellow;
- indicator.SetActive(false);
- villager.selectionIndicator = indicator;
- Destroy(indicator.GetComponent<Collider>());
- return villagerGO;
- }
- public GameObject CreateTownHall(Vector3 position)
- {
- GameObject townHallGO;
- if (gamePrefabs?.townHallPrefab != null)
- {
- townHallGO = Instantiate(gamePrefabs.townHallPrefab, position, Quaternion.identity);
- }
- else
- {
- // Fallback creation
- townHallGO = CreateBasicTownHall(position);
- }
- return townHallGO;
- }
- GameObject CreateBasicTownHall(Vector3 position)
- {
- GameObject townHallGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
- townHallGO.name = "TownHall";
- townHallGO.tag = "TownHall";
- townHallGO.transform.position = position;
- townHallGO.transform.localScale = new Vector3(3f, 2f, 3f);
- // Add TownHall component
- TownHall townHall = townHallGO.AddComponent<TownHall>();
- // Set material color
- townHallGO.GetComponent<Renderer>().material.color = Color.blue;
- // Create selection indicator
- GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- indicator.transform.SetParent(townHallGO.transform);
- indicator.transform.localPosition = Vector3.down * 1.2f;
- indicator.transform.localScale = Vector3.one * 1.5f;
- indicator.GetComponent<Renderer>().material.color = Color.yellow;
- indicator.SetActive(false);
- townHall.selectionIndicator = indicator;
- Destroy(indicator.GetComponent<Collider>());
- return townHallGO;
- }
- public GameObject CreateResourceNode(ResourceNodeType nodeType, Vector3 position)
- {
- GameObject nodePrefab = nodeType switch
- {
- ResourceNodeType.Tree => gamePrefabs?.treePrefab,
- ResourceNodeType.Stone => gamePrefabs?.stonePrefab,
- ResourceNodeType.Farm => gamePrefabs?.farmPrefab,
- _ => null
- };
- GameObject nodeGO;
- if (nodePrefab != null)
- {
- nodeGO = Instantiate(nodePrefab, position, Quaternion.identity);
- }
- else
- {
- nodeGO = CreateBasicResourceNode(nodeType, position);
- }
- return nodeGO;
- }
- GameObject CreateBasicResourceNode(ResourceNodeType nodeType, Vector3 position)
- {
- GameObject nodeGO;
- ResourceNode node;
- switch (nodeType)
- {
- case ResourceNodeType.Tree:
- nodeGO = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- nodeGO.transform.localScale = new Vector3(1f, 2f, 1f);
- nodeGO.GetComponent<Renderer>().material.color = Color.green;
- nodeGO.name = "Tree";
- nodeGO.tag = "Tree";
- node = nodeGO.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Tree;
- node.resourceType = ResourceType.Wood;
- node.maxResources = 50;
- node.currentResources = 50;
- node.regenerationRate = 2f;
- break;
- case ResourceNodeType.Stone:
- nodeGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
- nodeGO.transform.localScale = Vector3.one * 1.5f;
- nodeGO.GetComponent<Renderer>().material.color = Color.gray;
- nodeGO.name = "Stone";
- nodeGO.tag = "Stone";
- node = nodeGO.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Stone;
- node.resourceType = ResourceType.Stone;
- node.maxResources = 75;
- node.currentResources = 75;
- node.regenerationRate = 0.5f;
- break;
- case ResourceNodeType.Farm:
- nodeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
- nodeGO.transform.localScale = new Vector3(2f, 0.2f, 2f);
- nodeGO.GetComponent<Renderer>().material.color = Color.yellow;
- nodeGO.name = "Farm";
- nodeGO.tag = "Farm";
- node = nodeGO.AddComponent<ResourceNode>();
- node.nodeType = ResourceNodeType.Farm;
- node.resourceType = ResourceType.Food;
- node.maxResources = 100;
- node.currentResources = 100;
- node.regenerationRate = 3f;
- break;
- default:
- return null;
- }
- nodeGO.transform.position = position;
- // Create selection indicator
- CreateSelectionIndicatorForNode(nodeGO, node);
- return nodeGO;
- }
- void CreateSelectionIndicatorForNode(GameObject parent, ResourceNode node)
- {
- GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
- indicator.transform.SetParent(parent.transform);
- indicator.transform.localPosition = Vector3.down * 0.6f;
- indicator.transform.localScale = Vector3.one * 1.2f;
- if (gamePrefabs?.selectionMaterial != null)
- {
- indicator.GetComponent<Renderer>().material = gamePrefabs.selectionMaterial;
- }
- else
- {
- indicator.GetComponent<Renderer>().material.color = Color.cyan;
- }
- indicator.SetActive(false);
- node.selectionIndicator = indicator;
- Destroy(indicator.GetComponent<Collider>());
- }
- public Material GetGhostMaterial()
- {
- return gamePrefabs?.ghostMaterial;
- }
- }
|