Exemple #1
0
        private void Logout(byte[] msg)
        {
            //LogoutMsg logout = JsonUtility.FromJson<LogoutMsg>(msg);
            LogoutMsg logout = new LogoutMsg(msg);

            removePlayerQ.Enqueue(logout.from);
        }
Exemple #2
0
        private void Update()
        {
            while (instantiatePlayerQ.Count > 0)
            {
                //instatiate net player object in the game and store object and client info in users dictionary
                PlayerData player = new PlayerData(instantiatePlayerQ.Dequeue());
                player.obj              = Instantiate(playerPrefab, spawnpoint.position, spawnpoint.rotation, parent);
                player.playerControl    = player.obj.GetComponent <Player>();
                player.playerControl.id = player.clientData.id;
                users.Add(player.clientData.id, player);
                player.obj.name = "NET_PLAYER_" + player.clientData.id;
                AddPlayerMsg addPlayerMsg = new AddPlayerMsg(player.clientData.id, (int)player.GetPlayerType(), (int)player.GetPersonType());
                //addPlayerMsg = new AddPlayerMsg(addPlayerMsg.GetBytes());
                //Debug.Log(string.Format("Instantiate player: {0}", addPlayerMsg.playerType));



                KeyValuePair <int, PlayerData>[] players = users.ToArray();
                ////Debug.Log(String.Format("PLAYERS LENGTH: {0}: ", players.Length));
                foreach (KeyValuePair <int, PlayerData> playerData in players)
                {
                    if (playerData.Value.clientData.id == player.clientData.id)
                    {
                        continue;
                    }
                    ////Debug.Log("ADD USER SENT");
                    addPlayerMsg.from = player.clientData.id;
                    addPlayerMsg.to   = playerData.Value.clientData.id;
                    Send(addPlayerMsg.GetBytes(), addPlayerMsg.to);

                    addPlayerMsg.from       = playerData.Value.clientData.id;
                    addPlayerMsg.to         = player.clientData.id;
                    addPlayerMsg.personType = playerData.Value.clientData.personType;//test
                    Send(addPlayerMsg.GetBytes(), addPlayerMsg.to);
                }
                foreach (StructureChangeMsg structMsg in structures.Values)
                {
                    structMsg.to = player.clientData.id;
                    // //Debug.Log(String.Format("STRUCT SENT TO: {0}", structMsg.to));
                    Send(structMsg.GetBytes(), structMsg.to);
                }
                foreach (Snapshot snap in netobjects.Values)
                {
                    //Debug.Log(snap)

                    NetObjectMsg netObjMsg = snap.ThisObj;
                    Debug.Log(netObjMsg.objID);
                    //Debug.Log(netObjMsg.to);
                    //Debug.Log(player.clientData.id);
                    //netObjMsg.to = player.clientData.id;
                    //Debug.Log(String.Format("STRUCT SENT TO: {0}", netObjMsg.to));
                    //Send(netObjMsg.GetBytes(), netObjMsg.to);
                    Send(netObjMsg.GetBytes(), player.clientData.id);
                }
            }

            while (removePlayerQ.Count > 0)
            {
                int player = removePlayerQ.Dequeue();
                try
                {
                    //destory object and remove them from users
                    PlayerData playerD = users[player];
                    Destroy(playerD.obj);
                    playerD.clientData.socket.Close();
                    users.Remove(player);
                    LogoutMsg logoutMsg = new LogoutMsg(player);
                    KeyValuePair <int, PlayerData>[] players = users.ToArray();
                    foreach (KeyValuePair <int, PlayerData> playerData in players)
                    {
                        if (playerData.Value.clientData.id == player)
                        {
                            continue;
                        }
                        logoutMsg.to = playerData.Value.clientData.id;
                        Send(logoutMsg.GetBytes(), logoutMsg.to);
                    }
                }
                catch (Exception e)
                {
                    //Debug.Log(e);
                }
            }

            while (bulletQ.Count > 0)
            {
                ////Debug.Log("Gets here");
                ////Debug.Log(bulletQ.Count);
                ShootMsg shootMsg = bulletQ.Dequeue();
                if (!gameManger.IsWorldBreakable())
                {
                    continue;
                }
                //waiting on bullets
                //GameObject bull = Instantiate(bulletPrefab, shootMsg.position, Quaternion.identity);

                // UPDATE HERE

                //GameObject bull = Instantiate(bulletPrefab, shootMsg.position, shootMsg.rotation);

                /*Rigidbody rig = bull.GetComponent<Rigidbody>();
                 * rig.useGravity = false;
                 * //rig.AddForce(Physics.gravity * (rig.mass * rig.mass));
                 * //rig.AddForce((transform.forward + transform.up / 4) * 2.0f);
                 * int speed = 6;
                 *
                 * //rig.AddForce(shootMsg.direction * speed);
                 * rig.AddForce(shootMsg.direction * speed);
                 */

                Vector3 rot = shootMsg.rotation.eulerAngles;
                Vector3 fwd = shootMsg.position;
                //fwd -= Vector3.forward;
                //fwd = shootMsg.rotation * fwd;

                fwd = shootMsg.rotation * Vector3.forward;
                //fwd += shootMsg.position;



                /*Vector3 lookToPosition = transform.position;
                 * lookToPosition.y = (Mathf.Sin((rot.x % 360) * Mathf.Deg2Rad) * 100000);
                 * lookToPosition.x = (Mathf.Sin((rot.y % 360) * Mathf.Deg2Rad) * 100000);*/
                RaycastHit hit;
                Physics.Raycast(shootMsg.position, fwd, out hit, 100.0f);
                Debug.DrawRay(shootMsg.position, fwd * 20, Color.green, 5, false);
                //Debug.Log(string.Format("hit something? {0}", hit.transform.name));
                GameObject hitObj = hit.collider.gameObject;
                if (hitObj.tag == "Model")
                {
                    int        textType = hitObj.GetComponent <SmashDomeVoxel.VoxelModel>().Collide(hit.point, shootMsg.shootType);
                    GameObject newCube  = Instantiate(netCube, hit.point + new Vector3(0.1f, 0.1f, 0.1f), Quaternion.identity);
                    newCube.GetComponent <Snapshot>().textureType = textType;
                    if (shootMsg.shootType == 1)
                    {
                        newCube = Instantiate(netCube, hit.point + new Vector3(0.1f, 0.1f, 0.1f), Quaternion.identity);
                        newCube.GetComponent <Snapshot>().textureType = textType;
                        newCube = Instantiate(netCube, hit.point + new Vector3(0.1f, 0.1f, 0.1f), Quaternion.identity);
                        newCube.GetComponent <Snapshot>().textureType = textType;
                        newCube = Instantiate(netCube, hit.point + new Vector3(0.1f, 0.1f, 0.1f), Quaternion.identity);
                        newCube.GetComponent <Snapshot>().textureType = textType;
                    }
                }
                //Debug.Log(hitObj.tag);
                if (hitObj.tag == "NetPlayer")
                {
                    //Debug.Log("Player Got Shot In Network Manager");
                    hitObj.GetComponent <Player>().Shot();
                }

                //Debug.Log("FIRED");
            }
        }