// Start is called before the first frame update
 void Start()
 {
     GameType = GameStatusScript.getInstance().GameType;
     if (GameType == GameType.SinglePlayer)
     {
         Destroy(this);
     }
     PhotonView = GetComponent <PhotonView>();
     if (GameSetupScript.GetInstance() != null)
     {
         Striger = GameSetupScript.GetInstance().Striger;
     }
 }
    public void ToggleTurn()
    {
        var turn = Turn;

        if (AllScored())
        {
            /// Match result throw here...
            //SendMatchResultMsg resultMsg = new SendMatchResultMsg();
            var Winner = ScoreOne > ScoreTwo ? WinCase.Player1 : WinCase.Player2;
            if (ScoreOne == ScoreTwo)
            {
                Winner = WinCase.Draw;
            }
            var matchResult = new MatchResult();
            matchResult.Winner   = Winner;
            matchResult.ScoreOne = ScoreOne;
            matchResult.ScoreTwo = ScoreTwo;
            //Call endgme local here
            //PhotonView.RPC("EndGameRPC", RpcTarget.All, matchResult);
            EndGameRPC(matchResult);
            return;
        }
        if (QueenScored)
        {
            QueenScoredTurnCount += 1;
            if (QueenScoredTurnCount == 2)
            {
                QueenScored          = false;
                QueenScoredTurnCount = 0;
                if (!CurrentPlayerScored)
                {
                    if (turn == PlayerType.Player1)
                    {
                        ScoreOne -= 50;
                        GameSetupScript.GetInstance().respawnQueen();
                        // Call update score here
                        //PhotonView.RPC("UpdateScoreRPC", RpcTarget.All, ScoreOne, ScoreTwo);
                        UpdateScoreRPC(ScoreOne, ScoreTwo);
                    }
                }
            }
        }
        switch (turn)
        {
        case PlayerType.Player1:
            turn = CurrentPlayerScored ? PlayerType.Player1 : PlayerType.Player2;

            break;

        case PlayerType.Player2:
            turn = CurrentPlayerScored ? PlayerType.Player2 : PlayerType.Player1;
            break;

        default:
            break;
        }
        if (CurrentPlayerScored)
        {
            CurrentPlayerScored = false;
        }
        if (Striger != null)
        {
            // Set Striger here
            Striger.GetComponent <StrigerControllerSingle>().setStriger(turn);
        }

        //Upodate turn here
        //PhotonView.RPC("updateTurnRPC", RpcTarget.All, turn);
        updateTurnRPC(turn);
        notifyRPC(turn == PlayerType.Player1 ? "Turn: Player 1" : "Turn: Player 2");
        if (Turn == PlayerType.Player2)
        {
            // Player Computer turn here
            StartCoroutine(Striger.GetComponent <StrigerControllerSingle>().ComputerMovePossibleShot());
        }
    }
Esempio n. 3
0
    // Update is called once per frame
    void Update()
    {
        if (!photonView.IsMine)
        {
            GetComponent <Rigidbody>().position = Vector3.MoveTowards(GetComponent <Rigidbody>().position, NetworkPosition, Time.fixedDeltaTime);
            GetComponent <Rigidbody>().rotation = Quaternion.RotateTowards(GetComponent <Rigidbody>().rotation, NetworkRotation, Time.fixedDeltaTime * 100.0f);

            /* //Lag compensation
             * double timeToReachGoal = LagCurrentPacketTime - LagLastPacketTime;
             * LagCurrentTime += Time.deltaTime;
             *
             * //Update remote player
             * transform.position = Vector3.Lerp(PositionAtLastPacket, LagLatestPos, (float)(LagCurrentTime / timeToReachGoal));
             * transform.rotation = Quaternion.Lerp(RotationAtLastPacket, LagLatestRot, (float)(LagCurrentTime / timeToReachGoal));*/
        }
        if (GameStatusScript.getInstance() != null)
        {
            Turn = GameStatusScript.getInstance().Turn;
            if (!PlayersSet && GameStatusScript.getInstance().MatchStatus == MatchStatus.INGAME)
            {
                PlayerOne  = PhotonNetwork.PlayerList[0];
                PlayerTwo  = PhotonNetwork.PlayerList[1];
                PlayersSet = true;
            }
        }

        /*  if (!Input.GetMouseButtonDown(0))
         * {
         *    return;
         * }*/
        if (Turn == PlayerType.Player1)
        {
            if (PlayerOne.IsLocal)
            {
                if (GetComponent <PhotonView>().Owner != PlayerOne)
                {
                    GetComponent <PhotonView>().RequestOwnership();
                }
                if (StrigerMoving)
                {
                    StrigerMoveTime += Time.deltaTime;
                    if (StrigerMoveTime > 7)
                    {
                        PhotonView.RPC("UnfreezeAllYRPC", RpcTarget.All);
                        if (CoinController.GetInstance().AllSleeping() && GetComponent <Rigidbody>().IsSleeping())
                        {
                            StrigerMoveTime = 0;
                            StrigerMoving   = false;
                            // Toggle turn here.
                            if (GameSetupScript.GetInstance() != null)
                            {
                                //GameSetupScript.GetInstance().ToggleTurn();
                                PhotonNetwork.RaiseEvent(RaiseEventCodes.ToggleTurnEvent, null, new RaiseEventOptions()
                                {
                                    Receivers = ReceiverGroup.MasterClient
                                }, SendOptions.SendReliable);
                                GameStatusScript.getInstance().StrigerMoving = false;
                            }
                        }
                    }
                }
            }
            if (Dragging && PlayerOne.IsLocal)
            {
                Vector3 rayPoint = getRayPoint();
                if ((rayPoint.x >= -7.5 && rayPoint.x <= 7.5) && (rayPoint.z <= -10 && rayPoint.z >= -14))
                {
                    var point = rayPoint;
                    point.y            = transform.position.y;
                    point.z            = transform.position.z;
                    transform.position = point;
                    StrigerThrow       = false;

                    // Toggle Ray cast off here
                    StrigerRayCast.toggleLine(false);
                }
                if (rayPoint.z < -13)
                {
                    StrigerThrow = true;
                    // Toggle Ray cast on here
                    StrigerRayCast.toggleLine(false);
                    DragAngle  = AngleBetween(new Vector2(transform.position.x, transform.position.z), new Vector2(rayPoint.x, rayPoint.z));
                    DragLength = Vector3.Distance(transform.position, rayPoint);
                    ThrowAngle = 90 + DragAngle;
                    var x = 100 * Math.Cos(ThrowAngle * Mathf.Deg2Rad);
                    var z = 100 * Math.Sin(ThrowAngle * Mathf.Deg2Rad);
                    ThrowDirection = new Vector3((float)x, 0, (float)z);
                    Debug.DrawRay(transform.position, ThrowDirection, Color.green, 0.2f, false);
                    //  cast line
                    StrigerRayCast.castLine(transform.position, ThrowDirection);
                }
            }
        }
        else if (Turn == PlayerType.Player2)
        {
            if (PlayerTwo.IsLocal)
            {
                if (GetComponent <PhotonView>().Owner != PlayerTwo)
                {
                    GetComponent <PhotonView>().RequestOwnership();
                }
                if (StrigerMoving)
                {
                    StrigerMoveTime += Time.deltaTime;
                    if (StrigerMoveTime > 7)
                    {
                        PhotonView.RPC("UnfreezeAllYRPC", RpcTarget.All);
                        if (CoinController.GetInstance().AllSleeping() && GetComponent <Rigidbody>().IsSleeping())
                        {
                            StrigerMoveTime = 0;
                            StrigerMoving   = false;
                            GameStatusScript.getInstance().StrigerMoving = false;
                            // Toggle turn here.
                            if (GameSetupScript.GetInstance() != null)
                            {
                                PhotonNetwork.RaiseEvent(RaiseEventCodes.ToggleTurnEvent, null, new RaiseEventOptions()
                                {
                                    Receivers = ReceiverGroup.MasterClient
                                }, SendOptions.SendReliable);
                            }
                        }
                    }
                }
            }
            if (Dragging && PlayerTwo.IsLocal)
            {
                Vector3 rayPoint = getRayPoint();
                if ((rayPoint.x >= -7.5 && rayPoint.x <= 7.5) && (rayPoint.z >= 10 && rayPoint.z <= 12))
                {
                    var point = rayPoint;
                    point.y            = transform.position.y;
                    point.z            = transform.position.z;
                    transform.position = point;
                    StrigerThrow       = false;

                    // Toggle Ray cast off here
                    StrigerRayCast.toggleLine(false);
                }
                if (rayPoint.z > 12)
                {
                    StrigerThrow = true;
                    // Toggle Ray cast onn here
                    StrigerRayCast.toggleLine(true);
                    DragAngle  = AngleBetween(new Vector2(transform.position.x, transform.position.z), new Vector2(rayPoint.x, rayPoint.z));
                    DragLength = Vector3.Distance(transform.position, rayPoint);
                    ThrowAngle = DragAngle - 90;
                    var x = 100 * Math.Cos(ThrowAngle * Mathf.Deg2Rad);
                    var z = 100 * Math.Sin(ThrowAngle * Mathf.Deg2Rad);
                    ThrowDirection = new Vector3((float)x, 0, (float)z);
                    Debug.DrawRay(transform.position, ThrowDirection, Color.green, 0.2f, false);
                    //  cast line
                    StrigerRayCast.castLine(transform.position, ThrowDirection);
                }
            }
        }
    }
    // Update is called once per frame
    void Update()
    {
        if (PlayerOne == null && PlayerTwo == null)
        {
            if (PhotonNetwork.PlayerList.Length == 2)
            {
                PlayerOne = PhotonNetwork.PlayerList[0];
                PlayerTwo = PhotonNetwork.PlayerList[1];
            }
        }
        if (GameSetupScript.GetInstance() != null && Striger == null)
        {
            Striger = GameSetupScript.GetInstance().Striger;
        }/**/
        if (GameStatusScript.getInstance() != null)
        {
            Turn = GameStatusScript.getInstance().Turn;
            //CurrentPlayerScored = ScoreController.CurrentPlayerScored;
        }
    }

    private void OnEnable()
    {
        PhotonNetwork.NetworkingClient.EventReceived += NetworkingClient_EventReceived;
    }

    private void OnDisable()
    {
        PhotonNetwork.NetworkingClient.EventReceived -= NetworkingClient_EventReceived;
    }

    [PunRPC]
    void updateTurnRPC(PlayerType turn)
    {
        Turn = turn;
        GameStatusScript.getInstance().Turn = turn;
    }

    private void NetworkingClient_EventReceived(EventData obj)
    {
        if (obj.Code == RaiseEventCodes.ToggleTurnEvent)
        {
            Debug.Log("ToggleTurn Event Raised");
            var turn = Turn;
            if (GameSetupScript.GetInstance().AllScored())
            {
                /// Match result throw here...
                //SendMatchResultMsg resultMsg = new SendMatchResultMsg();
                var Winner = ScoreOne > ScoreTwo ? WinCase.Player1 : WinCase.Player2;
                if (ScoreOne == ScoreTwo)
                {
                    Winner = WinCase.Draw;
                }
                var matchResult = new MatchResult();
                matchResult.Winner   = Winner;
                matchResult.ScoreOne = ScoreOne;
                matchResult.ScoreTwo = ScoreTwo;
                PhotonView.RPC("EndGameRPC", RpcTarget.All, matchResult);
                return;
            }
            if (QueenScored)
            {
                QueenScoredTurnCount += 1;
                if (QueenScoredTurnCount == 2)
                {
                    QueenScored          = false;
                    QueenScoredTurnCount = 0;
                    if (!CurrentPlayerScored)
                    {
                        if (turn == PlayerType.Player1)
                        {
                            ScoreOne -= 50;
                            GameSetupScript.GetInstance().respawnQueen();
                            PhotonView.RPC("UpdateScoreRPC", RpcTarget.All, ScoreOne, ScoreTwo);
                        }
                    }
                }
            }
            switch (turn)
            {
            case PlayerType.Player1:
                turn = CurrentPlayerScored ? PlayerType.Player1 : PlayerType.Player2;

                break;

            case PlayerType.Player2:
                turn = CurrentPlayerScored ? PlayerType.Player2 : PlayerType.Player1;

                /*if (GameType == GameType.MultiPlayer)
                 * {
                 *  AssignStriger(Turn);
                 * }
                 * turnMsg = new SetTurnMsg();
                 * turnMsg.Turn = Turn;
                 * NetworkServer.SendToAll<SetTurnMsg>(turnMsg, SetTurnMsgId);*/
                break;