5Y5T3M/Assets/Scripts/Node.cs

69 lines
1.7 KiB
C#
Raw Normal View History

2025-09-15 23:01:07 +02:00
using UnityEngine;
2025-09-17 17:05:48 +02:00
using System.Collections.Generic;
2025-09-15 23:01:07 +02:00
2025-09-16 00:08:50 +02:00
[ExecuteAlways]
2025-09-15 23:01:07 +02:00
public class Node : MonoBehaviour
{
2025-09-16 14:20:19 +02:00
[Range(-1, 1)]
[SerializeField]
2025-09-17 10:12:15 +02:00
public int Owner = -1;
public Material materialOwnerSelf, materialOwnerOther, materialOwnerNone;
void Awake()
{
UpdateColor();
}
2025-09-16 14:20:19 +02:00
2025-09-15 23:01:07 +02:00
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
2025-09-15 23:01:07 +02:00
}
// Update is called once per frame
void Update()
{
UpdateTransform();
}
private void OnValidate()
{
UpdateColor();
UpdateTransform();
}
2025-09-17 17:05:48 +02:00
private void OnDestroy()
{
GameManager gm = FindFirstObjectByType<GameManager>();
List<GameManager.Connection> looseConnections = gm.connections.FindAll(c => c.nodeA == this || c.nodeB == this);
foreach(GameManager.Connection c in looseConnections)
{
DestroyImmediate(c.lineRenderer.gameObject);
gm.connections.Remove(c);
}
gm.FetchAllNodes();
}
public void UpdateTransform()
{
transform.localPosition = transform.localPosition.normalized * 20f;
2025-09-17 17:05:48 +02:00
if(transform.position != Vector3.zero)
transform.forward = transform.position;
}
public void UpdateColor()
{
2025-09-16 14:20:19 +02:00
switch (Owner)
{
case -1: transform.GetChild(0).GetComponent<Renderer>().sharedMaterial = materialOwnerNone; break;
case 0: transform.GetChild(0).GetComponent<Renderer>().sharedMaterial = materialOwnerSelf; break;
case 1: transform.GetChild(0).GetComponent<Renderer>().sharedMaterial = materialOwnerOther; break;
}
}
2025-09-16 17:40:34 +02:00
2025-09-15 23:01:07 +02:00
}