Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using Microsoft.Xna.Framework.Net;
  12. using Microsoft.Xna.Framework.Storage;
  13.  
  14. namespace President_Evil
  15. {
  16. class Line
  17. {
  18. public Vector2 Start, End;
  19. public float Length;
  20. public float KValue;
  21. public float MValue;
  22. public float Angle = 0;
  23.  
  24. public Sprite line;
  25.  
  26. public Rectangle Box;
  27.  
  28. public Line()
  29. {
  30. Length = 0;
  31. }
  32.  
  33. public Line(Vector2 start, Vector2 end)
  34. {
  35. if (end.X < start.X)
  36. {
  37. End = start;
  38. Start = end;
  39. }
  40. else
  41. {
  42. Start = start;
  43. End = end;
  44. }
  45.  
  46. if (Start.X == End.X)
  47. End.X += 0.001f;
  48.  
  49. Length = Vector2.Distance(Start, End);
  50. KValue = (End.Y - Start.Y) / (End.X - Start.X);
  51. MValue = Start.Y - Start.X * KValue;
  52.  
  53. float XDistance = Start.X - End.X;
  54. float YDistance = Start.Y - End.Y;
  55.  
  56. Angle = (float)Math.Atan2(YDistance, XDistance);
  57.  
  58. //Top = new Vector2(Start.X, (int)Math.Min(Start.Y, End.Y));
  59. //Bottom = new Vector2(End.X, (int)Math.Max(Start.Y, End.Y));
  60. 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)));
  61. }
  62.  
  63. public void recalculateKAndM()
  64. {
  65. KValue = (End.Y - Start.Y) / (End.X - Start.X);
  66. MValue = Start.Y - Start.X * KValue;
  67.  
  68. Box.X = (int)Start.X;
  69. Box.Y = (int)Math.Min(Start.Y, End.Y);
  70.  
  71. //Top = new Vector2(Start.X, (int)Math.Min(Start.Y, End.Y));
  72. //Bottom = new Vector2(End.X, (int)Math.Max(Start.Y, End.Y));
  73. }
  74.  
  75. public void recalculateLength()
  76. {
  77. Length = Vector2.Distance(Start, End);
  78. }
  79.  
  80. public static Vector2 MoveLineIfColliding(Line MoveLine, Line LineOne, Vector2 MoveSpeed)
  81. {
  82. Vector2 Move = Vector2.Zero;
  83.  
  84. MoveLine.Start += MoveSpeed;
  85. MoveLine.End += MoveSpeed;
  86. MoveLine.recalculateKAndM();
  87. while (Line.LineToLineCollision(LineOne, MoveLine) != new Vector2(-1, -1))
  88. {
  89. Move += MoveSpeed;
  90. MoveLine.Start += MoveSpeed;
  91. MoveLine.End += MoveSpeed;
  92. MoveLine.recalculateKAndM();
  93. if (Move.X > 50 || Move.X < -50 || Move.Y > 50 || Move.Y < -50)
  94. break;
  95. }
  96.  
  97. return Move;
  98. }
  99.  
  100. public static Vector2 LineToLineCollision(Line LineOne, Line LineTwo)
  101. {
  102. //if (LineTwo.Top.X > LineOne.Bottom.X)
  103. // return new Vector2(-1, -1);
  104. //if (LineTwo.Top.Y > LineOne.Bottom.Y)
  105. // return new Vector2(-1, -1);
  106. //if (LineTwo.Bottom.X < LineOne.Top.X)
  107. // return new Vector2(-1, -1);
  108. //if (LineTwo.Bottom.Y < LineOne.Top.Y)
  109. // return new Vector2(-1, -1);
  110.  
  111. if (!LineOne.Box.Intersects(LineTwo.Box))
  112. return new Vector2(-1, -1);
  113.  
  114. Vector2 Collision = new Vector2();
  115.  
  116. Collision.X = (LineTwo.MValue - LineOne.MValue) / (LineOne.KValue - LineTwo.KValue);
  117. Collision.Y = LineOne.KValue * Collision.X + LineOne.MValue;
  118.  
  119. bool lineOneCollision = Vector2.Distance(LineOne.Start, Collision) <= LineOne.Length && Vector2.Distance(LineOne.End, Collision) <= LineOne.Length;
  120. bool lineTwoCollision = Vector2.Distance(LineTwo.Start, Collision) <= LineTwo.Length && Vector2.Distance(LineTwo.End, Collision) <= LineTwo.Length;
  121. if (lineOneCollision && lineTwoCollision)
  122. return Collision;
  123. else
  124. return new Vector2(-1, -1);
  125. }
  126.  
  127. public void LoadContent(ContentManager content)
  128. {
  129. line = new Sprite(content, "Sprites//Pixel", Start);
  130. line._Color = Color.Red;
  131. line._Scale.X = Length;
  132. line._Rotation = (float)Math.Atan2((double)(End.Y - Start.Y),(double)(End.X - Start.X));
  133. }
  134.  
  135. public void Draw(SpriteBatch spritebatch, Vector2 CamerPosition)
  136. {
  137. if (line != null)
  138. {
  139. line._Position = Start;
  140. line._CameraPosition = CamerPosition;
  141. line.Draw(spritebatch, CamerPosition);
  142. }
  143. }
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement