Skip to content

Commit

Permalink
Fix rotation/tap on client
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Jul 8, 2024
1 parent cc7ff5c commit 49ec9bd
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
46 changes: 22 additions & 24 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vector2> _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<Quaternion> _rotationNetworkVariable;

public PointerEventData CurrentPointerEventData { get; protected set; }
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down
12 changes: 8 additions & 4 deletions Assets/Scripts/Cgs/CardGameView/Viewer/CardActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Collections.Generic;
using Cgs.CardGameView.Multiplayer;
using Cgs.Play.Multiplayer;
using JetBrains.Annotations;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 49ec9bd

Please sign in to comment.