Пример #1
0
 public PseudoFullObjectState(PseudoFullObjectState state, bool destroyed)
 {
     if (destroyed)
     {
         ObjectId                = state.ObjectId;
         VelocityChanged         = false;
         PositionChanged         = false;
         RotationChanged         = false;
         RotationVelocityChanged = false;
         RestitutionChanged      = false;
         SizeChanged             = false;
         IsSensorObject          = false;
         IsStaticObject          = false;
         WasDestroyed            = true;
         Velocity                = new HalfVector2();
         Rotation                = 0;
         RotationVelocity        = 0;
         Position                = new Vector2();
         Restitution             = 0;
         Size = new HalfVector2();
     }
     else
     {
         ObjectId                = state.ObjectId;
         VelocityChanged         = state.VelocityChanged;
         PositionChanged         = state.PositionChanged;
         RotationChanged         = state.RotationChanged;
         RotationVelocityChanged = state.RotationVelocityChanged;
         RestitutionChanged      = state.RestitutionChanged;
         SizeChanged             = state.SizeChanged;
         IsSensorObject          = state.IsSensorObject;
         IsStaticObject          = state.IsStaticObject;
         WasDestroyed            = state.WasDestroyed;
         Velocity                = state.Velocity;
         Rotation                = state.Rotation;
         RotationVelocity        = state.RotationVelocity;
         Position                = state.Position;
         Restitution             = state.Restitution;
         Size = state.Size;
     }
 }
        public PseudoFullGameWorldState MakeDelta(PseudoFullGameWorldState lastState)
        {
            var state = new PseudoFullGameWorldState();

            foreach (var obj in lastState.ObjectStates.Values)
            {
                //It was destroyed, flag it
                if (!ObjectStates.ContainsKey(obj.ObjectId))
                {
                    state._objectStates.Add(obj.ObjectId, new PseudoFullObjectState(obj, true));
                }
                //Otherwise, compute state differences
                else
                {
                    var objState = new PseudoFullObjectState(obj, ObjectStates[obj.ObjectId]);
                    if (objState.HasChanges(obj))
                    {
                        state._objectStates.Add(obj.ObjectId, objState);
                    }
                }
            }

            //Then do a reverse search to find the new ones
            foreach (var obj in ObjectStates.Values)
            {
                if (!lastState.ObjectStates.ContainsKey(obj.ObjectId) &&
                    !state.ObjectStates.ContainsKey(obj.ObjectId))
                {
                    state._objectStates.Add(obj.ObjectId, obj);
                }
            }

            state.CurrentGameStatus           = CurrentGameStatus;
            state.CurrentGameTimeMilliseconds = CurrentGameTimeMilliseconds;
            state.FriendlyFireEnabled         = FriendlyFireEnabled;

            return(state);
        }
Пример #3
0
        public PseudoFullObjectState(PseudoFullObjectState lastState, PseudoFullObjectState newState, bool destroyed = false)
        {
            ObjectId                = newState.ObjectId;
            VelocityChanged         = false;
            PositionChanged         = false;
            RotationChanged         = false;
            RotationVelocityChanged = false;
            RestitutionChanged      = false;
            SizeChanged             = false;
            IsSensorObject          = newState.IsSensorObject;
            IsStaticObject          = newState.IsStaticObject;
            WasDestroyed            = destroyed;

            Velocity         = newState.Velocity;
            Rotation         = newState.Rotation;
            RotationVelocity = newState.RotationVelocity;
            Position         = newState.Position;
            Restitution      = newState.Restitution;
            Size             = newState.Size;

            //Check if velocity changed
            if (MathHelper.Distance(lastState.Velocity.ToVector2().X,
                                    newState.Velocity.ToVector2().X) > _velocityThreshold ||
                MathHelper.Distance(lastState.Velocity.ToVector2().Y,
                                    newState.Velocity.ToVector2().Y) > _velocityThreshold)
            {
                VelocityChanged = true;
            }


            //Check if rotation changed
            if (MathHelper.Distance(lastState.Rotation, newState.Rotation) > _rotationThreshold)
            {
                RotationChanged = true;
            }

            //Check if position changed
            if (MathHelper.Distance(lastState.RotationVelocity, newState.RotationVelocity) > _rotationThreshold)
            {
                RotationVelocityChanged = true;
            }

            //Check if position changed
            if (MathHelper.Distance(lastState.Position.X, newState.Position.X) > _positionThreshold ||
                MathHelper.Distance(lastState.Position.Y, newState.Position.Y) > _positionThreshold)
            {
                PositionChanged = true;
                VelocityChanged = true; //Position + Velocity must be updated together to fix some
                //simulation bugs that occur
            }
            //Check if restitution changed
            if (MathHelper.Distance(lastState.Restitution, newState.Restitution) > _restitutionThreshold)
            {
                RestitutionChanged = true;
            }

            //Check if size changed
            if (MathHelper.Distance(lastState.Size.ToVector2().X, newState.Size.ToVector2().X) > _sizeThreshold ||
                MathHelper.Distance(lastState.Size.ToVector2().Y, newState.Size.ToVector2().Y) > _sizeThreshold)
            {
                SizeChanged = true;
            }
        }
Пример #4
0
 public bool HasChanges(PseudoFullObjectState last)
 {
     return(VelocityChanged || PositionChanged || RotationChanged || RotationVelocityChanged ||
            RestitutionChanged || SizeChanged || IsSensorObject != last.IsSensorObject ||
            IsStaticObject != last.IsStaticObject || WasDestroyed);
 }
Пример #5
0
 public PseudoFullObjectState(PseudoFullObjectState lastState, GameObject obj, bool destroyed = false)
     : this(lastState, new PseudoFullObjectState(obj, destroyed))
 {
 }
        public static PseudoFullGameWorldState Read(NetIncomingMessage message)
        {
            var state = new PseudoFullGameWorldState();

            state.CurrentGameStatus           = (GameCore.CurrentGameStatus)message.ReadByte();
            state.CurrentGameTimeMilliseconds = message.ReadDouble();
            state.FriendlyFireEnabled         = message.ReadBoolean();

            message.ReadPadBits();

            var ct = message.ReadUInt16();

            for (var i = 0; i < ct; i++)
            {
                var objState = new PseudoFullObjectState();
                objState.ObjectId                = message.ReadUInt16();
                objState.VelocityChanged         = message.ReadBoolean();
                objState.PositionChanged         = message.ReadBoolean();
                objState.RotationChanged         = message.ReadBoolean();
                objState.RotationVelocityChanged = message.ReadBoolean();
                objState.RestitutionChanged      = message.ReadBoolean();
                objState.SizeChanged             = message.ReadBoolean();
                objState.IsSensorObject          = message.ReadBoolean();
                objState.IsStaticObject          = message.ReadBoolean();
                objState.WasDestroyed            = message.ReadBoolean();

                message.ReadPadBits();

                if (objState.VelocityChanged)
                {
                    objState.Velocity = new HalfVector2 {
                        PackedValue = message.ReadUInt32()
                    }
                }
                ;

                if (objState.PositionChanged)
                {
                    objState.Position = new Vector2(message.ReadFloat(), message.ReadFloat());
                }

                if (objState.RotationChanged)
                {
                    objState.Rotation = new Half()
                    {
                        InternalValue = message.ReadUInt16()
                    }
                }
                ;

                if (objState.RotationVelocityChanged)
                {
                    objState.RotationVelocity = new Half()
                    {
                        InternalValue = message.ReadUInt16()
                    }
                }
                ;

                if (objState.RestitutionChanged)
                {
                    objState.Restitution = new Half()
                    {
                        InternalValue = message.ReadUInt16()
                    }
                }
                ;

                if (objState.SizeChanged)
                {
                    objState.Size = new HalfVector2()
                    {
                        PackedValue = message.ReadUInt32()
                    }
                }
                ;

                state._objectStates.Add(objState.ObjectId, objState);
            }

            return(state);
        }