Exemplo n.º 1
0
    /// <summary>
    /// Something spawned on world.
    /// </summary>
    /// <param name="value">Incoming message</param>
    private void OnSpawn(string[] value)
    {
        SNet_Network.Spawn readMessage = SNet_Network.SNetMessage.ReadMessage <SNet_Network.Spawn>(value[0]);
        readMessage.s = ulong.Parse(value[1]);

        if (GameMap.instance == null || !SNet_Network.instance.isHost(readMessage.s))
        {
            return;
        }

        SNet_Identity go = null;

        if (SNet_Identity.find(readMessage.i, out go))
        {
            // Already have that identity.
            return;
        }

        Transform prefab = ResourcesLoader.prefabs.Find(x => x.name == readMessage.b);

        if (prefab == null)
        {
            Debug.Log("Null spawn prefab: " + readMessage.b);
            return;
        }

        string pName = prefab.name;

        prefab      = Instantiate(prefab, readMessage.p, (readMessage.r != Quaternion.identity) ? readMessage.r : prefab.rotation);
        prefab.name = pName + " (Networked)";
        if (readMessage.c != Vector3.zero)
        {
            prefab.localScale = readMessage.c;
        }

        Destroy(prefab.GetComponent <Snet_SceneObject_AutoSpawn>());

        SNet_Identity identity = prefab.GetComponent <SNet_Identity>();

        if (identity != null)
        {
            identity.Set(readMessage.i, readMessage.b);

            Rigidbody rBody = identity.GetComponent <Rigidbody>();
            if (rBody != null)
            {
                // Default force
                rBody.AddForce(identity.transform.forward * readMessage.f);
            }
        }
    }
Exemplo n.º 2
0
    void Start()
    {
        list.Add(this);

        cameraHolder = transform.Find("CameraHolder"); // standart camera position
        if (cameraHolder != null)
        {
            defaultCamPosition = cameraHolder.localPosition;
        }
        focusHolder = transform.Find("FocusHolder"); // camera position when right mouse aimed

        aim       = new A();
        identity  = GetComponent <SNet_Identity>();
        health    = new Health(identity.identity);
        inventory = GetComponent <PlayerInventory>();
        ik        = GetComponent <IKFixer>();

        // Add default starting item of the map.
        if (!string.IsNullOrEmpty(GameMap.instance.startingItem))
        {
            inventory.AddItem(ResourcesLoader.prefabs.Find(x => x.name == GameMap.instance.startingItem).GetComponent <Item>().item);
        }
        //

        aimBone = identity.animator.animator.GetBoneTransform(HumanBodyBones.Spine);

        if (isLocalPlayer)
        {
            /*
             * Local players & host controlled NPCs must sync themselves
             * */
            MonoBehaviourExtensions.Invoke(this, GeneralSync, 5f);

            // Hide the player spawner panel, because we are already spawned.
            SNet_Manager.instance.panel_Game.transform.Find("playerSpawner").gameObject.SetActive(false);
        }
        else
        {
            // Other players UI
            UI_PlayerAgent = Instantiate(SNet_Manager.instance.UI_PlayerAgent, aimBone.parent);
            UI_PlayerAgent.localPosition = SNet_Manager.instance.UI_PlayerAgent.localPosition;
            UI_PlayerAgent.Find("steamAvatar").GetComponent <Facepunch_Avatar>().Fetch(identity.identity);
            UI_PlayerAgent.Find("steamName").GetComponent <Text>().text = Client.Instance.Friends.GetName(identity.identity);
        }

        /*
         * THIS IS A NEW PLAYER, UPDATE THE SPAWNED IDENTITIES FOR THE NEW PLAYER
         * */

        if (SNet_Network.instance.isHost())
        { // Host will update the spawneds
            Debug.Log("New player spawned, SpawnAll()");
            int sCount = SNet_Identity.list.Count;
            for (int i = 0; i < sCount; i++)
            {
                if (!SNet_Identity.list[i].controller)
                { // Refresh only non-player objects
                    SNet_Network.Spawn sp = new SNet_Network.Spawn
                                                (SNet_Identity.list[i].prefab,
                                                SNet_Identity.list[i].transform.position,
                                                SNet_Identity.list[i].transform.rotation,
                                                SNet_Identity.list[i].transform.localScale,
                                                0, true);

                    sp.i = SNet_Identity.list[i].identity;
                    SNet_Network.instance.Send_Message(sp, identity.identity);

                    /*
                     * UPDATE SYNCERS (Transforms, rigidbodies), But DON'T MAKE ANGRY STEAM SERVERS, SO USE RANDOM VALUES
                     * */
                    if (SNet_Identity.list[i].tform != null)
                    {
                        SNet_Identity.list[i].tform.ReUpdate();
                    }

                    if (SNet_Identity.list[i].rbody != null)
                    {
                        SNet_Identity.list[i].rbody.ReUpdate();
                    }

                    if (SNet_Identity.list[i].vehicle != null)
                    {
                        MonoBehaviourExtensions.Invoke(SNet_Identity.list[i].vehicle, SNet_Identity.list[i].vehicle.VehicleUpdate, 3f);
                    }
                }
            }
        }
    }