From 9bd2cdefc6e1a8f8315641a74852404595ca7b1a Mon Sep 17 00:00:00 2001 From: Ali Sharoz Date: Thu, 20 Feb 2025 20:49:39 +0500 Subject: [PATCH] SwapLines --- Assets/Prefabs/Character/PlayerAvatar.prefab | 8 +- Assets/Prefabs/SwapLineRenderer.prefab | 186 ++++++++++++++++++ Assets/Prefabs/SwapLineRenderer.prefab.meta | 7 + .../Character/LineColliderCapsules.cs | 62 ++++++ .../Character/LineColliderCapsules.cs.meta | 11 ++ .../Character/LineTouchDetector.cs | 89 +++++++++ .../Character/LineTouchDetector.cs.meta | 11 ++ .../Character/ScrollingLineRenderer.cs | 31 +++ .../Character/ScrollingLineRenderer.cs.meta | 11 ++ .../Character/ServerCharacter.cs | 110 +++++++++-- Assets/Scripts/ScrollingLineRenderer.cs | 10 +- Assets/Scripts/ScrollingLineRenderer.cs.meta | 2 +- 12 files changed, 514 insertions(+), 24 deletions(-) create mode 100644 Assets/Prefabs/SwapLineRenderer.prefab create mode 100644 Assets/Prefabs/SwapLineRenderer.prefab.meta create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs.meta create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs.meta create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs create mode 100644 Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs.meta diff --git a/Assets/Prefabs/Character/PlayerAvatar.prefab b/Assets/Prefabs/Character/PlayerAvatar.prefab index f96bda3..172f272 100644 --- a/Assets/Prefabs/Character/PlayerAvatar.prefab +++ b/Assets/Prefabs/Character/PlayerAvatar.prefab @@ -600,7 +600,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 5109095020609682098, guid: d3e4ffd5da62ee94e9eccf3ab52ec129, type: 3} propertyPath: GlobalObjectIdHash - value: 3177087589 + value: 3937158934 objectReference: {fileID: 0} - target: {fileID: 5935320730402577130, guid: d3e4ffd5da62ee94e9eccf3ab52ec129, type: 3} propertyPath: m_Name @@ -782,7 +782,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 4600110157238723790, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} propertyPath: GlobalObjectIdHash - value: 3177087589 + value: 3737985212 objectReference: {fileID: 0} - target: {fileID: 4600110157238723790, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} propertyPath: PrefabHashGenerator @@ -840,6 +840,10 @@ PrefabInstance: propertyPath: crowModel value: objectReference: {fileID: 7710689235683559329} + - target: {fileID: 7420593339233078707, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} + propertyPath: LinePrefab + value: + objectReference: {fileID: 7889671876836792639, guid: de92a155449a1dc4aa295b73aae3b557, type: 3} - target: {fileID: 7420593339233078707, guid: 0d2d836e2e83b754fa1a1c4022d6d65d, type: 3} propertyPath: m_ClientCharacter value: diff --git a/Assets/Prefabs/SwapLineRenderer.prefab b/Assets/Prefabs/SwapLineRenderer.prefab new file mode 100644 index 0000000..cee831a --- /dev/null +++ b/Assets/Prefabs/SwapLineRenderer.prefab @@ -0,0 +1,186 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &7889671876836792639 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 2190496012411949397} + - component: {fileID: 9057376229307534297} + - component: {fileID: 7429976590137184792} + - component: {fileID: 2673016220022461625} + - component: {fileID: 3158508003152714365} + m_Layer: 0 + m_Name: SwapLineRenderer + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &2190496012411949397 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7889671876836792639} + serializedVersion: 2 + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!120 &9057376229307534297 +LineRenderer: + serializedVersion: 2 + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7889671876836792639} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 0 + m_LightProbeUsage: 0 + m_ReflectionProbeUsage: 0 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 77e5ea3a019674146a9f2b3ed629b386, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Positions: + - {x: 0, y: 0, z: 0} + - {x: 0, y: 0, z: 1} + m_Parameters: + serializedVersion: 3 + widthMultiplier: 1 + widthCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + colorGradient: + serializedVersion: 2 + key0: {r: 1, g: 1, b: 1, a: 1} + key1: {r: 1, g: 1, b: 1, a: 1} + key2: {r: 0, g: 0, b: 0, a: 0} + key3: {r: 0, g: 0, b: 0, a: 0} + key4: {r: 0, g: 0, b: 0, a: 0} + key5: {r: 0, g: 0, b: 0, a: 0} + key6: {r: 0, g: 0, b: 0, a: 0} + key7: {r: 0, g: 0, b: 0, a: 0} + ctime0: 0 + ctime1: 65535 + ctime2: 0 + ctime3: 0 + ctime4: 0 + ctime5: 0 + ctime6: 0 + ctime7: 0 + atime0: 0 + atime1: 65535 + atime2: 0 + atime3: 0 + atime4: 0 + atime5: 0 + atime6: 0 + atime7: 0 + m_Mode: 0 + m_ColorSpace: -1 + m_NumColorKeys: 2 + m_NumAlphaKeys: 2 + numCornerVertices: 0 + numCapVertices: 0 + alignment: 0 + textureMode: 1 + textureScale: {x: 1, y: 1} + shadowBias: 0.5 + generateLightingData: 0 + m_MaskInteraction: 0 + m_UseWorldSpace: 1 + m_Loop: 0 + m_ApplyActiveColorSpace: 1 +--- !u!114 &7429976590137184792 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7889671876836792639} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1e6412647fb95594eb08353a0c8edd79, type: 3} + m_Name: + m_EditorClassIdentifier: + lineRenderer: {fileID: 9057376229307534297} + scrollingMaterial: {fileID: 2100000, guid: 77e5ea3a019674146a9f2b3ed629b386, type: 2} + speed: 1 + tiling: 0.5 + serverCharacter: {fileID: 0} +--- !u!114 &2673016220022461625 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7889671876836792639} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 275c684c245283442a60cb83984b7474, type: 3} + m_Name: + m_EditorClassIdentifier: + lineRenderer: {fileID: 9057376229307534297} + colliderRadius: 0.1 +--- !u!114 &3158508003152714365 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7889671876836792639} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: ae3b814bce97b534eb12f8d764c2be7e, type: 3} + m_Name: + m_EditorClassIdentifier: + lineRenderer: {fileID: 9057376229307534297} + touchRadius: 1 diff --git a/Assets/Prefabs/SwapLineRenderer.prefab.meta b/Assets/Prefabs/SwapLineRenderer.prefab.meta new file mode 100644 index 0000000..e7bbdb1 --- /dev/null +++ b/Assets/Prefabs/SwapLineRenderer.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: de92a155449a1dc4aa295b73aae3b557 +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs new file mode 100644 index 0000000..3c56ce2 --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs @@ -0,0 +1,62 @@ +using UnityEngine; +using System.Collections.Generic; + +public class LineColliderCapsules : MonoBehaviour +{ + public LineRenderer lineRenderer; + public float colliderRadius = 0.1f; // Adjust based on line width + + private List colliders = new List(); + + private void Start() + { + GenerateColliders(); + } + + private void Update() + { + UpdateColliders(); + } + + private void GenerateColliders() + { + // Destroy old colliders + foreach (var col in colliders) + { + Destroy(col); + } + colliders.Clear(); + + if (lineRenderer.positionCount < 2) return; + + for (int i = 0; i < lineRenderer.positionCount - 1; i++) + { + GameObject colObj = new GameObject("LineSegmentCollider"); + colObj.transform.parent = transform; + + CapsuleCollider capsule = colObj.AddComponent(); + capsule.radius = colliderRadius; + capsule.height = Vector3.Distance(lineRenderer.GetPosition(i), lineRenderer.GetPosition(i + 1)); + capsule.direction = 2; // Z-axis + + colliders.Add(colObj); + } + + UpdateColliders(); + } + + private void UpdateColliders() + { + if (lineRenderer.positionCount < 2) return; + + for (int i = 0; i < colliders.Count; i++) + { + Vector3 start = lineRenderer.GetPosition(i); + Vector3 end = lineRenderer.GetPosition(i + 1); + Vector3 midPoint = (start + end) / 2; + + colliders[i].transform.position = midPoint; + colliders[i].transform.LookAt(end); + } + } +} diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs.meta b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs.meta new file mode 100644 index 0000000..f9f080f --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineColliderCapsules.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 275c684c245283442a60cb83984b7474 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs new file mode 100644 index 0000000..8bef97d --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs @@ -0,0 +1,89 @@ +using UnityEngine; + +public class LineTouchDetector : MonoBehaviour +{ + public LineRenderer lineRenderer; + public float touchRadius = 0.5f; // Max distance to detect a touch + + private ScrollingLineRenderer scrollingLineRenderer; + + private void Start() + { + scrollingLineRenderer = GetComponentInParent(); + } + + private void Update() + { + DetectTouchOrClick(); + } + + private void DetectTouchOrClick() + { + Vector3 touchPos; + bool touched = false; + + // Detect touch on mobile + if (Input.touchCount > 0) + { + Touch touch = Input.GetTouch(0); + touchPos = touch.position; + touched = true; + } + // Detect mouse click on PC + else if (Input.GetMouseButtonDown(0)) + { + touchPos = Input.mousePosition; + touched = true; + } + else + { + return; // No input detected + } + + // Convert screen position to world position + Ray ray = Camera.main.ScreenPointToRay(touchPos); + if (Physics.Raycast(ray, out RaycastHit hit, 100f)) + { + // Check if the touch is close to the LineRenderer + if (IsTouchNearLine(hit.point)) + { + Debug.Log("[LineTouchDetector] Line touched! Sending decision to server..."); + scrollingLineRenderer.SendDecisionToServer(); + } + } + } + + private bool IsTouchNearLine(Vector3 touchPoint) + { + float minDistance = float.MaxValue; + Vector3[] positions = new Vector3[lineRenderer.positionCount]; + lineRenderer.GetPositions(positions); + + for (int i = 0; i < positions.Length - 1; i++) + { + float dist = DistanceFromPointToLineSegment(touchPoint, positions[i], positions[i + 1]); + if (dist < minDistance) + { + minDistance = dist; + } + } + + return minDistance <= touchRadius; + } + + private float DistanceFromPointToLineSegment(Vector3 point, Vector3 lineStart, Vector3 lineEnd) + { + Vector3 closestPoint = ClosestPointOnLineSegment(point, lineStart, lineEnd); + return Vector3.Distance(point, closestPoint); + } + + private Vector3 ClosestPointOnLineSegment(Vector3 point, Vector3 a, Vector3 b) + { + Vector3 ap = point - a; + Vector3 ab = b - a; + float ab2 = ab.sqrMagnitude; + float ap_ab = Vector3.Dot(ap, ab); + float t = Mathf.Clamp01(ap_ab / ab2); + return a + ab * t; + } +} diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs.meta b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs.meta new file mode 100644 index 0000000..4ea979f --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/LineTouchDetector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ae3b814bce97b534eb12f8d764c2be7e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs new file mode 100644 index 0000000..3e55058 --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs @@ -0,0 +1,31 @@ +using Unity.BossRoom.Gameplay.GameplayObjects.Character; +using UnityEngine; + +public class ScrollingLineRenderer : MonoBehaviour +{ + public LineRenderer lineRenderer; + public Material scrollingMaterial; + public float speed = 1.0f; // Adjust speed in Inspector + public float tiling = 1.0f; // Controls how many arrows fit in the line + public ServerCharacter serverCharacter; + private void Start() + { + lineRenderer.material = scrollingMaterial; + } + void Update() + { + if (scrollingMaterial == null) return; + + // Scroll texture over time + float offset = Time.time * speed; + scrollingMaterial.SetFloat("_Speed", speed); + scrollingMaterial.SetFloat("_Tiling", -tiling); + } + public void SendDecisionToServer() + { + if (serverCharacter != null) + { + serverCharacter.NotifySwapDecisionRpc(true); + } + } +} diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs.meta b/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs.meta new file mode 100644 index 0000000..05b8c4e --- /dev/null +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ScrollingLineRenderer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1e6412647fb95594eb08353a0c8edd79 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs index cc76d7d..2703cd7 100644 --- a/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs +++ b/Assets/Scripts/Gameplay/GameplayObjects/Character/ServerCharacter.cs @@ -142,7 +142,8 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character private AIBrain m_AIBrain; NetworkAvatarGuidState m_State; - public ulong? PendingSwapRequest { get; set; } + //public ulong? PendingSwapRequest { get; set; } + public Dictionary PendingSwapRequests = new(); public int? TargetPlatformId { get; private set; } = null; public int? CurrentPlatformId { get; private set; } = null; public int? PreviousPlatformId { get; private set; } = null; @@ -165,7 +166,6 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character m_State = GetComponent(); uIStateDisplayHandler = GetComponent(); heroActionBar = FindObjectOfType(); - abilitySystem = GetComponent(); } //Hazim @@ -402,17 +402,6 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character IsSwapping = true; ServerSendCharacterInputRpc(pltpos); // Send the movement request } - - //private void MoveToPlatform(Platform platform) - //{ - // SetTargetPlatform(platform.PlatformID); - // var pltpos = PlatformManager.Instance.GetPlatformPosition(platform.PlatformID); - // pltpos.y = 0; - // Debug.Log($"Platform position: {pltpos}."); - // IsSwapping = true; - // ServerSendCharacterInputRpc(pltpos); - //} - private void BecomeCrow() { ClearTargetPlatform(); @@ -420,13 +409,14 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character // Add additional logic for Crow role if needed } - - - AbilitySystem abilitySystem; [Rpc(SendTo.Server, RequireOwnership = false)] public void NotifySwapRequestRpc(ulong senderId, string senderName) { - PendingSwapRequest = senderId; + //PendingSwapRequest = senderId; + if (!PendingSwapRequests.ContainsKey(senderId)) + { + PendingSwapRequests[senderId] = OwnerClientId; // Store sender → receiver + } //ShowSwapConfirmationPanelClientRpc(senderName); if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(senderId, out var senderObj) && @@ -434,32 +424,112 @@ namespace Unity.BossRoom.Gameplay.GameplayObjects.Character { Vector3 senderPos = senderChar.transform.position; Vector3 receiverPos = transform.position; + OnSwapRequested(senderId,OwnerClientId,senderPos,receiverPos); CrowManager.Instance.NotifyForesightSwap(senderChar.OwnerClientId, OwnerClientId, senderPos, receiverPos); } } + private LineRenderer CreateLine() + { + //GameObject lineObj = new GameObject("ForesightLine"); + GameObject lineObj = Instantiate(LinePrefab); + ScrollingLineRenderer scrollingLineRenderer = lineObj.GetComponent(); + scrollingLineRenderer.serverCharacter = this; + LineRenderer line = lineObj.GetComponent(); + line.startWidth = 1f; + line.endWidth = 1f; + //line.material = new Material(Shader.Find("Sprites/Default")); + line.transform.SetParent(transform); + return line; + } + //public void OnSwapRequested(ulong senderId, ulong receiverId, Vector3 senderPos, Vector3 receiverPos) + //{ + // Debug.Log($"[SERVER] Sending SwapRequest RPC to: Sender({senderId}), Receiver({receiverId})"); + // ClientRpcParams clientRpcParams = new ClientRpcParams + // { + // Send = new ClientRpcSendParams + // { + // TargetClientIds = new ulong[] { receiverId } + // } + // }; + // OnSwapRequestedClientRpc(senderId, receiverId, senderPos, receiverPos, clientRpcParams); + //} + public void OnSwapRequested(ulong senderId, ulong receiverId, Vector3 senderPos, Vector3 receiverPos) + { + Debug.Log($"[SERVER] Sending SwapRequest RPC to: Sender({senderId}), Receiver({receiverId})"); + + ClientRpcParams clientRpcParams = new ClientRpcParams + { + Send = new ClientRpcSendParams + { + TargetClientIds = new ulong[] { receiverId } // Send to both + } + }; + + OnSwapRequestedClientRpc(senderId, receiverId, senderPos, receiverPos, clientRpcParams); + } + [ClientRpc] + void OnSwapRequestedClientRpc(ulong senderId, ulong receiverId, Vector3 senderPos, Vector3 receiverPos, ClientRpcParams clientRpcParams = default) + { + ulong localClientId = NetworkManager.Singleton.LocalClientId; + + if (localClientId == receiverId) // Only sender & receiver process it + { + if (!m_SwapLines.ContainsKey(senderId)) // If the line doesn’t exist, create one + { + Debug.Log($"[CLIENT {localClientId}] Creating Swap Line for request {senderId} -> {receiverId}"); + m_SwapLines[senderId] = CreateLine(); + } + // Update the line positions + m_SwapLines[senderId].SetPosition(0, senderPos); + m_SwapLines[senderId].SetPosition(1, Vector3.Lerp(senderPos, receiverPos, 0.5f)); + Debug.Log($"[CLIENT {localClientId}] Swap line drawn from {senderPos} to {receiverPos}"); + } + } + private Dictionary m_SwapLines = new(); + public GameObject LinePrefab; [Rpc(SendTo.Server, RequireOwnership = false)] + public void NotifySwapDecisionRpc(bool isAccepted) { - if (!PendingSwapRequest.HasValue) return; + if (PendingSwapRequests.Count == 0) return; - ulong initiatingPlayerId = PendingSwapRequest.Value; + ulong initiatingPlayerId = 0; + foreach (var entry in PendingSwapRequests) // Find a valid swap request + { + initiatingPlayerId = entry.Key; + break; + } + + if (!PendingSwapRequests.ContainsKey(initiatingPlayerId)) return; if (NetworkManager.Singleton.SpawnManager.SpawnedObjects.TryGetValue(initiatingPlayerId, out var initiatingPlayerObj) && initiatingPlayerObj.TryGetComponent(out ServerCharacter initiatingPlayer)) { Vector3 receiverPos = transform.position; CrowManager.Instance.NotifyForesightSwapDecision(initiatingPlayerObj.OwnerClientId, receiverPos, isAccepted); + if (isAccepted) { InitiateSwap(initiatingPlayer, this); Debug.Log($"Swap confirmed: {initiatingPlayer.name} and {this.name} are swapping."); + } + } + + // Remove all pending swap requests + PendingSwapRequests.Clear(); + // Destroy all swap lines + foreach (var line in m_SwapLines.Values) + { + if (line != null) + { + Destroy(line.gameObject); } } - PendingSwapRequest = null; + m_SwapLines.Clear(); // Clear the dictionary after destroying lines } diff --git a/Assets/Scripts/ScrollingLineRenderer.cs b/Assets/Scripts/ScrollingLineRenderer.cs index 0a663e5..4b25f90 100644 --- a/Assets/Scripts/ScrollingLineRenderer.cs +++ b/Assets/Scripts/ScrollingLineRenderer.cs @@ -1,3 +1,4 @@ +using Unity.BossRoom.Gameplay.GameplayObjects.Character; using UnityEngine; public class ScrollingLineRenderer : MonoBehaviour @@ -6,7 +7,7 @@ public class ScrollingLineRenderer : MonoBehaviour public Material scrollingMaterial; public float speed = 1.0f; // Adjust speed in Inspector public float tiling = 1.0f; // Controls how many arrows fit in the line - + public ServerCharacter serverCharacter; private void Start() { lineRenderer.material = scrollingMaterial; @@ -20,4 +21,11 @@ public class ScrollingLineRenderer : MonoBehaviour scrollingMaterial.SetFloat("_Speed", speed); scrollingMaterial.SetFloat("_Tiling", -tiling); } + private void SendDecisionToServer() + { + if (serverCharacter != null) + { + serverCharacter.NotifySwapDecisionRpc(true); + } + } } diff --git a/Assets/Scripts/ScrollingLineRenderer.cs.meta b/Assets/Scripts/ScrollingLineRenderer.cs.meta index 05b8c4e..9ca684a 100644 --- a/Assets/Scripts/ScrollingLineRenderer.cs.meta +++ b/Assets/Scripts/ScrollingLineRenderer.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 1e6412647fb95594eb08353a0c8edd79 +guid: 2ecd4fc2f2d4cee4a8fb4bf4d0a45ad0 MonoImporter: externalObjects: {} serializedVersion: 2