Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SpaceRTS2.Assets.Utils;
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using Vectrosity;
- namespace SpaceRTS2.Assets.Game.Ships
- {
- public sealed class SelectionHud : ShipComponent
- {
- private static readonly Vector3[] corners = new Vector3[8];
- private VectorLine selectionLines;
- private List<Vector2> selectionPoints;
- public BoxCollider boxCollider;
- public MeshRenderer mesh;
- public void Stop()
- {
- if (selectionLines != null)
- {
- VectorLine.Destroy(ref selectionLines);
- }
- }
- public void LateUpdate()
- {
- var camera = Camera.main;
- if (!ship || !ship.isSelected || boxCollider == null || camera == null || mesh == null || !mesh.isVisible)
- {
- return;
- }
- var minX = int.MaxValue;
- var minY = int.MaxValue;
- var maxX = int.MinValue;
- var maxY = int.MinValue;
- boxCollider.GetPoints(corners);
- Vector3 screenPosition;
- for (var i = 0; i < 8; i++)
- {
- screenPosition = camera.WorldToScreenPoint(boxCollider.transform.TransformPoint(corners[i]));
- minX = Math.Min(minX, (int)screenPosition.x);
- minY = Math.Min(minY, (int)screenPosition.y);
- maxX = Math.Max(maxX, (int)screenPosition.x);
- maxY = Math.Max(maxY, (int)screenPosition.y);
- }
- if (selectionLines == null)
- {
- selectionPoints = new List<Vector2>(16);
- for (var i = 0; i < 16; i++)
- {
- selectionPoints.Add(Vector2.zero);
- }
- selectionLines = new VectorLine("ShipSelection", selectionPoints, 1f, LineType.Discrete)
- {
- color = HudConstants.SelectionColor
- };
- }
- var w = HudConstants.SelectionRectLines;
- // Top Left
- selectionPoints[0] = new Vector2(minX, maxY);
- selectionPoints[1] = new Vector2(minX + w, maxY);
- selectionPoints[2] = new Vector2(minX, maxY);
- selectionPoints[3] = new Vector2(minX, maxY - w);
- // Bottom Left
- selectionPoints[4] = new Vector2(minX, minY);
- selectionPoints[5] = new Vector2(minX + w, minY);
- selectionPoints[6] = new Vector2(minX, minY);
- selectionPoints[7] = new Vector2(minX, minY + w);
- // Top Right
- selectionPoints[8] = new Vector2(maxX, maxY);
- selectionPoints[9] = new Vector2(maxX - w, maxY);
- selectionPoints[10] = new Vector2(maxX, maxY);
- selectionPoints[11] = new Vector2(maxX, maxY - w);
- // Bottom Left
- selectionPoints[12] = new Vector2(maxX, minY);
- selectionPoints[13] = new Vector2(maxX - w, minY);
- selectionPoints[14] = new Vector2(maxX, minY);
- selectionPoints[15] = new Vector2(maxX, minY + w);
- selectionLines.Draw();
- }
- }
- }
Add Comment
Please, Sign In to add comment