Esempio n. 1
0
        private void UpdatePosition(IPhysicsComponent physics, float frameTime)
        {
            var ent = physics.Entity;

            if (!physics.CanMove() || (physics.LinearVelocity.LengthSquared < Epsilon && MathF.Abs(physics.AngularVelocity) < Epsilon))
            {
                return;
            }

            if (physics.LinearVelocity != Vector2.Zero)
            {
                if (ContainerHelpers.IsInContainer(ent))
                {
                    var relayEntityMoveMessage = new RelayMovementEntityMessage(ent);
                    ent.Transform.Parent !.Owner.SendMessage(ent.Transform, relayEntityMoveMessage);
                    // This prevents redundant messages from being sent if solveIterations > 1 and also simulates the entity "colliding" against the locker door when it opens.
                    physics.LinearVelocity = Vector2.Zero;
                }
            }

            physics.Owner.Transform.DeferUpdates = true;
            _deferredUpdates.Add(physics);

            // Slow zone up in here
            if (physics.LinearVelocity.Length > _speedLimit)
            {
                physics.LinearVelocity = physics.LinearVelocity.Normalized * _speedLimit;
            }

            physics.WorldRotation += physics.AngularVelocity * frameTime;
            physics.WorldPosition += physics.LinearVelocity * frameTime;
        }
        private void UpdatePosition(IPhysicsComponent physics, float frameTime)
        {
            var ent = physics.Entity;

            if (!physics.CanMove() || (physics.LinearVelocity.LengthSquared < Epsilon && MathF.Abs(physics.AngularVelocity) < Epsilon))
            {
                return;
            }

            if (physics.LinearVelocity != Vector2.Zero)
            {
                if (ContainerHelpers.IsInContainer(ent))
                {
                    var relayEntityMoveMessage = new RelayMovementEntityMessage(ent);
                    ent.Transform.Parent !.Owner.SendMessage(ent.Transform, relayEntityMoveMessage);
                    // This prevents redundant messages from being sent if solveIterations > 1 and also simulates the entity "colliding" against the locker door when it opens.
                    physics.LinearVelocity = Vector2.Zero;
                }
            }

            physics.Owner.Transform.DeferUpdates = true;
            _deferredUpdates.Add(physics);

            // Slow zone up in here
            if (physics.LinearVelocity.Length > _speedLimit)
            {
                physics.LinearVelocity = physics.LinearVelocity.Normalized * _speedLimit;
            }

            var newPosition = physics.WorldPosition + physics.LinearVelocity * frameTime;
            var owner       = physics.Owner;
            var transform   = owner.Transform;

            // Change parent if necessary
            if (!ContainerHelpers.IsInContainer(owner))
            {
                // This shoouullddnnn'''tt de-parent anything in a container because none of that should have physics applied to it.
                if (_mapManager.TryFindGridAt(owner.Transform.MapID, newPosition, out var grid) &&
                    grid.GridEntityId.IsValid() &&
                    grid.GridEntityId != owner.Uid)
                {
                    if (grid.GridEntityId != transform.ParentUid)
                    {
                        transform.AttachParent(owner.EntityManager.GetEntity(grid.GridEntityId));
                    }
                }
                else
                {
                    transform.AttachParent(_mapManager.GetMapEntity(transform.MapID));
                }
            }

            physics.WorldRotation += physics.AngularVelocity * frameTime;
            physics.WorldPosition  = newPosition;
        }