public void BuildRoads(int gridPosX, int gridPosY) { if (Input.GetMouseButtonDown(0)) { //Start generation _roadGenerator.Start(); _mouseDownPosX = gridPosX; _mouseDownPosY = gridPosY; } if (Input.GetMouseButton(0)) { //Generating _roadGenerator.ClearSavedRoads(); if (_roadGenerator.CanCreateRoad(gridPosX, gridPosY)) { Vector2Int startPos = new Vector2Int(_mouseDownPosX, _mouseDownPosY); Vector2Int endPos = new Vector2Int(gridPosX, gridPosY); List <Vector2Int> path = Map.GetShortestPath(startPos, endPos); foreach (Vector2Int road in path) { _roadGenerator.Generate(road.x, road.y); } } } if (Input.GetMouseButtonUp(0)) { //End generation _roadGenerator.End(); } if (Input.GetMouseButtonDown(1)) { //Cancel generation _roadGenerator.Cancel(); } }
// Launches the generation of the specified road (or all of them if index = -1). public void RecalculateRoads(int index = -1) { for (int i = ((index < 0) ? 0 : index); i < ((index < 0) ? roadsDefinitionData.Count : (index + 1)); i++) { if (roadThreads.ContainsKey (i)) // If thread already running, substitute it. roadThreads [i].Abort (); Eppy.Tuple<Vector2, Vector2, float, float> roadData = roadsDefinitionData [i]; RoadGenerator thread = new RoadGenerator (roadData.Item1, roadData.Item2, roadData.Item3, roadData.Item4); thread.Start (); roadThreads [i] = thread; } // If the coroutine is already launch, no change is needed, since it will check for all currently running threads. if (roadsCoroutine == null) roadsCoroutine = StartCoroutine(WaitForRoadThreads()); }