145 lines
3.9 KiB
C#
145 lines
3.9 KiB
C#
using Fusion;
|
|
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using BNG;
|
|
|
|
public class NetworkMarkerEraser : NetworkBehaviour
|
|
{
|
|
//public float EraseRadius = 0.1f; // شعاع پاک کردن خطوط
|
|
public Transform EraserTransform; // ترانسفورم تخته پاک کن
|
|
// public List<LineRenderer> allLines = new List<LineRenderer>();
|
|
public static NetworkMarkerEraser instance;
|
|
|
|
[Networked]
|
|
public NetworkBool isDrawing { get; private set; } // مقدار همگامسازی شده
|
|
|
|
private void Update()
|
|
{
|
|
if (isDrawing)
|
|
{
|
|
// EraseNearbyLines();
|
|
}
|
|
if(Object.HasInputAuthority)
|
|
RPC_SetDrawingState(isDrawing);
|
|
}
|
|
|
|
public void Awake()
|
|
{
|
|
instance = this;
|
|
EraserTransform = transform;
|
|
/* GameObject[] gos = GameObject.FindGameObjectsWithTag("draw");
|
|
|
|
allLines = new List<LineRenderer>();
|
|
|
|
foreach (GameObject go in gos)
|
|
{
|
|
LineRenderer lr = go.GetComponent<LineRenderer>();
|
|
if (lr != null)
|
|
{
|
|
allLines.Add(lr);
|
|
}
|
|
}*/
|
|
|
|
}
|
|
|
|
public void StartDrawing()
|
|
{
|
|
RPC_SetDrawingState(true);
|
|
|
|
}
|
|
|
|
public void StopDrawing()
|
|
{
|
|
RPC_SetDrawingState(false);
|
|
}
|
|
|
|
private void SetDrawingState(bool state)
|
|
{
|
|
isDrawing = state;
|
|
Debug.Log($"isDrawing changed to: {isDrawing}");
|
|
}
|
|
|
|
[Rpc(RpcSources.All, RpcTargets.All)]
|
|
public void RPC_SetDrawingState(bool state)
|
|
{
|
|
SetDrawingState(state);
|
|
}
|
|
/*
|
|
private void EraseNearbyLines()
|
|
{
|
|
for (int lineIndex = allLines.Count - 1; lineIndex >= 0; lineIndex--)
|
|
{
|
|
LineRenderer line = allLines[lineIndex];
|
|
if (line == null) continue;
|
|
|
|
List<Vector3> segment = new List<Vector3>();
|
|
List<List<Vector3>> segments = new List<List<Vector3>>();
|
|
bool isErasing = false;
|
|
|
|
for (int i = 0; i < line.positionCount; i++)
|
|
{
|
|
Vector3 point = line.GetPosition(i);
|
|
if (Vector3.Distance(EraserTransform.position, point) > EraseRadius)
|
|
{
|
|
if (isErasing)
|
|
{
|
|
if (segment.Count > 0)
|
|
{
|
|
segments.Add(new List<Vector3>(segment));
|
|
segment.Clear();
|
|
}
|
|
isErasing = false;
|
|
}
|
|
segment.Add(point);
|
|
}
|
|
else
|
|
{
|
|
isErasing = true;
|
|
if (segment.Count > 0)
|
|
{
|
|
segments.Add(new List<Vector3>(segment));
|
|
segment.Clear();
|
|
}
|
|
}
|
|
}
|
|
if (segment.Count > 0)
|
|
segments.Add(segment);
|
|
|
|
allLines.RemoveAt(lineIndex);
|
|
Destroy(line.gameObject);
|
|
|
|
foreach (var seg in segments)
|
|
{
|
|
if (seg.Count > 1)
|
|
{
|
|
CreateNewLine(seg, line);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
/*
|
|
private void CreateNewLine(List<Vector3> points, LineRenderer line)
|
|
{
|
|
GameObject newLineObj = new GameObject("SplitLine");
|
|
LineRenderer newLine = newLineObj.AddComponent<LineRenderer>();
|
|
newLine.tag = "draw";
|
|
newLine.positionCount = points.Count;
|
|
newLine.material = line.material;
|
|
newLine.startColor = line.startColor;
|
|
newLine.endColor = line.endColor;
|
|
newLine.SetPositions(points.ToArray());
|
|
newLine.startWidth = line.startWidth;
|
|
newLine.endWidth = line.endWidth;
|
|
allLines.Add(newLine);
|
|
}
|
|
|
|
public void RegisterLine(LineRenderer line)
|
|
{
|
|
if (!allLines.Contains(line))
|
|
{
|
|
allLines.Add(line);
|
|
}
|
|
}*/
|
|
}
|