Example #1
0
    private BlockPlacementPermission PreviewPlacement(out Vector3Int coordinateToPlace)
    {
        coordinateToPlace = Vector3Int.zero;

        if (UIHelper.IsMouseOverUI())
        {
            return(BlockPlacementPermission.OutOfRange);
        }

        Ray ray = activeCamera.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out RaycastHit hit, 100f, layerMask))
        {
            Stub stub = hit.transform.GetComponent <Stub>();
            if (stub == null)
            {
                return(BlockPlacementPermission.OutOfRange);
            }
            //Debug.Log("hit stub: " + stub.name);
            //g.transform.position = hit.point;
            coordinateToPlace = stub.BlockCoordinate + Vector3Int.up;
            BlockPlacementPermission canPlace = WorldGrid.CanPlace(previewBlock, coordinateToPlace);
            //Debug.Log("try : " + coordinateToPlace + ": " + canPlace.ToString(), stub);
            while (canPlace == BlockPlacementPermission.Occupied)
            {
                coordinateToPlace += Vector3Int.up;
                //Debug.Log("try : " + coordinateToPlace, stub);
                canPlace = WorldGrid.CanPlace(previewBlock, coordinateToPlace);
            }
            if (canPlace == BlockPlacementPermission.Valid)
            {
                //Debug.Log("can : " + coordinateToPlace, stub);
                Vector3 previewPosition = WorldGrid.CoordinateToWorldPosition(coordinateToPlace);
                previewBlock.transform.position = previewPosition;
            }
            return(canPlace);
        }
        return(BlockPlacementPermission.OutOfRange);
    }
Example #2
0
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            prefabIndex = (prefabIndex + 1) % blockPrefabs.Length;
            CreatePreviewBlock();
        }
        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            prefabIndex = (prefabIndex - 1);
            if (prefabIndex < 0)
            {
                prefabIndex = blockPrefabs.Length - 1;
            }
            CreatePreviewBlock();
        }


        if (Input.GetKeyDown(KeyCode.R))
        {
            RotateClockwise();
        }

        BlockPlacementPermission canPlace = PreviewPlacement(out Vector3Int coordinateToPlace);

        if (canPlace == BlockPlacementPermission.Valid)
        {
            previewBlock.gameObject.SetActive(true);
            if (Input.GetMouseButtonDown(0))
            {
                PlaceBlockAt(coordinateToPlace);
            }
        }
        else
        {
            previewBlock.gameObject.SetActive(false);
        }
    }