51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using UnityEngine;
|
|
|
|
namespace Code.Lab6
|
|
{
|
|
[System.Serializable]
|
|
public class Vertex : MonoBehaviour
|
|
{
|
|
public int id = -1;
|
|
public List<Edge> neighbours;
|
|
|
|
[Header("Weight Settings")]
|
|
[Range(0.1f, 10f)]
|
|
public float weight = 1f;
|
|
|
|
[HideInInspector]
|
|
public Vertex prev;
|
|
|
|
private void Awake()
|
|
{
|
|
neighbours ??= new List<Edge>();
|
|
}
|
|
|
|
private void OnDrawGizmos()
|
|
{
|
|
var t = Mathf.InverseLerp(0.1f, 10f, weight);
|
|
Gizmos.color = Color.Lerp(Color.white, Color.black, t);
|
|
Gizmos.DrawSphere(transform.position, 0.2f + weight * 0.05f);
|
|
}
|
|
|
|
private void OnDrawGizmosSelected()
|
|
{
|
|
if (neighbours == null) return;
|
|
|
|
foreach (var edge in neighbours.Where(edge => edge?.Vertex != null))
|
|
{
|
|
var t = Mathf.InverseLerp(0.1f, 10f, edge.cost);
|
|
Gizmos.color = Color.Lerp(Color.green, Color.red, t);
|
|
Gizmos.DrawLine(transform.position, edge.Vertex.transform.position);
|
|
|
|
#if UNITY_EDITOR
|
|
var midPoint = (transform.position + edge.Vertex.transform.position) / 2f;
|
|
midPoint.y += 0.5f;
|
|
UnityEditor.Handles.color = Color.yellow;
|
|
UnityEditor.Handles.Label(midPoint, $"{edge.cost:F1}");
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
} |