Example #1
0
    private bool buildAtMouse()
    {
        Ray   ray      = Camera.main.ScreenPointToRay(Input.mousePosition);
        Plane plane    = new Plane(Vector3.up, Vector3.zero);
        float distance = 0;

        if (plane.Raycast(ray, out distance))
        {
            Vector3    intersect = ray.GetPoint(distance);
            int        row       = Mathf.FloorToInt(intersect.x);
            int        col       = Mathf.FloorToInt(intersect.z);
            GameObject prefab    = BuildingFactory.getBuildingPrefab(currentBuildingType);
            if (gridManager.canBuildAt(prefab, row, col))
            {
                //if (!gridManager.wouldBlockPathAt(prefab, row, col)) {
                return(gridManager.placeBuildingAt(BuildingFactory.createBuilding(currentBuildingType), row, col));
                //}
            }
        }
        return(false);
    }
Example #2
0
    private void updateBuildingPreview()
    {
        if (!buildMode || buildMenu)
        {
            return;
        }

        Color previewColor;
        float distance = 0;
        Plane plane    = new Plane(Vector3.up, Vector3.zero);
        Ray   ray      = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (plane.Raycast(ray, out distance))
        {
            GameObject buildingPrefab = BuildingFactory.getBuildingPrefab(currentBuildingType);

            createPreview(buildingPrefab);

            Vector3 intersect = ray.GetPoint(distance);
            int     row       = Mathf.FloorToInt(intersect.x);
            int     col       = Mathf.FloorToInt(intersect.z);
            if (gridManager.canBuildAt(buildingPrefab, row, col))
            {
                previewColor = PREVIEW_GREEN;
            }
            else
            {
                previewColor = PREVIEW_RED;
            }
            Vector3 center = gridManager.getCenterAt(buildingPrefab.GetComponent <Building>(), row, col);
            if (center != Vector3.zero)
            {
                preview.transform.position = center + PREVIEW_OFFSET;
            }

            Renderer renderer = preview.GetComponent <Renderer>();
            renderer.material.color = previewColor;
        }
    }