Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- namespace MathsPortfolio4
- {
- public class Game1 : Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Texture2D rectBtnImage, circleBtnImage;
- Texture2D rectBtnImageHighlighted, circleBtnImageHighlighted;
- Vector2 rectBtnPosition, circleBtnPosition;
- Texture2D invSlot, invSlotHighlighted, invItem;
- Vector2 invSlotPosition, invItemPosition;
- Vector2 rectanglePosition;
- bool draggingItem = false;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- rectBtnImage = Content.Load<Texture2D>("rectanglebutton");
- circleBtnImage = Content.Load<Texture2D>("circlebutton");
- rectBtnImageHighlighted = Content.Load<Texture2D>
- ("rectanglebuttonhighlighted");
- circleBtnImageHighlighted = Content.Load<Texture2D>
- ("circlebuttonhighlighted");
- rectBtnPosition = new Vector2(100, 100);
- circleBtnPosition = new Vector2(100, 200);
- invSlot = Content.Load<Texture2D>("inventorySlot");
- invSlotHighlighted = Content.Load<Texture2D>
- ("inventorySlotHighlighted");
- invItem = Content.Load<Texture2D>("item"); ;
- invSlotPosition = new Vector2(400,100);
- invItemPosition = new Vector2(400,300);
- rectanglePosition = new Vector2(400, 300);
- }
- protected override void Update(GameTime gameTime)
- {
- base.Update(gameTime);
- if (Mouse.GetState().LeftButton == ButtonState.Released && draggingItem)
- {
- // Player has just let go of an item they were dragging
- draggingItem = false;
- if (RectanglesOverlap(invItemPosition, invItem,
- invSlotPosition, invSlot))
- {
- // The item has been dragged into a slot; position it accordingly
- invItemPosition = new Vector2(
- invSlotPosition.X + invSlot.Width / 2 - invItem.Width / 2,
- invSlotPosition.Y + invSlot.Height / 2 - invItem.Height / 2);
- }
- }
- if (Mouse.GetState().LeftButton == ButtonState.Pressed &&
- rectangleContainsPoint(invItemPosition, invItem,
- new Point(Mouse.GetState().X, Mouse.GetState().Y)))
- {
- // The player is dragging an item
- invItemPosition.X = Mouse.GetState().X - invItem.Width / 2;
- invItemPosition.Y = Mouse.GetState().Y - invItem.Height / 2;
- draggingItem = true;
- }
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- Point mousePosition = new Point(Mouse.GetState().X, Mouse.GetState().Y);
- // draw rectangle button, highlighted or not depending on mouse position
- if (rectangleContainsPoint(rectBtnPosition, rectBtnImage, mousePosition))
- {
- spriteBatch.Draw(rectBtnImageHighlighted, rectBtnPosition,
- Color.White);
- }
- else
- {
- spriteBatch.Draw(rectBtnImage, rectBtnPosition, Color.White);
- }
- // draw circular button, highlighted depending on mouse position
- if (circleContainsPoint(circleBtnPosition, circleBtnImage, mousePosition))
- {
- spriteBatch.Draw(circleBtnImageHighlighted, circleBtnPosition,
- Color.White);
- }
- else
- {
- spriteBatch.Draw(circleBtnImage, circleBtnPosition, Color.White);
- }
- // draw inventory slot, highlighted depending on inventory item position
- if (RectanglesOverlap(invSlotPosition, invSlot, invItemPosition, invItem)
- && draggingItem)
- {
- spriteBatch.Draw(invSlotHighlighted, invSlotPosition, Color.White);
- }
- else
- {
- spriteBatch.Draw(invSlot, invSlotPosition, Color.White);
- }
- spriteBatch.Draw(invItem, invItemPosition, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- // This method creates a rectangle around the two images at the positions
- // specified, and returns true if they overlap.
- private bool RectanglesOverlap(Vector2 topLeftCorner1, Texture2D image1,
- Vector2 topLeftCorner2, Texture2D image2)
- {
- float top = topLeftCorner1.Y;
- float bottom = topLeftCorner1.Y + image1.Height;
- float left = topLeftCorner1.X;
- float right = topLeftCorner1.X + image1.Height;
- float Top = topLeftCorner2.Y;
- float Bottom = topLeftCorner2.Y + image2.Height;
- float Left = topLeftCorner2.X;
- float Right = topLeftCorner2.X + image2.Height;
- if (left > Right)
- {
- return false;
- }
- else if (top > Bottom)
- {
- return false;
- }
- else if (right < Left)
- {
- return false;
- }
- else if (bottom < Top) {
- return false;
- }
- draggingItem = true;
- return true;
- }
- // This method checks the distance between the point and the center of the
- // circle, and returns true if the point is within the circle's radius.
- private bool circleContainsPoint(
- Vector2 circlePosition,
- Texture2D circleImage,
- Point targetPoint)
- {
- float radius = circleImage.Height / 2;
- float Xd = targetPoint.X - circlePosition.X - radius;
- float Yd = targetPoint.Y - circlePosition.Y - radius;
- float distance = (float)Math.Sqrt(Xd * Xd + Yd * Yd);
- if (distance <= radius) { return true; }
- else { return false; }
- }
- // This method creates a rectangle around the image at the position specified,
- // and checks if the target point is within it.
- private bool rectangleContainsPoint(
- Vector2 rectanglePos,
- Texture2D rectangleImage,
- Point targetPoint)
- {
- float rectLeft = rectanglePos.X;
- float rectRight = rectanglePos.X + rectangleImage.Width;
- float rectTop = rectanglePos.Y;
- float rectBottom = rectanglePos.Y + rectangleImage.Height;
- if (targetPoint.X < rectLeft) { return false; }
- else if (targetPoint.X > rectRight) { return false; }
- else if (targetPoint.Y < rectTop) { return false; }
- else if (targetPoint.Y > rectBottom) { return false; }
- else { return true; }
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement