/// <summary> /// Confirm that this object is a neighbor and is still active in the game. /// </summary> /// <param name="gameObject">Object to check</param> /// <returns>Whether or not this object is a neighbor.</returns> public bool ValidateAsNeighbor(GameObject gameObject) { if (Game.Components.Contains(gameObject)) { if (NeighborBoundary.Intersects(gameObject.Boundary)) { if (!NeighboringObjects.Contains(gameObject)) { NeighboringObjects.Add(gameObject); } return(true); } else { if (NeighboringObjects.Contains(gameObject)) { NeighboringObjects.Remove(gameObject); } } } else { if (NeighboringObjects.Contains(gameObject)) { NeighboringObjects.Remove(gameObject); } if (gameObject == Anchor) { Anchor = null; } } return(false); }
/// <summary> /// Method called when player has landed on another game object. /// </summary> /// <param name="sender">Game object collided with.</param> /// <param name="args">Collision event arguments.</param> private void Landed(GameObject sender, CollisionEventArgs args) { if (sender.GetType() == typeof(Monster)) { var monster = sender as Monster; NeighboringObjects.Remove(monster); monster.IsDead = true; ApplyForce(0, -10); } }
/// <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; }
/// <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); } } }