コード例 #1
0
    void FixedUpdate()
    {
        debugStr = string.Format("{0}:{1}:{2}", gameStatus.ToString(), isWaiting, isOperating);
        //Debug.Log ("FixedUpdate: " + isOperating + isWaiting);
        // 用户正在操作,跳过所有逻辑
        if (isOperating)
        {
            return;
        }

        if (gameStarted && isPending)
        {
            int nPlayers = 0, nEnemies = 0;
            foreach (GameObject a in playerActors)
            {
                if (a.activeInHierarchy == false)
                {
                    continue;
                }
                nPlayers += 1;
                if (a.GetComponent <Rigidbody2D> ().velocity != Vector2.zero)
                {
                    isPending = true;
                    return;
                }
            }

            foreach (GameObject a in enemyActors)
            {
                if (a.activeInHierarchy == false)
                {
                    continue;
                }
                nEnemies += 1;
                if (a.GetComponent <Rigidbody2D> ().velocity != Vector2.zero)
                {
                    isPending = true;
                    return;
                }
            }

            arrowPlayer.SetActive(false);
            arrowEnemy.SetActive(false);

            // 判断游戏结束
            if (nPlayers == 0)
            {
                client.sendClose();
                Application.LoadLevel("lose");
            }

            if (nEnemies == 0)
            {
                client.sendClose();
                Application.LoadLevel("win");
            }

            isPending = false;
            // 如果是当前客户端为master,并且slave操作刚刚结束,则开始一个新回合
            if (client.isMaster() && gameStatus == ST_NEW)
            {
                newRound(false);
                isWaiting   = true;
                isOperating = false;
                return;
            }
        }

        //fixedUpdateLoopCounter += 1;
        if (isWaiting)
        {
            if (++fixedUpdateLoopCounter % 30 != 0)
            {
                return;
            }
            fixedUpdateLoopCounter = 0;
            //Debug.Log ("pulling msg ... ");
            string[] resp;
            // 从服务器拉取消息
            if (!client.pullMsg(out resp))
            {
                // 无消息,返回继续等待
                return;
            }

            if (resp[0] == "OK")
            {
                Debug.Assert(client.isMaster());
                //gameStatus += 1;	// master进入下一阶段
                Debug.Log("slave confirmed, move onto next stage");
                gameStatus = (gameStatus + 1) % 3;
                // master发布下一阶段指令
                switch (gameStatus)
                {
                case ST_NEW:
                    // slave加入,master开始游戏第一回合
                    // 隐藏等待窗口
                    startGame();
                    isWaiting   = true;
                    isOperating = false;
                    break;

                case ST_MASTER:
                    // master操作
                    isOperating = true;
                    isWaiting   = false;
                    arrowPlayer.SetActive(true);
                    break;

                case ST_SLAVE:
                    // 轮到slave操作,发送YOURS,等待响应
                    client.sendYours();
                    isWaiting = true;
                    arrowEnemy.SetActive(true);
                    break;

                default:
                    // 无效状态
                    Debug.Log("shouldn't be here");
                    Debug.Assert(false);
                    break;
                }
                return;
            }
            else if (resp[0] == "NEW")
            {
                Debug.Assert(!client.isMaster());
                // 收到master命令,开始新回合,创建动态道具
                if (gameStarted == false)
                {
                    // 首回合
                    startGame();
                }
                int len = resp.Length;
                for (int i = 3; i < len; i += 3)
                {
                    int itemID = int.Parse(resp[i]);
                    // master和slave屏幕方向相反,因此坐标取反
                    float posX = int.Parse(resp[i + 1]) / -10.0f;
                    float posY = int.Parse(resp[i + 2]) / -10.0f;

                    Transform iTrans = Instantiate(itemPrefabs[itemID]);
                    iTrans.position = new Vector2(posX, posY);
                }

                arrowEnemy.SetActive(true);
                arrowPlayer.SetActive(false);
                // 发送OK响应master
                client.sendOK();
            }
            else if (resp[0] == "MOVE")
            {
                // 解析对手操作
                int actorID, forceX, forceY;

                try {
                    actorID = int.Parse(resp[3]) - 1;
                    forceX  = -int.Parse(resp[4]);
                    forceY  = -int.Parse(resp[5]);

                    GameObject actor = enemyActors[actorID];
                    actor.GetComponent <HealthScript>().Attack(new Vector2(forceX, forceY));
                    //actor.GetComponent<Rigidbody2D>().AddForce();
                    isOperating = false;
                    isWaiting   = false;
                    isPending   = true;
                    // slave收到master的操作后响应OK
                    if (!client.isMaster())
                    {
                        client.sendOK();
                    }
                } catch {
                    // 消息解析错误
                    Debug.Log("invalid msg ...");
                    Debug.Assert(false);
                }

                // 老鼠运动刚刚开始,不需要后面的检查过程
                if (client.isMaster())
                {
                    // 如果本机是master,则对手刚刚执行完操作,
                    // 等待所有actor停止运动然后开始新回合
                    gameStatus = ST_NEW;
                }
                else
                {
                    // 如果本机是slave,则等待master的下一步指令
                    isWaiting = true;
                }
                return;
            }
            else if (resp[0] == "YOURS")
            {
                Debug.Assert(!client.isMaster());
                isOperating = true;
                isPending   = false;
                isWaiting   = false;
                arrowEnemy.SetActive(false);
                arrowPlayer.SetActive(true);
                return;
            }
            else if (resp[0] == "CLOSED")
            {
                // 对方退出或掉线,游戏结束,自动获胜
                isWaiting = false;
                Application.LoadLevel("win");
            }
            else
            {
                // 无效消息,尝试重新获取,其实并没有什么卵用,因为服务器和对手都不会重发
                Debug.Log("invalid msg: " + resp[0]);
                isWaiting = true;
            }
        }

        // slave等待master发布下一阶段指令
        if (!client.isMaster())
        {
            isWaiting = true;
        }
        else
        {
        }

        // 老鼠运动都结束,游戏进入下一阶段
        //gameStatus = (gameStatus + 1) % 3;

        return;
    }