예제 #1
0
    // Update is called once per frame
    void Update()
    {
        //如果生命为0
        if (m_life <= 0)
        {
            return;
        }

        // 按下鼠标操作
        bool press = Input.GetMouseButton(0);

        // 松开鼠标操作
        bool mouseup = Input.GetMouseButtonUp(0);

        // 获得鼠标位置
        Vector3 mousepos = Input.mousePosition;

        // 获得鼠标移动距离
        float mx = Input.GetAxis("Mouse X");
        float my = Input.GetAxis("Mouse Y");

        // 如果当前按钮ID大于0,并且处于松开鼠标操作
        if (m_ID > 0 && mouseup)
        {
            //如果小于5个点数,返回,什么也不做
            if (m_point < 5)
            {
                m_ID = 0;
                m_button.SetOnActiv(false);
                return;
            }

            //创建一条从摄像机射出的射线
            Ray ray = Camera.main.ScreenPointToRay(mousepos);

            //计算射线与地面的碰撞
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit, 100, m_groundlayer))
            {
                //获得碰撞点的位置
                int ix = (int)hit.point.x;
                int iz = (int)hit.point.z;

                if (ix >= GridMap.Instance.MapSizeX || iz >= GridMap.Instance.MapSizeZ || ix < 0 || iz < 0)
                {
                    return;
                }

                // 如果当前单元格可以摆放防守单位
                if (GridMap.Instance.m_map[ix, iz].fieldtype == MapData.FieldTypeID.GuardPosition)
                {
                    Vector3 pos = new Vector3((int)hit.point.x + 0.5f, 0, (int)hit.point.z + 0.5f);

                    // 创建防守单位
                    Instantiate(m_guardPrefab, pos, Quaternion.identity);
                    m_ID = 0;

                    // 按钮重新恢复到正常状态
                    m_button.SetOnActiv(false);

                    // 减少5个点数
                    SetPoint(-5);
                }
            }
        }

        // 获得按钮的ID
        int id = m_button.UpdateState(mouseup, Input.mousePosition);

        if (id > 0)
        {
            m_ID = id;
            m_button.SetOnActiv(true);
            return;
        }

        // 移动摄像机
        GameCamera.Inst.Control(press, mx, my);
    }