如何断言生成是“填充”?

时间:2015-05-26 20:49:21

标签: unity3d network-programming photon

我有两个产卵点,玩家会在连接时出现。

我需要的是,当其中一个玩家连接其中一个位置时,任何其他玩家将总是在另一个位置产生。

以下是一些视觉效果:https://goo.gl/Y0ohZC

提前致谢, IC

1 个答案:

答案 0 :(得分:1)

您可以将这两个可能的生成点添加为空游戏对象。 然后我制作一个布尔数组并将其状态设置为true或false,具体取决于该点是否被占用。斑点不直接存储在此数组中,因此您应该创建另一个数组。 在C#中,这看起来大概如下:

    public GameObject[] Spots = new GameObject[2]; //Drag the spots in here (In the editor)
bool[] OccupiedSpawnSpots = new bool[2];
int mySpotNumber = -1;

void Start() {
    PhotonNetwork.ConnectUsingSettings ("v1.0.0");
}

void OnGUI() {
    GUILayout.Label (PhotonNetwork.connectionStateDetailed.ToString ());
}

void OnJoinedLobby() 
{
    Debug.Log ("Joined the lobby");
    PhotonNetwork.JoinRandomRoom ();
}

void OnPhotonRandomJoinFailed() 
{
    Debug.Log ("No room found. Creating a new one...");
    PhotonNetwork.CreateRoom (null);
}

void OnPhotonPlayerConnected(PhotonPlayer player)
{
    //If we are the MasterClient, send the list to the connected player
    if (PhotonNetwork.isMasterClient)
        GetComponent<PhotonView>().RPC("RPC_SendList", player, OccupiedSpawnSpots);
}

void OnApplicationQuit()
{
    //If I am outside and others want to connect, my spot shouldn't be still set as occupied:
    //I mean if the application quits I'm of course going to be disconnected. 
    //You have to do this in every possibility of getting disconnected or leaving the room
    OccupiedSpawnSpots[mySpotNumber] = false;
    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_SendList(bool[] ListOfMasterClient)
{
    OccupiedSpawnSpots = ListOfMasterClient;

    //Get the free one
    if (OccupiedSpawnSpots[0] == false)
    {
        //Spawn your player at 0
        SpawnMyPlayer(0);
        //Set it to occupied
        OccupiedSpawnSpots[0] = true;
    } 
    else //so the other must be free
    {
        //Spawn your player at 1
        SpawnMyPlayer(1);
        //Set it to occupied
        OccupiedSpawnSpots[1] = true;
    }

    //Send the changed List to the others
    GetComponent<PhotonView>().RPC("RPC_UpdateList", PhotonTargets.All, OccupiedSpawnSpots);
}

[RPC]
void RPC_UpdateList(bool[] RecentList)
{
    OccupiedSpawnSpots = RecentList;
}

void SpawnMyPlayer(int SpotNumber) {
    // Check if spawnspots are set OK
    if (Spots == null) {
        Debug.LogError ("SpawnSpots aren't assigned!");
        return;
    }

    mySpotNumber = SpotNumber;

    // The player object for the network
    GameObject myPlayerGO = (GameObject)PhotonNetwork.Instantiate ("PlayerController", 
                                                                   Spots[SpotNumber].transform.position,
                                                                   Spots[SpotNumber].transform.rotation, 0);

    // Enable a disabled script for *this player only, or all would have the same camera, movement, etc
    //((MonoBehaviour)myPlayerGO.GetComponent("FPSInputController")).enabled = true;

    // Set a camera just for this player
    //myPlayerGO.transform.FindChild ("Main Camera").gameObject.SetActive (true);

    //standbyCamera.SetActive(false);
}

注意:如果你有两个以上的玩家,那就会变得更加困难。这段代码没有经过优化,所以也许你会找到一个更好的代码,但它应该给你正确的想法。

请问是否有任何事情不清楚......