Change turn Tic Tac Toe PUN

Started by
0 comments, last by UserDev 3 years, 10 months ago

Hi all,

I am developing Tic Tac Toe Game and I am having a problem. Whenever I click on the Tile, it changes to oSprite on both Clients, that ok. At the beginning the MasterClient's turn and, the problem is that after I click it, it changes the turn for the client and quickly changes the turn for the MasterCliente. In other words, only turn work in MasterCliente.

Can someone help me?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;

public class PlayerController : MonoBehaviourPun
{
    public Player photonPlayer;                 // Photon.Realtime.Player class

    public static PlayerController me;          // local player
    public static PlayerController enemy;       // non-local enemy player

    public bool canPlay;

    private RaycastHit2D hit;

    // called when the game begins
    [PunRPC]
    void Initialize (Player player)
    {
        photonPlayer = player;

        // if this is our local player, spawn the units
        if(player.IsLocal)
        {
            me = this;
        }
        else
            enemy = this;

        // set the player text
        GameUI.instance.SetPlayerText(this);
    }

    void Update ()
    {
        // only the local player can control this player
        if(!photonView.IsMine)
            return;

        if (Input.GetMouseButton(0))
        {
            photonView.RPC("SendMousePositionOnline", RpcTarget.AllBuffered, Camera.main.ScreenToWorldPoint(Input.mousePosition));
        }

    }

    [PunRPC]
    void SendMousePositionOnline(Vector3 worldPosition)
    {
        hit = Physics2D.Raycast((Vector2)worldPosition, Vector2.zero);
        MarkTiles();
    }

    [PunRPC]
    public void MarkTiles()
    {
        if (hit.collider != null && GameManager.instance.curPlayer == GameManager.instance.leftPlayer && canPlay)
        {
            hit.collider.gameObject.GetComponent<Tile>().clicked = true;
            hit.collider.enabled = false;
            hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite = GameManager.instance.oSprite;
            me.EndTurn();
        }
        else if (hit.collider != null && GameManager.instance.curPlayer == GameManager.instance.rightPlayer && canPlay)
        {
            hit.collider.gameObject.GetComponent<Tile>().clicked = true;
            hit.collider.enabled = false;
            hit.collider.gameObject.GetComponent<SpriteRenderer>().sprite = GameManager.instance.xSprite;
            //me.EndTurn();
        }
    }

    // called when our turn ends
    public void EndTurn ()
    {
        // start the next turn
        GameManager.instance.photonView.RPC("SetNextTurn", RpcTarget.All);
        canPlay = false;
    }

    // called when our turn has begun
    public void BeginTurn ()
    {
        canPlay = true;
    }
}

This topic is closed to new replies.

Advertisement