예제 #1
0
 /// <summary>
 /// Re-sends the current world data to a client.
 /// </summary>
 /// <param name="client">An active <see cref="ConnectedClient"/> object.</param>
 protected void Sync(ConnectedClient client)
 {
     foreach (Monster monster in this.monsters.Values)
     {
         Msg syncMsg = MsgBuilder.Server()
                       .Command(Msgs.CMD_SYNC)
                       .Subcommand(Msgs.SCMD_SYNC_MONSTER)
                       .ClientID(client.ClientID)
                       .Data(monster.ObjectID)
                       .Build();
         MessageBroker.Broadcast(syncMsg);
     }
     // TODO: info about other clients should be sent as well
 }
        /// <summary>
        /// Called every fixed frame update.
        /// </summary>
        void FixedUpdate()
        {
            // If we received a position from the server, lerp to that position now instead of following user input
            if (this.syncPosition != Vector3.zero)
            {
                this.ignoreInputDirection = true;
                this.direction            = Vector3.zero;
                StartCoroutine(BlendSyncPosition(this.syncPosition));
                this.syncPosition = Vector3.zero;
                return;
            }

            // Move us in the direction indicated by the player
            if (this.direction == Vector3.zero)
            {
                this._rigidbody.angularVelocity = Vector3.zero;
                this._rigidbody.velocity        = Vector3.zero;
            }
            else
            {
                this.transform.LookAt(this.transform.position + this.direction);
                this._rigidbody.velocity = this.direction * Speed;
            }

            // Send positional updates periodically, PKT_SEND_TOLERANCE determines how far we wait for the
            // player to move (in world distance) before sending an update.
            if (Vector3.Distance(this.lastSentPosition, this.transform.position) > PKT_SEND_TOLERANCE)
            {
                // update the server on client player position
                Msg update = MsgBuilder.Server()
                             .Command(Msgs.CMD_POS)
                             .Subcommand(Msgs.SCMD_POS_UPDATE)
                             .SourceID(1) // TODO: actual player ID
                             .Vector(this.transform.position.x, this.transform.position.y, this.transform.position.z)
                             .Build();
                MessageBroker.Instance.Enqueue(update);

                this.lastSentPosition = this.transform.position;
            }
        }