コード例 #1
0
    protected override void SyncFunction()
    {
        base.SyncFunction();

        if (!base.IsIDAssigned)
        {
            throw new UnityException("ID not assigned.");
        }

        if (Network.isServer)
        {
            Vector3 pos         = this.transform.position;
            Vector3 orientation = this.transform.eulerAngles;

            if (pos != this.previousPos || orientation != this.previousOrientation)
            {
                ObjectRPC.ObjectPosition(base.Owner, base.GlobalID, pos, orientation);
                this.previousPos         = pos;
                this.previousOrientation = orientation;
            }
        }

        if (base.IsOwner)
        {
            Vector3 translation = this.objectTransformer.Translation;
            Vector3 rotation    = this.objectTransformer.Rotation;

            if (translation != this.previousTranslation || rotation != this.previousRotation)
            {
                if (Network.peerType == NetworkPeerType.Server)
                {
                    ObjectRPC.ObjectVelocityServer(base.Owner, base.GlobalID, translation, rotation);
                }
                else if (Network.peerType == NetworkPeerType.Client)
                {
                    ObjectRPC.ObjectVelocityClient(base.Owner, base.GlobalID, translation, rotation);
                }

                this.previousTranslation = translation;
                this.previousRotation    = rotation;
            }
        }
    }
コード例 #2
0
    public override void SyncNewPlayer(Player newPlayer)
    {
        base.SyncNewPlayer(newPlayer);

        // Sync the objects that are not associated with any player.
        ObjectTable serverTable = base.ObjectTables.GetPlayerTable(base.NetworkControl.ThisPlayer);
        ICollection <GameObject> serverObjects = serverTable.GetAllObjects();

        Debug.Log("Number of server objects: " + serverObjects.Count);

        foreach (GameObject obj in serverObjects)
        {
            ObjectSync objSync = obj.GetComponent <ObjectSync>();

            switch (objSync.Type)
            {
            case ObjectSyncType.Mothership:
                ObjectRPC.CreateMothership(newPlayer.NetworkPlayerInfo, objSync.Owner, objSync.GlobalID, obj.layer);

                // Temporary mothership positions.
                if (obj.layer == (int)Layers.Team1Mothership)
                {
                    ObjectRPC.ObjectPosition(objSync.Owner, objSync.GlobalID, new Vector3(1000, 0, 0), Vector3.zero);
                }
                else
                {
                    ObjectRPC.ObjectPosition(objSync.Owner, objSync.GlobalID, new Vector3(-1000, 0, 0), Vector3.zero);
                }

                break;

            case ObjectSyncType.Drone:
                ObjectRPC.CreateDrone(newPlayer.NetworkPlayerInfo, objSync.Owner, objSync.GlobalID, obj.transform.position, obj.layer);
                break;

            case ObjectSyncType.Asteroid:
                ObjectRPC.CreateAsteroid(newPlayer.NetworkPlayerInfo, objSync.Owner, objSync.GlobalID, obj.transform.position, obj.transform.localScale, obj.name);
                break;
            }
        }

        // Sync the objects that belong to other players.
        foreach (Player p in base.Players.Values)
        {
            // Skip all objects for the server player (already synced) and the new player (empty).
            if (!(p.ID == base.NetworkControl.LocalViewID || p.ID == newPlayer.ID))
            {
                // Sync player ships and player spawnpoints
                PlayerObjects playerObjects = base.ObjectTables.PlayerObjects[p];

                GameObject spawnPoint = base.GetObject(p, playerObjects.PlayerSpawnPointID);

                ObjectRPC.CreatePlayerSpawnpoint(
                    newPlayer.NetworkPlayerInfo
                    , p
                    , playerObjects.PlayerSpawnPointID
                    , spawnPoint.transform.position);

                GameObject playerShip = base.GetObject(p, playerObjects.PlayerShipID);

                PlayerShipRPC.CreatePlayerShip(newPlayer.NetworkPlayerInfo, p, playerObjects.PlayerShipID);
                //ObjectRPC.SetObjectLayer(newPlayer.NetworkPlayerInfo, p, playerObjects.PlayerShipID, (Layers)playerShip.layer);
            }
        }

        // Create the objects for the new player.
        int spawnPointID = base.GUIDGenerator.GenerateID();
        int playerShipID = base.GUIDGenerator.GenerateID();

        GameObject mothership    = base.GetMothership(newPlayer.Team);
        Vector3    spawnPointPos = mothership.transform.position + new Vector3(0, 500, 0);

        // The order in which the following RPCs are sent is critical!
        ObjectRPC.CreatePlayerSpawnpoint(newPlayer, spawnPointID, spawnPointPos);
        PlayerShipRPC.CreatePlayerShip(newPlayer, playerShipID);
        PlayerShipRPC.SpawnPlayerShip(newPlayer, spawnPointID, playerShipID);
    }