Advertisement
Guest User

Untitled

a guest
Jul 27th, 2013
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SFML.Graphics;
  7. using SFML.Window;
  8.  
  9. namespace ComponentEngine
  10. {
  11.     class Program
  12.     {
  13.         static RenderWindow window;
  14.         static Sprite sp;
  15.         static Vector2i currentMousePos;
  16.         static float speed;
  17.  
  18.         static void Main(string[] args)
  19.         {
  20.             INIParser iniparser = new INIParser();
  21.             Dictionary<String, int> VideoSettings = iniparser.ParseINI();
  22.             window = new RenderWindow(new VideoMode((uint)VideoSettings["width"], (uint)VideoSettings["height"], 32), "Component Engine");
  23.  
  24.             sp = new Sprite(new Texture("car.png"));
  25.  
  26.             Vector2f pos = new Vector2f();
  27.             pos.X = window.Size.X / 2;
  28.             pos.Y = window.Size.Y / 2;
  29.             sp.Position = pos;
  30.             sp.Origin = new Vector2f(sp.Texture.Size.X / 2, 0);
  31.             currentMousePos = new Vector2i((int)window.Size.X / 2, (int)window.Size.Y / 2);
  32.             speed = 1.0f;
  33.             while (window.IsOpen())
  34.             {
  35.                 if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
  36.                     window.Close();
  37.                 currentMousePos = window.InternalGetMousePosition();
  38.                 window.DispatchEvents();
  39.                 window.Clear();
  40.                 MoveSprite(currentMousePos);
  41.                 window.Draw(sp);
  42.                 window.Display();
  43.             }
  44.         }
  45.  
  46.         private static void MoveSprite(Vector2i mousepos)
  47.         {
  48.             Vector2f targetV = new Vector2f(mousepos.X, mousepos.Y);
  49.             Vector2f directionV = Normalize(targetV - sp.Position);
  50.             float newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;
  51.             if (sp.Rotation != newRot)
  52.             {
  53.                 sp.Rotation = newRot;
  54.             }
  55.             Console.WriteLine((mousepos.X - sp.Position.X) + "," + (mousepos.Y - sp.Position.Y));
  56.             sp.Position += directionV * speed;
  57.         }
  58.  
  59.         private static Vector2f Normalize(Vector2f v1)
  60.         {
  61.             float length = (float)Math.Sqrt((v1.X * v1.X) + (v1.Y * v1.Y));
  62.             float x = v1.X / length;
  63.             float y = v1.Y / length;
  64.             return new Vector2f(x, y);
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement