SettlementContext.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using UnityEngine;
  2. /// <summary>
  3. /// Singleton that persists settlement data across scene transitions
  4. /// Ensures town generation is consistent based on settlement type
  5. /// </summary>
  6. public class SettlementContext : MonoBehaviour
  7. {
  8. public static SettlementContext Instance { get; private set; }
  9. [Header("Current Settlement Data")]
  10. public string settlementName = "Haven Village";
  11. public SettlementType settlementType = SettlementType.Town;
  12. public bool hasHarbor = false;
  13. public Vector2Int mapPosition = Vector2Int.zero;
  14. public int settlementSeed = 0; // For consistent generation
  15. [Header("Generated Data")]
  16. public TownShop[] availableShops;
  17. public ShopData[] generatedShopData; // Data-only shop definitions
  18. public bool isGenerated = false;
  19. [Header("Debug")]
  20. public bool debugMode = false;
  21. private void Awake()
  22. {
  23. // Singleton pattern - persist across scenes
  24. if (Instance == null)
  25. {
  26. Instance = this;
  27. DontDestroyOnLoad(gameObject);
  28. if (debugMode) Debug.Log("SettlementContext created and marked as DontDestroyOnLoad");
  29. }
  30. else
  31. {
  32. if (debugMode) Debug.Log("SettlementContext already exists, destroying duplicate");
  33. Destroy(gameObject);
  34. }
  35. }
  36. /// <summary>
  37. /// Set settlement data before entering town scene
  38. /// </summary>
  39. public void SetSettlement(string name, SettlementType type, Vector2Int position, bool harbor = false)
  40. {
  41. settlementName = name;
  42. settlementType = type;
  43. mapPosition = position;
  44. hasHarbor = harbor;
  45. // Generate consistent seed based on position and name
  46. settlementSeed = (position.x * 1000 + position.y) ^ name.GetHashCode();
  47. // Clear existing generation to force regeneration
  48. isGenerated = false;
  49. availableShops = null;
  50. Debug.Log($"Settlement context set: {name} ({type}) at {position}, seed: {settlementSeed}");
  51. }
  52. /// <summary>
  53. /// <summary>
  54. /// Generate shops based on settlement type - just logs what would be created
  55. /// TownShopManager handles the actual shop configuration
  56. /// </summary>
  57. public TownShop[] GenerateShops()
  58. {
  59. if (isGenerated)
  60. {
  61. Debug.Log("Settlement already generated");
  62. return new TownShop[0];
  63. }
  64. Debug.Log($"Planning shops for {settlementType}: {settlementName}");
  65. // Use settlement seed for consistent generation logging
  66. var oldState = Random.state;
  67. Random.InitState(settlementSeed);
  68. if (settlementType == SettlementType.Town)
  69. {
  70. Debug.Log("Town will have: Weapons, Armor, Potions, General shops");
  71. if (Random.value > 0.3f) // 70% chance
  72. {
  73. Debug.Log("Town will also have: Adventurer's Guild");
  74. }
  75. if (hasHarbor && Random.value > 0.5f) // 50% chance if near water
  76. {
  77. Debug.Log("Town will also have: Harbor Trading Post");
  78. }
  79. }
  80. else // Village
  81. {
  82. Debug.Log("Village will have: General Store");
  83. if (Random.value > 0.4f) // 60% chance
  84. {
  85. var shopTypes = new string[] { "Herb Shop", "Old Weaponsmith" };
  86. int index = Random.Range(0, shopTypes.Length);
  87. Debug.Log($"Village will also have: {shopTypes[index]}");
  88. }
  89. }
  90. // Restore random state
  91. Random.state = oldState;
  92. isGenerated = true;
  93. Debug.Log("Shop configuration complete - TownShopManager will handle activation");
  94. return new TownShop[0]; // No MonoBehaviour instances created here
  95. }
  96. /// <summary>
  97. /// Get shops for current settlement (generates if needed)
  98. /// </summary>
  99. public TownShop[] GetShops()
  100. {
  101. if (!isGenerated || availableShops == null)
  102. {
  103. return GenerateShops();
  104. }
  105. return availableShops;
  106. }
  107. /// <summary>
  108. /// Clear settlement data (for testing)
  109. /// </summary>
  110. [ContextMenu("Clear Settlement Data")]
  111. public void ClearSettlementData()
  112. {
  113. isGenerated = false;
  114. availableShops = null;
  115. Debug.Log("Settlement data cleared");
  116. }
  117. /// <summary>
  118. /// Debug method to test different settlement types
  119. /// </summary>
  120. [ContextMenu("Test Village Generation")]
  121. public void TestVillageGeneration()
  122. {
  123. SetSettlement("Test Village", SettlementType.Village, new Vector2Int(50, 50));
  124. var shops = GenerateShops();
  125. Debug.Log($"Village shops: {string.Join(", ", System.Array.ConvertAll(shops, s => s.buildingName))}");
  126. }
  127. [ContextMenu("Test Town Generation")]
  128. public void TestTownGeneration()
  129. {
  130. SetSettlement("Test Town", SettlementType.Town, new Vector2Int(100, 100), true);
  131. var shops = GenerateShops();
  132. Debug.Log($"Town shops: {string.Join(", ", System.Array.ConvertAll(shops, s => s.buildingName))}");
  133. }
  134. }