示例#1
0
        /// <summary>
        /// Move this object on it's X-coordinate.
        /// </summary>
        /// <param name="value">Value for this object to move.</param>
        public void MoveX(int value)
        {
            //Padded boundary for safe collision checks on smaller movements.
            var territoryBoundary = new Rectangle((int)X, (int)Y, Boundary.Width + 2, Boundary.Height + 2);

            //Determine if this movement can or cannot be made.
            bool canMoveRight = true;
            bool canMoveLeft  = true;

            //Checks if this object will be prevented by any solid game object neighbors.
            foreach (var no in NeighboringObjects.ToList())
            {
                if (territoryBoundary.Intersects(no.Boundary))
                {
                    var depth = RectangleExtensions.GetIntersectionDepth(territoryBoundary, no.Boundary);

                    if (depth.Y < -8 || depth.Y > 8)
                    {
                        if (depth.X < 0 && depth.X > -8)
                        {
                            canMoveRight = false;
                            OnRightCollided(no, new CollisionEventArgs()
                            {
                                Depth = depth
                            });
                        }
                        if (depth.X > 0 && depth.X < 8)
                        {
                            canMoveLeft = false;
                            OnLeftCollided(no, new CollisionEventArgs()
                            {
                                Depth = depth
                            });
                        }
                    }
                }
            }

            if (value > 0 && canMoveRight)
            {
                X += value;
            }
            if (value < 0 && canMoveLeft)
            {
                X += value;
            }

            VelocityX = value;
        }
示例#2
0
        /// <summary>
        /// Updates neighbor collection and checks for colision.
        /// </summary>
        private void UpdateNeighbors()
        {
            foreach (var gameObject in NeighboringObjects.ToList())
            {
                if (ValidateAsNeighbor(gameObject))
                {
                    var depth         = RectangleExtensions.GetIntersectionDepth(Boundary, gameObject.Boundary);
                    var collisionArgs = new CollisionEventArgs()
                    {
                        Depth = depth
                    };

                    OnCollisionDetected(gameObject, collisionArgs);
                    CheckCollision(gameObject, collisionArgs);
                }
            }
        }