PrefabManager.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using UnityEngine;
  2. [CreateAssetMenu(fileName = "GamePrefabs", menuName = "Gatherer of Souls/Game Prefabs")]
  3. public class GamePrefabs : ScriptableObject
  4. {
  5. [Header("Core Prefabs")]
  6. public GameObject villagerPrefab;
  7. public GameObject townHallPrefab;
  8. [Header("Resource Nodes")]
  9. public GameObject treePrefab;
  10. public GameObject stonePrefab;
  11. public GameObject farmPrefab;
  12. [Header("Buildings")]
  13. public GameObject storagePrefab;
  14. public GameObject lumberMillPrefab;
  15. public GameObject quarryPrefab;
  16. public GameObject housePrefab;
  17. [Header("Materials")]
  18. public Material villagerMaterial;
  19. public Material ghostMaterial;
  20. public Material selectionMaterial;
  21. [Header("UI")]
  22. public GameObject uiCanvasPrefab;
  23. }
  24. public class PrefabManager : MonoBehaviour
  25. {
  26. public static PrefabManager Instance { get; private set; }
  27. [Header("Prefab Data")]
  28. public GamePrefabs gamePrefabs;
  29. void Awake()
  30. {
  31. if (Instance != null && Instance != this)
  32. {
  33. Destroy(gameObject);
  34. return;
  35. }
  36. Instance = this;
  37. DontDestroyOnLoad(gameObject);
  38. }
  39. public GameObject CreateVillager(Vector3 position, string villagerName = "")
  40. {
  41. GameObject villagerGO;
  42. if (gamePrefabs?.villagerPrefab != null)
  43. {
  44. villagerGO = Instantiate(gamePrefabs.villagerPrefab, position, Quaternion.identity);
  45. }
  46. else
  47. {
  48. // Fallback creation
  49. villagerGO = CreateBasicVillager(position);
  50. }
  51. Villager villager = villagerGO.GetComponent<Villager>();
  52. if (villager != null && !string.IsNullOrEmpty(villagerName))
  53. {
  54. villager.villagerName = villagerName;
  55. }
  56. return villagerGO;
  57. }
  58. GameObject CreateBasicVillager(Vector3 position)
  59. {
  60. GameObject villagerGO = GameObject.CreatePrimitive(PrimitiveType.Capsule);
  61. villagerGO.name = "Villager";
  62. villagerGO.tag = "Villager";
  63. villagerGO.transform.position = position;
  64. // Add Villager component
  65. Villager villager = villagerGO.AddComponent<Villager>();
  66. // Apply material if available
  67. if (gamePrefabs?.villagerMaterial != null)
  68. {
  69. villagerGO.GetComponent<Renderer>().material = gamePrefabs.villagerMaterial;
  70. }
  71. // Create selection indicator
  72. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  73. indicator.transform.SetParent(villagerGO.transform);
  74. indicator.transform.localPosition = Vector3.down * 0.6f;
  75. indicator.transform.localScale = Vector3.one * 1.2f;
  76. indicator.GetComponent<Renderer>().material.color = Color.yellow;
  77. indicator.SetActive(false);
  78. villager.selectionIndicator = indicator;
  79. Destroy(indicator.GetComponent<Collider>());
  80. return villagerGO;
  81. }
  82. public GameObject CreateTownHall(Vector3 position)
  83. {
  84. GameObject townHallGO;
  85. if (gamePrefabs?.townHallPrefab != null)
  86. {
  87. townHallGO = Instantiate(gamePrefabs.townHallPrefab, position, Quaternion.identity);
  88. }
  89. else
  90. {
  91. // Fallback creation
  92. townHallGO = CreateBasicTownHall(position);
  93. }
  94. return townHallGO;
  95. }
  96. GameObject CreateBasicTownHall(Vector3 position)
  97. {
  98. GameObject townHallGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
  99. townHallGO.name = "TownHall";
  100. townHallGO.tag = "TownHall";
  101. townHallGO.transform.position = position;
  102. townHallGO.transform.localScale = new Vector3(3f, 2f, 3f);
  103. // Add TownHall component
  104. TownHall townHall = townHallGO.AddComponent<TownHall>();
  105. // Set material color
  106. townHallGO.GetComponent<Renderer>().material.color = Color.blue;
  107. // Create selection indicator
  108. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  109. indicator.transform.SetParent(townHallGO.transform);
  110. indicator.transform.localPosition = Vector3.down * 1.2f;
  111. indicator.transform.localScale = Vector3.one * 1.5f;
  112. indicator.GetComponent<Renderer>().material.color = Color.yellow;
  113. indicator.SetActive(false);
  114. townHall.selectionIndicator = indicator;
  115. Destroy(indicator.GetComponent<Collider>());
  116. return townHallGO;
  117. }
  118. public GameObject CreateResourceNode(ResourceNodeType nodeType, Vector3 position)
  119. {
  120. GameObject nodePrefab = nodeType switch
  121. {
  122. ResourceNodeType.Tree => gamePrefabs?.treePrefab,
  123. ResourceNodeType.Stone => gamePrefabs?.stonePrefab,
  124. ResourceNodeType.Farm => gamePrefabs?.farmPrefab,
  125. _ => null
  126. };
  127. GameObject nodeGO;
  128. if (nodePrefab != null)
  129. {
  130. nodeGO = Instantiate(nodePrefab, position, Quaternion.identity);
  131. }
  132. else
  133. {
  134. nodeGO = CreateBasicResourceNode(nodeType, position);
  135. }
  136. return nodeGO;
  137. }
  138. GameObject CreateBasicResourceNode(ResourceNodeType nodeType, Vector3 position)
  139. {
  140. GameObject nodeGO;
  141. ResourceNode node;
  142. switch (nodeType)
  143. {
  144. case ResourceNodeType.Tree:
  145. nodeGO = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  146. nodeGO.transform.localScale = new Vector3(1f, 2f, 1f);
  147. nodeGO.GetComponent<Renderer>().material.color = Color.green;
  148. nodeGO.name = "Tree";
  149. nodeGO.tag = "Tree";
  150. node = nodeGO.AddComponent<ResourceNode>();
  151. node.nodeType = ResourceNodeType.Tree;
  152. node.resourceType = ResourceType.Wood;
  153. node.maxResources = 50;
  154. node.currentResources = 50;
  155. node.regenerationRate = 2f;
  156. break;
  157. case ResourceNodeType.Stone:
  158. nodeGO = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  159. nodeGO.transform.localScale = Vector3.one * 1.5f;
  160. nodeGO.GetComponent<Renderer>().material.color = Color.gray;
  161. nodeGO.name = "Stone";
  162. nodeGO.tag = "Stone";
  163. node = nodeGO.AddComponent<ResourceNode>();
  164. node.nodeType = ResourceNodeType.Stone;
  165. node.resourceType = ResourceType.Stone;
  166. node.maxResources = 75;
  167. node.currentResources = 75;
  168. node.regenerationRate = 0.5f;
  169. break;
  170. case ResourceNodeType.Farm:
  171. nodeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
  172. nodeGO.transform.localScale = new Vector3(2f, 0.2f, 2f);
  173. nodeGO.GetComponent<Renderer>().material.color = Color.yellow;
  174. nodeGO.name = "Farm";
  175. nodeGO.tag = "Farm";
  176. node = nodeGO.AddComponent<ResourceNode>();
  177. node.nodeType = ResourceNodeType.Farm;
  178. node.resourceType = ResourceType.Food;
  179. node.maxResources = 100;
  180. node.currentResources = 100;
  181. node.regenerationRate = 3f;
  182. break;
  183. default:
  184. return null;
  185. }
  186. nodeGO.transform.position = position;
  187. // Create selection indicator
  188. CreateSelectionIndicatorForNode(nodeGO, node);
  189. return nodeGO;
  190. }
  191. void CreateSelectionIndicatorForNode(GameObject parent, ResourceNode node)
  192. {
  193. GameObject indicator = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
  194. indicator.transform.SetParent(parent.transform);
  195. indicator.transform.localPosition = Vector3.down * 0.6f;
  196. indicator.transform.localScale = Vector3.one * 1.2f;
  197. if (gamePrefabs?.selectionMaterial != null)
  198. {
  199. indicator.GetComponent<Renderer>().material = gamePrefabs.selectionMaterial;
  200. }
  201. else
  202. {
  203. indicator.GetComponent<Renderer>().material.color = Color.cyan;
  204. }
  205. indicator.SetActive(false);
  206. node.selectionIndicator = indicator;
  207. Destroy(indicator.GetComponent<Collider>());
  208. }
  209. public Material GetGhostMaterial()
  210. {
  211. return gamePrefabs?.ghostMaterial;
  212. }
  213. }