Guest User

Untitled

a guest
Aug 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. public void handleCollision(GameObject OtherObject)
  2. {
  3. if(this.CollidesWith(OtherObject))
  4. {
  5. if(OtherObject is Stage)
  6. {
  7. float rightEdgeDistance = OtherObject.BoundRect.X - (Position.X + this.BoundRect.Width);
  8. float leftEdgeDistance = OtherObject.BoundRect.X + OtherObject.BoundRect.Width - Position.X;
  9.  
  10. float TopEdgeDistance = OtherObject.BoundRect.Y - (Position.Y + this.BoundRect.Height);
  11. float BottomEdgeDistance = OtherObject.BoundRect.Y + OtherObject.BoundRect.Height - Position.Y;
  12.  
  13.  
  14. float Left_Right_SmallerDistance = Math.Min(Math.Abs(rightEdgeDistance), Math.Abs(leftEdgeDistance));
  15. float Top_Bottom_SmallerDistance = Math.Min(Math.Abs(TopEdgeDistance), Math.Abs(BottomEdgeDistance));
  16.  
  17. float smallerDistance = Math.Min(Math.Abs(Left_Right_SmallerDistance), Math.Abs(Top_Bottom_SmallerDistance));
  18.  
  19. if (smallerDistance == Math.Abs(leftEdgeDistance))
  20. {
  21. Position.X = OtherObject.BoundRect.X + OtherObject.BoundRect.Width;
  22. Velocity.X = 0;
  23. }
  24. else if (smallerDistance == Math.Abs(rightEdgeDistance))
  25. {
  26. Position.X = OtherObject.BoundRect.X - this.BoundRect.Width;
  27. Velocity.X = 0;
  28. }
  29. else if (smallerDistance == Math.Abs(BottomEdgeDistance))
  30. {
  31. Position.Y = OtherObject.BoundRect.Y + OtherObject.BoundRect.Height;
  32. Velocity.Y = 0;
  33. }
  34. else if (smallerDistance == Math.Abs(TopEdgeDistance))
  35. {
  36. Position.Y = OtherObject.BoundRect.Y - this.BoundRect.Height;
  37. Velocity.Y = 0;
  38. }
  39. }
  40. }
  41.  
  42. public void inputHandler(GameTime gameTime,GameObject OtherObject)
  43. {
  44. Velocity = Vector2.Zero;
  45.  
  46. if (Keyboard.GetState().IsKeyDown(Keys.W))
  47. Velocity.Y -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  48. if (Keyboard.GetState().IsKeyDown(Keys.S))
  49. Velocity.Y += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  50. if (Keyboard.GetState().IsKeyDown(Keys.A))
  51. Velocity.X -= moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  52. if (Keyboard.GetState().IsKeyDown(Keys.D))
  53. Velocity.X += moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
  54.  
  55.  
  56. handleCollision(OtherObject);
  57. Position += Velocity;
  58. }
  59.  
  60. protected override void Update(GameTime gameTime)
  61. {
  62. if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
  63. this.Exit();
  64.  
  65. for (int i = 0; i < 3; i++)
  66. player.Update(gameTime, stage[i]);
  67. powerUp.Update(gameTime,player);
  68. base.Update(gameTime);
  69. }
Add Comment
Please, Sign In to add comment