Ejemplo n.º 1
0
    void GetNearestRayDir(ref Ray _screenRay, ref Vector3 _srcPos, ref Vector3 _dir, ref Vector3 _dstPos)
    {
        Ray[] _rayGroup = new Ray[6];
        _rayGroup[PXAxis] = new Ray(_srcPos, m_axisPX);
        _rayGroup[NXAxis] = new Ray(_srcPos, m_axisNX);
        _rayGroup[PZAxis] = new Ray(_srcPos, m_axisPZ);
        _rayGroup[NZAxis] = new Ray(_srcPos, m_axisNZ);
        _rayGroup[PYAxis] = new Ray(_srcPos, m_axisPY);
        _rayGroup[NYAxis] = new Ray(_srcPos, m_axisNY);

        float _minDistSq = float.MaxValue;
        float _s = 0.0f, _t = 0.0f;
        int   _iDir = 0;

        for (int _i = 0; _i != 6; ++_i)
        {
            if (_rayGroup[_i].direction.x == 0.0f && _rayGroup[_i].direction.y == 0.0f && _rayGroup[_i].direction.z == 0.0f)
            {
                continue;
            }
            float _tempS = 0.0f, _tempT = 0.0f;
            float _distSq = AuxLib.Get().DistSqRayRay(ref _rayGroup[_i], ref _screenRay, ref _tempS, ref _tempT);
            if (_minDistSq > _distSq)
            {
                _minDistSq = _distSq;
                _s         = _tempS;
                _t         = _tempT;
                _iDir      = _i;
            }
        }

        _dir    = _rayGroup[_iDir].direction;
        _dstPos = _rayGroup[_iDir].origin + _rayGroup[_iDir].direction * _s;
    }
Ejemplo n.º 2
0
    void UpdateDragBrick()
    {
        Ray     _ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Vector3 _pos = m_firstBrick.m_go.transform.position;

        Ray[] _rayGroup = new Ray[6];

        if (m_firstBrick.m_dirP != NoDir && m_firstBrick.m_dirN != NoDir)
        {
            if (m_firstBrick.m_dirP == PXAxis)
            {
                _rayGroup[PXAxis] = new Ray(_pos, m_axisPX);
            }

            if (m_firstBrick.m_dirN == NXAxis)
            {
                _rayGroup[NXAxis] = new Ray(_pos, m_axisNX);
            }

            if (m_firstBrick.m_dirP == PZAxis)
            {
                _rayGroup[PZAxis] = new Ray(_pos, m_axisPZ);
            }

            if (m_firstBrick.m_dirN == NZAxis)
            {
                _rayGroup[NZAxis] = new Ray(_pos, m_axisNZ);
            }

            if (m_firstBrick.m_dirP == PYAxis)
            {
                _rayGroup[PYAxis] = new Ray(_pos, m_axisPY);
            }

            if (m_firstBrick.m_dirN == NYAxis)
            {
                _rayGroup[NYAxis] = new Ray(_pos, m_axisNY);
            }
        }
        else
        {
            _rayGroup[PXAxis] = new Ray(_pos, m_axisPX);
            _rayGroup[NXAxis] = new Ray(_pos, m_axisNX);
            _rayGroup[PZAxis] = new Ray(_pos, m_axisPZ);
            _rayGroup[NZAxis] = new Ray(_pos, m_axisNZ);
            _rayGroup[PYAxis] = new Ray(_pos, m_axisPY);
            //_rayGroup[NYAxis] = new Ray(_pos, m_axisNY);
        }

        float _minDistSq = float.MaxValue;
        float _s = 0.0f, _t = 0.0f;
        int   _iDir = 0;

        for (int _i = 0; _i != 6; ++_i)
        {
            if (_rayGroup[_i].direction.x == 0.0f && _rayGroup[_i].direction.y == 0.0f && _rayGroup[_i].direction.z == 0.0f)
            {
                continue;
            }
            float _tempS = 0.0f, _tempT = 0.0f;
            float _distSq = AuxLib.Get().DistSqRayRay(ref _rayGroup[_i], ref _ray, ref _tempS, ref _tempT);
            if (_minDistSq > _distSq)
            {
                _minDistSq = _distSq;
                _s         = _tempS;
                _t         = _tempT;
                _iDir      = _i;
            }
        }

        if (m_dragDir != _iDir)
        {
            DestroyDragList();
            m_dragIdx    = new IntVec3(m_firstBrick.m_go.transform.position);
            m_bCollision = false;
        }
        m_dragDir = _iDir;

        Vector3 _destPos = new Vector3();

        _destPos = _rayGroup[_iDir].origin + _rayGroup[_iDir].direction * _s;
        IntVec3 _destIdx = new IntVec3(_destPos);

        if (m_dragIdx != _destIdx)
        {
            int  _iOffset = 0;
            bool _bInc    = true;
            switch (m_dragDir)
            {
            case PXAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.x - m_dragIdx.x);
                if (_destIdx.x < m_dragIdx.x)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.x += _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PXAxis;
                            _cb.m_dirN = NXAxis;

                            _cb.m_go = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }

            case NXAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.x - m_dragIdx.x);
                if (_destIdx.x > m_dragIdx.x)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.x -= _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PXAxis;
                            _cb.m_dirN = NXAxis;
                            _cb.m_go   = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }

            case PYAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.y - m_dragIdx.y);
                if (_destIdx.y < m_dragIdx.y)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.y += _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PYAxis;
                            _cb.m_dirN = NYAxis;
                            _cb.m_go   = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }

            case NYAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.y - m_dragIdx.y);
                if (_destIdx.y > m_dragIdx.y)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.y -= _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PYAxis;
                            _cb.m_dirN = NYAxis;
                            _cb.m_go   = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }

            case PZAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.z - m_dragIdx.z);
                if (_destIdx.z < m_dragIdx.z)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.z += _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PZAxis;
                            _cb.m_dirN = NZAxis;
                            _cb.m_go   = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }

            case NZAxis:
            {
                _iOffset = Mathf.Abs(_destIdx.z - m_dragIdx.z);
                if (_destIdx.z > m_dragIdx.z)
                {
                    _bInc = false;
                }

                for (int _i = 1; _i <= _iOffset; ++_i)
                {
                    IntVec3 _curIdx = new IntVec3(m_dragIdx);
                    if (_bInc == true)
                    {
                        if (m_bCollision == true)
                        {
                            break;
                        }
                        _curIdx.z -= _i;
                        if (m_buildBrickMap.ContainsKey(_curIdx) == false)
                        {
                            CubeBrick _cb = new CubeBrick();
                            _cb.m_dirP = PZAxis;
                            _cb.m_dirN = NZAxis;
                            _cb.m_go   = (GameObject)Instantiate(m_prefabBrick, new Vector3(_curIdx.x + 0.5f, _curIdx.y + 0.5f, _curIdx.z + 0.5f), Quaternion.identity);
                            m_dragBrickList.Add(_cb);
                        }
                        else
                        {
                            m_bCollision = true;
                        }
                    }
                    else
                    {
                        int _iPos = m_dragBrickList.Count - 1;
                        if (_iPos >= 0)
                        {
                            CubeBrick _cb = m_dragBrickList[_iPos];
                            Object.DestroyImmediate(_cb.m_go);
                            m_dragBrickList.RemoveAt(_iPos);
                        }
                    }
                }
                break;
            }
            }

            m_dragIdx = _destIdx;
        }
    }