Ejemplo n.º 1
0
        public override void ReadPacket()
        {
            SequenceNumber = ReadUShort();

            // Read the byte flag representing update types and reconstruct it
            var updateTypeFlag = ReadByte();
            // Keep track of value of current bit
            var currentTypeValue = 1;

            for (var i = 0; i < (int)UpdateType.Count; i++)
            {
                // If this bit was set in our flag, we add the type to the list
                if ((updateTypeFlag & currentTypeValue) != 0)
                {
                    UpdateTypes.Add((UpdateType)currentTypeValue);
                }

                // Increase the value of current bit
                currentTypeValue *= 2;
            }

            // Based on the update types, we read the corresponding values
            if (UpdateTypes.Contains(UpdateType.PlayerUpdate))
            {
                // First we read the length of the player update list
                var numPlayerUpdates = ReadByte();

                // Then we read all the values into PlayerUpdate instances
                for (var i = 0; i < numPlayerUpdates; i++)
                {
                    // We create a new instance
                    var playerUpdate = new PlayerUpdate();

                    // Read the information into the new instance
                    ReadPlayerUpdate(playerUpdate);

                    // Add the instance to the list
                    PlayerUpdates.Add(playerUpdate);
                }
            }

            if (UpdateTypes.Contains(UpdateType.EntityUpdate))
            {
                // First we read the length of the entity update list
                var numEntityUpdates = ReadByte();

                // Then we read all the values into the EntityUpdates dictionary
                for (var i = 0; i < numEntityUpdates; i++)
                {
                    // Create a new EntityUpdate instance
                    var entityUpdate = new EntityUpdate();

                    // Read the values into the instance
                    ReadEntityUpdate(entityUpdate);

                    // Add it to the list
                    EntityUpdates.Add(entityUpdate);
                }
            }
        }
Ejemplo n.º 2
0
        /**
         * Reads a single EntityUpdate of values from the packet and puts them in the
         * given EntityUpdates dictionary.
         * This assumes that the given EntityUpdate parameter is already instantiated.
         */
        protected void ReadEntityUpdate(EntityUpdate entityUpdate)
        {
            entityUpdate.EntityType = (EntityType)ReadByte();
            entityUpdate.Id         = ReadByte();

            // Read the byte flag representing update types and reconstruct it
            var updateTypeFlag = ReadByte();
            // Keep track of value of current bit
            var currentTypeValue = 1;

            for (var i = 0; i < (int)EntityUpdateType.Count; i++)
            {
                // If this bit was set in our flag, we add the type to the list
                if ((updateTypeFlag & currentTypeValue) != 0)
                {
                    entityUpdate.UpdateTypes.Add((EntityUpdateType)currentTypeValue);
                }

                // Increase the value of current bit
                currentTypeValue *= 2;
            }

            // Based on the update types, we read the corresponding values
            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.Position))
            {
                entityUpdate.Position = ReadVector2();
            }

            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.State))
            {
                entityUpdate.StateIndex = ReadByte();
            }

            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.Variables))
            {
                // We first read how many bytes are in the array
                var numBytes = ReadByte();

                for (var i = 0; i < numBytes; i++)
                {
                    var readByte = ReadByte();
                    entityUpdate.FsmVariables.Add(readByte);
                }
            }
        }
Ejemplo n.º 3
0
        /**
         * Write the given EntityUpdate instance to the underlying packet
         */
        protected void WriteEntityUpdate(EntityUpdate entityUpdate)
        {
            Write((byte)entityUpdate.EntityType);
            Write(entityUpdate.Id);

            // Construct the byte flag representing update types
            byte updateTypeFlag = 0;

            foreach (var updateType in entityUpdate.UpdateTypes)
            {
                updateTypeFlag |= (byte)updateType;
            }

            // Write the update type flag
            Write(updateTypeFlag);

            // Conditionally write the state and data fields
            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.Position))
            {
                Write(entityUpdate.Position);
            }

            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.State))
            {
                Write(entityUpdate.StateIndex);
            }

            if (entityUpdate.UpdateTypes.Contains(EntityUpdateType.Variables))
            {
                // First write the number of bytes we are writing
                Write((byte)entityUpdate.FsmVariables.Count);

                foreach (var b in entityUpdate.FsmVariables)
                {
                    Write(b);
                }
            }
        }