斗地主滑动选牌算法

using UnityEngine;
using UnityEngine.EventSystems;
/// 
/// 牌类
/// 
public class DDZ_Card : MonoBehaviour,IPointerEnterHandler,IPointerExitHandler,IPointerClickHandler
{
    public int value;//牌值
    public string color;//花色
    public string Name
    {
        get {
            return value.ToString() + color;
        }
    }
    public Transform mask;//遮罩
    public Transform me;//牌的游戏对象
    private DDZ_CardState state=DDZ_CardState.stop;//牌的状态
    public DDZ_CardState State
    {
        get {
            return state;
        }
        set {
            ChangeState();
        }
    }

    private bool isEnter = false;//鼠标(左键和在移动端的手一样效果)是否进入牌
    private bool isClick = false;//鼠标是否点击了牌
    /// 
    /// 初始化牌的权重和花色等
    /// 
    /// 
    /// 
    public void Init(int value, string color)
    {
        this.value = value;
        this.color = color;
        transform.name = Name;
        me = transform;
        mask = me.Find("mask");
        mask.gameObject.SetActive(false);
    }
    private void Start()
    {
    }

    private void Update()
    {
        if (Input.GetMouseButton(0) && isEnter == true)//鼠标左键按下且进入了牌
        {
            mask.gameObject.SetActive(true);//激活遮罩
            if (DDZ_Manager.upList.Contains(this) == false)//添加到选择牌的集合中
            {
                DDZ_Manager.upList.Add(this);
            }
        }
        else if (Input.GetMouseButton(0) == false)
        {
            isEnter = false;
            if (DDZ_Manager.upList.Count > 1)
            {
                for (int i = 0; i < DDZ_Manager.upList.Count; i++)
                {
                    DDZ_Manager.upList[i].mask.gameObject.SetActive(false);
                    DDZ_Manager.upList[i].ChangeState();
                }
                DDZ_Manager.upList.Clear();
            }
        }

    }

    /// 
    /// 鼠标进入牌
    /// 
    /// 
    public void OnPointerEnter(PointerEventData eventData)
    {
        isEnter = true;
        Debug.Log(me.name + "--->" + isEnter);
    }

    /// 
    /// 鼠标离开牌
    /// 
    /// 
    public void OnPointerExit(PointerEventData eventData)
    {
        isEnter = false;
    }
    /// 
    /// 鼠标点击牌
    /// 
    /// 
    public void OnPointerClick(PointerEventData eventData)
    {
        if (DDZ_Manager.upList.Count > 0)
        {
            for (int i = 0; i < DDZ_Manager.upList.Count; i++)
            {
                DDZ_Manager.upList[i].mask.gameObject.SetActive(false);
            }
            DDZ_Manager.upList.Clear();
        }
        ChangeState();
    }
    /// 
    /// 改变牌的状态
    /// 
    public void ChangeState()
    {
        if (state == DDZ_CardState.stop)
        {
            me.transform.localPosition += new Vector3(0, 10, 0);
            state = DDZ_CardState.cilick;
        }
        else if (state == DDZ_CardState.cilick)
        {
            me.transform.localPosition -= new Vector3(0, 10, 0);
            state = DDZ_CardState.stop;
        }
    }


    /// 
    /// 牌的状态枚举
    /// 
    public enum DDZ_CardState
    {
        stop,
        cilick,
    }
}

挂在在游戏对象上用于创建手牌

using UnityEngine;
using System.Collections.Generic;
public class DDZ_Manager 
{
    #region 单利
    private static DDZ_Manager instance = null;
    public static DDZ_Manager Instance {
        get {
            if (instance == null)
            {
                instance = new DDZ_Manager();
            }
            return instance;
        }
    }
    #endregion

    public static List upList = new List();//存储选择的牌
    public Transform grid;//牌的父节点
    public GameObject prefab;//牌的预制体
    public void Init()
    {
        if (prefab == null)
        {
            prefab = Resources.Load("Card");
            grid= GameObject.Find("grid").transform;
        }
    }
    /// 
    /// 创建手牌
    /// 
    public void CreateShouPai()
    {
        for (int i = 0; i < 27; i++)
        {
            GameObject obj = GameObject.Instantiate(prefab);
            obj.transform.SetParent(grid);
            obj.transform.localScale = Vector3.one;
            DDZ_Card card = obj.AddComponent();
            card.Init(i, "a");
        }
    }
}
using UnityEngine;
using System.Collections;

public class Main : MonoBehaviour {
    void Start()
    {
        DDZ_Manager.Instance.Init();
        DDZ_Manager.Instance.CreateShouPai();
    }

}

案例地址:https://gitee.com/GAXM/bucket_landlord_sliding_selection_algorithm.git

你可能感兴趣的:(纸牌)