虽然回来很累了,但还是小小坚持一下吧,不然真的平时的想法都废了。

在完成初步显示了,就要思考一下游戏逻辑了。

首先把数据先分离出来,其实除掉图形,在游戏面板上就是一堆的数字了,比如0代表红,1代表绿等。

想想dos时代,用数字搞出个俄罗斯方块那样。。

先把数据抽象出来:

using UnityEngine;
using System.Collections;
using System;

/// <summary>
/// FileName: GameData.cs
/// Author: Star
/// Date: 12/12/16
/// Description: class for game data
/// </summary>
public class GameData
{
    public enum DataType
    {
        Green,
        Orange,
        Pink
    }
    
    private DataType[,] m_Data;
    private int m_NumRow;
    private int m_NumCol;
    
    public int NumRow
    {
        get { return m_NumRow; }
        set { m_NumRow = value; }
    }
    
    public int NumCol
    {
        get { return m_NumCol; }
        set { m_NumCol = value; }
    }
    
    public DataType[,] Data
    {
        get { return m_Data; }
        set { m_Data = value; }
    }
    
    public GameData (int row, int col)
    {
        m_Data = new DataType[row, col];
        m_NumCol = col;
        m_NumRow = row;
        Generate();
    }
    
    public void Generate()
    {
//        Debug.Log("length:"+Enum.GetNames(typeof(DataType)).Length);
        for (int row = 0; row < NumRow; row++)
        {
            for (int col = 0; col < NumCol; col++)
            {
                m_Data[row,col] = (DataType)UnityEngine.Random.Range(0, Enum.GetNames(typeof(DataType)).Length);
//                Debug.Log("["+row+","+col+"]"+":"+(int)m_Data[row,col]);
            }
        }
    }
    
    public DataType GetTypeOf(int row, int col)
    {
        return m_Data[row,col];
    }
    
    
}


之后写一个简单的移动,目前定的移动方式是,一拖拖动整条方块,只有上下和左右两种情况,我们以第一次在哪个方向上拖动的距离大来定义本次拖动的方向。

代码如下:

using UnityEngine;
using System.Collections;

/// <summary>
/// FileName: UIManager.cs
/// Author: Star
/// Date: 12/12/16
/// Description: Manager of game UI
/// </summary>
public class UIManager : MonoBehaviour {
    
    public GameObject m_ItemPrefab;
    public GameObject m_GamePanel;
    
    Vector3 m_TopLeft = Vector3.zero;
    Vector3 m_GameBoardTopLeft = Vector3.zero;
    Vector3 m_GameBoardBottomRight = Vector3.zero;
    
    float m_LeftPercent = 0.2f;
    
    int NumRow = 10;
    int NumCol = 5;
    
    float m_fItemWidth = 0;
    float m_fItemHeight = 0;
    
    enum MoveDir
    {
        None,
        UpDown,
        LeftRight
    }
    
    MoveDir m_curDir = MoveDir.None;
    
    GameData m_GameData = null;
    GameObject[,] m_Items;

    // Use this for initialization
    void Start () {
        
        m_TopLeft = new Vector3(-Screen.width/2, Screen.height/2, 0);
        m_GameBoardTopLeft = m_TopLeft + new Vector3(Screen.width * m_LeftPercent, 0, 0);
        m_GameBoardBottomRight = new Vector3(Screen.width/2, -Screen.height/2, 0);
        
        m_fItemWidth = Screen.width * (1-m_LeftPercent) / NumCol;
        m_fItemHeight = Screen.height / NumRow;
        
        m_GameData = new GameData(NumRow, NumCol);
        m_Items = new GameObject[NumRow,NumCol];
        
        InitItemsWith(m_GameData.Data);
    }
    
    void InitItemsWith(GameData.DataType[,] data)
    {
        GameObject go = null;
        UISprite img = null;
        
        for (int row = 0; row < NumRow; row++)
        {
            for (int col = 0; col < NumCol; col++)
            {
                go = NGUITools.AddChild(m_GamePanel, m_ItemPrefab);
                go.transform.localPosition = new Vector3(col * m_fItemWidth + m_fItemWidth/2,
                    -row * m_fItemHeight - m_fItemHeight/2, 0) + m_GameBoardTopLeft;
                img = go.transform.FindChild("ItemImg").GetComponent<UISprite>();
                img.gameObject.transform.localScale = new Vector3(m_fItemWidth, m_fItemHeight, 0);
                img.spriteName = GetSpriteByType(data[row,col]);
                go.name = row + "|" + col;
                UIEventListener.Get(go).onPress += OnPressItem;
                UIEventListener.Get(go).onDrag += OnDragItem;
                m_Items[row,col] = go;
            }
        }
    }
    
    string GetSpriteByType(GameData.DataType type)
    {
        switch(type)
        {
        case GameData.DataType.Green:
            return "green";
        case GameData.DataType.Orange:
            return "orange";
        case GameData.DataType.Pink:
            return "pink";
        default:
            return "green";
        }
    }
    
    public void OnPressItem(GameObject go, bool state)
    {
        string[] pos = go.name.Split('|');
        int row = int.Parse(pos[0]);
        int col = int.Parse(pos[1]);
        
        Debug.Log("Click:["+row+","+col+"]");
        m_curDir = MoveDir.None;
    }
    
    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)
        {
            for (int i = 0; i < NumCol; i++)
            {
                m_Items[row,i].transform.localPosition += new Vector3(delta.x, 0, 0);
            }
        }
        else if (m_curDir == MoveDir.UpDown)
        {
            for (int j = 0; j < NumRow; j++)
            {
                m_Items[j,col].transform.localPosition += new Vector3(0, delta.y, 0);
            }
        }
    }
    
    // Update is called once per frame
    void Update () {
    
    }
}

 

最后效果如下:

发现不对劲了吧,嘿嘿,没有裁减,用NUGI的clipPanel吧。

在start里加入如下代码:

UIPanel panel = m_GamePanel.GetComponent<UIPanel>();
panel.clipRange = new Vector4(Screen.width * m_LeftPercent/2, 0, Screen.width * (1-m_LeftPercent), Screen.height);


最后效果:

搞定,收工,睡觉了。