コード例 #1
0
    void Dragging()
    {
        Ray        cameraRay = _currentCamera.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(cameraRay, out hit, float.PositiveInfinity, ~LayerMask.GetMask("Cube")))
        {
            transform.position = hit.point + Vector3.up * 0.5f;
            _currentOnCell     = BaseCellPlane.GetCell(BaseCellPlane.ToGridPosition(hit.point));
            if (!_currentOnCell)
            {
                return;
            }

            _highLightObj.gameObject.SetActive(true);
            _highLightObj.transform.position = _currentOnCell.WorldPosition + Vector3.up * 0.001f;

            _highLightObj.material.color =
                _currentOnCell.CanBuildBuilding && _currentOnCell.Building == null ?
                _buildableHighLightColor :
                _unbuildableHighLightColor;
        }
        else
        {
            transform.position = _currentCamera.ScreenToWorldPoint(Input.mousePosition + Vector3.forward);
            _highLightObj.gameObject.SetActive(false);
            _currentOnCell = null;
        }
    }
コード例 #2
0
    private void InstanceHighLightObjs(int range, int rangeBuidlingSet, Vector2Int position, GameObject highLightObj)
    {
        Vector3 gridToWorldpos = BaseCellPlane.ToWorldPosition(position);

        Instantiate(highLightObj, gridToWorldpos + Vector3.up * 0.001f, this.transform.rotation, this.transform);


        range++;
        if (range > rangeBuidlingSet)
        {
            return;
        }

        Vector2Int[] nextPosition =
        {
            position + Vector2Int.right,
            position + Vector2Int.up,
            position + Vector2Int.left,
            position + Vector2Int.down
        };

        foreach (var pos in nextPosition)
        {
            InstanceHighLightObjs(range, rangeBuidlingSet, pos, highLightObj);
        }
    }
コード例 #3
0
    void GetScores(Vector2Int position, Building building, int range, List <Component> checkedList, ref float score)   // Position 是当前building所放置的位置,building是当前拖拽的building,
    {
        var cell = BaseCellPlane.GetCell(position);

        if (cell != null)
        {
            float value = 0;

            if (cell.Building != null &&
                !checkedList.Contains(cell.Building))
            {
                checkedList.Add(cell.Building);
                if (CheckBuilding(building, cell.Building, out value))
                {
                    PopMessage(value, cell.Building.transform.position);
                    score += value;
                }
            }

            if (!checkedList.Contains(cell) && !isCellTypeChecked[(int)cell.CellType])
            {
                checkedList.Add(cell);
                isCellTypeChecked[(int)cell.CellType] = true;
                if (CheckCell(building, cell, out value))
                {
                    PopMessage(value, building.transform.position);
                    score += value;
                }
            }
        }

        range++;
        if (range > building.Range)
        {
            return;
        }

        Vector2Int[] nextPositions =
        {
            position + Vector2Int.right,
            position + Vector2Int.up,
            position + Vector2Int.left,
            position + Vector2Int.down
        };

        foreach (var pos in nextPositions)
        {
            GetScores(pos, building, range, checkedList, ref score);
        }
    }
コード例 #4
0
    bool outOfRange(Vector2Int position)
    {
        Vector3    leftBottomObjPos = BaseCellPlane.getLeftBottomCell().transform.position;
        Vector3    rightTopObjPos   = BaseCellPlane.getRightTopCell().transform.position;
        Vector3    gridSize         = (rightTopObjPos - leftBottomObjPos) / cellSize;
        Vector2Int gridSizeInt      = new Vector2Int(
            Mathf.RoundToInt(gridSize.x),
            Mathf.RoundToInt(gridSize.z));
        Vector2Int leftBottomObjGridPos = BaseCellPlane.ToGridPosition(leftBottomObjPos);
        Vector2Int rightTopObjGridPos   = BaseCellPlane.ToGridPosition(rightTopObjPos);

        if ((position.x - leftBottomObjGridPos.x) > gridSizeInt.x || (position.y - leftBottomObjGridPos.y) > gridSizeInt.y ||
            Mathf.Abs((position.x - rightTopObjGridPos.x)) > gridSizeInt.x || Mathf.Abs((position.y - rightTopObjGridPos.y)) > gridSizeInt.y)
        {
            return(true);
        }

        return(false);
    }
コード例 #5
0
    public static float Calculate(Building building)
    {
        float            score       = 0;
        Vector2Int       position    = BaseCellPlane.ToGridPosition(building.transform.position);
        List <Component> checkedList = new List <Component>();

        checkedList.Add(building);
        checkedList.Add(BaseCellPlane.GetCell(position));

        for (int i = 0; i < Instance.isCellTypeChecked.Length; i++)
        {
            Instance.isCellTypeChecked[i] = false;
        }

        Instance.GetScores(position, building, 0, checkedList, ref score);
        Instance.totalScore    += score;
        Instance.scoreText.text = Instance.totalScore.ToString();

        print(Instance.totalScore);
        return(score);
    }
コード例 #6
0
 private void Update()
 {
     position = BaseCellPlane.ToGridPosition(GetComponent <Building>().transform.position);
     InstanceHighLightObjs(0, rangeBuildingSet, position, highLightObj);
 }