private void CheckForCollision(IMoveableBody c, Rectangle s) { if (IsTouchingRight(c, s)) { float distanceX = c.Rectangle.Left - s.Right; c.Velocity = new Vector2(-distanceX, c.Velocity.Y); //c.Velocity = new Vector2(0, c.Velocity.Y); //c.Position = new Vector2(c.Position.X - distanceX, c.Position.Y); } else if (IsTouchingLeft(c, s)) { float distanceX = s.Left - c.Rectangle.Right; c.Velocity = new Vector2(distanceX, c.Velocity.Y); //c.Velocity = new Vector2(0, c.Velocity.Y); //c.Position = new Vector2(c.Position.X + distanceX, c.Position.Y); } if (IsTouchingBottom(c, s)) { float distanceY = c.Rectangle.Top - s.Bottom; c.Velocity = new Vector2(c.Velocity.X, -distanceY); //c.Velocity = new Vector2(c.Velocity.X, 0); //c.Position = new Vector2(c.Position.X, c.Position.Y - distanceY); } if (IsTouchingTop(c, s)) { float distanceY = s.Top - c.Rectangle.Bottom; c.Velocity = new Vector2(c.Velocity.X, distanceY); //c.Velocity = new Vector2(c.Velocity.X, 0); //c.Position = new Vector2(c.Position.X, c.Position.Y + distanceY); } }
private bool IsTouchingBottom(IMoveableBody c, Rectangle r) { return(c.Rectangle.Top + c.Velocity.Y < r.Bottom && c.Rectangle.Bottom > r.Bottom && c.Rectangle.Right > r.Left && c.Rectangle.Left < r.Right); }
private bool IsTouchingRight(IMoveableBody c, Rectangle r) { return(c.Rectangle.Left + c.Velocity.X < r.Right && c.Rectangle.Right > r.Right && c.Rectangle.Bottom > r.Top && c.Rectangle.Top < r.Bottom); }
private bool IsTouchingTop(IMoveableBody c, Rectangle r) { return(c.Rectangle.Bottom + c.Velocity.Y > r.Top && c.Rectangle.Top < r.Top && c.Rectangle.Right > r.Left && c.Rectangle.Left < r.Right); }
private void CheckForCollisionAndLoseHeart(IMoveableBody c, Rectangle s) { bool touchingAnything = false; if (IsTouchingRight(c, s)) { float distanceX = c.Rectangle.Left - s.Right; c.Velocity = new Vector2(-distanceX, c.Velocity.Y); touchingAnything = true; } else if (IsTouchingLeft(c, s)) { float distanceX = s.Left - c.Rectangle.Right; c.Velocity = new Vector2(distanceX, c.Velocity.Y); touchingAnything = true; } if (IsTouchingBottom(c, s)) { float distanceY = c.Rectangle.Top - s.Bottom; c.Velocity = new Vector2(c.Velocity.X, -distanceY); touchingAnything = true; } if (IsTouchingTop(c, s)) { float distanceY = s.Top - c.Rectangle.Bottom; c.Velocity = new Vector2(c.Velocity.X, distanceY); touchingAnything = true; } if (touchingAnything) { c.LoseHeart(); } }
public void DeleteBody(IMoveableBody c) { if (moveableBodies.Contains(c)) { moveableBodies.Remove(c); } else { throw new ArgumentException("Body not found"); } }
private void UpdateBodyState(IMoveableBody c) { if (c.MoveableBodyState == MoveableBodyStates.Attacking) { return; } if (c.Velocity.X > 0 && c.Velocity.Y != 0) { c.MoveableBodyState = MoveableBodyStates.InAirRight; } else if (c.Velocity.X < 0 && c.Velocity.Y != 0) { c.MoveableBodyState = MoveableBodyStates.InAirLeft; } else if (c.Velocity.Y != 0) { c.MoveableBodyState = MoveableBodyStates.InAir; } else if (c.Velocity.X > 0) { if (CollisionManager.InAir(c)) { c.MoveableBodyState = MoveableBodyStates.InAirRight; } else if (c.MoveableBodyState != MoveableBodyStates.Attacking) { c.MoveableBodyState = MoveableBodyStates.WalkRight; } } else if (c.Velocity.X < 0) { if (CollisionManager.InAir(c)) { c.MoveableBodyState = MoveableBodyStates.InAirLeft; } else if (c.MoveableBodyState != MoveableBodyStates.Attacking) { c.MoveableBodyState = MoveableBodyStates.WalkLeft; } } else { if (CollisionManager.InAir(c)) { c.MoveableBodyState = MoveableBodyStates.InAir; } else { c.MoveableBodyState = MoveableBodyStates.Idle; } } }
private void MoveBody(IMoveableBody c) { c.Position += c.Velocity; if (c.Velocity.X > 0) { c.Velocity = new Vector2(c.Velocity.X - 1f, c.Velocity.Y); if (c.Velocity.X < 0) { c.Velocity = new Vector2(0, c.Velocity.Y); } } else if (c.Velocity.X < 0) { c.Velocity = new Vector2(c.Velocity.X + 1f, c.Velocity.Y); if (c.Velocity.X > 0) { c.Velocity = new Vector2(0, c.Velocity.Y); } } }
public bool InAir(IMoveableBody c) { Rectangle collidableEnlarged = new Rectangle( (int)c.Position.X, (int)c.Position.Y, (int)c.Size.X, (int)c.Size.Y + 1); foreach (var s in staticBodies) { if (collidableEnlarged.Intersects(s)) { return(false); } } foreach (var spike in staticSpikes) { if (collidableEnlarged.Intersects(spike)) { return(false); } } return(true); }
private bool CheckForCollision(IMoveableBody c, IMoveableBody c2) { return(c.Rectangle.Intersects(c2.Rectangle)); }
public void AddMoveableBody(IMoveableBody c) { moveableBodies.Add(c); }
private void ApplyDownForce(IMoveableBody c, float downForce) { c.Velocity = new Vector2(c.Velocity.X, c.Velocity.Y + downForce); }