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;
- using Microsoft.Xna.Framework.Net;
- using Microsoft.Xna.Framework.Storage;
- namespace President_Evil
- {
- class Line
- {
- public Vector2 Start, End;
- public float Length;
- public float KValue;
- public float MValue;
- public float Angle = 0;
- public Sprite line;
- public Rectangle Box;
- public Line()
- {
- Length = 0;
- }
- public Line(Vector2 start, Vector2 end)
- {
- if (end.X < start.X)
- {
- End = start;
- Start = end;
- }
- else
- {
- Start = start;
- End = end;
- }
- if (Start.X == End.X)
- End.X += 0.001f;
- Length = Vector2.Distance(Start, End);
- KValue = (End.Y - Start.Y) / (End.X - Start.X);
- MValue = Start.Y - Start.X * KValue;
- float XDistance = Start.X - End.X;
- float YDistance = Start.Y - End.Y;
- Angle = (float)Math.Atan2(YDistance, XDistance);
- //Top = new Vector2(Start.X, (int)Math.Min(Start.Y, End.Y));
- //Bottom = new Vector2(End.X, (int)Math.Max(Start.Y, End.Y));
- Box = new Rectangle((int)Start.X, (int)Math.Min(Start.Y, End.Y), (int)(End.X - Start.X), (int)(Math.Max(Start.Y, End.Y) - Math.Min(Start.Y, End.Y)));
- }
- public void recalculateKAndM()
- {
- KValue = (End.Y - Start.Y) / (End.X - Start.X);
- MValue = Start.Y - Start.X * KValue;
- Box.X = (int)Start.X;
- Box.Y = (int)Math.Min(Start.Y, End.Y);
- //Top = new Vector2(Start.X, (int)Math.Min(Start.Y, End.Y));
- //Bottom = new Vector2(End.X, (int)Math.Max(Start.Y, End.Y));
- }
- public void recalculateLength()
- {
- Length = Vector2.Distance(Start, End);
- }
- public static Vector2 MoveLineIfColliding(Line MoveLine, Line LineOne, Vector2 MoveSpeed)
- {
- Vector2 Move = Vector2.Zero;
- MoveLine.Start += MoveSpeed;
- MoveLine.End += MoveSpeed;
- MoveLine.recalculateKAndM();
- while (Line.LineToLineCollision(LineOne, MoveLine) != new Vector2(-1, -1))
- {
- Move += MoveSpeed;
- MoveLine.Start += MoveSpeed;
- MoveLine.End += MoveSpeed;
- MoveLine.recalculateKAndM();
- if (Move.X > 50 || Move.X < -50 || Move.Y > 50 || Move.Y < -50)
- break;
- }
- return Move;
- }
- public static Vector2 LineToLineCollision(Line LineOne, Line LineTwo)
- {
- //if (LineTwo.Top.X > LineOne.Bottom.X)
- // return new Vector2(-1, -1);
- //if (LineTwo.Top.Y > LineOne.Bottom.Y)
- // return new Vector2(-1, -1);
- //if (LineTwo.Bottom.X < LineOne.Top.X)
- // return new Vector2(-1, -1);
- //if (LineTwo.Bottom.Y < LineOne.Top.Y)
- // return new Vector2(-1, -1);
- if (!LineOne.Box.Intersects(LineTwo.Box))
- return new Vector2(-1, -1);
- Vector2 Collision = new Vector2();
- Collision.X = (LineTwo.MValue - LineOne.MValue) / (LineOne.KValue - LineTwo.KValue);
- Collision.Y = LineOne.KValue * Collision.X + LineOne.MValue;
- bool lineOneCollision = Vector2.Distance(LineOne.Start, Collision) <= LineOne.Length && Vector2.Distance(LineOne.End, Collision) <= LineOne.Length;
- bool lineTwoCollision = Vector2.Distance(LineTwo.Start, Collision) <= LineTwo.Length && Vector2.Distance(LineTwo.End, Collision) <= LineTwo.Length;
- if (lineOneCollision && lineTwoCollision)
- return Collision;
- else
- return new Vector2(-1, -1);
- }
- public void LoadContent(ContentManager content)
- {
- line = new Sprite(content, "Sprites//Pixel", Start);
- line._Color = Color.Red;
- line._Scale.X = Length;
- line._Rotation = (float)Math.Atan2((double)(End.Y - Start.Y),(double)(End.X - Start.X));
- }
- public void Draw(SpriteBatch spritebatch, Vector2 CamerPosition)
- {
- if (line != null)
- {
- line._Position = Start;
- line._CameraPosition = CamerPosition;
- line.Draw(spritebatch, CamerPosition);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement