Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. using System;
  2. using Microsoft.Xna.Framework;
  3. using Microsoft.Xna.Framework.Graphics;
  4. using Microsoft.Xna.Framework.Input;
  5.  
  6. namespace MathsPortfolio4
  7. {
  8. public class Game1 : Game
  9. {
  10. GraphicsDeviceManager graphics;
  11. SpriteBatch spriteBatch;
  12. Texture2D rectBtnImage, circleBtnImage;
  13. Texture2D rectBtnImageHighlighted, circleBtnImageHighlighted;
  14. Vector2 rectBtnPosition, circleBtnPosition;
  15. Texture2D invSlot, invSlotHighlighted, invItem;
  16. Vector2 invSlotPosition, invItemPosition;
  17. Vector2 rectanglePosition;
  18. bool draggingItem = false;
  19.  
  20. public Game1()
  21. {
  22. graphics = new GraphicsDeviceManager(this);
  23. Content.RootDirectory = "Content";
  24. IsMouseVisible = true;
  25. }
  26.  
  27. protected override void Initialize()
  28. {
  29. base.Initialize();
  30. }
  31.  
  32. protected override void LoadContent()
  33. {
  34. spriteBatch = new SpriteBatch(GraphicsDevice);
  35. rectBtnImage = Content.Load<Texture2D>("rectanglebutton");
  36. circleBtnImage = Content.Load<Texture2D>("circlebutton");
  37. rectBtnImageHighlighted = Content.Load<Texture2D>
  38. ("rectanglebuttonhighlighted");
  39. circleBtnImageHighlighted = Content.Load<Texture2D>
  40. ("circlebuttonhighlighted");
  41. rectBtnPosition = new Vector2(100, 100);
  42. circleBtnPosition = new Vector2(100, 200);
  43. invSlot = Content.Load<Texture2D>("inventorySlot");
  44. invSlotHighlighted = Content.Load<Texture2D>
  45. ("inventorySlotHighlighted");
  46. invItem = Content.Load<Texture2D>("item"); ;
  47. invSlotPosition = new Vector2(400,100);
  48. invItemPosition = new Vector2(400,300);
  49. rectanglePosition = new Vector2(400, 300);
  50. }
  51.  
  52. protected override void Update(GameTime gameTime)
  53. {
  54. base.Update(gameTime);
  55.  
  56.  
  57. if (Mouse.GetState().LeftButton == ButtonState.Released && draggingItem)
  58. {
  59. // Player has just let go of an item they were dragging
  60. draggingItem = false;
  61. if (RectanglesOverlap(invItemPosition, invItem,
  62. invSlotPosition, invSlot))
  63. {
  64. // The item has been dragged into a slot; position it accordingly
  65. invItemPosition = new Vector2(
  66. invSlotPosition.X + invSlot.Width / 2 - invItem.Width / 2,
  67. invSlotPosition.Y + invSlot.Height / 2 - invItem.Height / 2);
  68.  
  69. }
  70. }
  71. if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
  72. rectangleContainsPoint(invItemPosition, invItem,
  73. new Point(Mouse.GetState().X, Mouse.GetState().Y)))
  74. {
  75.  
  76. // The player is dragging an item
  77. invItemPosition.X = Mouse.GetState().X - invItem.Width / 2;
  78. invItemPosition.Y = Mouse.GetState().Y - invItem.Height / 2;
  79. draggingItem = true;
  80.  
  81. }
  82. }
  83.  
  84. protected override void Draw(GameTime gameTime)
  85. {
  86. GraphicsDevice.Clear(Color.CornflowerBlue);
  87. spriteBatch.Begin();
  88. Point mousePosition = new Point(Mouse.GetState().X, Mouse.GetState().Y);
  89.  
  90. // draw rectangle button, highlighted or not depending on mouse position
  91. if (rectangleContainsPoint(rectBtnPosition, rectBtnImage, mousePosition))
  92. {
  93. spriteBatch.Draw(rectBtnImageHighlighted, rectBtnPosition,
  94. Color.White);
  95. }
  96. else
  97. {
  98. spriteBatch.Draw(rectBtnImage, rectBtnPosition, Color.White);
  99. }
  100.  
  101. // draw circular button, highlighted depending on mouse position
  102. if (circleContainsPoint(circleBtnPosition, circleBtnImage, mousePosition))
  103. {
  104. spriteBatch.Draw(circleBtnImageHighlighted, circleBtnPosition,
  105. Color.White);
  106. }
  107. else
  108. {
  109. spriteBatch.Draw(circleBtnImage, circleBtnPosition, Color.White);
  110. }
  111.  
  112. // draw inventory slot, highlighted depending on inventory item position
  113. if (RectanglesOverlap(invSlotPosition, invSlot, invItemPosition, invItem)
  114. && draggingItem)
  115. {
  116. spriteBatch.Draw(invSlotHighlighted, invSlotPosition, Color.White);
  117. }
  118. else
  119. {
  120. spriteBatch.Draw(invSlot, invSlotPosition, Color.White);
  121. }
  122. spriteBatch.Draw(invItem, invItemPosition, Color.White);
  123. spriteBatch.End();
  124. base.Draw(gameTime);
  125. }
  126.  
  127. // This method creates a rectangle around the two images at the positions
  128. // specified, and returns true if they overlap.
  129. private bool RectanglesOverlap(Vector2 topLeftCorner1, Texture2D image1,
  130. Vector2 topLeftCorner2, Texture2D image2)
  131. {
  132. float top = topLeftCorner1.Y;
  133. float bottom = topLeftCorner1.Y + image1.Height;
  134. float left = topLeftCorner1.X;
  135. float right = topLeftCorner1.X + image1.Height;
  136.  
  137. float Top = topLeftCorner2.Y;
  138. float Bottom = topLeftCorner2.Y + image2.Height;
  139. float Left = topLeftCorner2.X;
  140. float Right = topLeftCorner2.X + image2.Height;
  141.  
  142. if (left > Right)
  143. {
  144. return false;
  145. }
  146. else if (top > Bottom)
  147. {
  148. return false;
  149. }
  150. else if (right < Left)
  151. {
  152. return false;
  153. }
  154. else if (bottom < Top) {
  155. return false;
  156. }
  157. draggingItem = true;
  158.  
  159. return true;
  160. }
  161.  
  162. // This method checks the distance between the point and the center of the
  163. // circle, and returns true if the point is within the circle's radius.
  164. private bool circleContainsPoint(
  165. Vector2 circlePosition,
  166. Texture2D circleImage,
  167. Point targetPoint)
  168. {
  169.  
  170. float radius = circleImage.Height / 2;
  171.  
  172. float Xd = targetPoint.X - circlePosition.X - radius;
  173. float Yd = targetPoint.Y - circlePosition.Y - radius;
  174. float distance = (float)Math.Sqrt(Xd * Xd + Yd * Yd);
  175. if (distance <= radius) { return true; }
  176. else { return false; }
  177.  
  178. }
  179.  
  180. // This method creates a rectangle around the image at the position specified,
  181. // and checks if the target point is within it.
  182. private bool rectangleContainsPoint(
  183. Vector2 rectanglePos,
  184. Texture2D rectangleImage,
  185. Point targetPoint)
  186. {
  187. float rectLeft = rectanglePos.X;
  188. float rectRight = rectanglePos.X + rectangleImage.Width;
  189. float rectTop = rectanglePos.Y;
  190. float rectBottom = rectanglePos.Y + rectangleImage.Height;
  191.  
  192. if (targetPoint.X < rectLeft) { return false; }
  193. else if (targetPoint.X > rectRight) { return false; }
  194. else if (targetPoint.Y < rectTop) { return false; }
  195. else if (targetPoint.Y > rectBottom) { return false; }
  196. else { return true; }
  197.  
  198. return false;
  199. }
  200. }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement