Exemplo n.º 1
0
    IEnumerator MainCycle()
    {
        while (true)
        {
            if (msgQueue.Count < 1)
            {
                // haven't any packets
                yield return(new WaitForSeconds(0.01f));
            }
            else
            {
                PacketMSG receivedPacket = (PacketMSG)msgQueue.Dequeue();
                int       packetId       = receivedPacket.Cmd;
                Cmd       cmd            = (Cmd)packetId;
                switch (cmd)
                {
                case Cmd.CmdPing:
                    break;

                case Cmd.CmdLogin:
                    break;

                case Cmd.CmdLoginSuccess:
                    break;

                case Cmd.CmdLoginFailed:
                    break;

                case Cmd.CmdSetFamilyRequest:
                    managerUI.SetStateSetFamily();
                    break;

                case Cmd.CmdSetFamily:
                    break;

                case Cmd.CmdSetFamilySuccess:
                    break;

                case Cmd.CmdSetFamilyFailed:
                    break;

                case Cmd.CmdPreviewAvatar:
                    break;

                case Cmd.CmdPreviewAvatars:

                    // очень грязный хак
                    chooseCharacterWindow.ClearSubs();
                    // </>

                    PacketPreviewAvatars packetPreviewAvatars = PacketPreviewAvatars.Parser.ParseFrom(receivedPacket.Data);
                    foreach (PacketPreviewAvatar packetPreviewAvatar in packetPreviewAvatars.Avatars)
                    {
                        chooseCharacterWindow.AddCharacter(packetPreviewAvatar.AvatarId, packetPreviewAvatar.Name, packetPreviewAvatar.Level);
                    }
                    int startArrayId = (packetPreviewAvatars.Avatars.Count == 0) ? 1 : packetPreviewAvatars.Avatars.Count;
                    for (int avatarArrayId = startArrayId; avatarArrayId <= packetPreviewAvatars.MaxAvatars; avatarArrayId++)
                    {
                        if (avatarArrayId <= packetPreviewAvatars.AvailableAvatars)
                        {
                            chooseCharacterWindow.AddAvailable();
                        }
                        else
                        {
                            chooseCharacterWindow.AddDisable();
                        }
                    }

                    managerUI.SetStateChooseAvatar();
                    break;

                case Cmd.CmdSetNewAvatar:
                    managerUI.SetStateSetNewAvatar();
                    break;

                case Cmd.CmdSetNewAvatarSuccess:
                    break;

                case Cmd.CmdSetNewAvatarFailed:
                    break;

                case Cmd.CmdJoinToChannel:
                    break;

                case Cmd.CmdAvatar:
                    break;

                case Cmd.CmdAvatars:
                    // action
                    PacketAvatars packetAvatars = PacketAvatars.Parser.ParseFrom(receivedPacket.Data);

                    foreach (PacketAvatar packetAvatar in packetAvatars.Avatars)
                    {
                        handlePacketAvatar(packetAvatar, respawn: false);
                    }
                    break;

                case Cmd.CmdJoinedAvatar:
                    if (players.Count == 0)
                    {
                        managerUI.SetStateGameplay();
                    }

                    PacketJoinedAvatar packetJoinedAvatar = PacketJoinedAvatar.Parser.ParseFrom(receivedPacket.Data);
                    PacketAvatar       packetAvatarJA     = packetJoinedAvatar.Avatar;
                    PacketTransform    packetTransformJA  = packetAvatarJA.Transform;
                    Vector3            receivedPositionJA = new Vector3(packetAvatarJA.Transform.Position.X, packetAvatarJA.Transform.Position.Y, packetAvatarJA.Transform.Position.Z);
                    Vector3            receivedRotationJA = new Vector3(0f, packetAvatarJA.Transform.Rotation.Yaw, 0f);

                    GameObject newPlayer;

                    if (packetAvatarJA.AvatarId == this.avatarId)                       // if it's our player, we spawn prefab with input controller
                    {
                        newPlayer = (GameObject)Instantiate(playerPrefab, receivedPositionJA, Quaternion.identity);
                        CameraFollow cameraScript = gameplayCamera.AddComponent <CameraFollow> ();
                        cameraScript.OnAwake(newPlayer.transform);
                    }
                    else                         // otherwise prebaf enemy without input controller
                    {
                        newPlayer = (GameObject)Instantiate(enemyPrefab, receivedPositionJA, Quaternion.identity);
                    }

                    newPlayer.name = packetAvatarJA.Name;
                    newPlayer.transform.position    = receivedPositionJA;
                    newPlayer.transform.eulerAngles = receivedRotationJA;

                    // add health Avatar script to avatar
                    Avatar avatarScript = newPlayer.GetComponent <Avatar> ();
                    if (packetAvatarJA.AvatarId == this.avatarId)                       // if it's our player
                    {
                        avatarScript.isMine = true;
                    }
                    avatarScript.AvatarId   = packetAvatarJA.AvatarId;
                    avatarScript.AvatarName = packetAvatarJA.Name;
                    avatarScript.FamilyId   = packetAvatarJA.FamilyId;
                    avatarScript.FamilyName = packetAvatarJA.FamilyName;
                    avatarScript.Level      = packetAvatarJA.Level;
                    avatarScript.AnimState  = packetAvatarJA.AnimState;

                    ///////////////////////////////////////////////////////////////////////// HEALTH
                    // add Health script to avatar
                    Health health = newPlayer.GetComponent <Health> ();

                    ///////////////////////////////////////////////////////////////////////// MANNA
                    // add manna script to avatar
                    Manna manna = newPlayer.GetComponent <Manna> ();

                    // gameplay UI
                    HealthView globalHealthView = null;
                    MannaView  globalMannaView  = null;

                    if (packetAvatarJA.AvatarId == this.avatarId)                       // if it's our player, we spawn prefab with input controller
                    // find avatar bars
                    {
                        GameObject avatarBars = GameObject.FindGameObjectWithTag("AvatarBars");

                        // add health UI
                        GameObject globalHealthUI = (GameObject)Instantiate(healthUIPrefab, Vector3.zero, Quaternion.identity);
                        globalHealthUI.transform.SetParent(avatarBars.transform, false);
                        globalHealthView = globalHealthUI.GetComponent <HealthView> ();
                        health.AddGlobalHealthView(globalHealthView);

                        // add manna UI
                        GameObject globalMannaUI = (GameObject)Instantiate(mannaUIPrefab, Vector3.zero, Quaternion.identity);
                        globalMannaUI.transform.SetParent(avatarBars.transform, false);
                        globalMannaView = globalMannaUI.GetComponent <MannaView> ();
                        manna.AddGlobalMannaView(globalMannaView);
                    }

                    avatarScript.Health = health;
                    health.Maximum      = packetAvatarJA.Health.Maximum;
                    health.Current      = packetAvatarJA.Health.Current;

                    avatarScript.Manna = manna;
                    manna.Maximum      = packetAvatarJA.Manna.Maximum;
                    manna.Current      = packetAvatarJA.Manna.Current;
                    CapsuleCollider capsuleCollider = newPlayer.GetComponent <CapsuleCollider> ();
                    capsuleCollider.enabled = true;
                    players.Add(packetAvatarJA.AvatarId, newPlayer);

                    break;

                case Cmd.CmdShootToAvatar:
                    break;

                case Cmd.CmdShootSuccess:
                    PacketShootSuccess packetShootSuccess = PacketShootSuccess.Parser.ParseFrom(receivedPacket.Data);
                    PacketAvatar       senderAvatarOne    = packetShootSuccess.SenderAvatar;
                    PacketPosition     packetPosition     = packetShootSuccess.ShootEndPosition;
                    Vector3            shootEndPosition   = new Vector3(packetPosition.X, packetPosition.Y, packetPosition.Z);

                    ShootSync shootSyncOne = players [senderAvatarOne.AvatarId].GetComponentInChildren <ShootSync> ();
                    shootSyncOne.ShootOn(shootEndPosition);
                    break;

                case Cmd.CmdShootToAvatarSuccess:
                    PacketShootToAvatarSuccess packetShootToAvatarSuccess = PacketShootToAvatarSuccess.Parser.ParseFrom(receivedPacket.Data);
                    int senderShootAvatarId   = packetShootToAvatarSuccess.SenderShootAvatarId;
                    int receiverShootAvatarId = packetShootToAvatarSuccess.ReceiverShootAvatarId;

                    ShootSync shootSyncTwo = players [senderShootAvatarId].GetComponentInChildren <ShootSync> ();
                    shootSyncTwo.ShootOn(players [receiverShootAvatarId].transform.position);
                    break;

                case Cmd.CmdShootFailed:
                    Debug.Log("Shoot failed");
                    break;

                case Cmd.CmdKilledBy:
                    Debug.Log("CmdKilledBy");
                    PacketKilledBy packetKilledBy = PacketKilledBy.Parser.ParseFrom(receivedPacket.Data);
                    int            killerAvatarId = packetKilledBy.KillerAvatarId;
                    int            victimAvatarId = packetKilledBy.VictimAvatarId;

                    if (victimAvatarId == this.avatarId)
                    {
                        gameplayWindow.KilledByCoroutineOn(string.Format("You were killed by {0}", players [killerAvatarId].name));
                    }
                    else
                    {
                        gameplayWindow.KilledByCoroutineOn(string.Format("{0} is killed by {1}", players [victimAvatarId].name, players [killerAvatarId].name));
                    }

                    break;

                case Cmd.CmdRespawn:
                    PacketRespawn packetRespawn = PacketRespawn.Parser.ParseFrom(receivedPacket.Data);
                    handlePacketAvatar(packetRespawn.Avatar, respawn: true);
                    break;

                case Cmd.CmdAvatarLeft:
                    PacketAvatarLeft packetAvatarLeft = PacketAvatarLeft.Parser.ParseFrom(receivedPacket.Data);
                    GameObject       link             = players [packetAvatarLeft.AvatarId];
                    players.Remove(packetAvatarLeft.AvatarId);
                    DestroyImmediate(link);
                    break;

                case Cmd.CmdBanned:
                    break;

                case Cmd.CmdDisconnect:
                    break;

                default:
                    break;
                }         // end switch
            }             // end else
            yield return(new WaitForSeconds(0.01f));
        }
    }