public void Read(DataStreamReader reader, ref DataStreamReader.Context ctx)
 {
     b0 = reader.ReadUInt(ref ctx);
     b1 = reader.ReadUInt(ref ctx);
     b2 = reader.ReadUInt(ref ctx);
     b3 = reader.ReadUInt(ref ctx);
 }
Exemplo n.º 2
0
    public static void PlayerUpdate(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        //Debug.Log("Got Player Update from server");

        MyClientBehaviour client = caller as MyClientBehaviour;
        uint numPlayers          = stream.ReadUInt(ref context);

        for (int i = 0; i < numPlayers; ++i)
        {
            uint  playerIndex = stream.ReadUInt(ref context);
            float posX        = stream.ReadFloat(ref context);
            float posY        = stream.ReadFloat(ref context);
            float posZ        = stream.ReadFloat(ref context);
            float rotX        = stream.ReadFloat(ref context);
            float rotY        = stream.ReadFloat(ref context);
            float rotZ        = stream.ReadFloat(ref context);

            ClientUpdate clientUpdate;
            clientUpdate.position.x = posX;
            clientUpdate.position.y = posY;
            clientUpdate.position.z = posZ;
            clientUpdate.euler.x    = rotX;
            clientUpdate.euler.y    = rotY;
            clientUpdate.euler.z    = rotZ;

            client.ApplyClientUpdate(clientUpdate, playerIndex);
        }
    }
Exemplo n.º 3
0
    public static void PlayerShoot(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        MyClientBehaviour client = caller as MyClientBehaviour;

        //playerId and bullet id
        uint playerId  = stream.ReadUInt(ref context);
        uint networkId = stream.ReadUInt(ref context);

        //position
        float posX = stream.ReadFloat(ref context);
        float posY = stream.ReadFloat(ref context);
        float posZ = stream.ReadFloat(ref context);
        //rotation
        float rotX = stream.ReadFloat(ref context);
        float rotY = stream.ReadFloat(ref context);
        float rotZ = stream.ReadFloat(ref context);

        //spawn a bullet with the received networkId and position/direction
        GameObject bullet;
        uint       id = NetworkId.Spawn(Resources.Load("ClientBullet"), ref client.repository, networkId);

        if (NetworkId.Find(id, out bullet, ref client.repository))
        {
            //set playerId on bullet script, set position and direction of player that's shooting
            bullet.transform.rotation = Quaternion.Euler(rotX, rotY, rotZ);
            bullet.transform.position = new Vector3(posX, posY, posZ);
        }
    }
    public static void RequestDenied(DataStreamReader stream)
    {
        stream.ReadUInt();

        uint msgId = stream.ReadUInt();

        Debug.Log("Client: Request denied");
    }
Exemplo n.º 5
0
    public static void ClientInputChanged(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        MyServerBehaviour server = caller as MyServerBehaviour;
        uint  playerIndex        = stream.ReadUInt(ref context);
        uint  dirtFlags          = stream.ReadUInt(ref context);
        uint  buttonState        = stream.ReadUInt(ref context);
        float mouseX             = stream.ReadFloat(ref context);
        float mouseY             = stream.ReadFloat(ref context);
        float mouseZ             = stream.ReadFloat(ref context);

        server.UpdateClientInput(playerIndex, buttonState, mouseX, mouseY, mouseZ);

        Debug.Log("Got Client Input for " + playerIndex);
    }
            public unsafe void Execute(Entity entity, int index, [ReadOnly] ref PlayerStateComponentData playerState)
            {
                if (playerState.PlayerShip == Entity.Null)
                {
                    return;
                }

                var buffer = cmdBuffer[entity];

                if (buffer.Length == 0)
                {
                    return;
                }
                DataStreamReader reader = DataStreamUnsafeUtility.CreateReaderFromExistingData((byte *)buffer.GetUnsafePtr(), buffer.Length);
                var ctx       = default(DataStreamReader.Context);
                var inputTick = reader.ReadUInt(ref ctx);
                var left      = reader.ReadByte(ref ctx);
                var right     = reader.ReadByte(ref ctx);
                var thrust    = reader.ReadByte(ref ctx);
                var shoot     = reader.ReadByte(ref ctx);

                buffer.Clear();
                //Debug.Log("Input delay: " + (int)(currentTick - inputTick));

                // If ship, store commands in network command buffer
                var input = shipInput[playerState.PlayerShip];

                input.mostRecentPos               = (input.mostRecentPos + 1) % 32;
                input.tick[input.mostRecentPos]   = inputTick;
                input.left[input.mostRecentPos]   = left;
                input.right[input.mostRecentPos]  = right;
                input.thrust[input.mostRecentPos] = thrust;
                input.shoot[input.mostRecentPos]  = shoot;
                shipInput[playerState.PlayerShip] = input;
            }
Exemplo n.º 7
0
    private void ProcessCommandReceived(DataStreamReader stream)
    {
        var readerCtx = default(DataStreamReader.Context);

        Server.Command.Type commandType = (Server.Command.Type)stream.ReadUInt(ref readerCtx);

        switch (commandType)
        {
        case Server.Command.Type.KEEP_ALIVE:
            //VisualLog.instance.log("Received " + commandType.ToString() + " from Server");
            break;

        case Server.Command.Type.CUSTOM_MESSAGE:
            string message = Encoding.UTF8.GetString(stream.ReadBytesAsArray(ref readerCtx, stream.Length - 4));
            VisualLog.instance.log("Received " + commandType.ToString() + " from Server. Message: " + message);
            break;

        case Server.Command.Type.POSITION:
            float   x        = stream.ReadFloat(ref readerCtx);
            float   y        = stream.ReadFloat(ref readerCtx);
            float   z        = stream.ReadFloat(ref readerCtx);
            Vector3 position = new Vector3(x, y, z);
            VisualLog.instance.log("Received " + commandType.ToString() + " from Server. Position: " + position);
            break;

        default:
            break;
        }
    }
Exemplo n.º 8
0
 ///<summary>
 ///Handle the received chat message.
 ///</summary>
 void HandleMessage(DataStreamReader streamReader)
 {
     //playername: message
     chat.text += $"<{otherPlayers[streamReader.ReadUInt()].name}>:{streamReader.ReadFixedString64().ToString()}\n";
     chatBoxScroll.verticalNormalizedPosition = 0;
     //Debug.Log($"Received message: {messageContent.ToString()}");
 }
Exemplo n.º 9
0
        public unsafe void Execute(Entity entity, int index, [ReadOnly] ref CommandTargetComponent commandTarget)
        {
            if (commandTarget.targetEntity == Entity.Null)
            {
                return;
            }

            var buffer = cmdBuffer[entity];

            if (buffer.Length == 0)
            {
                return;
            }
            DataStreamReader reader = DataStreamUnsafeUtility.CreateReaderFromExistingData((byte *)buffer.GetUnsafePtr(), buffer.Length);
            var ctx             = default(DataStreamReader.Context);
            var tick            = reader.ReadUInt(ref ctx);
            var receivedCommand = default(TCommandData);

            receivedCommand.Deserialize(tick, reader, ref ctx);
            buffer.Clear();

            // Store received commands in the network command buffer
            var command = commandData[commandTarget.targetEntity];

            command.AddCommandData(receivedCommand);
        }
    public static void NewPlayer(DataStreamReader stream)
    {
        stream.ReadUInt();

        Player p = new Player
        {
            id       = stream.ReadInt(),
            hexColor = stream.ReadUInt(),
            name     = stream.ReadString().ToString()
        };

        Debug.Log("Client: Player we got sent: " +
                  $"ID: {p.id}, HexColor: {p.hexColor}, Name: {p.name}");

        NewPlayerReceived?.Invoke(p);
    }
Exemplo n.º 11
0
        /// <summary>
        /// NetowrkIdendtity経由でデータを送ってもらう
        /// </summary>
        /// <param name="packet"></param>
        internal void OnRecieveSyncTransformPacket(ushort senderPlayerId, ref DataStreamReader packet, ref DataStreamReader.Context ctx)
        {
            if (CacheRecordableIdentity.hasAuthority)
            {
                return;
            }

            var progressTime   = packet.ReadUInt(ref ctx);
            var position       = packet.ReadVector3(ref ctx);
            var rotation       = packet.ReadQuaternion(ref ctx);
            var velocity       = packet.ReadVector3(ref ctx);
            var anglerVelocity = packet.ReadVector3(ref ctx);

            //受信パケットに記録した時間が古いものなら使わない.
            if (progressTime < prevReceiveTime)
            {
                Debug.Log("eject");
                return;
            }

            //Rigidbodyの値はすぐに適用
            if (CacheRigidbody != null)
            {
                CacheRigidbody.velocity        = velocity;
                CacheRigidbody.angularVelocity = anglerVelocity;
            }

            prevReceiveTime    = progressTime;
            recPosition        = position;
            recRotation        = rotation;
            recVelocity        = velocity;
            recvAnglerVelicity = anglerVelocity;
            //Debug.Log ("OnRecieveSyncTransformPacket");
        }
Exemplo n.º 12
0
    public static void ReadHelloServerPacket(DataStreamReader stream, ref DataStreamReader.Context context, ref List <object> data)
    {
        uint value = stream.ReadUInt(ref context);

        Debug.Log("Received Hello Server: " + value);
        data.Add(value);
    }
    private void ProcessCommandReceived(NetworkConnection connection, DataStreamReader stream)
    {
        var readerCtx = default(DataStreamReader.Context);

        Client.Command.Type commandType = (Client.Command.Type)stream.ReadUInt(ref readerCtx);

        switch (commandType)
        {
        case Client.Command.Type.KEEP_ALIVE:
            //VisualLog.instance.log("Received " + commandType.ToString() + " from Client " + connection.InternalId.ToString() + ", Hash: " + connection.GetHashCode());
            SendCommand(new Command(Command.Type.KEEP_ALIVE, 0, connection.InternalId));
            break;

        case Client.Command.Type.CUSTOM_MESSAGE:
            string message = Encoding.UTF8.GetString(stream.ReadBytesAsArray(ref readerCtx, stream.Length - 4));
            VisualLog.instance.log("Received " + commandType.ToString() + " from Client " + connection.InternalId.ToString() + ", Hash: " + connection.GetHashCode() + ". Message: " + message);
            break;

        case Client.Command.Type.POSITION:
            float   x        = stream.ReadFloat(ref readerCtx);
            float   y        = stream.ReadFloat(ref readerCtx);
            float   z        = stream.ReadFloat(ref readerCtx);
            Vector3 position = new Vector3(x, y, z);
            VisualLog.instance.log("Received " + commandType.ToString() + " from Client " + connection.InternalId.ToString() + ", Hash: " + connection.GetHashCode() + ". Position: " + position);
            break;

        default:
            break;
        }
    }
Exemplo n.º 14
0
    ///<summary>
    ///Forward the position of one player to all others.
    ///</summary>
    void HandlePositionData(DataStreamReader streamReader, NetworkConnection sender)
    {
        //Get the data.
        uint    playerID = streamReader.ReadUInt();
        Vector3 pos      = new Vector3()
        {
            x = streamReader.ReadFloat(),
            y = streamReader.ReadFloat(),
            z = streamReader.ReadFloat()
        };

        //Send it
        for (int i = 0; i < connections.Length; i++)
        {
            if (!connections[i].IsCreated || connections[i] == sender)
            {
                continue;
            }

            var writer = networkDriver.BeginSend(connections[i]);
            writer.WriteUInt((uint)MessageType.Position);
            writer.WriteUInt(playerID);
            writer.WriteFloat(pos.x);
            writer.WriteFloat(pos.y);
            writer.WriteFloat(pos.z);
            networkDriver.EndSend(writer);
        }
    }
Exemplo n.º 15
0
    public static void DestroyNetworkedObject(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        MyClientBehaviour client = caller as MyClientBehaviour;
        uint networkId           = stream.ReadUInt(ref context);

        NetworkId.DestroyNetworked(networkId, ref client.repository);
    }
Exemplo n.º 16
0
        public override void DeserializeObject(ref DataStreamReader reader)
        {
            base.DeserializeObject(ref reader);

            PlayerID     = reader.ReadInt();
            PlayerColour = reader.ReadUInt();
        }
Exemplo n.º 17
0
        public override void Recieve(UdpCNetworkDriver driver, NetworkConnection connection, DataStreamReader stream)
        {
            var  readerCtx = default(DataStreamReader.Context);
            uint number    = stream.ReadUInt(ref readerCtx);

            Debug.Log("CLIENT: Server PONG");
        }
    public static void RoomInfo(DataStreamReader stream)
    {
        stream.ReadUInt();

        Room cRoom = new Room()
        {
            possibleDirections = (Direction)stream.ReadByte(),
            treasureAmount     = stream.ReadUShort(),

            containsMonster = System.Convert.ToBoolean(stream.ReadByte()),
            containsExit    = System.Convert.ToBoolean(stream.ReadByte()),

            numberOfOtherPlayers = stream.ReadByte()
        };

        List <int> otherPlayerIds = new List <int>();

        for (int i = 0; i < cRoom.numberOfOtherPlayers; i++)
        {
            otherPlayerIds.Add(stream.ReadInt());
        }

        cRoom.otherPlayerIds = otherPlayerIds.ToArray();

        GameManager.Instance.StartCoroutine(LateInvokeRoom(cRoom));
    }
Exemplo n.º 19
0
    ///<summary>
    ///Handle when a player leaves / disconnects.
    ///</summary>
    void HandlePlayerLeave(DataStreamReader streamReader)
    {
        uint       playerID = streamReader.ReadUInt();
        GameObject op       = otherPlayers[playerID].transform.gameObject;

        otherPlayers.Remove(playerID);
        Destroy(op);
    }
    public static void ObtainTreasure(DataStreamReader stream)
    {
        stream.ReadUInt();

        ushort amountGained = stream.ReadUShort();

        ObtainTreasureReceived?.Invoke(amountGained);
    }
    public static void PlayerTurn(DataStreamReader stream)
    {
        stream.ReadUInt();

        int turnId = stream.ReadInt();

        PlayerTurnReceived?.Invoke(turnId);
    }
    public static void PlayerDies(DataStreamReader stream)
    {
        stream.ReadUInt();

        int playerId = stream.ReadInt();

        PlayerDiesReceived?.Invoke(playerId);
    }
    public static void PlayerLeftDungeon(DataStreamReader stream)
    {
        stream.ReadUInt();

        int playerId = stream.ReadInt();

        PlayerLeftDungeonReceived?.Invoke(playerId);
    }
    public static void Welcome(DataStreamReader stream)
    {
        stream.ReadUInt();

        int  myPlayerId    = stream.ReadInt();
        uint myPlayerColor = stream.ReadUInt();

        Debug.Log($"Client: I have received {myPlayerId} as ID and {myPlayerColor} as my color");

        Player p = new Player
        {
            id       = myPlayerId,
            hexColor = myPlayerColor
        };

        WelcomeReceived?.Invoke(p);
    }
    public static void PlayerLeft(DataStreamReader stream)
    {
        stream.ReadUInt();

        int leftPlayerId = stream.ReadInt();

        //GameManager.Instance.RemovePlayer(leftPlayerId);
        PlayerLeftLobbyReceived?.Invoke(leftPlayerId);
    }
Exemplo n.º 26
0
    public static void PlayerJoined(object caller, DataStreamReader stream, ref DataStreamReader.Context context, NetworkConnection source)
    {
        //got remote player joined from server
        MyClientBehaviour client = caller as MyClientBehaviour;

        uint index = stream.ReadUInt(ref context);

        client.CreateRemoteClientForIndex(index);
    }
Exemplo n.º 27
0
    void HandleDataEvent(DataStreamReader stream)
    {
        var       readerCtx = default(DataStreamReader.Context);
        GameEvent eventType = (GameEvent)stream.ReadUInt(ref readerCtx);

        Debug.Log("Got the value = " + eventType.ToString() + " back from the server");

        GameEvents.EventFunctions[eventType](this, stream, ref readerCtx, m_Connection);
    }
    public static void HitMonster(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId    = stream.ReadInt();
        ushort damageDealt = stream.ReadUShort();

        HitMonsterReceived?.Invoke(playerId, damageDealt);
    }
    public static void HitByMonster(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId     = stream.ReadInt();
        ushort newCurrentHp = stream.ReadUShort();

        HitByMonsterReceived?.Invoke(playerId, newCurrentHp);
    }
    public static void PlayerDefends(DataStreamReader stream)
    {
        stream.ReadUInt();

        int    playerId     = stream.ReadInt();
        ushort newCurrentHp = stream.ReadUShort();

        PlayerDefendsReceived?.Invoke(playerId, newCurrentHp);
    }