using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// Handles line drawing. Stores data for a line.
///
[RequireComponent (typeof(SpriteRenderer))]
public class Line : MonoBehaviour
{
///
/// Start position in world space. Calculates new line position when value changed.
///
/// The start position.
public Vector2 start {
get {
return startPosition;
}
set {
startPosition = value;
UpdatePosition ();
}
}
///
/// End position in world space. Calculates new line position when value changed.
///
/// The end position.
public Vector2 end {
get {
return endPosition;
}
set {
endPosition = value;
UpdatePosition ();
}
}
///
/// Width of line. Updates sprite renderer when value changed.
///
/// The width.
public float width {
get { return lineWidth; }
set {
lineWidth = value;
UpdateWidth ();
}
}
///
/// Line color. Updates sprite renderer color when value changed.
///
/// The color.
public Color color {
get { return lineColor; }
set {
lineColor = value;
UpdateColor ();
}
}
private Vector2 startPosition;
private Vector2 endPosition;
private float lineWidth;
private Color lineColor;
private SpriteRenderer lineRenderer;
void Awake ()
{
lineRenderer = GetComponent ();
}
///
/// Initialise the specified start, end, width and color of sprite.
///
/// Start position.
/// End position.
/// Width.
/// Color.
public void Initialise(Vector2 start, Vector2 end, float width, Color color)
{
startPosition = start;
endPosition = end;
lineWidth = width;
lineColor = color;
UpdatePosition ();
UpdateWidth ();
UpdateColor ();
}
private void UpdatePosition ()
{
var heading = endPosition - startPosition;
var distance = heading.magnitude;
var direction = heading / distance;
Vector3 centerPos = new Vector3 (startPosition.x + endPosition.x, startPosition.y + endPosition.y) / 2;
lineRenderer.transform.position = centerPos;
float angle = Mathf.Atan2 (direction.y, direction.x) * Mathf.Rad2Deg;
lineRenderer.transform.rotation = Quaternion.AngleAxis (angle, Vector3.forward);
var objectWidthSize = 10f / 5f; // 10 = pixels of line sprite, 5f = pixels per units of line sprite.
lineRenderer.transform.localScale = new Vector3 (distance / objectWidthSize, width, lineRenderer.transform.localScale.z);
}
private void UpdateWidth ()
{
lineRenderer.transform.localScale = lineRenderer.transform.localScale.WithY (lineWidth);
}
private void UpdateColor ()
{
lineRenderer.color = lineColor;
}
}