Files
aibis-dream/Assets/Scripts/FixSystem/Line/InteractiveLineSegment.cs
T

89 lines
2.3 KiB
C#

using Unity.VisualScripting;
using UnityEngine;
using AibisDream;
public class InteractiveLineSegment : VisualLineSegment
{
[SerializeField]
private Transform startTransform;
public Transform StartTransform
{
get { return startTransform; }
set { startTransform = value; }
}
[SerializeField]
private Transform endTransform;
public Transform EndTransform
{
get { return endTransform; }
set { endTransform = value; }
}
private BoxCollider2D boxCollider;
private YarnOption _option;
public YarnOption Option
{
get => _option;
set => _option = value;
}
public bool IsCuttable
{
get { return _isCuttable; }
set
{
_isCuttable = value;
// Change the color of the line segment based on its cuttability
GetComponent<LineRenderer>().material.color = value ? Color.white : Color.black;
}
}
private bool _isCuttable=false;
protected override void Awake()
{
base.Awake();
boxCollider = GetComponent<BoxCollider2D>();
}
public void Start()
{
Initialize();
}
public void Initialize()
{
if (StartTransform == null || EndTransform == null)
{
Debug.LogError("StartTransform or EndTransform is not set.");
return;
}
IsCuttable=false;
base.Initialize(StartTransform.position, EndTransform.position);
// Calculate the length of the line segment
float length = Vector3.Distance(StartPoint, EndPoint);
// Calculate the direction of the line segment
Vector3 direction = (EndPoint - StartPoint).normalized;
// Set the size of the BoxCollider2D
boxCollider.size = new Vector2(length, 0.6f); // Change 0.1f to the desired thickness of the line
// Set the center of the BoxCollider2D
boxCollider.offset = new Vector2(length / 2, 0);
// Rotate the BoxCollider2D to align with the line segment
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
boxCollider.transform.rotation = Quaternion.Euler(0, 0, angle);
// Set the position of the BoxCollider2D to the start point
boxCollider.transform.position = StartPoint;
}
}