Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- ///
- /// A simple example to make object move where the mouse is clicked
- ///
- namespace GotoLocation
- {
- #if WINDOWS || XBOX
- static class Program
- {
- /// <summary>
- /// The main entry point for the application.
- /// </summary>
- static void Main(string[] args)
- {
- using (Game1 game = new Game1())
- {
- game.Run();
- }
- }
- }
- #endif
- public class Game1 : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Vector2 direction = Vector2.Zero;
- Vector2 position = Vector2.Zero;
- float speed = 2f;
- public Game1()
- {
- graphics = new GraphicsDeviceManager(this);
- Content.RootDirectory = "Content";
- this.IsMouseVisible = true;
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- position.X = (graphics.PreferredBackBufferWidth / 2) - (Content.Load<Texture2D>("dot").Width / 2);
- position.Y = (graphics.PreferredBackBufferHeight / 2) - (Content.Load<Texture2D>("dot").Height / 2);
- direction = position;
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
- this.Exit();
- if (Mouse.GetState().LeftButton == ButtonState.Pressed)
- {
- direction = new Vector2(Mouse.GetState().X-(Content.Load<Texture2D>("dot").Width / 2), Mouse.GetState().Y-(Content.Load<Texture2D>("dot").Height / 2));
- }
- Vector2 move = Helper_Direction.MoveTowards(position, direction, speed);
- position.X += move.X;
- position.Y += move.Y;
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- spriteBatch.Begin();
- spriteBatch.Draw(Content.Load<Texture2D>("dot"), position, Color.White);
- spriteBatch.End();
- base.Draw(gameTime);
- }
- }
- public static class Helper_Direction
- {
- public static double FaceObject(Vector2 position, Vector2 target)
- {
- return (Math.Atan2(position.Y - target.Y, position.X - target.X) * (180 / Math.PI));
- }
- public static Vector2 MoveTowards(Vector2 position, Vector2 target, float speed)
- {
- double direction = (float)(Math.Atan2(target.Y - position.Y, target.X - position.X) * 180 / Math.PI);
- Vector2 move = new Vector2(0, 0);
- move.X = (float)Math.Cos(direction * Math.PI / 180) * speed;
- move.Y = (float)Math.Sin(direction * Math.PI / 180) * speed;
- return move;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement