// Start is called before the first frame update void Start() { startMousePos = Input.mousePosition; startMouseWorldPos = Crosshair.MouseToWorldPos(); boxSelection.gameObject.SetActive(false); isBuilding = false; selectedUnits = new List <Unit>(); }
// Update is called once per frame void Update() { // If we're building don't accept user input from here. if (isBuilding) { return; } // LMB has been pressed if (Input.GetMouseButtonDown(0)) { startMousePos = Input.mousePosition; startMouseWorldPos = Crosshair.MouseToWorldPos(); boxSelection.gameObject.SetActive(true); SingleSelect(); } // LMB is being held if (Input.GetMouseButton(0)) { currentMousePos = Input.mousePosition; currentMouseWorldPos = Crosshair.MouseToWorldPos(); UpdateBoxSelection(); } // LMB has been released if (Input.GetMouseButtonUp(0)) { BoxSelectUnits(); boxSelection.gameObject.SetActive(false); } // RMB has been pressed if (Input.GetMouseButtonDown(1)) { CommandUnits(); // Move, Attack, Gather } // TODO: Add Villager Commands (Build, etc.) // Create House if (Input.GetKeyDown(KeyCode.Q)) { // Check if we have enough resources to build if (!GameController.Instance.CheckResourceCost(buildingPrefabs[0].GetComponent <Building>().BuildCosts)) { Debug.Log("Not enough resources to build: " + buildingPrefabs[0].name); return; } // Check if we have villagers selected to build. if (IsVillagerSelected()) { StartCoroutine(PlaceBuilding(buildingPrefabs[0])); } else { Debug.Log("Cannot build: " + buildingPrefabs[0].name + " as there are no villagers selected."); } } // Create Barracks if (Input.GetKeyDown(KeyCode.W)) { // Check if we have enough resources to build if (!GameController.Instance.CheckResourceCost(buildingPrefabs[1].GetComponent <Building>().BuildCosts)) { Debug.Log("Not enough resources to build: " + buildingPrefabs[1].name); return; } // Check if we have villagers selected to build. if (IsVillagerSelected()) { StartCoroutine(PlaceBuilding(buildingPrefabs[1])); } else { Debug.Log("Cannot build: " + buildingPrefabs[1].name + " as there are no villagers selected."); } } }
private IEnumerator PlaceBuilding(GameObject buildingPrefab) { isBuilding = true; // Create the building Vector3 offset = new Vector3(0, buildingPrefab.transform.position.y, 0); GameObject building = Instantiate(buildingPrefab, Crosshair.MouseToWorldPos() + offset, buildingPrefab.transform.rotation); Collider col = building.GetComponent <Collider>(); Building b = building.GetComponent <Building>(); b.enabled = false; // Make sure a building component is attached if (b == null) { Debug.LogWarning("Place Building Error: Provided building gameObject does not have Building component attached."); yield break; } // While we're still building, update the building to be at the mouse position and check for collisions while (isBuilding) { building.transform.position = Crosshair.MouseToWorldPos() + offset; // Try to place building if LMB is pressed if (Input.GetMouseButtonDown(0)) { // Check if the building's collider is overlapping with anything other than the ground and itself before placing Collider[] cols = Physics.OverlapBox(building.transform.position, col.bounds.extents); if (cols.Length > 2) { Debug.Log("Not enough space to place: " + building.name + "here."); yield return(new WaitForFixedUpdate()); // Wait for end of fixed update to prevent freeze continue; } // If there was space, command the selected villagers to build the building. selectedUnits.ForEach(u => { Villager vil = u.GetComponent <Villager>(); if (vil != null) { vil.Build(b); } }); b.StartBuilding(); isBuilding = false; yield break; } // Cancel build if RMB is pressed if (Input.GetMouseButtonDown(1)) { Debug.Log("RMB pressed."); Destroy(building); // wait a moment before setting is building to false, so that selected units don't try and follow commands yield return(new WaitForSeconds(0.1f)); isBuilding = false; yield break; } // Wait for end of fixed update to prevent freeze yield return(new WaitForFixedUpdate()); } // Done yield break; }