Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SFML.Graphics;
- using SFML.Window;
- namespace ComponentEngine
- {
- class Program
- {
- static RenderWindow window;
- static Sprite sp;
- static Vector2i currentMousePos;
- static float speed;
- static void Main(string[] args)
- {
- INIParser iniparser = new INIParser();
- Dictionary<String, int> VideoSettings = iniparser.ParseINI();
- window = new RenderWindow(new VideoMode((uint)VideoSettings["width"], (uint)VideoSettings["height"], 32), "Component Engine");
- sp = new Sprite(new Texture("car.png"));
- Vector2f pos = new Vector2f();
- pos.X = window.Size.X / 2;
- pos.Y = window.Size.Y / 2;
- sp.Position = pos;
- sp.Origin = new Vector2f(sp.Texture.Size.X / 2, 0);
- currentMousePos = new Vector2i((int)window.Size.X / 2, (int)window.Size.Y / 2);
- speed = 1.0f;
- while (window.IsOpen())
- {
- if (Keyboard.IsKeyPressed(Keyboard.Key.Escape))
- window.Close();
- currentMousePos = window.InternalGetMousePosition();
- window.DispatchEvents();
- window.Clear();
- MoveSprite(currentMousePos);
- window.Draw(sp);
- window.Display();
- }
- }
- private static void MoveSprite(Vector2i mousepos)
- {
- Vector2f targetV = new Vector2f(mousepos.X, mousepos.Y);
- Vector2f directionV = Normalize(targetV - sp.Position);
- float newRot = (float)Math.Atan2(directionV.Y, directionV.X) * 180 / (float)Math.PI + 90;
- if (sp.Rotation != newRot)
- {
- sp.Rotation = newRot;
- }
- Console.WriteLine((mousepos.X - sp.Position.X) + "," + (mousepos.Y - sp.Position.Y));
- sp.Position += directionV * speed;
- }
- private static Vector2f Normalize(Vector2f v1)
- {
- float length = (float)Math.Sqrt((v1.X * v1.X) + (v1.Y * v1.Y));
- float x = v1.X / length;
- float y = v1.Y / length;
- return new Vector2f(x, y);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement