60 lines
2 KiB
C#
60 lines
2 KiB
C#
using System;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
|
|
[ExecuteAlways]
|
|
public class Connection : MonoBehaviour
|
|
{ public enum BuildState { EMPTY, DECONSTRUCTING, CONSTRUCTING, BUILT };
|
|
|
|
public Node nodeA, nodeB;
|
|
public bool allowed = true;
|
|
public BuildState state = BuildState.EMPTY;
|
|
public bool hovered = false;
|
|
public LineRenderer lineRenderer;
|
|
public int constructingPlayerId = -1;
|
|
|
|
public void SetConnect()
|
|
{
|
|
if (!allowed) return;
|
|
nodeA.connected.Add(nodeB);
|
|
nodeB.connected.Add(nodeA);
|
|
}
|
|
|
|
public void DelConnect()
|
|
{
|
|
nodeA.connected.Remove(nodeB);
|
|
nodeB.connected.Remove(nodeA);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
// On Click
|
|
if (Input.GetMouseButtonDown(0) && hovered && allowed)
|
|
{
|
|
GameManager.Instance.pressedCon = this;
|
|
}
|
|
if(Input.GetMouseButtonUp(0) && hovered && allowed && GameManager.Instance.pressedCon == this)
|
|
{
|
|
GameManager.Instance.PushNewAction(nodeA.id, nodeB.id, true);
|
|
}
|
|
|
|
if (Application.isPlaying && !allowed)
|
|
lineRenderer.enabled = false;
|
|
else
|
|
{
|
|
float width = (hovered ? 0.6f : 0.3f) * (allowed ? state != BuildState.BUILT ? 0.6f : 1f : 0.1f);
|
|
|
|
Color unbuiltColor = !allowed || state == BuildState.EMPTY ? new Color(0.2f, 0.2f, 0.2f) : new Color(0.8f, 0.8f, 0f);
|
|
|
|
lineRenderer.startColor = allowed && state == BuildState.BUILT ? nodeA.GetComponent<Renderer>().sharedMaterial.color : unbuiltColor;
|
|
lineRenderer.endColor = allowed && state == BuildState.BUILT ? nodeB.GetComponent<Renderer>().sharedMaterial.color : unbuiltColor;
|
|
lineRenderer.startWidth = width;
|
|
lineRenderer.endWidth = width;
|
|
|
|
lineRenderer.SetGreatCircleArc(nodeA.transform.position, nodeB.transform.position, 5, 20.5f);
|
|
lineRenderer.enabled = true;
|
|
}
|
|
|
|
hovered = false;
|
|
}
|
|
}
|