SettlementContext.cs 4.9 KB

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