void Update() { // Process mercenary selection if (Input.GetMouseButtonDown(0)) { GameObject old_selection = soldierSelected; // Find all mercenaries GameObject[] mercenaries = GameObject.FindGameObjectsWithTag("Mercenary"); foreach (GameObject mercenary_go in mercenaries) { // Get component var mercenary_ctrl = mercenary_go.GetComponent<SoldierController>(); if (mercenary_ctrl.position == cursor.tile) { // Same selection, do nothing if (old_selection == mercenary_go) { old_selection = null; break; } // Associate new selection soldierSelected = mercenary_go; // Attach hover.transform.parent = mercenary_go.transform; hover.transform.localPosition = Vector3.zero; break; } } // Wasn't new selection, move if (soldierSelected != null && old_selection == soldierSelected) { // Only if target valid if (cursor.tile.walkable()) { // Search for path var soldier_controller = soldierSelected.GetComponentInChildren<SoldierController>(); var path = pathManager.CreatePath(terrainManager, soldier_controller.position, cursor.tile); // Parse path and create actions soldiersPaths[soldier_controller] = new SoldierPathManager(soldier_controller, path); // Visualize path pathVisualizer.CreatePath(path); } } } // Run all paths var paths_to_remove = new List<SoldierController>(); foreach (var it in soldiersPaths) { // Update action if (!it.Value.finished) it.Value.Run(); // Has finished, remove it else paths_to_remove.Add(it.Key); } // Remove unneeded one foreach(var it in paths_to_remove) { soldiersPaths.Remove(it); } }
void Update() { // Process mercenary selection if (Input.GetMouseButtonDown(0)) { GameObject old_selection = soldierSelected; // Find all mercenaries GameObject[] mercenaries = GameObject.FindGameObjectsWithTag("Mercenary"); foreach (GameObject mercenary_go in mercenaries) { // Get component var mercenary_ctrl = mercenary_go.GetComponent <SoldierController>(); if (mercenary_ctrl.position == cursor.tile) { // Same selection, do nothing if (old_selection == mercenary_go) { old_selection = null; break; } // Associate new selection soldierSelected = mercenary_go; // Attach hover.transform.parent = mercenary_go.transform; hover.transform.localPosition = Vector3.zero; break; } } // Wasn't new selection, move if (soldierSelected != null && old_selection == soldierSelected) { // Only if target valid if (cursor.tile.walkable()) { // Search for path var soldier_controller = soldierSelected.GetComponentInChildren <SoldierController>(); var path = pathManager.CreatePath(terrainManager, soldier_controller.position, cursor.tile); // Parse path and create actions soldiersPaths[soldier_controller] = new SoldierPathManager(soldier_controller, path); // Visualize path pathVisualizer.CreatePath(path); } } } // Run all paths var paths_to_remove = new List <SoldierController>(); foreach (var it in soldiersPaths) { // Update action if (!it.Value.finished) { it.Value.Run(); } // Has finished, remove it else { paths_to_remove.Add(it.Key); } } // Remove unneeded one foreach (var it in paths_to_remove) { soldiersPaths.Remove(it); } }