You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
5.0 KiB
C#
141 lines
5.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using Unity.Netcode;
|
|
|
|
public class CrowsForesightPrefab : NetworkBehaviour
|
|
{
|
|
private Dictionary<ulong, LineRenderer> foresightLines = new Dictionary<ulong, LineRenderer>();
|
|
private ulong crowClientId;
|
|
private Coroutine destroyCoroutine;
|
|
public GameObject LinePrefab;
|
|
public void Initialize(ulong crowId, CrowsForesightAbility ability)
|
|
{
|
|
crowClientId = crowId;
|
|
if (IsServer)
|
|
{
|
|
Debug.Log("[CrowsForesightPrefab] Starting auto-destroy coroutine.");
|
|
StartCoroutine(DelayedDestroy(ability.abilityDuration));
|
|
}
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
Debug.Log($"[CrowsForesightPrefab] OnNetworkSpawn called - LocalClientId: {NetworkManager.Singleton.LocalClientId}, crowClientId: {crowClientId}");
|
|
if (NetworkManager.Singleton.LocalClientId != CrowManager.Instance.GetCurrentCrow().OwnerClientId && !NetworkManager.Singleton.IsServer)
|
|
{
|
|
gameObject.SetActive(false); // Hide from non-crow clients
|
|
}
|
|
}
|
|
|
|
private IEnumerator DelayedDestroy(float time = 10)
|
|
{
|
|
yield return new WaitForSeconds(time); // Adjust according to abilityDuration
|
|
|
|
if (IsServer)
|
|
{
|
|
Debug.Log("[CrowsForesightPrefab] Ability duration expired. Despawning on server.");
|
|
DespawnForesight();
|
|
}
|
|
}
|
|
|
|
public void DespawnForesight()
|
|
{
|
|
if (!IsServer) return;
|
|
|
|
Debug.Log("[CrowsForesightPrefab] Destroying foresight effect and clearing all lines.");
|
|
|
|
// Remove all foresight lines before despawning
|
|
foreach (var line in foresightLines.Values)
|
|
{
|
|
if (line != null)
|
|
{
|
|
Destroy(line.gameObject);
|
|
}
|
|
}
|
|
foresightLines.Clear();
|
|
|
|
if (NetworkObject.IsSpawned)
|
|
{
|
|
Debug.Log("[CrowsForesightPrefab] Despawning network object.");
|
|
NetworkObject.Despawn(true);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("[CrowsForesightPrefab] WARNING: Tried to despawn, but NetworkObject was already despawned or missing!");
|
|
}
|
|
}
|
|
|
|
|
|
public void OnSwapRequested(ulong senderId, ulong receiverId, Vector3 senderPos, Vector3 receiverPos)
|
|
{
|
|
Debug.Log($"[CrowsForesightPrefab] OnSwapRequested - Sender: {senderId}, Receiver: {receiverId}, SenderPos: {senderPos}, ReceiverPos: {receiverPos}");
|
|
|
|
if (!foresightLines.ContainsKey(senderId))
|
|
{
|
|
Debug.Log($"[CrowsForesightPrefab] Creating foresight line for swap request {senderId} -> {receiverId}");
|
|
foresightLines[senderId] = CreateLine();
|
|
}
|
|
|
|
foresightLines[senderId].SetPosition(0, senderPos);
|
|
foresightLines[senderId].SetPosition(1, Vector3.Lerp(senderPos, receiverPos, 0.5f));
|
|
|
|
Debug.Log($"[CrowsForesightPrefab] Halfway foresight line drawn from {senderPos} to midpoint.");
|
|
}
|
|
|
|
public void OnSwapAccepted(ulong senderId, Vector3 receiverPos)
|
|
{
|
|
Debug.Log($"[CrowsForesightPrefab] OnSwapAccepted - Sender: {senderId}, ReceiverPos: {receiverPos}");
|
|
|
|
if (!foresightLines.ContainsKey(senderId))
|
|
{
|
|
Debug.LogError($"[CrowsForesightPrefab] ERROR: No foresight line found for sender {senderId} when swap was accepted!");
|
|
return;
|
|
}
|
|
|
|
foresightLines[senderId].SetPosition(1, receiverPos);
|
|
if (foresightLines.ContainsKey(senderId))
|
|
{
|
|
// Destroy the line associated with the sender
|
|
Destroy(foresightLines[senderId].gameObject ,1f);
|
|
foresightLines.Remove(senderId);
|
|
|
|
Debug.Log($"[CrowsForesightPrefab] Removed foresight line for rejected swap request from {senderId}.");
|
|
}
|
|
Debug.Log($"[CrowsForesightPrefab] Foresight line completed from sender {senderId} to {receiverPos}.");
|
|
}
|
|
|
|
|
|
public void OnSwapRejected(ulong senderId, Vector3 receiverPos)
|
|
{
|
|
Debug.Log($"[CrowsForesightPrefab] OnSwapRejected - Sender: {senderId}, ReceiverPos: {receiverPos}");
|
|
|
|
if (foresightLines.ContainsKey(senderId))
|
|
{
|
|
// Destroy the line associated with the sender
|
|
Destroy(foresightLines[senderId].gameObject);
|
|
foresightLines.Remove(senderId);
|
|
|
|
Debug.Log($"[CrowsForesightPrefab] Removed foresight line for rejected swap request from {senderId}.");
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"[CrowsForesightPrefab] WARNING: No foresight line found for sender {senderId}. Swap rejection may have happened before drawing.");
|
|
}
|
|
}
|
|
|
|
|
|
private LineRenderer CreateLine()
|
|
{
|
|
//GameObject lineObj = new GameObject("ForesightLine");
|
|
GameObject lineObj = Instantiate(LinePrefab);
|
|
LineRenderer line = lineObj.GetComponent<LineRenderer>();
|
|
line.startWidth = 1f;
|
|
line.endWidth = 1f;
|
|
//line.material = new Material(Shader.Find("Sprites/Default"));
|
|
line.transform.SetParent(transform);
|
|
return line;
|
|
}
|
|
}
|