回来吃点东西,打把DOTA就1点了。。还是坚持下写点东西,不然就越推越迟了。

 

今天主要把拖动的逻辑写完,现在可以自由上下左右移动了。

 

主要的代码如下,写的略丑,有时间看能重构下最好:

    void OnPressReleased()
    {
        Debug.Log("released");
        
        if (m_curDir == MoveDir.LeftRight)
        {
            OnLRReleased();
            ResetPositionOfRow(m_CurPressRow);
        }
        else if (m_curDir == MoveDir.UpDown)
        {
            OnUDReleased();
            ResetPositionOfCol(m_CurPressCol);
        }
            
        RemoveObjectOfList(m_TailTempItemList);
        RemoveObjectOfList(m_HeadTempItemList);
        
        Debug.Log(m_GameData.ToString());
    }
    
    void OnLRReleased()
    {
        float curX = m_Items[m_CurPressRow,m_CurPressCol].transform.localPosition.x;
        float deltaPos = curX - m_CurPressItemOriPos.x;
        int deltaNum = (int)(deltaPos/m_fItemWidth);
        float leftDis = Mathf.Abs(curX - m_CurPressItemOriPos.x) - Mathf.Abs(m_fItemWidth * deltaNum);
        Debug.Log("leftDis:"+leftDis);
        Debug.Log("deltaNum:"+deltaNum);
        int count = Mathf.Abs(deltaNum);
                
        if (leftDis > m_fItemWidth/2)
        {
            count += 1;
        }

        // move left
        if (deltaPos < 0)
        {
            // swap the game items with temp items
            for (int i = 0; i < count; i++)
            {
                GameObject item = m_Items[m_CurPressRow, i];
                ((GameObject)m_TailTempItemList[i]).name = item.name;
                ((GameObject)m_TailTempItemList[i]).GetComponent<BoxCollider>().size = item.GetComponent<BoxCollider>().size; 
                m_Items[m_CurPressRow, i] = (m_TailTempItemList[i] as GameObject);
                UIEventListener.Get(m_Items[m_CurPressRow, i]).onPress += OnPressItem;
                UIEventListener.Get(m_Items[m_CurPressRow, i]).onDrag += OnDragItem;
                m_TailTempItemList[i] = item;
                Debug.Log("change");
            }
                    
            ResetRowIndex(m_CurPressRow, count, true);
        }
        else if (deltaPos > 0)
        {    
            // swap the game items with temp items
            for (int i = 0; i < count; i++)
            {
                GameObject item = m_Items[m_CurPressRow, NumCol-1-i];
                ((GameObject)m_HeadTempItemList[i]).name = item.name;
                ((GameObject)m_HeadTempItemList[i]).GetComponent<BoxCollider>().size = item.GetComponent<BoxCollider>().size; 
                m_Items[m_CurPressRow, NumCol-1-i] = (m_HeadTempItemList[i] as GameObject);
                UIEventListener.Get(m_Items[m_CurPressRow, NumCol-1-i]).onPress += OnPressItem;
                UIEventListener.Get(m_Items[m_CurPressRow, NumCol-1-i]).onDrag += OnDragItem;
                m_HeadTempItemList[i] = item;
            }
            
            
            ResetRowIndex(m_CurPressRow, count, false);
        }
    }
    
    void OnUDReleased()
    {
        float curY = m_Items[m_CurPressRow,m_CurPressCol].transform.localPosition.y;
        float deltaPos = curY - m_CurPressItemOriPos.y;
        int deltaNum = (int)(deltaPos/m_fItemHeight);
        float leftDis = Mathf.Abs(curY - m_CurPressItemOriPos.y) - Mathf.Abs(m_fItemHeight * deltaNum);
        Debug.Log("leftDis:"+leftDis);
        Debug.Log("deltaNum:"+deltaNum);
        int count = Mathf.Abs(deltaNum);
                
        if (leftDis > m_fItemHeight/2)
        {
            count += 1;
        }
        
        // move down
        if (deltaPos < 0)
        {
            // swap the game items with temp items
            for (int i = 0; i < count; i++)
            {
                GameObject item = m_Items[NumRow-1-i,m_CurPressCol];
                ((GameObject)m_TailTempItemList[i]).name = item.name;
                ((GameObject)m_TailTempItemList[i]).GetComponent<BoxCollider>().size = item.GetComponent<BoxCollider>().size; 
                m_Items[NumRow-1-i, m_CurPressCol] = (m_TailTempItemList[i] as GameObject);
                UIEventListener.Get(m_Items[NumRow-1-i, m_CurPressCol]).onPress += OnPressItem;
                UIEventListener.Get(m_Items[NumRow-1-i, m_CurPressCol]).onDrag += OnDragItem;
                m_TailTempItemList[i] = item;    
            }
            ResetColIndex(m_CurPressCol, count, true);
        }
        else if (deltaPos > 0)
        {
            // swap the game items with temp items
            for (int i = 0; i < count; i++)
            {
                GameObject item = m_Items[i, m_CurPressCol];
                ((GameObject)m_HeadTempItemList[i]).name = item.name;
                ((GameObject)m_HeadTempItemList[i]).GetComponent<BoxCollider>().size = item.GetComponent<BoxCollider>().size; 
                m_Items[i, m_CurPressCol] = (m_HeadTempItemList[i] as GameObject);
                UIEventListener.Get(m_Items[i, m_CurPressCol]).onPress += OnPressItem;
                UIEventListener.Get(m_Items[i, m_CurPressCol]).onDrag += OnDragItem;
                m_HeadTempItemList[i] = item;
            }
            ResetColIndex(m_CurPressCol, count, false);
        }
    }
    
    void ResetRowIndex(int row, int count, bool left)
    {
        string nameIndex;
        ItemData.DataType type;
        GameObject go;
        int resetCount = 0;
        int resetNameCount = 0;
        
        if (left)
        {
            resetCount = NumCol - count;
            resetNameCount = count;
        }
        else
        {
            resetCount = count;
            resetNameCount = NumCol - count;
        }
        
        // correct the items reference
        for (int i = 0; i < resetCount; i++)
        {
            go = m_Items[row, NumCol-1];
            type = m_GameData.Data[row, NumCol-1].Type;
            for (int k = NumCol-1; k > 0; k--)
            {
                m_Items[row, k] = m_Items[row, k-1];
                m_GameData.Data[row, k].Type = m_GameData.Data[row, k-1].Type;
            }
            m_Items[row, 0] = go;
            m_GameData.Data[row, 0].Type = type;
        }
        //correct the gameobject name index
        for (int i = 0; i < resetNameCount; i++)
        {
            nameIndex = m_Items[row,NumCol-1].name;
                    
            for (int k = NumCol-1; k > 0; k--)
            {
                m_Items[row, k].name = m_Items[row, k-1].name;        
            }
            
            m_Items[row, 0].name = nameIndex;        
        }
    }
    
    void ResetColIndex(int col, int count, bool up)
    {
        string nameIndex;
        ItemData.DataType type;
        GameObject go;
        int resetCount = 0;
        int resetNameCount = 0;
        
        if (up)
        {
            resetCount = count;
            resetNameCount = NumRow - count;        
        }
        else
        {
            resetCount = NumRow - count;
            resetNameCount = count;
        }
        
        // correct the items reference
        for (int i = 0; i < resetCount; i++)
        {
            go = m_Items[NumRow - 1, col];
            type = m_GameData.Data[NumRow-1,col].Type;
            for (int k = NumRow - 1; k > 0; k--)
            {
                m_Items[k,col] = m_Items[k-1,col];
                m_GameData.Data[k, col].Type = m_GameData.Data[k-1, col].Type;
            }
            m_Items[0,col] = go;
            m_GameData.Data[0,col].Type = type;
        }
        //correct the gameobject name index
        for (int i = 0; i < resetNameCount; i++)
        {
            nameIndex = m_Items[NumRow-1, col].name;        
            for (int k = NumRow-1; k > 0; k--)
            {
                m_Items[k, col].name = m_Items[k-1, col].name;        
            }
            m_Items[0,col].name = nameIndex;    
        }
    }
    
    void ResetPositionOfRow(int row)
    {
        for (int k = 0; k < NumCol; k++)
        {
            m_Items[row,k].transform.localPosition = new Vector3(k * m_fItemWidth + m_fItemWidth/2,
                        -row * m_fItemHeight - m_fItemHeight/2, 0) + m_GameBoardTopLeft;
        }
    }
    
    void ResetPositionOfCol(int col)
    {
        for (int k = 0; k < NumRow; k++)
        {
            m_Items[k,col].transform.localPosition = new Vector3(col * m_fItemWidth + m_fItemWidth/2,
                        -k * m_fItemHeight - m_fItemHeight/2, 0) + m_GameBoardTopLeft;
        }
    }
    
    public void OnDragItem(GameObject go, Vector2 delta)
    {
        string[] pos = go.name.Split('|');
        int row = int.Parse(pos[0]);
        int col = int.Parse(pos[1]);
        
        //Debug.Log("Click:["+row+","+col+"]");
        
        if (m_curDir == MoveDir.None)
        {
            if (Mathf.Abs(delta.x) > Mathf.Abs(delta.y))
            {
                m_curDir = MoveDir.LeftRight;
            }
            else
            {
                m_curDir = MoveDir.UpDown;
            }
        }
        
        if (m_curDir == MoveDir.LeftRight)
        {
            OnLRDrag(row, col, delta);
        }
        else if (m_curDir == MoveDir.UpDown)
        {
            OnUpDownDrag(row, col, delta);
        }
    }
    
    void OnLRDrag(int row, int col, Vector2 delta)
    {
        m_fCurMoveDelta = m_Items[row,col].transform.localPosition.x + delta.x - m_CurPressItemOriPos.x;
        Debug.Log(m_fCurMoveDelta);
        
        // if move to the end, stop it
        if (Mathf.Abs(m_fCurMoveDelta) < m_fItemWidth * (NumCol - 1))
        {
            // move items with drag delta
            foreach(GameObject temp in m_HeadTempItemList)
            {
                temp.transform.localPosition += new Vector3(delta.x, 0, 0);
            }
            foreach(GameObject temp in m_TailTempItemList)
            {
                temp.transform.localPosition += new Vector3(delta.x, 0, 0);
            }
            
            for (int i = 0; i < NumCol; i++)
            {    
                m_Items[row,i].transform.localPosition += new Vector3(delta.x, 0, 0);        
            }        
            
            // calculate the num of items we moved
            m_CurDeltaNum = Mathf.Abs((int)(m_fCurMoveDelta/m_fItemWidth));
            Debug.Log("num:"+m_CurDeltaNum);
            if (m_fCurMoveDelta > 0)
            {
                // if we move a harf item, we still need to create one temp items
                if (m_CurDeltaNum + 1 > m_HeadTempItemList.Count)
                {
                    GameObject tempItem = null;
                    UISprite img = null;
                    // don't have any temp items when you start to move the items
                    if (m_HeadTempItemList.Count == 0)
                    {
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = m_Items[row,0].transform.localPosition + new Vector3(-m_fItemWidth, 0, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[row,NumCol-1].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_HeadTempItemList.Add(tempItem);
                    }
                    else
                    {
                        GameObject last = m_HeadTempItemList[m_HeadTempItemList.Count - 1] as GameObject;
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = last.transform.localPosition + new Vector3(-m_fItemWidth, 0, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[row,NumCol-1-m_CurDeltaNum].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_HeadTempItemList.Add(tempItem);
                    }
                }
            }
            else if(m_fCurMoveDelta < 0)
            {
                // if we move a harf item, we still need to create one temp items
                if (m_CurDeltaNum + 1 > m_TailTempItemList.Count)
                {
                    GameObject tempItem = null;
                    UISprite img = null;
                    // don't have any temp items when you start to move the items
                    if (m_TailTempItemList.Count == 0)
                    {
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = m_Items[row,NumCol-1].transform.localPosition + new Vector3(m_fItemWidth, 0, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[row,0].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_TailTempItemList.Add(tempItem);
                    }
                    else
                    {
                        GameObject last = m_TailTempItemList[m_TailTempItemList.Count - 1] as GameObject;
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = last.transform.localPosition + new Vector3(m_fItemWidth, 0, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[row,m_CurDeltaNum].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_TailTempItemList.Add(tempItem);
                    }
                }
            }
        }// end of side judge
    }
    
    void OnUpDownDrag(int row, int col, Vector2 delta)
    {
        m_fCurMoveDelta = m_Items[row,col].transform.localPosition.y + delta.y - m_CurPressItemOriPos.y;
        Debug.Log(m_fCurMoveDelta);
        
        // if move to the end, stop it
        if (Mathf.Abs(m_fCurMoveDelta) < m_fItemHeight * (NumRow - 1))
        {
            // move items with drag delta
            foreach(GameObject temp in m_HeadTempItemList)
            {
                temp.transform.localPosition += new Vector3(0, delta.y, 0);
            }
            foreach(GameObject temp in m_TailTempItemList)
            {
                temp.transform.localPosition += new Vector3(0, delta.y, 0);
            }
            
            for (int i = 0; i < NumRow; i++)
            {    
                m_Items[i,col].transform.localPosition += new Vector3(0, delta.y, 0);        
            }        
            
            // calculate the num of items we moved
            m_CurDeltaNum = Mathf.Abs((int)(m_fCurMoveDelta/m_fItemHeight));
            Debug.Log("num:"+m_CurDeltaNum);
            if (m_fCurMoveDelta > 0)
            {
                // if we move a harf item, we still need to create one temp items
                if (m_CurDeltaNum + 1 > m_HeadTempItemList.Count)
                {
                    GameObject tempItem = null;
                    UISprite img = null;
                    // don't have any temp items when you start to move the items
                    if (m_HeadTempItemList.Count == 0)
                    {
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = m_Items[NumRow - 1,col].transform.localPosition + new Vector3(0, -m_fItemHeight, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[0,col].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_HeadTempItemList.Add(tempItem);
                    }
                    else
                    {
                        GameObject last = m_HeadTempItemList[m_HeadTempItemList.Count - 1] as GameObject;
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = last.transform.localPosition + new Vector3(0, -m_fItemHeight, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[m_CurDeltaNum,col].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_HeadTempItemList.Add(tempItem);
                    }
                }
            }
            else if(m_fCurMoveDelta < 0)
            {
                // if we move a harf item, we still need to create one temp items
                if (m_CurDeltaNum + 1 > m_TailTempItemList.Count)
                {
                    GameObject tempItem = null;
                    UISprite img = null;
                    // don't have any temp items when you start to move the items
                    if (m_TailTempItemList.Count == 0)
                    {
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = m_Items[0,col].transform.localPosition + new Vector3(0, m_fItemHeight, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[NumRow - 1,col].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_TailTempItemList.Add(tempItem);
                    }
                    else
                    {
                        GameObject last = m_TailTempItemList[m_TailTempItemList.Count - 1] as GameObject;
                        tempItem = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                        tempItem.transform.localPosition = last.transform.localPosition + new Vector3(0, m_fItemHeight, 0);
                        img = tempItem.transform.FindChild("ItemImg").GetComponent<UISprite>();
                        img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                        img.spriteName = m_Items[NumRow-1-m_CurDeltaNum,col].transform.FindChild("ItemImg").GetComponent<UISprite>().spriteName;
                        m_TailTempItemList.Add(tempItem);
                    }
                }
            }
        }// end of side judge
    }

 

 

下面要做的就是如何来定义消除规则了,直接的三消似乎比较蛋疼,暂时有个想法是用类似围棋的矩形来消除:

通过不断的移动来拼凑出矩形引爆那片区域,这样的移动感觉就有点魔方的味道了。挺益智的嘿嘿!

 

程序上我就打算用一个策略模式来做消除判断,这样以后我可以方便的替换其它各种消除规则。

看看周末能不能出个可玩的DEMO,睡觉了。PS:北京据说周末会降到-16度。