SimpleTeamPlacement.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. /// <summary>
  5. /// Simple standalone team placement script that randomly places the team on a settlement
  6. /// and creates a visible marker in front of the map.
  7. /// </summary>
  8. public class SimpleTeamPlacement : MonoBehaviour
  9. {
  10. [Header("Team Marker Settings")]
  11. public GameObject teamMarkerPrefab;
  12. public Material teamMarkerMaterial;
  13. [Range(0.5f, 3f)]
  14. public float markerSize = 1.5f;
  15. [Range(-5f, 5f)]
  16. public float markerHeight = 0.5f; // Y coordinate height above map tiles
  17. public Color markerColor = Color.green;
  18. [Header("Animation Settings")]
  19. public bool enableBlinking = true;
  20. [Range(0.1f, 2f)]
  21. public float blinkSpeed = 0.5f;
  22. // Private variables
  23. private GameObject teamMarkerInstance;
  24. private Vector2Int currentTeamPosition;
  25. private bool isTeamPositionSet = false;
  26. private bool isBlinking = false;
  27. private MapMaker2 mapMaker;
  28. public static SimpleTeamPlacement Instance { get; private set; }
  29. /// <summary>
  30. /// Gets the current team marker GameObject instance
  31. /// </summary>
  32. public GameObject TeamMarkerInstance => teamMarkerInstance;
  33. /// <summary>
  34. /// Gets the world position of the team marker
  35. /// </summary>
  36. public Vector3 TeamMarkerWorldPosition
  37. {
  38. get
  39. {
  40. if (teamMarkerInstance != null)
  41. return teamMarkerInstance.transform.position;
  42. return Vector3.zero;
  43. }
  44. }
  45. void Awake()
  46. {
  47. if (Instance == null)
  48. {
  49. Instance = this;
  50. }
  51. else
  52. {
  53. Debug.LogWarning("Multiple SimpleTeamPlacement instances found. Destroying this one.");
  54. Destroy(gameObject);
  55. return;
  56. }
  57. }
  58. void Start()
  59. {
  60. // Find MapMaker2
  61. mapMaker = FindFirstObjectByType<MapMaker2>();
  62. if (mapMaker == null)
  63. {
  64. return;
  65. }
  66. // Wait a bit for map generation, then place team
  67. StartCoroutine(WaitAndPlaceTeam());
  68. }
  69. void Update()
  70. {
  71. // Debug keys
  72. if (Input.GetKeyDown(KeyCode.R))
  73. {
  74. RandomlyPlaceTeam();
  75. }
  76. if (Input.GetKeyDown(KeyCode.M))
  77. {
  78. ShowMarkerInfo();
  79. }
  80. // Debug key to check saved positions
  81. if (Input.GetKeyDown(KeyCode.P))
  82. {
  83. DebugSavedPositions();
  84. }
  85. // Travel system integration - allow clicking on map to plan travel
  86. if (Input.GetKeyDown(KeyCode.T))
  87. {
  88. ShowTravelInfo();
  89. }
  90. }
  91. private IEnumerator WaitAndPlaceTeam()
  92. {
  93. // Wait for map generation
  94. yield return new WaitForSeconds(1.5f);
  95. // Check if this is a new game that should force random placement
  96. // First ensure GameStateManager exists, create it if needed
  97. if (GameStateManager.Instance == null)
  98. {
  99. GameObject gameStateObj = new GameObject("GameStateManager");
  100. gameStateObj.AddComponent<GameStateManager>();
  101. // Wait a frame for the GameStateManager to initialize
  102. yield return null;
  103. }
  104. bool isNewGame = GameStateManager.Instance?.IsNewGameForceRandom() ?? false;
  105. // ALWAYS show new game detection debug logs (even if showDebugLogs is false)
  106. if (isNewGame)
  107. {
  108. RandomlyPlaceTeam();
  109. // Clear the new game flag after placement
  110. GameStateManager.Instance?.ClearNewGameFlag();
  111. yield break;
  112. }
  113. // Try multiple times to load saved position (in case MapMaker2 isn't ready yet)
  114. int attempts = 0;
  115. const int maxAttempts = 5;
  116. while (attempts < maxAttempts)
  117. {
  118. attempts++;
  119. // Try to load saved position first
  120. if (TryLoadSavedPosition())
  121. {
  122. yield break; // Exit successfully
  123. }
  124. // If failed and MapMaker seed is still 0, wait a bit more
  125. if (mapMaker?.seed == 0 && attempts < maxAttempts)
  126. {
  127. yield return new WaitForSeconds(0.5f);
  128. }
  129. else
  130. {
  131. break; // Either loaded successfully or MapMaker is ready but no saved position
  132. }
  133. }
  134. // No saved position or invalid, place randomly
  135. RandomlyPlaceTeam();
  136. }
  137. private bool TryLoadSavedPosition()
  138. {
  139. // Get the current map seed to make position specific to this map
  140. int currentSeed = mapMaker?.seed ?? 0;
  141. string positionKey = $"TeamPosition_{currentSeed}";
  142. if (currentSeed == 0)
  143. {
  144. return false;
  145. }
  146. if (!PlayerPrefs.HasKey($"{positionKey}_X") || !PlayerPrefs.HasKey($"{positionKey}_Y"))
  147. {
  148. return false;
  149. }
  150. Vector2Int savedPosition = new Vector2Int(
  151. PlayerPrefs.GetInt($"{positionKey}_X"),
  152. PlayerPrefs.GetInt($"{positionKey}_Y")
  153. );
  154. // Verify position is still valid (any valid map position, not just settlements)
  155. if (IsValidMapPosition(savedPosition))
  156. {
  157. PlaceTeamAt(savedPosition);
  158. return true;
  159. }
  160. return false;
  161. }
  162. public void RandomlyPlaceTeam()
  163. {
  164. if (mapMaker?.GetMapData() == null)
  165. {
  166. return;
  167. }
  168. // For new games, ensure we're using the current random state for truly random placement
  169. bool isNewGame = GameStateManager.Instance?.IsNewGameForceRandom() ?? false;
  170. var mapData = mapMaker.GetMapData();
  171. var allSettlements = new List<Settlement>();
  172. // Combine towns and villages into one list for random selection
  173. allSettlements.AddRange(mapData.GetTowns());
  174. allSettlements.AddRange(mapData.GetVillages());
  175. if (allSettlements.Count == 0)
  176. {
  177. return;
  178. }
  179. // Randomly select a settlement
  180. int randomIndex = Random.Range(0, allSettlements.Count);
  181. Settlement selectedSettlement = allSettlements[randomIndex];
  182. PlaceTeamAt(selectedSettlement.position);
  183. }
  184. public void PlaceTeamAt(Vector2Int position)
  185. {
  186. currentTeamPosition = position;
  187. isTeamPositionSet = true;
  188. // Save position with map seed for persistence
  189. int currentSeed = mapMaker?.seed ?? 0;
  190. string positionKey = $"TeamPosition_{currentSeed}";
  191. bool isNewGame = GameStateManager.Instance?.IsNewGameForceRandom() ?? false;
  192. PlayerPrefs.SetInt($"{positionKey}_X", position.x);
  193. PlayerPrefs.SetInt($"{positionKey}_Y", position.y);
  194. PlayerPrefs.Save();
  195. // Create/update visual marker
  196. CreateTeamMarker();
  197. // Notify MapMaker2 of position change for event-driven exploration
  198. if (mapMaker != null)
  199. {
  200. mapMaker.OnTeamPositionChanged(position);
  201. // Set TeamMarker reference in SettlementInteractionManager
  202. var settlementManager = FindFirstObjectByType<SettlementInteractionManager>();
  203. if (settlementManager != null)
  204. {
  205. settlementManager.SetTeamMarker(teamMarkerInstance.transform);
  206. Debug.Log($"[SimpleTeamPlacement] Set TeamMarker reference in SettlementInteractionManager");
  207. }
  208. else
  209. {
  210. Debug.LogWarning("[SimpleTeamPlacement] SettlementInteractionManager not found!");
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// Cleans up position data for old map seeds (optional maintenance)
  216. /// </summary>
  217. public void CleanupOldPositionData()
  218. {
  219. int currentSeed = mapMaker?.seed ?? 0;
  220. int cleanedCount = 0;
  221. // This is a simple cleanup - in a real game you might want more sophisticated management
  222. for (int i = 0; i < 10; i++) // Check last 10 potential seeds for cleanup
  223. {
  224. int testSeed = currentSeed - i;
  225. if (testSeed != currentSeed && testSeed > 0)
  226. {
  227. string positionKey = $"TeamPosition_{testSeed}";
  228. if (PlayerPrefs.HasKey($"{positionKey}_X"))
  229. {
  230. PlayerPrefs.DeleteKey($"{positionKey}_X");
  231. PlayerPrefs.DeleteKey($"{positionKey}_Y");
  232. cleanedCount++;
  233. }
  234. }
  235. }
  236. }
  237. /// <summary>
  238. /// Debug method to check what positions are saved in PlayerPrefs
  239. /// </summary>
  240. private void DebugSavedPositions()
  241. {
  242. int currentSeed = mapMaker?.seed ?? 0;
  243. // Check current seed position
  244. string currentKey = $"TeamPosition_{currentSeed}";
  245. bool hasCurrentPosition = PlayerPrefs.HasKey($"{currentKey}_X") && PlayerPrefs.HasKey($"{currentKey}_Y");
  246. if (hasCurrentPosition)
  247. {
  248. Vector2Int currentPos = new Vector2Int(
  249. PlayerPrefs.GetInt($"{currentKey}_X"),
  250. PlayerPrefs.GetInt($"{currentKey}_Y")
  251. );
  252. }
  253. else
  254. {
  255. }
  256. // Check for other potential seeds (common ones)
  257. int[] testSeeds = { 0, 12345, 1000, 5000, 9999 };
  258. foreach (int testSeed in testSeeds)
  259. {
  260. if (testSeed == currentSeed) continue; // Already checked
  261. string testKey = $"TeamPosition_{testSeed}";
  262. if (PlayerPrefs.HasKey($"{testKey}_X") && PlayerPrefs.HasKey($"{testKey}_Y"))
  263. {
  264. Vector2Int testPos = new Vector2Int(
  265. PlayerPrefs.GetInt($"{testKey}_X"),
  266. PlayerPrefs.GetInt($"{testKey}_Y")
  267. );
  268. }
  269. }
  270. }
  271. private void CreateTeamMarker()
  272. {
  273. // Stop any existing blinking first
  274. if (isBlinking)
  275. {
  276. StopAllCoroutines(); // This will stop the BlinkMarker coroutine
  277. isBlinking = false;
  278. }
  279. // Remove existing marker
  280. if (teamMarkerInstance != null)
  281. {
  282. DestroyImmediate(teamMarkerInstance);
  283. }
  284. // Create new marker
  285. if (teamMarkerPrefab != null)
  286. {
  287. teamMarkerInstance = Instantiate(teamMarkerPrefab);
  288. }
  289. else
  290. {
  291. // Create simple sphere marker
  292. teamMarkerInstance = GameObject.CreatePrimitive(PrimitiveType.Sphere);
  293. teamMarkerInstance.transform.localScale = Vector3.one * markerSize;
  294. teamMarkerInstance.tag = "Player";
  295. }
  296. // Position marker
  297. PositionMarker();
  298. // Apply material/color
  299. ApplyMarkerMaterial();
  300. // Set name
  301. teamMarkerInstance.name = "TeamMarker";
  302. // Start blinking if enabled
  303. if (enableBlinking)
  304. {
  305. StartBlinking();
  306. }
  307. // Notify camera to center on the newly created team marker
  308. var cameraController = FindFirstObjectByType<MMCameraController>();
  309. if (cameraController != null)
  310. {
  311. cameraController.OnTeamMarkerCreated();
  312. }
  313. }
  314. /// <summary>
  315. /// Get current team position
  316. /// </summary>
  317. public Vector2Int GetCurrentTeamPosition()
  318. {
  319. return currentTeamPosition;
  320. }
  321. /// <summary>
  322. /// Update team marker position when map coordinates change (called by exploration system)
  323. /// </summary>
  324. public void UpdateMarkerAfterMapChange(Vector2Int newVisiblePosition)
  325. {
  326. if (!isTeamPositionSet || teamMarkerInstance == null)
  327. {
  328. return;
  329. }
  330. // Store the world position before updating coordinates
  331. Vector3 currentWorldPos = teamMarkerInstance.transform.position;
  332. // Update the current team position to the new visible coordinates
  333. Vector2Int oldPosition = currentTeamPosition;
  334. currentTeamPosition = newVisiblePosition;
  335. // Reposition the marker to maintain world position consistency
  336. PositionMarker();
  337. }
  338. private void PositionMarker()
  339. {
  340. // Get tile size from MapVisualizer
  341. float tileSize = 1f;
  342. if (mapMaker?.mapVisualizer != null)
  343. {
  344. tileSize = mapMaker.mapVisualizer.tileSize;
  345. }
  346. // Calculate world position:
  347. // In exploration mode, we need to convert exploration coordinates to full map coordinates for world positioning
  348. Vector2Int worldCoordinates = currentTeamPosition;
  349. // Check if we're using exploration system
  350. if (mapMaker != null && mapMaker.useExplorationSystem)
  351. {
  352. var explorationManager = mapMaker.GetExplorationManager();
  353. if (explorationManager != null)
  354. {
  355. // Use exploration manager to convert coordinates properly
  356. Vector3 correctWorldPos = explorationManager.ConvertExplorationToWorldCoordinates(currentTeamPosition);
  357. teamMarkerInstance.transform.position = new Vector3(
  358. correctWorldPos.x,
  359. markerHeight, // Y coordinate height above map tiles
  360. correctWorldPos.z // Use Z coordinate for map Y position
  361. );
  362. return;
  363. }
  364. }
  365. // Fallback for non-exploration mode: use direct coordinate mapping
  366. Vector3 worldPosition = new Vector3(
  367. worldCoordinates.x * tileSize,
  368. markerHeight, // Y coordinate height above map tiles
  369. worldCoordinates.y * tileSize // Map Y becomes world Z for top-down view
  370. );
  371. teamMarkerInstance.transform.position = worldPosition;
  372. }
  373. private void ApplyMarkerMaterial()
  374. {
  375. Renderer renderer = teamMarkerInstance.GetComponent<Renderer>();
  376. if (renderer == null) return;
  377. if (teamMarkerMaterial != null)
  378. {
  379. renderer.material = teamMarkerMaterial;
  380. }
  381. else
  382. {
  383. // Create simple colored material using URP shaders first
  384. Shader shader = null;
  385. // Try URP shaders first
  386. shader = Shader.Find("Universal Render Pipeline/Lit");
  387. if (shader == null)
  388. shader = Shader.Find("Universal Render Pipeline/Simple Lit");
  389. if (shader == null)
  390. shader = Shader.Find("Universal Render Pipeline/Unlit");
  391. // Fallback to built-in shaders
  392. if (shader == null)
  393. shader = Shader.Find("Standard");
  394. if (shader == null)
  395. shader = Shader.Find("Legacy Shaders/Diffuse");
  396. if (shader == null)
  397. shader = Shader.Find("Unlit/Color");
  398. // Last resort fallback
  399. if (shader == null)
  400. {
  401. return;
  402. }
  403. Material material = new Material(shader);
  404. // Set base color (works for both URP and built-in)
  405. if (material.HasProperty("_BaseColor"))
  406. {
  407. material.SetColor("_BaseColor", markerColor); // URP property
  408. }
  409. else if (material.HasProperty("_Color"))
  410. {
  411. material.SetColor("_Color", markerColor); // Built-in property
  412. }
  413. material.color = markerColor; // Fallback
  414. // Add emission for better visibility
  415. if (material.HasProperty("_EmissionColor"))
  416. {
  417. material.EnableKeyword("_EMISSION");
  418. material.SetColor("_EmissionColor", markerColor * 0.3f);
  419. }
  420. renderer.material = material;
  421. }
  422. }
  423. private void StartBlinking()
  424. {
  425. if (!isBlinking)
  426. {
  427. StartCoroutine(BlinkMarker());
  428. }
  429. else
  430. {
  431. // Force restart blinking as a fallback
  432. StopAllCoroutines();
  433. isBlinking = false;
  434. StartCoroutine(BlinkMarker());
  435. }
  436. }
  437. private IEnumerator BlinkMarker()
  438. {
  439. isBlinking = true;
  440. Renderer markerRenderer = teamMarkerInstance?.GetComponent<Renderer>();
  441. if (markerRenderer == null)
  442. {
  443. isBlinking = false;
  444. yield break;
  445. }
  446. // Capture the enableBlinking state at start to avoid timing issues
  447. bool shouldBlink = enableBlinking;
  448. int blinkCount = 0;
  449. while (teamMarkerInstance != null && markerRenderer != null && shouldBlink)
  450. {
  451. // Hide marker
  452. markerRenderer.enabled = false;
  453. yield return new WaitForSeconds(blinkSpeed);
  454. // Show marker
  455. if (markerRenderer != null && teamMarkerInstance != null)
  456. {
  457. markerRenderer.enabled = true;
  458. yield return new WaitForSeconds(blinkSpeed);
  459. blinkCount++;
  460. }
  461. else
  462. {
  463. break;
  464. }
  465. // Re-check enableBlinking periodically (every 10 blinks) in case user changes it
  466. if (blinkCount % 10 == 0)
  467. {
  468. shouldBlink = enableBlinking;
  469. }
  470. }
  471. isBlinking = false;
  472. }
  473. private bool IsValidSettlementPosition(Vector2Int position)
  474. {
  475. if (mapMaker?.GetMapData() == null) return false;
  476. var mapData = mapMaker.GetMapData();
  477. if (!mapData.IsValidPosition(position.x, position.y)) return false;
  478. // Check if position has a settlement
  479. foreach (var town in mapData.GetTowns())
  480. {
  481. if (town.position == position) return true;
  482. }
  483. foreach (var village in mapData.GetVillages())
  484. {
  485. if (village.position == position) return true;
  486. }
  487. return false;
  488. }
  489. /// <summary>
  490. /// Checks if a position is valid for team placement (any valid map position)
  491. /// Used for persistent positioning - team can be anywhere on the map
  492. /// </summary>
  493. private bool IsValidMapPosition(Vector2Int position)
  494. {
  495. if (mapMaker?.GetMapData() == null)
  496. {
  497. return false;
  498. }
  499. var mapData = mapMaker.GetMapData();
  500. return mapData.IsValidPosition(position.x, position.y);
  501. }
  502. private void ShowMarkerInfo()
  503. {
  504. if (teamMarkerInstance == null)
  505. {
  506. return;
  507. }
  508. }
  509. private void ShowTravelInfo()
  510. {
  511. var travelSystem = TeamTravelSystem.Instance;
  512. if (travelSystem != null)
  513. {
  514. }
  515. else
  516. {
  517. }
  518. }
  519. // Public methods for external control
  520. public Vector2Int GetTeamPosition() => currentTeamPosition;
  521. public bool IsTeamPlaced() => isTeamPositionSet;
  522. public GameObject GetTeamMarker() => teamMarkerInstance;
  523. [ContextMenu("Randomly Place Team")]
  524. public void ContextRandomlyPlaceTeam()
  525. {
  526. RandomlyPlaceTeam();
  527. }
  528. [ContextMenu("Show Marker Info")]
  529. public void ContextShowMarkerInfo()
  530. {
  531. ShowMarkerInfo();
  532. }
  533. [ContextMenu("Force New Game Random Placement")]
  534. public void ContextForceNewGamePlacement()
  535. {
  536. // Temporarily set new game flag for testing
  537. if (GameStateManager.Instance != null)
  538. {
  539. GameStateManager.Instance.isNewGame = true;
  540. }
  541. RandomlyPlaceTeam();
  542. // Clear the flag after testing
  543. GameStateManager.Instance?.ClearNewGameFlag();
  544. }
  545. }