Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. using SpaceRTS2.Assets.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Vectrosity;
  6.  
  7. namespace SpaceRTS2.Assets.Game.Ships
  8. {
  9. public sealed class SelectionHud : ShipComponent
  10. {
  11. private static readonly Vector3[] corners = new Vector3[8];
  12. private VectorLine selectionLines;
  13. private List<Vector2> selectionPoints;
  14.  
  15. public BoxCollider boxCollider;
  16.  
  17. public MeshRenderer mesh;
  18.  
  19. public void Stop()
  20. {
  21. if (selectionLines != null)
  22. {
  23. VectorLine.Destroy(ref selectionLines);
  24. }
  25. }
  26.  
  27. public void LateUpdate()
  28. {
  29. var camera = Camera.main;
  30.  
  31. if (!ship || !ship.isSelected || boxCollider == null || camera == null || mesh == null || !mesh.isVisible)
  32. {
  33. return;
  34. }
  35.  
  36. var minX = int.MaxValue;
  37. var minY = int.MaxValue;
  38.  
  39. var maxX = int.MinValue;
  40. var maxY = int.MinValue;
  41.  
  42. boxCollider.GetPoints(corners);
  43.  
  44. Vector3 screenPosition;
  45.  
  46. for (var i = 0; i < 8; i++)
  47. {
  48. screenPosition = camera.WorldToScreenPoint(boxCollider.transform.TransformPoint(corners[i]));
  49.  
  50. minX = Math.Min(minX, (int)screenPosition.x);
  51. minY = Math.Min(minY, (int)screenPosition.y);
  52.  
  53. maxX = Math.Max(maxX, (int)screenPosition.x);
  54. maxY = Math.Max(maxY, (int)screenPosition.y);
  55. }
  56.  
  57. if (selectionLines == null)
  58. {
  59. selectionPoints = new List<Vector2>(16);
  60.  
  61. for (var i = 0; i < 16; i++)
  62. {
  63. selectionPoints.Add(Vector2.zero);
  64. }
  65.  
  66. selectionLines = new VectorLine("ShipSelection", selectionPoints, 1f, LineType.Discrete)
  67. {
  68. color = HudConstants.SelectionColor
  69. };
  70. }
  71.  
  72. var w = HudConstants.SelectionRectLines;
  73.  
  74. // Top Left
  75. selectionPoints[0] = new Vector2(minX, maxY);
  76. selectionPoints[1] = new Vector2(minX + w, maxY);
  77.  
  78. selectionPoints[2] = new Vector2(minX, maxY);
  79. selectionPoints[3] = new Vector2(minX, maxY - w);
  80.  
  81. // Bottom Left
  82. selectionPoints[4] = new Vector2(minX, minY);
  83. selectionPoints[5] = new Vector2(minX + w, minY);
  84.  
  85. selectionPoints[6] = new Vector2(minX, minY);
  86. selectionPoints[7] = new Vector2(minX, minY + w);
  87.  
  88. // Top Right
  89. selectionPoints[8] = new Vector2(maxX, maxY);
  90. selectionPoints[9] = new Vector2(maxX - w, maxY);
  91.  
  92. selectionPoints[10] = new Vector2(maxX, maxY);
  93. selectionPoints[11] = new Vector2(maxX, maxY - w);
  94.  
  95. // Bottom Left
  96. selectionPoints[12] = new Vector2(maxX, minY);
  97. selectionPoints[13] = new Vector2(maxX - w, minY);
  98.  
  99. selectionPoints[14] = new Vector2(maxX, minY);
  100. selectionPoints[15] = new Vector2(maxX, minY + w);
  101.  
  102. selectionLines.Draw();
  103. }
  104. }
  105. }
Add Comment
Please, Sign In to add comment