From 49ec9bd43a51de0cd1a2bfc5d558e448a36e7876 Mon Sep 17 00:00:00 2001 From: David Finol Date: Mon, 8 Jul 2024 17:38:10 -0500 Subject: [PATCH] Fix rotation/tap on client --- .../Multiplayer/CgsNetPlayable.cs | 46 +++++++++---------- .../Cgs/CardGameView/Viewer/CardActions.cs | 12 +++-- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs b/Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs index 86d91db1..32e2e041 100644 --- a/Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs +++ b/Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs @@ -74,34 +74,36 @@ public GameObject Container public Vector2 Position { - get => IsSpawned ? _positionNetworkVariable.Value : _position; + get => IsSpawned ? _positionNetworkVariable.Value : transform.localPosition; set { - _position = value; - if (!transform.localPosition.Equals(_position)) - transform.localPosition = _position; - if (IsSpawned) - _positionNetworkVariable.Value = _position; + transform.localPosition = value; + if (!IsSpawned) + return; + if (IsServer) + _positionNetworkVariable.Value = transform.localPosition; + else + RequestUpdatePosition(transform.localPosition); } } - private Vector2 _position = Vector2.zero; private NetworkVariable _positionNetworkVariable; public Quaternion Rotation { - get => IsSpawned ? _rotationNetworkVariable.Value : _rotation; + get => IsSpawned ? _rotationNetworkVariable.Value : transform.localRotation; set { - _rotation = value; - if (!transform.localRotation.Equals(_rotation)) - transform.localRotation = _rotation; - if (IsSpawned) - _rotationNetworkVariable.Value = _rotation; + transform.localRotation = value; + if (!IsSpawned) + return; + if (IsServer) + _rotationNetworkVariable.Value = transform.localRotation; + else + RequestUpdateRotation(transform.localRotation); } } - private Quaternion _rotation = Quaternion.identity; private NetworkVariable _rotationNetworkVariable; public PointerEventData CurrentPointerEventData { get; protected set; } @@ -186,14 +188,14 @@ public override void OnNetworkSpawn() if (transform.parent == null) ParentTo(Container == null ? PlayController.Instance.playAreaCardZone.transform : Container.transform); - if (_positionNetworkVariable.Value != _position && !Vector2.zero.Equals(_position)) - _positionNetworkVariable.Value = _position; + if (IsServer && !Vector2.zero.Equals(transform.localPosition)) + _positionNetworkVariable.Value = transform.localPosition; if (!Vector2.zero.Equals(Position)) transform.localPosition = Position; - if (_rotationNetworkVariable.Value != _rotation && !Quaternion.identity.Equals(_rotation)) - _rotationNetworkVariable.Value = _rotation; + if (IsServer && !Quaternion.identity.Equals(transform.localRotation)) + _rotationNetworkVariable.Value = transform.localRotation; if (!Quaternion.identity.Equals(Rotation)) transform.localRotation = Rotation; @@ -531,9 +533,7 @@ private void UpdatePositionServerRpc(Vector2 position) [PublicAPI] public void OnChangePosition(Vector2 oldValue, Vector2 newValue) { - _position = newValue; - if (!IsOwner) - transform.localPosition = newValue; + transform.localPosition = newValue; } private void RequestUpdateRotation(Quaternion rotation) @@ -550,9 +550,7 @@ private void UpdateRotationServerRpc(Quaternion rotation) [PublicAPI] public void OnChangeRotation(Quaternion oldValue, Quaternion newValue) { - _rotation = newValue; - if (!IsOwner) - transform.localRotation = newValue; + transform.localRotation = newValue; } [ServerRpc] diff --git a/Assets/Scripts/Cgs/CardGameView/Viewer/CardActions.cs b/Assets/Scripts/Cgs/CardGameView/Viewer/CardActions.cs index b0fd4d7f..b43f64c8 100644 --- a/Assets/Scripts/Cgs/CardGameView/Viewer/CardActions.cs +++ b/Assets/Scripts/Cgs/CardGameView/Viewer/CardActions.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using Cgs.CardGameView.Multiplayer; +using Cgs.Play.Multiplayer; using JetBrains.Annotations; using UnityEngine; using UnityEngine.UI; @@ -61,10 +62,13 @@ public static void Tap(CardModel cardModel) return; } - var isVertical = cardModel.transform.rotation.Equals(Quaternion.identity); - cardModel.Rotation = isVertical - ? Quaternion.AngleAxis(CardGameManager.Current.GameCardRotationDegrees, Vector3.back) - : Quaternion.identity; + var unTappedRotation = Quaternion.identity; + if (CgsNetManager.Instance != null && CgsNetManager.Instance.LocalPlayer != null) + unTappedRotation = CgsNetManager.Instance.LocalPlayer.DefaultRotation; + var isTapped = !unTappedRotation.Equals(cardModel.Rotation); + var tappedRotation = unTappedRotation * + Quaternion.Euler(0, 0, -CardGameManager.Current.GameCardRotationDegrees); + cardModel.Rotation = isTapped ? unTappedRotation : tappedRotation; } public static void Zoom(CardModel cardModel)