Beispiel #1
0
        /// <summary>
        /// Perform pre-collision velocity and position updating.
        /// </summary>
        /// <param name="map">The map.</param>
        /// <param name="deltaTime">The amount of that that has elapsed time since last update.</param>
        public virtual void UpdateVelocity(IMap map, int deltaTime)
        {
            _lastPosition = Position;

            // Only perform movement if moving
            if (IsOnGround && Velocity == Vector2.Zero)
            {
                return;
            }

#if !TOPDOWN
            Vector2 gravity;
            if (map == null)
            {
                gravity = _defaultGravity;
            }
            else
            {
                gravity = map.Gravity;
            }

            if (StandingOn != null)
            {
                if (!StandingOn.IsEntityStandingOn(this))
                {
                    StandingOn = null;
                }
            }

            if (StandingOn == null)
            {
                // Increase the velocity by the gravity
                var displacement = gravity * (Weight * deltaTime);
                Vector2.Add(ref _velocity, ref displacement, out _velocity);
            }
#endif

            // Check for surpassing the maximum velocity
            if (_velocity.X > _maxVelocity.X)
            {
                _velocity.X = _maxVelocity.X;
            }
            else if (_velocity.X < -_maxVelocity.X)
            {
                _velocity.X = -_maxVelocity.X;
            }

            if (_velocity.Y > _maxVelocity.Y)
            {
                _velocity.Y = _maxVelocity.Y;
            }
            else if (_velocity.Y < -_maxVelocity.Y)
            {
                _velocity.Y = -_maxVelocity.Y;
            }

            // Move according to the velocity
            Move(_velocity * deltaTime);
        }
Beispiel #2
0
        /// <summary>
        /// Handles updating this <see cref="Entity"/>.
        /// </summary>
        /// <param name="imap">The map the <see cref="Entity"/> is on.</param>
        /// <param name="deltaTime">The amount of time (in milliseconds) that has elapsed since the last update.</param>
        protected virtual void HandleUpdate(IMap imap, int deltaTime)
        {
            // If moving, perform collision detection
            if (Velocity != Vector2.Zero)
            {
                imap.CheckCollisions(this);
            }

#if !TOPDOWN
            // If the entity is standing on a wall, make sure they are still standing on it. If they aren't, check if they
            // are standing on top of something else.
            if (StandingOn != null)
            {
                if (!StandingOn.IsEntityStandingOn(this))
                {
                    StandingOn = imap.FindStandingOn(this);
                }
            }
#endif
        }