示例#1
0
    // LOTSA GAME LOGIC
    private void OnUserVariableUpdate(BaseEvent e)
    {
        List <string> changedVars = (List <string>)e.Params["changedVars"];

        SFSUser user = (SFSUser)e.Params["user"];

        if (user == sfs.MySelf)
        {
            return;
        }

        if (!remotePlayers.ContainsKey(user))
        {
            // New client just started transmitting - lets create remote player
            Vector3 pos = new Vector3(0, 0);
            if (user.ContainsVariable("x") && user.ContainsVariable("y"))
            {
                pos.x = (float)user.GetVariable("x").GetDoubleValue();
                pos.y = (float)user.GetVariable("y").GetDoubleValue();
            }

            SpawnRemotePlayer(user, pos);
        }

        // Check if the remote user changed position
        if (changedVars.Contains("x") && changedVars.Contains("y"))
        {
            // Move the character to a new position... (holy shit lol)
            remotePlayers[user].GetComponent <SimpleRemoteInterpolation>().SetTransform(
                new Vector3((float)user.GetVariable("x").GetDoubleValue(), (float)user.GetVariable("y").GetDoubleValue()), true);
        }
    }
    // When user variable is updated on any client, then this callback is being received
    // This is where most of the magic happens
    private void OnUserVariableUpdate(BaseEvent e)
    {
        SFSUser user = e.Params.GetSFSUser();

        if (user == smartfox.MySelf)
        {
            return;
        }
        if (!remotePlayers.ContainsKey(user))
        {
            return;
        }

        ArrayList changedVars = e.Params.GetChangedVars();

        // Check if the remote user changed his position or rotation
        if (changedVars.Contains("x") || changedVars.Contains("y") || changedVars.Contains("z") || changedVars.Contains("rot"))
        {
            // Move the character to a new position...
            Vector3    pos  = new Vector3((float)user.GetVariable("x").GetDoubleValue(), 1, (float)user.GetVariable("z").GetDoubleValue());
            Quaternion quat = Quaternion.Euler(0, (float)user.GetVariable("rot").GetDoubleValue(), 0);
            SimpleRemoteInterpolation interp = remotePlayers[user].GetComponent <SimpleRemoteInterpolation>();

            interp.SetTransform(pos, quat, true);
        }

        // Remote client got new name?
        if (changedVars.Contains("name"))
        {
            remotePlayers[user].GetComponentInChildren <TextMesh>().text = user.Name;
        }
    }
示例#3
0
    private void OnUserVariablesUpdate(BaseEvent evt)
    {
        ArrayList changedVars = (ArrayList)evt.Params["changedVars"];

        SFSUser user = (SFSUser)evt.Params["user"];

        if (!users.ContainsKey(user.Id))
        {
            Debug.LogWarning("Got variable update for an user that is not in the users list.");
            return;
        }

        if (changedVars.Contains("pos"))
        {
            GameObject playerGo = user.Properties["GameObject"] as GameObject;

            Vector3   newPosition = new Vector3();
            ISFSArray pos         = user.GetVariable("pos").GetSFSArrayValue();
            newPosition.x = pos.GetFloat(0);
            newPosition.y = pos.GetFloat(1) - 1.0f;
            newPosition.z = pos.GetFloat(2);

            var pi = playerGo.GetComponent <PlayerInterpolation>();
            pi.latestPosition = newPosition;
        }

        if (changedVars.Contains("ori"))
        {
            GameObject playerGo = user.Properties["GameObject"] as GameObject;

            Quaternion newOrientation = new Quaternion();
            ISFSArray  ori            = user.GetVariable("ori").GetSFSArrayValue();
            newOrientation.x = ori.GetFloat(0);
            newOrientation.y = ori.GetFloat(1);
            newOrientation.z = ori.GetFloat(2);
            newOrientation.w = ori.GetFloat(3);

            var pi = playerGo.GetComponent <PlayerInterpolation>();
            pi.latestOrientation = newOrientation;
        }

        if (changedVars.Contains("turning"))
        {
            GameObject playerGo = user.Properties["GameObject"] as GameObject;

            Boolean turning = user.GetVariable("turning").GetBoolValue();

            var pi = playerGo.GetComponent <PlayerInterpolation>();
            pi.turning = turning;
        }
    }
示例#4
0
    /**
     * When user variable is updated on any client within the AoI, then this event is received.
     * This is where most of the game logic for this example is contained.
     */
    public void OnUserVariableUpdate(BaseEvent evt)
    {
        List <string> changedVars = (List <string>)evt.Params["changedVars"];

        SFSUser user = (SFSUser)evt.Params["user"];

        if (user == sfs.MySelf)
        {
            return;
        }

        changedVars.Contains("x");
        changedVars.Contains("y");
        changedVars.Contains("z");
        changedVars.Contains("rot");

        // Check if the remote user changed his position or rotation
        if (changedVars.Contains("x") || changedVars.Contains("y") || changedVars.Contains("z") || changedVars.Contains("rot"))
        {
            if (remotePlayers.ContainsKey(user))
            {
                // Move the character to a new position...
                remotePlayers[user].GetComponent <SimpleRemoteInterpolation>().SetTransform(
                    new Vector3((float)user.GetVariable("x").GetDoubleValue(), 1, (float)user.GetVariable("z").GetDoubleValue()),
                    Quaternion.Euler(0, (float)user.GetVariable("rot").GetDoubleValue(), 0),
                    true
                    );
            }
        }

        // Remote client selected new model?
        if (changedVars.Contains("model"))
        {
            SpawnRemotePlayer(user, user.GetVariable("model").GetIntValue(), user.GetVariable("mat").GetIntValue(), remotePlayers[user].transform.position, remotePlayers[user].transform.rotation);
        }

        // Remote client selected new material?
        if (changedVars.Contains("mat"))
        {
            remotePlayers[user].GetComponentInChildren <Renderer>().material = playerMaterials[user.GetVariable("mat").GetIntValue()];
        }
    }
示例#5
0
 public void SetRemotePlayerModel(SFSUser user, int modelIndex)
 {
     if (user == CommunicationManager.MySelf)
     {
         return;
     }
     if (remotePlayers.ContainsKey(user))
     {
         PlayerType pType = PlayerType.NORMAL;
         Player     p;
         if (players.TryGetValue(user.Id, out p))
         {
             pType = p.Type;
         }
         Vector3    pos         = remotePlayers[user].transform.position;
         Quaternion rot         = remotePlayers[user].transform.rotation;
         string     displayName = (user.ContainsVariable("displayName")) ? user.GetVariable("displayName").GetStringValue() : user.Name;
         CreateRemotePlayer(user, modelIndex, pos, rot, user.GetVariable("op").GetStringValue(), user.GetVariable("parseId").GetStringValue(), displayName, user.GetVariable("team").GetIntValue(), pType, user.GetVariable("sit").GetBoolValue());
     }
 }
示例#6
0
    private void UserEnterRoom(SFSUser user)
    {
        ISFSArray pos = user.GetVariable("pos").GetSFSArrayValue();
        ISFSArray ori = user.GetVariable("ori").GetSFSArrayValue();

        GameObject playerGo = Instantiate(playerPrefab,
                                          new Vector3(pos.GetFloat(0), pos.GetFloat(1) - 1.0f, pos.GetFloat(2)),
                                          new Quaternion(ori.GetFloat(0), ori.GetFloat(2), ori.GetFloat(2), ori.GetFloat(3)))
                              as GameObject;

        //playerGo.AddComponent<PlayerInput>();
        playerGo.AddComponent <PlayerInterpolation>();

        playerGo.GetComponent <Animator>().runtimeAnimatorController = animatorController;

        user.Properties.Add("GameObject", playerGo);
        users.Add(user.Id, user);

        //chaserCamera.target = playerGo.transform;
    }
示例#7
0
    /**
     * When user variable is updated on any client within the AoI, then this event is received.
     * This is where most of the game logic for this example is contained.
     */
    public void OnUserVariableUpdate(BaseEvent evt)
    {
                #if UNITY_WSA && !UNITY_EDITOR
        List <string> changedVars = (List <string>)evt.Params["changedVars"];
                #else
        ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
                #endif

        SFSUser user = (SFSUser)evt.Params["user"];

        if (user == smartFox.MySelf)          //spwanLocalPlayer
        {
            if (localPlayer == null)
            {
                NetworkTransform transform = NetworkTransform.FromUserVariables((float)user.GetVariable("x").GetDoubleValue(), 5.36f, (float)user.GetVariable("z").GetDoubleValue(), 0, (float)user.GetVariable("r").GetDoubleValue(), 0, Convert.ToDouble(user.GetVariable("t").Value));

                SpawnLocalPlayer(Convert.ToInt16(user.GetVariable("g").Value), transform);
            }
            return;
        }

        if (recipients.ContainsKey(user.Id))
        {
            // Check if the remote user changed his position or rotation
            if (changedVars.Contains("x") || changedVars.Contains("z") || changedVars.Contains("r"))
            {
                // Move the character to a new position...
                NetworkTransform         transform = NetworkTransform.FromUserVariables((float)user.GetVariable("x").GetDoubleValue(), 5.36f, (float)user.GetVariable("z").GetDoubleValue(), 0, (float)user.GetVariable("r").GetDoubleValue(), 0, Convert.ToDouble(user.GetVariable("t").Value));
                NetworkTransformReceiver recipent  = recipients [user.Id];
                if (recipent != null)
                {
                    recipent.ReceiveTransform(transform);
                }
            }
            // Remote client selected new model?
            if (changedVars.Contains("model"))
            {
                SpawnRemotePlayer(user, user.GetVariable("model").GetIntValue(), remotePlayers[user.Id].transform.position, remotePlayers[user.Id].transform.localEulerAngles);
            }
        }
    }
示例#8
0
    public void OnUserVariableUpdate(BaseEvent evt)
    {
        //Debug.LogWarning("OnUserVariableUpdate");
        List <string> changedVars = (List <string>)evt.Params["changedVars"];
        SFSUser       user        = (SFSUser)evt.Params["user"];

        if (user == sfs.MySelf)
        {
            return;
        }
        if (!remotePlayers.ContainsKey(user))
        {
            // New client just started transmitting - lets create remote player
            Vector3 pos = new Vector3(0, 1, 0);
            if (user.ContainsVariable("x") && user.ContainsVariable("y") && user.ContainsVariable("z"))
            {
                pos.x = (float)user.GetVariable("x").GetDoubleValue();
                pos.y = (float)user.GetVariable("y").GetDoubleValue();
                pos.z = (float)user.GetVariable("z").GetDoubleValue();
            }
            float rotAngle = 0;
            if (user.ContainsVariable("rot"))
            {
                rotAngle = (float)user.GetVariable("rot").GetDoubleValue();
            }
            int numModel = 0;
            if (user.ContainsVariable("model"))
            {
                numModel = user.GetVariable("model").GetIntValue();
            }
            SpawnRemotePlayer(user, numModel, pos, Quaternion.Euler(0, rotAngle, 0));
        }
        // Check if the remote user changed his position or rotation
        if (changedVars.Contains("x") && changedVars.Contains("y") && changedVars.Contains("z") && changedVars.Contains("rot"))
        {
            // Move the character to a new position...
            remotePlayers[user].GetComponent <SimpleRemoteInterpolation>().SetTransform(
                new Vector3((float)user.GetVariable("x").GetDoubleValue(), (float)user.GetVariable("y").GetDoubleValue(), (float)user.GetVariable("z").GetDoubleValue()),
                Quaternion.Euler(0, (float)user.GetVariable("rot").GetDoubleValue(), 0),
                true);
        }
    }
示例#9
0
    private void OnUserVariableUpdate(BaseEvent evt)
    {
        #if UNITY_WSA && !UNITY_EDITOR
        List <string> changedVars = (List <string>)evt.Params["changedVars"];
        #else
        ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
        #endif
        SFSUser user = (SFSUser)evt.Params["user"];
        if (user == sfs.MySelf)
        {
            return;
        }
        if (!remotePlayers.ContainsKey(user))
        {
            Vector3 pos = new Vector3(0, 1, 0);
            if (user.ContainsVariable("x") && user.ContainsVariable("y"))
            {
                pos.x = (float)user.GetVariable("x").GetDoubleValue();
                pos.y = (float)user.GetVariable("y").GetDoubleValue();
            }
            float rotAngle = 0;
            if (user.ContainsVariable("rot"))
            {
                rotAngle = (float)user.GetVariable("rot").GetDoubleValue();
            }
            SpawnRemotePlayer(user, pos, Quaternion.Euler(0, rotAngle, 0));
        }
        if (changedVars.Contains("x") && changedVars.Contains("y") && changedVars.Contains("rot"))
        {
            remotePlayers[user].GetComponent <SimpleRemoteInterpolation>().SetTransform(
                new Vector3((float)user.GetVariable("x").GetDoubleValue(), (float)user.GetVariable("y").GetDoubleValue(), (float)0),
                Quaternion.Euler(0, 0, (float)user.GetVariable("rot").GetDoubleValue()), true);
        }


        if (changedVars.Contains("shot"))
        {
            remotePlayers[user].GetComponent <shoot>().Fire();
        }
    }
示例#10
0
    public void OnUserVariableUpdate(BaseEvent evt)
    {
        // When user variable is updated on any client, then this callback is being received

        ArrayList changedVars = (ArrayList)evt.Params["changedVars"];
        SFSUser   user        = (SFSUser)evt.Params["user"];

        if (user == CommunicationManager.MySelf || user == null)
        {
            return;
        }

        //GameGUI.Inst.WriteToConsoleLog("OnUserVariableUpdate() " + Time.time);


        if (!remotePlayers.ContainsKey(user))
        {
            SpawnRemotePlayer(user);
        }

        Player remotePlayer = players[user.Id];

        try {
            // Check if the remote user changed his position or rotation
            if (changedVars.Contains("x") || changedVars.Contains("y") || changedVars.Contains("z") || changedVars.Contains("rot"))
            {
                float   rotAngle   = (float)user.GetVariable("rot").GetDoubleValue();
                bool    rotChanged = rotAngle != remotePlayer.gameObject.transform.rotation.eulerAngles.y;
                Vector3 newPos     = new Vector3((float)user.GetVariable("x").GetDoubleValue(), (float)user.GetVariable("y").GetDoubleValue(), (float)user.GetVariable("z").GetDoubleValue());
                remotePlayer.UpdateTransform(newPos, rotAngle);
                if (rotChanged)
                {
                    remotePlayer.playerController.shuffleTime = 0.25f;
                }
            }

            if (changedVars.Contains("scl"))
            {
                remotePlayer.UpdateScale(user.GetVariable("scl"));
            }

            if (changedVars.Contains("modelAnimation"))
            {
                remotePlayer.UpdateAnimState(user.GetVariable("modelAnimation"));
            }

            if (changedVars.Contains("spd"))
            {
                remotePlayer.UpdateSpeed(user.GetVariable("spd"));
            }

            if (changedVars.Contains("gt"))
            {
                remotePlayer.playerController.gazePanTilt.x = (float)user.GetVariable("gt").GetDoubleValue();
                remotePlayer.playerController.gazePanTilt.y = (float)user.GetVariable("gp").GetDoubleValue();
            }

            if (changedVars.Contains("ptype"))
            {
                remotePlayer.UpdateType(user.GetVariable("ptype"));
            }

            if (changedVars.Contains("playerModel"))
            {
                SetRemotePlayerModel(user, user.GetVariable("playerModel").GetIntValue());
                Debug.Log("remote changed model!:" + user.GetVariable("playerModel").GetIntValue());
            }
        }
        catch (KeyNotFoundException e) {
            Debug.LogError("KeyNotFoundException, changed vars dont exist for user, msg from different level? " + e.StackTrace);
        }
    }
示例#11
0
    private void OnUserVariablesUpdate(BaseEvent evt)
    {
        List <string> changedVars = (List <string>)evt.Params["changedVars"];

        SFSUser user = (SFSUser)evt.Params["user"];

        if (user == sfs.MySelf)
        {
            return;
        }

        Vector3 pos   = Posplayer.position;
        int     index = 0;

        if (!remotePlayers.ContainsKey(user))
        {
            if (user.ContainsVariable("x") && user.ContainsVariable("y"))
            {
                pos.x = (float)user.GetVariable("x").GetDoubleValue();
                pos.y = (float)user.GetVariable("y").GetDoubleValue();
                pos.z = (float)user.GetVariable("z").GetDoubleValue();
            }

            if (user.ContainsVariable("index"))
            {
                index = (int)user.GetVariable("index").GetIntValue();
            }



            GameObject remotePlayer = GameObject.Instantiate(Players[index]) as GameObject;
            remotePlayer.AddComponent <SimpleRemoteInterpolation>();
            remotePlayer.GetComponent <SimpleRemoteInterpolation>().SetTransform(pos, Quaternion.identity, false);

            // Color and name


            // Lets track the dude
            remotePlayers.Add(user, remotePlayer);
        }


        if (changedVars.Contains("x") && changedVars.Contains("y") && changedVars.Contains("z"))
        {
            // Move the character to a new position...
            remotePlayers[user].GetComponent <SimpleRemoteInterpolation>().SetTransform(
                new Vector3((float)user.GetVariable("x").GetDoubleValue(), (float)user.GetVariable("y").GetDoubleValue(), (float)user.GetVariable("z").GetDoubleValue()),
                //Quaternion.Euler(0, (float)user.GetVariable("rot").GetDoubleValue(), 0),
                Quaternion.identity,
                true);
        }

        //if (changedVars.Contains("x_Health") && changedVars.Contains("y_Health") && changedVars.Contains("z_Health"))
        //{
        //    remotePlayers[user].transform.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetComponent<SimpleRemoteInterpolation>().SetTransform(
        //        new Vector3((float)user.GetVariable("x_Health").GetDoubleValue(), (float)user.GetVariable("y_Health").GetDoubleValue(),
        //        (float)user.GetVariable("z_Health").GetDoubleValue()), Quaternion.identity, true);
        //}
        //if(user.ContainsVariable("index"))
        //{
        //    Debug.Log(" Gia tri Index:" + user.GetVariable("index").GetIntValue());
        //}
    }
示例#12
0
    private void userVarUpdateHandler(BaseEvent evt)
    {
        List <string> changedVars = (List <string>)evt.Params["changedVars"];

        SFSUser    user = (SFSUser)evt.Params["user"];
        GameObject remotePlayer;

        // Exit from funciton if user is the local player himself
        if (user == sfs.MySelf)
        {
            return;
        }

        // Get remote player form list
        if (gm.remotePlayers.ContainsKey(user))
        {
            remotePlayer = gm.remotePlayers[user];
        }
        else // Spawn remote player if it's not ins the list
        {
            gm.SpawnRemotePlayer(user);
            remotePlayer = gm.remotePlayers[user];
        }

        //gm.trace("(" + user.Name + ") Changed its vars!");

        // Change remote player's position and velocity on change
        if (changedVars.Contains("px") || changedVars.Contains("py"))
        {
            remotePlayer.transform.position = new Vector3((float)user.GetVariable("px").GetDoubleValue(), (float)user.GetVariable("py").GetDoubleValue(), 0);
        }
        if (changedVars.Contains("vx"))
        {
            remotePlayer.GetComponent <Rigidbody2D>().velocity = new Vector3((float)user.GetVariable("vx").GetDoubleValue(), (float)user.GetVariable("vy").GetDoubleValue(), 0);
        }
        if (changedVars.Contains("vely"))
        {
            remotePlayer.GetComponent <Rigidbody2D>().velocity = new Vector3((float)user.GetVariable("vx").GetDoubleValue(), (float)user.GetVariable("vy").GetDoubleValue(), 0);
        }

        // Change remote player's animation based on velocity
        if (remotePlayer.GetComponent <Rigidbody2D>().velocity.x == 0 && remotePlayer.GetComponent <Rigidbody2D>().velocity.y == 0)
        {
            remotePlayer.GetComponent <Animator>().SetBool("Running", false);
        }
        else
        {
            remotePlayer.GetComponent <Animator>().SetBool("Running", true);
        }


        // Redirecting remote player direction
        if (remotePlayer.GetComponent <Rigidbody2D>().velocity.x != 0)
        {
            remotePlayerDirection = (remotePlayer.GetComponent <Rigidbody2D>().velocity.x < 0) ? false : true;
        }
        Vector3 trueDirection = new Vector3(remotePlayerDirection ? 1 : -1, 1, 1);

        remotePlayer.GetComponent <Transform>().localScale = trueDirection;
        remotePlayer.transform.FindChild("name").GetComponent <Transform>().localScale = trueDirection;
        //if(user.ContainsVariable("vx"))
        //    if (user.GetVariable("vx").GetDoubleValue() != 0)
        //        remotePlayerDirection = (user.GetVariable("vx").GetDoubleValue() < 0) ? false : true;
    }