/// <summary> /// Create new building GameObject and render it, /// attach BuildingModel, PlaceBuildingController and BuildingObjectController to the GameObject, /// pass the needed references to those components /// </summary> /// <param name="buildingModel">BuildingModel of the building</param> private void RenderBuilding(BuildingModel buildingModel, bool placeInstantly) { buildingModel.CbRegisterResourcesChanged(OnResourcesChanged); GameObject cube = Instantiate(g); // TODO: Placeholder //cube.SetActive(false); cube.transform.parent = this.transform; cube.name = buildingModel.buildingType.GetName(); cube.GetComponent <Renderer>().material.color = Color.red; cube.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f); if (!placeInstantly) { cube.AddComponent <PlaceBuildingController>().SetReferences(buildingModel, mapLayer); } else { buildingModel.NotifyPlaced(); } cube.AddComponent <BuildingObjectController>().SetReferences(buildingModel, placeInstantly); buildingMenuPanel.SetActive(false); }
/// <summary> /// Update is called once per frame /// Lets the building position follow the mouse position /// </summary> void Update() { if (mapCollider != null) { if (Input.GetMouseButtonDown(0)) // Place the building if GameObject is active = positioned and left mouse button is clicked { Debug.Log("Place Building here"); buildingModel.NotifyPlaced(); Destroy(this); } else // Update the building position to the mouse position on the map { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (mapCollider.Raycast(ray, out hit, 100.0F)) { int x = (int)Mathf.Floor(hit.point.x); int z = (int)Mathf.Floor(hit.point.z); //Debug.Log("Move to " + x + "/" + z); buildingModel.SetPosition(x, z); } } } }