unity麻将开发视频教程_一屏双人单机手机游戏

2022-09-27 10:15:15 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

小实习生一枚,利用一周的时间做了个单机简单的二人小麻将


先上成品图

因为公司都用比较稳定的版本,所以我是从比较老的版本unity 4.7上手学习的 刚开始学什么都不懂,参考了一个别人写的斗地主demo,所以代码比较累赘…你们可以改良一下

二人麻将介绍

牌型分为:条,筒,万(没有东西南北发) 共108张 玩家: 二人 吃,碰,杠,胡均为简单算法,没有分种类和牌型(因为我不会打牌所以写的时候没有考虑暗杠)

功能实现

1:发牌 2:桌面摆牌 3:AI实现吃,碰,杠,胡功能 4:自己的牌实现智能提示吃,碰,杠,胡

步骤

首先导入ngui,导入ngui还是非常简单的,百度上面教程贼多,我就不写了

新建一个GameObject,改名为GameControll

绑上我们新建的C#脚本,这个脚本就是我们整个项目核心的控制点了

我在这里简单设置了一下参数,比如底分和全场倍数 ,接下来就可以开始往里面填东西la

代码语言:javascript复制
 public class GameController : MonoBehaviour{
    public int basePointPerMatch;//底分
    private int multiples;//全场倍数
                          // Use this for initialization
    void Start()
    {
        multiples = 1;
        basePointPerMatch = 100;
        InitStar();//初始化场景
    }

 }

可以看到我这里写了一个初始化场景的方法InitStar() 使用代码动态生成页面 动态生成页面要准备好预制体,也就是画UI, 可以看到这里我将主要的场景分为两个预制体,一个是专门放player的,一个是专门放置按钮的

这是我的StartPanel预制体做好的样子

这是我的buttonPanel预制体

这里还有一个重要的预制体需要画好,即卡牌的预制体,要绑定一个类,我这里取名为CardSprite,进行卡牌精灵的处理

注意,所有调用的预制体必须放置在Resources文件夹下!我调用预制体的方法为Resources.Load(“XXX”)

做好这些准备,可以开始在GameControll类里面写初始化代码了

代码语言:javascript复制
 /// <summary>
    /// 初始化面板
    /// </summary>
    public void InitStar()
    {
        GameObject buttonpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("ButtonPanel"));
        buttonpanel.AddComponent<buttonControl>();
        buttonpanel.name = buttonpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)
        

        GameObject mainpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("StartPanel"));//加载StartPanel第一个场景
        mainpanel.name = mainpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)
        GameObject AI= mainpanel.transform.Find("AI").gameObject;
        HandCards AI1Cards = AI.AddComponent<HandCards>();//处理手牌的类
        AI1Cards.cType = CharacterType.AI;//设定角色的类型
        AI.AddComponent<SimpleSmartCard>();//ai自动出牌的类
        AI.transform.Find("playpoint").gameObject.SetActive(false);//隐藏AI放手牌的点

        AI.transform.Find("showType").gameObject.SetActive(false);//这里可以处理图片

        GameObject Player = mainpanel.transform.Find("Player").gameObject;
        HandCards playerCards = Player.AddComponent<HandCards>();
        Player.AddComponent<PlayCard>();//玩家出牌调用的类
        playerCards.cType = CharacterType.Player;
        Player.transform.Find("playpoint").gameObject.SetActive(false);
        print("初始化"   "toStar()");
    }

写完上面的代码,运行你会发现你写好的预制体被动态加载出来了

可以看到这里我给预制体绑定了很多处理的辅助类,我将这几个类直接贴出来吧,相信很容易看得懂

代码语言:javascript复制
//  用于规定枚举类型
using UnityEngine;
using System.Collections;

    /// <summary>
    /// 角色类型
    /// </summary>
    public enum CharacterType
    {
        Library = 0,
        Player,
        AI,
        Desk
    }


    /// <summary>
    /// 牌类
    /// </summary>
    public enum Suits
    {
        Tiao,
        Tong,
        Wan,
    }

    /// <summary>
    /// 卡牌权值
    /// </summary>
    public enum Weight
    {
        _1 = 0,
        _2,
        _3,
        _4,
        _5,
        _6,
        _7,
        _8,
        _9,
    }

    /// <summary>
    /// 身份
    /// </summary>
    public enum Identity
    {
        loser,
        fightui_host,
        none
    }

    /// <summary>
    /// 出牌类型
    /// </summary>
    public enum CardsType //enum 枚举关键字
    {
        //未知类型
        None=0,
        fightui_chi ,
        fightui_peng,
        fightui_gang,
        fightui_angang,
        fightui_hu,

    }

/// <summary>
/// 牌的元素
/// </summary>
public enum Cardselement //enum 枚举关键字
{
    //未知类型
    None = 0,
    Double,//一堆将
    ThereCount,//三顺
    ThereSame,//三连
    Gang

}
代码语言:javascript复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//封装卡牌
public class Card
{
    private readonly string cardName;//牌名
    private readonly Weight weight;//权重
    private readonly Suits suits; //种类
    private CharacterType belongTo;//牌的归属
    private bool makedSprite;



    public string CardName
    {
        get
        {
            return cardName;
        }
    }

    public Weight Weight
    {
        get
        {
            return weight;
        }
        
    }

    public Suits Suits
    {
        get
        {
            return suits;
        }
    }

    public CharacterType BelongTo
    {
        get
        {
            return belongTo;
        }

        set
        {
            belongTo = value;
        }
    }

    public bool MakedSprite
    {
        get
        {
            return makedSprite;
        }

        set
        {
            makedSprite = value;
        }
    }


    public Card(string name, Weight weight, Suits suits, CharacterType belongTo)
    {
        MakedSprite = false;
        cardName = name;
        this.weight = weight;
        this.suits = suits;
        this.BelongTo = belongTo;
    }

}
代码语言:javascript复制
using System.Collections.Generic;
using UnityEngine;
//生成手牌的类
public class HandCards : MonoBehaviour
{
    public CharacterType cType;
    private List<Card> library;
    private Identity identity;
    private Card newCard=null;
    private int multiples;//玩家倍数
    private int integration=1000;//积分
    private bool Once = true;

    void Start()
    {
        multiples = 1;
        identity = Identity.loser;
        library = new List<Card>();
    }

    /// <summary>
    /// 积分
    /// </summary>
    public int Integration
    {
        set { integration = value; }
        get { return integration; }
    }

    /// <summary>
    /// 玩家倍数
    /// </summary>
    public int Multiples
    {
        set { multiples *= value; }
        get { return multiples; }
    }

    /// <summary>
    /// 手牌数
    /// </summary>
    public int CardsCount
    {
        get { return library.Count; }
    }

    /// <summary>
    /// 访问身份
    /// </summary>
    public Identity AccessIdentity
    {
        set
        {
            identity = value;
        }
        get { return identity; }
    }

    public Card NewCard
    {
        get
        {
            return newCard;
        }

       
    }

    public bool theOnce
    {
        get
        {
            return Once;
        }

        set
        {
            Once = value;
        }
    }

    /// <summary>
    /// 获取手牌
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public Card this[int index]
    {
        get { return library[index]; }
    }

    /// <summary>
    /// 获取值的索引   索引器.
    /// </summary>
    /// <param name="card"></param>
    /// <returns></returns>
    public int this[Card card]
    {
        get { return library.IndexOf(card); }
    }

    /// <summary>
    /// 添加手牌
    /// </summary>
    /// <param name="card"></param>
    public void AddCard(Card card)
    {
        
        card.BelongTo = cType;
        library.Add(card);
        newCard = card;
    }

    /// <summary>
    /// 出牌
    /// </summary>
    /// <returns></returns>
    public void PopCard(Card card)
    {
        //从手牌移除
        if (card!=null) {
            library.Remove(card);
        }
    }

    /// <summary>
    /// 手牌排序
    /// </summary>
    public void Sort()
    {
        CardRules.SortCards(library, false);//CardRules类用于处理手牌,排序,判断胡,碰,杠等
    }

}

一定要绑到牌的预制体上哟!

代码语言:javascript复制
//给卡牌预制体绑定的处理精灵的方法
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class CardSprite : MonoBehaviour
{
    private Card card;
    public UISprite sprite;
    private bool isSelected=false;
    private bool canSeeCard;

    public Card Poker
    {
        set
        {
            card = value;
            card.MakedSprite = true;
            SetSprite(canSeeCard);//显示ui
        }
        get { return card; }
    }

    /// <summary>
    /// 是否被点击中
    /// </summary>
    public bool Select
    {
        set { isSelected = value; }
        get { return isSelected; }
    }

    public bool CanSeeCard
    {
        get
        {
            return canSeeCard;
        }

        set
        {
            canSeeCard = value;
        }
    }

    private void Start()
    {
        isSelected = false;
    }

    /// <summary>
    /// 设置UISprite的显示
    /// </summary>
    void SetSprite(bool canSeeCard )
    {
        if (canSeeCard)
        {
            //明牌
            sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
        }
        else {
            if (card.BelongTo == CharacterType.Player || card.BelongTo == CharacterType.Desk)
            {
                sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
            }
            else
            {
                sprite.spriteName = "Top_Back";
            }
        }
        
    }

    /// <summary>
    /// 销毁精灵
    /// </summary>
    public void Destroy()
    {
        if (card!=null) {
            if (card.MakedSprite) {
                //精灵化false
                card.MakedSprite = false;
                //销毁对象
                Destroy(this.gameObject);
            }
           
        }
       
    }

    /// <summary>
    /// 调整位置
    /// </summary>
    public void GoToPosition(CharacterType who, int index,int cardmargin)
    {
        if (sprite != null)
        {
            GameObject parent = GameObject.Find(who.ToString());
           
            if (card.BelongTo == CharacterType.Player)//如果牌是player的,那么把牌放在player的点
            {
                GameObject mycon = parent.transform.FindChild("cardpengpoint").gameObject;
                CardSprite[] myconsp = mycon.GetComponentsInChildren<CardSprite>();
                transform.localPosition = new Vector3(0, 0, 0);
                CardSprite[] mypoint = parent.GetComponentsInChildren<CardSprite>();
               
                int margin = 0;
                switch (myconsp.Length)
                {
                    case 4:
                        margin = 180;
                        break;
                    case 8:
                        margin = 200 * 2;
                        break;
                    case 12:
                        margin = 200 * 3;
                        break;
                    case 16:
                        margin =210 * 4;
                        break;
                    default:
                        break;
                };
                sprite.width = 78;
                sprite.height = 115;
                if (Select)
                {
                    sprite.spriteName = "Bottom_Stand_Front_Light";
                }
                else {
                    sprite.spriteName = "Bottom_Stand_Front";
                }
                sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
                sprite.transform.FindChild("mypointS").GetComponent<UISprite>().depth =2;
                sprite.transform.localScale = new Vector3((float)1.2, (float)1.2, 1);
                transform.localPosition = new Vector3(-550 cardmargin*index margin, -80,  0);
            
                 }
            else if (card.BelongTo == CharacterType.AI)
            {
                if (canSeeCard)
                {
                    sprite.width = 78;
                    sprite.height = 115;
                    sprite.spriteName = "Bottom_Stand_Front";
                    sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
                    sprite.depth = 1;
                    sprite.transform.FindChild("mypointS").GetComponent<UISprite>().depth =2;
                    sprite.transform.localScale = new Vector3((float)0.6, (float)0.6, 1);
                    sprite.transform.localPosition =
                    GameObject.Find(who.ToString()).transform.FindChild("playpoint").transform.localPosition   Vector3.right * 45 * index;
                }
                else {
                    sprite.spriteName = "Top_Back";
                    sprite.depth = 3;
                    sprite.width = 45;
                    sprite.height = 70;
                   // sprite.transform.localScale = new Vector3((float)0.6, (float)0.6, 1);
                    sprite.transform.localPosition=
                    GameObject.Find(who.ToString()).transform.FindChild("playpoint").transform.localPosition   Vector3.right * 45 * index;
                    
                }
              
            }
            else if (card.BelongTo == CharacterType.Desk)
            {
                sprite.width = 88;
                sprite.height = 120;
                sprite.spriteName = "Bottom_Front";
                transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.target = sprite.transform;
                transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.relative = 0;
                transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.absolute = 0;
                transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.relative = 1;
                transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.absolute = -2;
                transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.relative = 0;
                transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.absolute = 29;
                transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.relative = 1;
                transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.absolute = 0;
                sprite.depth = 1;
                sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
                sprite.width = 78;
                sprite.height = 115;
                sprite.transform.localScale = new Vector3((float)0.5, (float)0.5, 1);
                sprite.transform.localPosition =
            transform.localPosition = GameObject.Find("showpoint").transform.localPosition ;
            }
            //else if (card.BelongTo == CharacterType.Desk)
            //{
            //    sprite.spriteName = card.CardName;
            //    sprite.width = 40;
            //    sprite.height = 50;
            //    transform.localPosition = GameObject.Find("showplayerpoint").transform.localPosition   Vector3.right * 40 * index;
            //}


        }


    }

    /// <summary>
    /// 碰,杠,吃的位置
    /// </summary>
    public void Changeposition(CharacterType who,int index) {
        GameObject point = GameObject.Find(who.ToString()).transform.FindChild("cardpengpoint").gameObject;
        
        sprite.width = 88;
        sprite.height = 120;
        sprite.spriteName = "Bottom_Front";
        transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.target = sprite.transform;
        transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.relative = 0;
        transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.absolute = 0;
        transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.relative = 1;
        transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.absolute = -2;
        transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.relative = 0;
        transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.absolute = 29;
        transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.relative = 1;
        transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.absolute = 0;
        if (!CanSeeCard)
        {
            sprite.spriteName = "Bottom_Back";
            transform.Find("mypointS").GetComponent<UISprite>().spriteName = "Bottom_Back";
            transform.Find("mypointS").GetComponent<UISprite>().depth = 3;
            sprite.depth = 2;

        }
        else {
            sprite.transform.FindChild("mypointS").GetComponent<UISprite>().spriteName = card.CardName;
        }
        sprite.transform.localScale = new Vector3((float)0.8, (float)0.8, 1);

    }



    /// <summary>
    /// 卡牌点击
    /// </summary>
    void OnClick()
     {
        if (card.BelongTo == CharacterType.Player)//在这里判断是否当前出牌为player,否则取消卡牌点击事件
        {
            GameObject parent = GameObject.Find(CharacterType.Player.ToString());
            CardSprite[] sp = parent.GetComponentsInChildren<CardSprite>();
            List<CardSprite> list = new List<CardSprite>();
            list.Clear();
            for (int i = 0; i < sp.Length; i  ) {
                if (sp[i].sprite!=sprite) {
                    if (sp[i].Select) {
                        sp[i].Select = false;
                        sp[i].sprite.spriteName = "Bottom_Stand_Front";
                    } 
                }
               
            }

            isSelected = true;
            sprite.spriteName = "Bottom_Stand_Front_Light";
           
           
        }
        
         
     }
    
}
代码语言:javascript复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//生成牌库
public class CardControl {
    private List<Card> library ;//可以通过判断library中的牌数量为零时结束对局
    private static  CardControl instance;
    private CharacterType ctype;
    private bool isEnd = false;

    public static CardControl Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new CardControl();
            }
            return instance;
        }
    }

    /// <summary>
    /// 获取牌库中牌的数量
    /// </summary>
    public int CardsCount
    {
        get { return library.Count; }
    }

    public bool IsEnd
    {
        get
        {
            return isEnd;
        }

        set
        {
            isEnd = value;
        }
    }

    /// <summary>
    /// 索引器
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public Card this[int index]
    {
        get
        {
            return library[index];
        }
    }


    /// <summary>
    /// 私有构造
    /// </summary>
    private CardControl()
    {
        library = new List<Card>();
        ctype = CharacterType.Library;
        CreateCards();
    }

    public void ReCreate() {
        library = new List<Card>();
        ctype = CharacterType.Library;
        CreateCards();
    }

    /// <summary>
    /// 创建一副牌
    /// </summary>
     void CreateCards()
    {
        //创建普通牌
        for (int num=0;num<4;num  ) {
            for (int type = 0; type < 3; type  )
            {
                for (int value = 0; value < 9; value  )
                {
                    Weight w = (Weight)value;
                    Suits s = (Suits)type;
                    string name = s.ToString()   w.ToString();
                    Card card = new Card(name, w, s, ctype);
                    library.Add(card);
                }
            }
        }
        
        
    }

    /// <summary>
    /// 洗牌
    /// </summary>      foreach语句是c#中新增的循环语句,他对于处理数组及集合等数据类型特别方便。
    //                  foreach语句的一般语法格式如下:
    //                  foreach(数据类型 标识符 in 表达式){ 循环体 }
    public void Shuffle()
    {
        if (CardsCount == 108)
        {
            System.Random random = new System.Random();
            List<Card> newList = new List<Card>();
            foreach (Card item in library)
            {
                newList.Insert(random.Next(newList.Count   1), item);//ListCount的意思为:表项个数,用于反回表项的总个数,但它的下标从零开始,如果需要获得list的总长度需要 1
            }

            library.Clear();

            foreach (Card item in newList)
            {
                library.Add(item);
            }

            newList.Clear();
        }
    }

    /// <summary>
    /// 发牌
    /// </summary>
    public Card Deal()
    {
        Card ret = library[library.Count - 1];
        library.Remove(ret);
        return ret;
    }

    /// <summary>
    /// 向牌库中添加牌
    /// </summary>
    /// <param name="card"></param>
    public void AddCard(Card card)
    {
        card.BelongTo = ctype;
        library.Add(card);
    }
    

}

弹出框的两个类

代码语言:javascript复制
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MyConSprite : MonoBehaviour {

    private bool select;
    private GameObject myCheck;
    private CardSprite[] sp = null;
    private int CarspCount;
    public bool Select
    {
        get
        {
            return select;
        }

        set
        {
            select = value;
        }
    }

    public int CarspCount1
    {
        get
        {
            return CarspCount;
        }

        set
        {
            CarspCount = value;
        }
    }

    public CardSprite[] Sp
    {
        get
        {
            return sp;
        }

        set
        {
            sp = value;
        }
    }

    // Use this for initialization
    void Start () {
        myCheck = GameObject.Find("myCheck").gameObject;
        myCheck.GetComponent<UIGrid>().repositionNow = true;
        myCheck.GetComponent<UIGrid>().enabled = true;
        myCheck.transform.localPosition = new Vector3(-280, -21, 0);
    }
	


   public void OntoGo()
    {
            Sp = transform.GetComponentsInChildren<CardSprite>();
            CarspCount1 = Sp.Length;
            ConSpCache.Instance.CardClear();
            for (int i=0; i<Sp.Length;i  ) {
                ConSpCache.Instance.AddCard(Sp[i]);
            }
            Select = true;
            GameController controller = GameObject.Find("GameControll").GetComponent<GameController>();
            controller.getConSelect();
        for (int k = 0; k < myCheck.transform.childCount; k  )
        {
            GameObject go = myCheck.transform.GetChild(k).gameObject;
            Destroy(go);
            
        }
        myCheck.SetActive(false);

    }

   


}
代码语言:javascript复制
using System.Collections.Generic;
using UnityEngine;
public class ConSpCache
{
    private List<CardSprite> sp;
    private static ConSpCache instance;

    public static ConSpCache Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new ConSpCache();
            }
            return instance;
        }
    }

    /// <summary>
    /// 私有构造
    /// </summary>
    private ConSpCache()
    {
        sp = new List<CardSprite>();

    }

    public List<CardSprite> Sp
    {
        get
        {
            return sp;
        }
    }

    /// <summary>
    /// 向牌库中添加牌
    /// </summary>
    /// <param name="card"></param>
    public void AddCard(CardSprite card)
    {
        Sp.Add(card);
    }

    public void CardClear() {
        if (sp!=null) {
            sp.Clear();
        }
        
    }

    

}

在GameControll中写洗牌,发牌,生成精灵等的方法,第一次写博客真是不知道怎么循序渐进,我直接把GameControll脚本全部贴出来吧

代码语言:javascript复制
 using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameController : MonoBehaviour{
    public int basePointPerMatch;//底分
    private int multiples;//全场倍数
                          // Use this for initialization
    void Start()
    {
        multiples = 1;
        basePointPerMatch = 100;
        InitStar();
    }

    /// <summary>
    /// 全场倍数
    /// </summary>
    public int Multiples
    {
        set { multiples *= value; }
        get { return multiples; }
    }

    public int BasePointPerMatch
    {
        set { basePointPerMatch = value; }
        get { return basePointPerMatch; }
    }

    /// <summary>
    /// 初始化面板
    /// </summary>
    public void InitStar()
    {
        GameObject buttonpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("ButtonPanel"));
        buttonpanel.AddComponent<buttonControl>();
        buttonpanel.name = buttonpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)
        

        GameObject mainpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("StartPanel"));//加载StartPanel第一个场景
        
        mainpanel.name = mainpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)
        GameObject AI= mainpanel.transform.Find("AI").gameObject;
        HandCards AI1Cards = AI.AddComponent<HandCards>();
        AI1Cards.cType = CharacterType.AI;
        AI.AddComponent<SimpleSmartCard>();
        AI.transform.Find("playpoint").gameObject.SetActive(false);

        AI.transform.Find("showType").gameObject.SetActive(false);//这里可以处理图片

        GameObject Player = mainpanel.transform.Find("Player").gameObject;
        HandCards playerCards = Player.AddComponent<HandCards>();
        Player.AddComponent<PlayCard>();
        playerCards.cType = CharacterType.Player;
        Player.transform.Find("playpoint").gameObject.SetActive(false);
        print("初始化"   "toStar()");
    }

    /// <summary>
    /// 洗牌
    /// </summary>
    public void DealCards(CharacterType Startperson )
    {
        Debug.Log("洗牌"   "DealCards()--");

        //洗牌
        CardControl.Instance.Shuffle();

        //发牌
        CharacterType currentCharacter = CharacterType.Player;
        for (int i = 0; i < 26; i  )
        {
            if (currentCharacter == CharacterType.Desk)//循环到Desk的时候跳过,从player发
            {
                currentCharacter = CharacterType.Player;
            }
            DoSend(currentCharacter);
            currentCharacter  ;
            Debug.Log("洗牌"   "DealCards()--"   "currentCharacter  :"   currentCharacter);
        }

        if (Startperson!=CharacterType.AI) {
            //发一张给
            DoSend(Startperson);
        }

        for (int i = 1; i < 4; i  )
        {
            MakeHandCardsSprite((CharacterType)i, false,false,false);
        }
        Readjust(CharacterType.Player,true);
    }

    /// <summary>
    /// 发牌
    /// </summary>
    public Card DoSend(CharacterType person)
    {
        Card movedCard;
        if (CardControl.Instance.CardsCount == 0)
        {
            movedCard = null;
            CardControl.Instance.IsEnd = true;
        }
        else {
            
            if (person == CharacterType.Desk)
            {
                movedCard = CardControl.Instance.Deal();
                CardCache.Instance.AddCard(movedCard);
            }
            else
            {
                GameObject playerObj = GameObject.Find(person.ToString());
                HandCards cards = playerObj.GetComponent<HandCards>();
                movedCard = CardControl.Instance.Deal();
                cards.AddCard(movedCard);
            }
        }
       

        return movedCard;

    }

    /// <summary>
    /// 精灵化角色手牌
    /// </summary>
    /// <param name="type"></param>
    /// <param name="isSelected"></param>
    void MakeHandCardsSprite(CharacterType type, bool isSelected,bool canSeeCard,bool isSend)
    {
        print("精灵化角色手牌"   "MakeHandCardsSprite() 获得的type"   type);

        if (type==CharacterType.AI||type==CharacterType.Player) {
            GameObject parent = GameObject.Find(type.ToString());
            parent.transform.Find("playpoint").gameObject.SetActive(true);
        }
        

        if (type == CharacterType.Desk)
        {
            CardCache instance = CardCache.Instance;
            for (int i = 0; i < instance.CardsCount; i  )
            {
                MakeSprite(type, instance[i], isSelected,canSeeCard);
            }
        }
        else
        {
            //这个牌是给player和电脑

            GameObject parentObj = GameObject.Find(type.ToString());
            HandCards cards = parentObj.GetComponent<HandCards>();
            //排序
            //cards.Sort();
            //精灵化
            for (int i = 0; i < cards.CardsCount; i  )
            {
                if (!cards[i].MakedSprite)
                {
                    MakeSprite(type, cards[i], isSelected,canSeeCard);
                }
            }
            
       
        }
        //调整精灵位置
        AdjustCardSpritsPosition(type,canSeeCard);
    }

    /// <summary>
    /// 使卡牌精灵化
    /// </summary>
    /// <param name="type"></param>
    /// <param name="card"></param>
    /// <param name="selected"></param>
    void MakeSprite(CharacterType type, Card card, bool selected,bool canSeeCard)
    {
        print("使卡牌精灵化"   "MakeSprite() type"   type "card:" card.Weight "--" card.Suits);
        
        if (!card.MakedSprite)
        {
            GameObject obj = Resources.Load("thepoint") as GameObject;
            GameObject myCard;
            if (type == CharacterType.Desk)
            {
                    myCard = NGUITools.AddChild(GameObject.Find("showpoint"), obj);
               
            }
            else
            {
                myCard = NGUITools.AddChild(GameObject.Find(type.ToString()).gameObject, obj);
            }
            CardSprite sprite = myCard.gameObject.GetComponent<CardSprite>();//脚本需要手动添加
            sprite.Poker = card;
            sprite.Select = selected;
            sprite.CanSeeCard = canSeeCard;

        }
    }

    /// <summary>
    /// 调整手牌位置
    /// </summary>
    /// <param name="type"></param>
    public static void AdjustCardSpritsPosition(CharacterType type,bool canSeeCard)
    {
        print("调整手牌位置"   "AdjustCardSpritsPosition()   type"   type);
        if (type == CharacterType.Desk)
        {
            CardCache instance = CardCache.Instance;
            CardSprite[] cs;
            cs = GameObject.Find("showpoint").GetComponentsInChildren<CardSprite>();
            for (int i=0; i<cs.Length;i  ) {
                Destroy(cs[i].sprite.gameObject);
            }
        }
        else
        {
            int index = 0;
            HandCards hc = GameObject.Find(type.ToString()).GetComponent<HandCards>();
            CardSprite[] cs = GameObject.Find(type.ToString()).GetComponentsInChildren<CardSprite>();
            for (int i = 0; i < hc.CardsCount; i  )
            {
                for (int j = 0; j < cs.Length; j  )
                {
                    if (cs[j].Poker == hc[i])
                    {
                        if (cs[j].Poker == hc.NewCard)
                        {
                            cs[j].GoToPosition(type, index,95);
                        }
                        else {
                            cs[j].GoToPosition(type, index, 90);
                        }
                        cs[j].CanSeeCard = canSeeCard;
                        index  ;

                    }
                }
            }
            
        }

    }

    /// <summary>
    /// 出完牌后二次排序
    /// </summary>
    public void Readjust(CharacterType type,bool cansee)
    {
        GameObject parentObj = GameObject.Find(type.ToString());
        HandCards cards = parentObj.GetComponent<HandCards>();
        //排序
        cards.Sort();
        HandCards hc = GameObject.Find(type.ToString()).GetComponent<HandCards>();
        CardSprite[] cs = GameObject.Find(type.ToString()).GetComponentsInChildren<CardSprite>();
        for (int i = 0; i < hc.CardsCount; i  )
        {
            for (int j = 0; j < cs.Length; j  )
            {
                if (cs[j].Poker == hc[i])
                {
                    cs[j].GoToPosition(type, i, 90);
                    cs[j].CanSeeCard = cansee;
                    
                }
            }
        }
    }

    /// <summary>
    /// 庄
    /// </summary>
    public void DealWith() {
        CharacterType Mainidentity,Normalidentity;
        Identity identity_host,identity_normal;
        identity_host = Identity.fightui_host;
        identity_normal = Identity.loser;
        Mainidentity= CharacterType.Player;
        Normalidentity= CharacterType.AI;
         
        //更新身份
        IdentityUpdate(Mainidentity, identity_host);
        IdentityUpdate(Normalidentity, identity_normal);

        DealCards(Mainidentity);//洗牌
    }

    /// <summary>
    /// ///更新身份
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="identity"></param>
    public void IdentityUpdate(CharacterType type,Identity identity) {
        GameObject parent=GameObject.Find(type.ToString());
        parent.transform.FindChild("identity").GetComponent<UISprite>().spriteName = identity.ToString();
        HandCards cards = GameObject.Find(type.ToString()).GetComponent<HandCards>();
        cards.AccessIdentity = identity;
        //更新积分信息
        //int inte = cards.Multiples * Multiples * basePointPerMatch * (int)(cards.AccessIdentity   1);
        cards.Integration = cards.Integration ;
        UpdateGrade(cards.Integration, type);
        
    }

    //更新积分信息
    public void UpdateGrade(int inte,CharacterType type){
        GameObject obj = GameObject.Find(type.ToString()).transform.FindChild("gradelabel").gameObject;
        obj.GetComponent<UILabel>().text = "积分:"   inte;
        HandCards cards = GameObject.Find(type.ToString()).GetComponent<HandCards>();
        cards.Integration = cards.Integration   inte;
        Debug.Log("积分---------------- inte:" inte);
    }

    /// <summary>
    /// 统计积分信息
    /// </summary>
    /// <returns></returns>
    public int integration(CharacterType type, Identity winner) {
        //积分计算
        HandCards cards = GameObject.Find(type.ToString()).GetComponent<HandCards>();

        int inte = cards.Multiples * Multiples * basePointPerMatch * (int)(cards.AccessIdentity   1);

        if (cards.AccessIdentity != winner)
            inte = -inte;

        cards.Integration = cards.Integration   inte;
        
        GameObject obj = GameObject.Find(type.ToString()).transform.FindChild("gradelabel").gameObject;
        obj.GetComponent<UILabel>().text = "积分:"   inte;
        Debug.Log(type "" winner "inte" inte);
        return inte;
    }

    /// <summary>
    /// 获取指定卡牌
    /// </summary>
    /// <param name="cards"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public static Card GetWeight(Card[] cards, Suits type)
    {
        print("获取指定卡牌"   "GetWeight()");
        if(cards==null){
            return null;
        }
        return cards[0];
    }

    

    /// <summary>
    ///  下一位出牌
    /// </summary>
    public void TrunNext(CharacterType person) {
        person  ;
        bool auto=PlayerChache.Instance.Auto;
        if (person==CharacterType.Desk) {
            person = CharacterType.Player;
        }

        if (person == CharacterType.Player) {
            AICache.Instance.Iscurrent1 = false;
            PlayerChache.Instance.Iscurrent1 = true;
            PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
            //轮到玩家出牌,激活玩家出牌按钮
            buttonControl bc = GameObject.Find("ButtonPanel").GetComponent<buttonControl>();
            if (auto)
            {
                //调用自动出牌的方法
                playCard.StarAuto(true,CharacterType.Player);
            }
            else {
                bc.Cando();
                bc.checkSend();
            }
        }
        if (person==CharacterType.AI) {
            AICache.Instance.Iscurrent1 = true;
            PlayerChache.Instance.Iscurrent1 = false;
            if (CardControl.Instance.CardsCount == 0)
            {
                Debug.Log("在这里结束啦啦啦啦啦啊");
                GameOver(Identity.none);

            }
            else {
                //开启自动出牌
                SimpleSmartCard smart = GameObject.Find("AI").GetComponent<SimpleSmartCard>();
                smart.startSend(true);
                Debug.Log("CharacterType:zidognchupai------------------------");
                //调用玩家出牌的方法
                PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
                if (auto)
                {
                    playCard.StarAuto(true, CharacterType.AI);
                }
               
            }
           
        }
        

    }

    /// <summary>
    /// 补牌 即发一张牌
    /// </summary>
    public Card AddCard(CharacterType currentperson) {
        if (currentperson==CharacterType.AI) {
            Debug.Log("发一张给AI                  ");
        }
        GameObject parentObj = GameObject.Find(currentperson.ToString());
        HandCards cards = parentObj.GetComponent<HandCards>();
        buttonControl bc = GameObject.Find("ButtonPanel").GetComponent<buttonControl>();
        //排序
       // cards.Sort();
        if (CardControl.Instance.CardsCount!= 0) {
            //发一张给
            Card sendcard = DoSend(currentperson);
            
                MakeHandCardsSprite(currentperson, false,false,true);
            return sendcard;
        }
        if (CardCache.Instance.CardsCount!=0) {
            bc.checkeCanhu();
        }
       
        return null;
    }


    /// <summary>
    /// 游戏结束  游戏结束  弹出游戏结束画面 删除所有按钮 激活快速开始按钮  电脑明牌  分数结算
    /// </summary>
    public void GameOver(Identity winner)
    {
        Debug.Log("游戏结束!!!");
        CardControl.Instance.IsEnd = true;
        //结束界面
        buttonControl bc = GameObject.Find("ButtonPanel").GetComponent<buttonControl>();
        //统计所有人的分数
        int playerGrade= integration(CharacterType.Player, winner);
        int AIGrade = integration(CharacterType.AI, winner);
        bc.isOver(playerGrade,AIGrade);
        //明牌
        CardSprite[] sprites = GameObject.Find(CharacterType.AI.ToString()).GetComponentsInChildren<CardSprite>();
        MakeHandCardsSprite(CharacterType.AI, false,true,false);
        AdjustCardSpritsPosition(CharacterType.AI,true);
    }

    public void GameRestart() {
        //删除所有牌  将所有牌回到牌库 删除精灵
        BackToDeck();
        DestroyAllSprites();
        //清除桌面缓存
        AICache.Instance.ClearAll();
        PlayerChache.Instance.ClearAll();
        Destroy(GameObject.Find("ButtonPanel").gameObject);
        CardCache.Instance.Clear();
        CardControl.Instance.ReCreate();
        Destroy(GameObject.Find("StartPanel").gameObject);
        //GameObject mainpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("StartPanel"));//加载StartPanel第一个场景
        //mainpanel.AddComponent<toStar>();
        //mainpanel.name = mainpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)
        //GameObject buttonpanel = NGUITools.AddChild(UICamera.mainCamera.gameObject, (GameObject)Resources.Load("ButtonPanel"));
        //buttonpanel.AddComponent<buttonControl>();
        //buttonpanel.name = buttonpanel.name.Replace("(Clone)", "");//可以替换掉后缀(Clone)

        //for (int i = 1; i < 4; i  )
        //{
        //    if (GameObject.Find(((CharacterType)i).ToString()))
        //    {
        //        IdentityUpdate((CharacterType)i, Identity.loser);
        //    }
        //}
        InitStar();
    }

    /// <summary>
    /// 改变牌放的点
    /// </summary>
    public void ChangePosition(CharacterType person, CardsType type,List<Card> clist)
    {
        Card deskCard = CardCache.Instance.TotalWeight;
        
        //销毁牌精灵
        // CardSprite[] sprites = GameObject.Find("showpoint").GetComponentsInChildren<CardSprite>();
        CardSprite[] sprites2 = GameObject.Find(person.ToString()).GetComponentsInChildren<CardSprite>();

        //for (int i = 0; i < sprites.Length; i  )
        //{
        //    sprites[i].Destroy();
        //}

        for (int i = 0; i < sprites2.Length; i  ){
            for (int j=0; j<clist.Count;j  ) {
                if (sprites2[i].Poker!=deskCard) {
                    if (sprites2[i].Poker == clist[j])
                    {
                        sprites2[i].Destroy();
                        continue;
                    }
                }
              
            }
        }
        if (type != CardsType.fightui_chi)
        {
            clist.Add(deskCard);
        }
        Debug.Log("桌上牌-----------clist" clist.Count type clist[1].CardName );
        GameObject myCon = Resources.Load("myCon") as GameObject;
        GameObject parent = GameObject.Find(person.ToString()).transform.FindChild("cardpengpoint").gameObject;
        GameObject myCard = NGUITools.AddChild(parent, myCon);
        parent.GetComponent<UIGrid>().repositionNow = true;
        parent.GetComponent<UIGrid>().enabled = true;
        //精灵化
        for (int i = 0; i < 3; i  )
        {
            GameObject obj = Resources.Load("thepoint") as GameObject;
            obj= NGUITools.AddChild(myCard, obj);
            obj.GetComponent<BoxCollider>().enabled = false;//设置不能够点击
            CardSprite sprite = obj.gameObject.GetComponent<CardSprite>();//脚本需要手动添加
            if (clist.Count == 4&& type== CardsType.fightui_gang)
            {
                if (i == 1)
                {
                    sprite.Poker = clist[i];
                    sprite.CanSeeCard = false;
                }
                else
                {
                    sprite.CanSeeCard = true;
                    sprite.Poker = clist[i];
                }
            }
            else {
                sprite.CanSeeCard = true;
                sprite.Poker = clist[i];
            }
            
           
           
            sprite.Changeposition(person,i);

        }
        Debug.Log("精灵化成功---调整牌的位置");


        //调整牌的位置
        AdjustCardSpritsPosition(person,false);
       
    }

    /// <summary>
    /// 场上的所有牌回到牌库中
    /// </summary>
    public void BackToDeck()
    {
        print("场上的所有牌回到牌库中"   "BackToDeck()");
        HandCards[] handCards = new HandCards[2];
        handCards[0] = GameObject.Find("Player").GetComponent<HandCards>();
        handCards[1] = GameObject.Find("AI").GetComponent<HandCards>();

        for (int i = 0; i < handCards.Length; i  )
        {
            while (handCards[i].CardsCount != 0)
            {
                Card last = handCards[i][handCards[i].CardsCount - 1];
                
                CardControl.Instance.AddCard(last);
                handCards[i].PopCard(last);
            }
        }
    }

    /// <summary>
    /// 销毁精灵
    /// </summary>
    public void DestroyAllSprites()
    {
        print("销毁精灵"   "DestroyAllSprites()");
        CardSprite[][] cardSprites = new CardSprite[2][];
        cardSprites[0] = GameObject.Find("Player").GetComponentsInChildren<CardSprite>();
        cardSprites[1] = GameObject.Find("AI").GetComponentsInChildren<CardSprite>();
       // Destroy(GameObject.Find("showpoint").gameObject);
       
        for (int i = 0; i < cardSprites.GetLength(0); i  )
        {
            for (int j = 0; j < cardSprites[i].Length; j  )
            {
                cardSprites[i][j].Destroy();
            }
        }
    }

    /// <summary>
    /// 创建桌面牌的精灵
    /// </summary>
    /// <param name="fromperson"></param>
    /// <param name="card"></param>
    public static void CreateSprites(CharacterType fromperson,Card card)
    {
        List<Card> cards=new List<Card>();
        cards.Clear();
        if (fromperson == CharacterType.AI)
        {
            AICache.Instance.AddCard(card);
            cards = AICache.Instance.Library;
        }
        else {
            cards = PlayerChache.Instance.Library;
            PlayerChache.Instance.AddCard(card);
          
        }
        GameObject myCheck = GameObject.Find(fromperson.ToString()).transform.Find("showardpoint").gameObject;
        for (int k = 0; k < myCheck.transform.childCount; k  )
        {
            GameObject go = myCheck.transform.GetChild(k).gameObject;
            Destroy(go);

        }
        // Destroy(show1[i].gameObject);//.gameObject才会删除组件,否则只会删除脚本

        //myCheck.transform.Find("showardpoint").GetComponent<UIGrid>().RemoveChild();

        for (int i=0; i< cards.Count;i  ) {//最后添加的一张牌要高亮
            bool light=false;
            if (i==cards.Count-1) {
                light = true;
            }

            ShowSprite(fromperson, cards[i], light);
        }
    }

    /// <summary>
    /// 显示桌面上的牌
    /// </summary>
    public static void ShowSprite(CharacterType fromperson,Card card,bool light) {
        GameObject myCard=null;
        GameObject obj = Resources.Load("thepoint") as GameObject;
        myCard = NGUITools.AddChild(GameObject.Find(fromperson.ToString()).transform.FindChild("showardpoint").gameObject, obj);
        GameObject.Find(fromperson.ToString()).transform.FindChild("showardpoint").GetComponent<UITable>().enabled = true;
        GameObject.Find(fromperson.ToString()).transform.FindChild("showardpoint").GetComponent<UITable>().repositionNow = true;
       
        CardSprite sp = myCard.gameObject.GetComponent<CardSprite>();
        sp.Poker = card;
        sp.sprite.width = 88;
        sp.sprite.height =120;
        if (light)
        {
            sp.sprite.spriteName = "Bottom_Front_Light";
        }
        else {

            sp.sprite.spriteName = "Bottom_Front";
        }
        sp.transform.localScale = new Vector3((float)0.7, (float)0.7, 1);
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.target = sp.transform;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.relative = 0;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().leftAnchor.absolute = 0;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.relative = 1;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().rightAnchor.absolute =-2;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.relative = 0;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().bottomAnchor.absolute = 29;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.relative = 1;
        sp.transform.FindChild("mypointS").GetComponent<UISprite>().topAnchor.absolute = 0;
    }
    
  

    public static void MakeDialogCell(List<Card> clist) {

        buttonControl bc = GameObject.Find("ButtonPanel").GetComponent<buttonControl>();
        bc.ShowmyCheck(clist);
        bc.transform.Find("myCheck").GetComponent<UIGrid>().Reposition();// 这个标记会让元素立即重新排列。
        

        // GameObject root = GameObject.Find("UI Root");
        // GameObject parent = root.transform.Find("Camera/StartPanel(Clone)/myCheck").gameObject;

        //parent.active=true;
        //MyConSprite myCardparent = parent.GetComponent<MyConSprite>();
        //parent.active = false;

    }

    //获取选择事件
    public void getConSelect() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        playCard.GetMySelectList();//去吃
                                   //吃完 删除桌面上面被吃的牌 
        AICache.Instance.DeleteCard(CardCache.Instance.TotalWeight);
        //ReCreateSprites(CharacterType.Player, PlayerChache.Instance.Library);
        ReCreateSprites(CharacterType.AI, AICache.Instance.Library);


    }
    
    //如果没有其他选择,只为单选项
    public static void CometoEat(List<Card> card) {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        playCard.noChoice(card);//直接去吃
        //吃完 删除桌面上面被吃的牌 
        GameController controller = GameObject.Find("GameControll").GetComponent<GameController>();
        AICache.Instance.DeleteCard(CardCache.Instance.TotalWeight);
        //controller.ReCreateSprites(CharacterType.Player,PlayerChache.Instance.Library);
        controller.ReCreateSprites(CharacterType.AI,AICache.Instance.Library);
    }

    public void ReCreateSprites(CharacterType fromperson, List<Card> cards)
    {
        if (cards.Count>3) {
            Debug.Log("ReCreateSprites: type=="   fromperson.ToString()   "cards:"   cards[0].CardName   cards[1].CardName cards[2].CardName);
        }
        GameObject myCheck = GameObject.Find(fromperson.ToString()).transform.Find("showardpoint").gameObject;
        for (int k = 0; k < myCheck.transform.childCount; k  )
        {
            GameObject go = myCheck.transform.GetChild(k).gameObject;
            Destroy(go);

        }
        // Destroy(show1[i].gameObject);//.gameObject才会删除组件,否则只会删除脚本

        //myCheck.transform.Find("showardpoint").GetComponent<UIGrid>().RemoveChild();

        for (int i = 0; i < cards.Count; i  )
        {
            bool light = false;

            ShowSprite(fromperson, cards[i], light);
        }
    }

    /// <summary>
    /// 删除桌面上被吃/碰/杠的牌
    /// </summary>
    public void deleteCardOnDestory(CharacterType person,List<Card> cards) {
       
        GameObject myCheck = GameObject.Find(person.ToString()).transform.Find("showardpoint").gameObject;
        for (int k = 0; k < myCheck.transform.childCount; k  )
        {
            GameObject go = myCheck.transform.GetChild(k).gameObject;
            Destroy(go);
        }
        // Destroy(show1[i].gameObject);//.gameObject才会删除组件,否则只会删除脚本

        //myCheck.transform.Find("showardpoint").GetComponent<UIGrid>().RemoveChild();

        

        for (int i = 0; i < cards.Count; i  )
        {
            ShowSprite(person, cards[i], false);
        }
    }


    //private void Update()
    //{
    //    StartCoroutine(Up1());

    //}
    //private IEnumerator Up1()
    //{
    //    GameObject root = GameObject.Find("UI Root");
    //    GameObject parent = root.transform.Find("Camera/StartPanel(Clone)/myCheck").gameObject;
    //    parent.active = true;
    //    yield return new WaitForSeconds(5);
    //    parent.active = false;
    //}



}

开始控制按钮了,建一个脚本,绑定各种按钮,里面是按钮绑定的事件

代码语言:javascript复制
public class buttonControl : MonoBehaviour {

    private GameObject Startbtn;//开始游戏
    private GameObject sendbtn;//出牌
    private GameObject addbtn;//补牌
    private GameObject hubtn;//胡
    private GameObject pengbtn;//碰
    private GameObject eatbtn;//吃
    private GameObject gangbtn;//杠
    private GameObject IsEnd;//游戏结束图
    private GameObject restarBtn;//重新开始
    private GameController controller;
    private GameObject myCheck;//弹出框
    private GameObject spupSprite;//托管最上层的遮罩
    private GameObject autoreSetBtn;//取消托管按钮
    private GameObject autocontrolbtn;//托管
    private GameObject cancelbtn;//取消按钮
    // Use this for initialization
    void Start()
    {
        Startbtn = gameObject.transform.Find("Startbtn").gameObject;
        sendbtn = gameObject.transform.Find("sendbtn").gameObject;
        addbtn = gameObject.transform.Find("addbtn").gameObject;
        hubtn = gameObject.transform.Find("hubtn").gameObject;
        pengbtn = gameObject.transform.Find("pengbtn").gameObject;
        eatbtn = gameObject.transform.Find("eatbtn").gameObject;
        gangbtn = gameObject.transform.Find("gangbtn").gameObject;
        IsEnd = gameObject.transform.Find("IsEnd").gameObject;
        restarBtn = gameObject.transform.Find("restarBtn").gameObject;
        myCheck = gameObject.transform.Find("myCheck").gameObject;
        controller = GameObject.Find("GameControll").GetComponent<GameController>();
        spupSprite = gameObject.transform.Find("spup").gameObject;
        autoreSetBtn = gameObject.transform.Find("autoreSet").gameObject;
        autocontrolbtn = gameObject.transform.Find("autocontrolbtn").gameObject;
       

        Startbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(StartGameCallBack));
        sendbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(SendCallBack));
        addbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(addCallBack));
        hubtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(HuCallBack));
        pengbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(PengCallBack));
        eatbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(EatCallBack));
        gangbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(GangCallBack));
        restarBtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(RestarCallBack));
        autocontrolbtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(AutoControlCallBack));
        autoreSetBtn.GetComponent<UIButton>().onClick.Add(new EventDelegate(AutoCancel));
    
        spupSprite.gameObject.SetActive(false);
        autoreSetBtn.gameObject.SetActive(false);
        autocontrolbtn.SetActive(false);

        Startbtn.SetActive(true);
        sendbtn.gameObject.active = false;
        addbtn.gameObject.active = false;
        hubtn.gameObject.active = false;
        pengbtn.gameObject.active = false;
        eatbtn.gameObject.active = false;
        gangbtn.gameObject.active = false;
        IsEnd.gameObject.active = false;
        restarBtn.gameObject.active = false;
        cancelbtn.gameObject.active = false;
        myCheck.SetActive(false);
    }

//按下开始游戏按钮
    public void StartGameCallBack() {
        Startbtn.gameObject.active = false;
        controller.DealWith();//开启GameControll中的发牌方法
        sendbtn.SetActive(true);
    }


    //出牌按钮
    public void SendCallBack() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        controller.Readjust(CharacterType.Player, true);
        if (playCard.CheckSelectCards())
        {
            //轮到下一位出牌
            controller.TrunNext(CharacterType.Player);
            
        }
        
    }

    //过牌 过牌之后不能碰 杠 只能胡
    public void addCallBack(){
        addbtn.SetActive(false);
        eatbtn.SetActive(false);
        pengbtn.SetActive(false);
        gangbtn.SetActive(false);
        hubtn.SetActive(false);
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        PlayerChache.Instance.Cando = false;
        if (playCard.checkCardsLength())
        {
            controller.Readjust(CharacterType.Player, true);
            controller.AddCard(CharacterType.Player);
            if (checkeCanhu())
            {
                sendbtn.SetActive(false);
            }
            else { sendbtn.SetActive(true); }
        }
        else {
            sendbtn.SetActive(true);
        }

    }

    //轮到玩家出牌
    public void Resend() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (!checkeCanhu()) {
            sendbtn.gameObject.active = true;
        }
       
    }

    //吃牌
    public void EatCallBack() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_chi, true))
        {
            eatbtn.gameObject.active = false;
            addbtn.SetActive(false);
            hubtn.SetActive(false);
            pengbtn.SetActive(false);
            gangbtn.SetActive(false);
            controller.Readjust(CharacterType.Player,true);
            
        }

    }

    /// <summary>
    /// 检查是否能吃
    /// </summary>
    public bool checkeat()
    {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_chi,false))
        {
            eatbtn.gameObject.active = true;
            addbtn.gameObject.active = true;
            return true;
        }
        return false;
    }

    /// <summary>
    /// 吃
    /// </summary>
    public void canEat() {//这时候才真正吃
        PlayerChache.Instance.Cando = false;
        eatbtn.gameObject.active = false;
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (!checkeCanhu())
        {
            GameController controller = GameObject.Find("GameControll").GetComponent<GameController>();
            if (playCard.checkCardsLength())
            {
                controller.Readjust(CharacterType.Player, true);
                controller.AddCard(CharacterType.Player);
                if (checkeCanhu())
                {
                    sendbtn.SetActive(false);
                }
                else { sendbtn.SetActive(true); }
            }
            else
            {
                controller.Readjust(CharacterType.Player, true);
                sendbtn.SetActive(true);
            }
        }
       
        
    }

    //碰
    public void PengCallBack() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_peng, true))
        {
            PlayerChache.Instance.Cando = false;
            pengbtn.SetActive(false);
            addbtn.SetActive(false);
            gangbtn.gameObject.active = false;
            eatbtn.gameObject.active = false;
            hubtn.gameObject.active = false;
            AICache.Instance.DeleteCard(CardCache.Instance.TotalWeight);
            controller.ReCreateSprites(CharacterType.AI, AICache.Instance.Library);
            controller.Readjust(CharacterType.Player, true);
            if (playCard.checkCardsLength())
            {
               controller.Readjust(CharacterType.Player, true);
                controller.AddCard(CharacterType.Player);
                if (checkeCanhu())
                {
                    sendbtn.SetActive(false);
                }
                else { sendbtn.SetActive(true); }
            }
            else
            {
                sendbtn.SetActive(true);
            }
            // controller.ReCreateSprites(CharacterType.AI, AICache.Instance.Library);
        }
           
    }

    /// <summary>
    /// 查是否能碰
    /// </summary>
    public bool checkPeng()
    {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        controller.Readjust(CharacterType.Player, true);
        if (playCard.CheckCardsType(CardsType.fightui_peng, false))
        {
            pengbtn.SetActive(true);
            addbtn.gameObject.active = true;
            return true;
            }
        return false;
    }

    //杠
    public void GangCallBack() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_gang,true))
        {
            PlayerChache.Instance.Cando = false;
            addbtn.SetActive(false);
            gangbtn.gameObject.active = false;
            eatbtn.gameObject.active = false;
            pengbtn.gameObject.active = false;
            hubtn.gameObject.active = false;
            //杠完补牌
            checkSend();
            if (!checkeCanhu())
            {
                AICache.Instance.DeleteCard(CardCache.Instance.TotalWeight);
                controller.deleteCardOnDestory(CharacterType.AI,AICache.Instance.Library);
                //controller.ReCreateSprites(CharacterType.Player, PlayerChache.Instance.Library);
                controller.ReCreateSprites(CharacterType.AI, AICache.Instance.Library);
                controller.Readjust(CharacterType.Player, true);
            }
            

        }
    }

    /// <summary>
    /// 检查能否杠
    /// </summary>
    public bool checkGang() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        controller.Readjust(CharacterType.Player, true);
        if (playCard.CheckCardsType(CardsType.fightui_gang, false))
        {   
            gangbtn.SetActive(true);
            addbtn.SetActive(true);
            sendbtn.SetActive(false);
            return true;
        }
        return false;
    }

    /// <summary>
    /// 胡的按钮
    /// </summary>
    public void HuCallBack() {
        hubtn.SetActive(false);
       // cancelbtn.SetActive(false);
        autocontrolbtn.SetActive(false);
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_hu,true))
        {
            GameController controller = GameObject.Find("GameControll").GetComponent<GameController>();
            controller.GameOver(Identity.fightui_host);
        }
    }

    public void isOver(int playergrade, int AIgrade) {
        addbtn.SetActive(false);
        sendbtn.SetActive(false);
        eatbtn.SetActive(false);
        gangbtn.SetActive(false);
        pengbtn.SetActive(false);
        hubtn.SetActive(false);
        Destroy(addbtn.gameObject);
        Destroy(sendbtn.gameObject);
        Destroy(eatbtn.gameObject);
        Destroy(gangbtn.gameObject);
        Destroy(pengbtn.gameObject);
        Destroy(hubtn.gameObject);
        
        restarBtn.gameObject.active = true;
        IsEnd.gameObject.active = true;
       // autocontrolbtn.SetActive ( false);
        IsEnd.transform.Find("playergrade").gameObject.GetComponent<UILabel>().text=playergrade "";
        IsEnd.transform.Find("AIgrade").gameObject.GetComponent<UILabel>().text = AIgrade   "";
      

    }

    public void RestarCallBack() {
        IsEnd.gameObject.active = false;
        restarBtn.gameObject.active = false;
        GameController controller = GameObject.Find("GameControll").GetComponent<GameController>();
        controller.GameRestart();
        Startbtn.gameObject.active = true;
    }

    public bool checkeCanhu() {
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
        if (playCard.CheckCardsType(CardsType.fightui_hu,false))
        {
            hubtn.SetActive(true);
            addbtn.SetActive(true);
          //  cancelbtn.SetActive(true);
            sendbtn.gameObject.active = false;
            return true;
        }
        return false;
    }

    public void ShowmyCheck(List<Card> clist) {
        myCheck.SetActive(true);
        GameObject myCard = NGUITools.AddChild(myCheck, Resources.Load("myCondia") as GameObject);
        myCard.GetComponent<UIGrid>().enabled = true;
        myCard.GetComponent<UIGrid>().repositionNow = true;
        //精灵化
        for (int i = 0; i < 3; i  )
        {
            GameObject obj = Resources.Load("thepoint") as GameObject;
            obj = NGUITools.AddChild(myCard, obj);
            obj.GetComponent<BoxCollider>().enabled = false;//设置不能够点击
            CardSprite sprite = obj.gameObject.GetComponent<CardSprite>();//脚本需要手动添加
            sprite.Poker = clist[i];
            sprite.sprite.width = 78;
            sprite.sprite.height = 115;
            sprite.transform.localScale = new Vector3((float)1, (float)1, 1);
         }
    }

    /// <summary>
    /// 自动查牌
    /// </summary>
    public void Cando() {
        if (checkeCanhu())
        {
            sendbtn.SetActive(false);
            PlayerChache.Instance.Cando = true;
        }
        PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();

        if (playCard.checkCardsLength()) {
            if (checkeat())
            {
                sendbtn.SetActive(false);
                PlayerChache.Instance.Cando = true;
            }
            if (checkGang())
            {
                sendbtn.SetActive(false);
                PlayerChache.Instance.Cando = true;
            }
            if (checkPeng())
            {
                sendbtn.SetActive(false);
                PlayerChache.Instance.Cando = true;
            }
            if (checkGang())
            {
                sendbtn.SetActive(false);
                PlayerChache.Instance.Cando = true;
            }
        }   
    }

    /// <summary>
    /// 检查是否能够发牌
    /// </summary>
    /// <returns></returns>
    public void checkSend() {
        checkeCanhu();
        if (!PlayerChache.Instance.Cando) {
            //检查没有吃碰杠胡事件 检查卡牌长度
            PlayCard playCard = GameObject.Find("Player").GetComponent<PlayCard>();
            if (playCard.checkCardsLength())
            {
                controller.Readjust(CharacterType.Player,true);
                controller.AddCard(CharacterType.Player);
            }
            if (!checkeCanhu())
            {
                if (PlayerChache.Instance.Iscurrent1)
                {//查当前出牌者
                    sendbtn.SetActive(true);
                }
            }
        }
    }
}

数据处理的卡牌缓存和算法写在第二部分了

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/175692.html原文链接:https://javaforall.cn

0 人点赞