坦克大战 第九章 房间系统


提示框功能:
public class TipPanel : PanelBase
{
    private Text text;
    private Button btn;
    string str = "" ;
    #region 生命周期
    //初始化
    public override void Init( params object [] args)
    {
        base .Init(args);
        skinPath = "TipPanel" ;
        layer = PanelLayer .Tips;
        //参数 args[1]代表提示的内容
        if (args.Length == 1)
        { //获取第一个参数,既要显示的内容
            str = ( string )args[0];
        }
    }
    //显示之前
    public override void OnShowing()
    {
        base .OnShowing();
        Transform skinTrans = skin.transform;
        //文字
        text = skinTrans.Find( "Text" ).GetComponent< Text >();
        text.text = str;
        //关闭按钮
        btn = skinTrans.Find( "Btn" ).GetComponent< Button >();
        btn.onClick.AddListener(OnBtnClick);
    }
    #endregion
    //按下“知道了”按钮的事件
    public void OnBtnClick()
    {
        Close();
    }
}



using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RoomListPanel : PanelBase
{
    private Text idText;
    private Text winText;
    private Text lostText;
    private Transform content;
    private GameObject roomPrefab;
    private Button closeBtn;
    private Button newBtn;
    private Button reflashBtn;
    #region 生命周期
    /// <summary> 初始化 </summary>
    public override void Init( params object [] args)
    {
        base .Init(args);
        skinPath = "RoomListPanel" ;
        layer = PanelLayer .Panel;
    }
        /// <summary>
        /// 获取组件
        /// </summary>
    public override void OnShowing()
    {
        base .OnShowing();
        //获取Transform
        Transform skinTrans = skin.transform;
        Transform listTrans = skinTrans.Find( "ListImage" );
        Transform winTrans = skinTrans.Find( "WinImage" );
        //获取成绩栏部件
        idText = winTrans.Find( "IDText" ).GetComponent< Text >();
        winText = winTrans.Find( "WinText" ).GetComponent< Text >();
        lostText = winTrans.Find( "LostText" ).GetComponent< Text >();
        //获取列表栏部件
        Transform scroolRect = listTrans.Find( "ScrollRect" );
        content = scroolRect.Find( "Content" );
               //拿到房间预制体  隐藏
        roomPrefab = content.Find( "RoomPrefab" ).gameObject;
        roomPrefab.SetActive( false );
        closeBtn = listTrans.Find( "CloseBtn" ).GetComponent< Button >();
        newBtn = listTrans.Find( "NewBtn" ).GetComponent< Button >();
        reflashBtn = listTrans.Find( "ReflashBtn" ).GetComponent< Button >();
        //按钮事件
        reflashBtn.onClick.AddListener(OnReflashClick);
        newBtn.onClick.AddListener(OnNewClick);
        closeBtn.onClick.AddListener(OnCloseClick);
        //监听
        NetMgr .srvConn.msgDist.AddListener( "GetAchieve" , RecvGetAchieve);
        NetMgr .srvConn.msgDist.AddListener( "GetRoomList" , RecvGetRoomList);
        //发送查询
        ProtocolBytes protocol = new ProtocolBytes ();
        protocol.AddString( "GetRoomList" );
        NetMgr .srvConn.Send(protocol);
        protocol = new ProtocolBytes ();
        protocol.AddString( "GetAchieve" );
        NetMgr .srvConn.Send(protocol);
    }
    public override void OnClosing()
    {
        NetMgr .srvConn.msgDist.DelListener( "GetAchieve" , RecvGetAchieve);
        NetMgr .srvConn.msgDist.DelListener( "GetRoomList" , RecvGetRoomList);
    }
    #endregion
        //获取玩家成绩
    //收到GetAchieve协议
    public void RecvGetAchieve( ProtocolBase protocol)
    {
        //解析协议
        ProtocolBytes proto = ( ProtocolBytes )protocol;
        int start = 0;
        string protoName = proto.GetString(start, ref start);
        int win = proto.GetInt(start, ref start);
        int lost = proto.GetInt(start, ref start);
        //处理
        idText.text = "指挥官:" + GameMgr .instance.id;
        winText.text = win.ToString();
        lostText.text = lost.ToString();
    }
        //收到GetRoomList协议    房间列表  房间数量  房间信息1  房间信息2.。。
        public void RecvGetRoomList( ProtocolBase protocol)
    {
        //清理
        ClearRoomUnit();
        //解析协议
        ProtocolBytes proto = ( ProtocolBytes )protocol;
        int start = 0;
        string protoName = proto.GetString(start, ref start);
        int count = proto.GetInt(start, ref start);
        for ( int i = 0; i < count; i++)
        {
            int num = proto.GetInt(start, ref start); //玩家数量
            int status = proto.GetInt(start, ref start); //房间数量
            GenerateRoomUnit(i, num, status); //创建一个房间
        }
    }
    public void ClearRoomUnit()
    {
        for ( int i = 0; i < content.childCount; i++)
            if (content.GetChild(i).name.Contains( "Clone" ))
                Destroy(content.GetChild(i).gameObject);
    }
    //创建一个房间单元
    //参数 i,房间序号(从0开始)
    //参数num,房间里的玩家数
    //参数status,房间状态,1-准备中 2-战斗中
    public void GenerateRoomUnit( int i, int num, int status)
    {
        //添加房间
        content.GetComponent< RectTransform >().sizeDelta = new Vector2 (0, (i + 1) * 110);
        GameObject o = Instantiate(roomPrefab);
        o.transform.SetParent(content);
        o.SetActive( true );
        //房间信息
        Transform trans = o.transform;
        Text nameText = trans.Find( "nameText" ).GetComponent< Text >();
        Text countText = trans.Find( "CountText" ).GetComponent< Text >();
        Text statusText = trans.Find( "StatusText" ).GetComponent< Text >();
        nameText.text = "序号:" + (i + 1).ToString();
        countText.text = "人数:" + num.ToString();
        if (status == 1) //1准备中
        {
            statusText.color = Color .black;
            statusText.text = "状态:准备中" ;
        }
        else //2开战中
        {
            statusText.color = Color .red;
            statusText.text = "状态:开战中" ;
        }
        //按钮事件
        Button btn = trans.Find( "JoinButton" ).GetComponent< Button >();
        btn.name = i.ToString();   //改变按钮的名字,以便给OnJoinBtnClick传参
        btn.onClick.AddListener( delegate ()
        {
            OnJoinBtnClick(btn.name);
        }
        );
    }
    //刷新按钮
    public void OnReflashClick()
    { //发送刷新协议
        ProtocolBytes protocol = new ProtocolBytes ();
        protocol.AddString( "GetRoomList" );
        NetMgr .srvConn.Send(protocol);
    }
    //加入按钮   加入协议,我们向服务端发送“加入协议”协议添加要加入房间序号此处利用观察者模式,监听返回的协议,解析协议是否加入成功,成功则实例化房间面板,服务端在玩家加入或离开房,间向房间里广播刷新房间信息的协议(我们还是利用观察者模式监听更新房间信息的协议,收到之后执行更新面板方法),以便客户端更新界面。
    public void OnJoinBtnClick( string name)
    {
        ProtocolBytes protocol = new ProtocolBytes ();
        protocol.AddString( "EnterRoom" ); //加入房间协议
        protocol.AddInt( int .Parse(name)); //加入房间序号
        NetMgr .srvConn.Send(protocol, OnJoinBtnBack); //回掉  返回加入结果 0加入成功  -1失败
        Debug .Log( "请求进入房间 " + name);
    }
        //加入按钮的回掉  返回加入结果 0加入成功  -1失败
        public void OnJoinBtnBack( ProtocolBase protocol)
    {
        //解析参数
        ProtocolBytes proto = ( ProtocolBytes )protocol;
        int start = 0;
        string protoName = proto.GetString(start, ref start);
        int ret = proto.GetInt(start, ref start);
        //处理
        if (ret == 0)
        {
            PanelMgr .instance.OpenPanel< TipPanel >( "" , "成功进入房间!" );
            PanelMgr .instance.OpenPanel< RoomPanel >( "" );
            Close();
        }
        else
        {
            PanelMgr .instance.OpenPanel< TipPanel >( "" , "进入房间失败" );
        }
    }
    //新建按钮
    public void OnNewClick()
    {
        ProtocolBytes protocol = new ProtocolBytes ();
        protocol.AddString( "CreateRoom" );
        NetMgr .srvConn.Send(protocol, OnNewBack);
    }
    //新建按钮返回
    public void OnNewBack( ProtocolBase protocol)
    {
        //解析参数
        ProtocolBytes proto = ( ProtocolBytes )protocol;
        int start = 0;
        string protoName = proto.GetString(start, ref start);
        int ret = proto.GetInt(start, ref start);
        //处理
        if (ret == 0)
        {
            PanelMgr .instance.OpenPanel< TipPanel >( "" , "创建成功!" );
            PanelMgr .instance.OpenPanel< RoomPanel >( "" );
            Close();
        }
        else
        {
            PanelMgr .instance.OpenPanel< TipPanel >( "" , "创建房间失败!" );
        }
    }
    //登出按钮
    public void OnCloseClick()
    {
        ProtocolBytes protocol = new ProtocolBytes ();
        protocol.AddString( "Logout" );
        NetMgr .srvConn.Send(protocol, OnCloseBack);
    }
    //登出返回
    public void OnCloseBack( ProtocolBase protocol)
    {
        PanelMgr .instance.OpenPanel< TipPanel >( "" , "登出成功!" );
        PanelMgr .instance.OpenPanel< LoginPanel >( "" , "" );
        NetMgr .srvConn.Close();
    }
}

猜你喜欢

转载自blog.csdn.net/Pandora12138/article/details/80997995