Skip to content

Commit

Permalink
Bug-Fix: Cards Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Dec 2, 2024
1 parent 2676d3c commit 5cf3ffe
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 55 deletions.
87 changes: 48 additions & 39 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CardModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Cgs.CardGameView.Multiplayer
public class CardModel : CgsNetPlayable, ICardDisplay, ICardDropHandler, IStackDropHandler
{
public const string DropErrorMessage = "Error: Card dropped on Card outside of play area!";
public string DiscardPrompt => $"Delete cannot be undone. Delete {gameObject.name}?";
public override string DeletePrompt => $"Delete cannot be undone. Delete {gameObject.name}?";

private const float ZoomHoldTime = 0.5f;
private const float MovementSpeed = 600f;
Expand Down Expand Up @@ -243,8 +243,18 @@ protected override void OnUpdatePlayable()
if (Inputs.IsOption && CardViewer.Instance.PreviewCardModel == this || HoldTime > ZoomHoldTime)
RequestZoomOnThis();

if (CurrentDragPhase == DragPhase.End && !IsProcessingSecondaryDragAction && PlaceHolder != null)
if (ParentCardZone == null && CurrentDragPhase == DragPhase.End && !IsMovingToPlaceHolder)
{
Debug.LogWarning(
"OnUpdatePlayable: ParentCardZone == null && CurrentDragPhase == DragPhase.End && !IsMovingToPlaceHolder");
if (PlaceHolder == null)
{
Debug.LogWarning("OnUpdatePlayable: PlaceHolder == null");
RecoverLostPlaceholder();
}

IsMovingToPlaceHolder = true;
}

UpdateForMovingToPlaceHolder();
}
Expand Down Expand Up @@ -348,15 +358,15 @@ public void OnDrop(CardModel cardModel)
PlayController.Instance.CreateCardStack(PlayController.DefaultStackName, cards, Position, Rotation,
!IsFacedown);

Debug.Log($"Discarding {cardModel.gameObject.name} and {gameObject.name} OnDrop");
cardModel.Discard();
Discard();
Debug.Log($"OnDrop {cardModel.gameObject.name} on {gameObject.name} delete");
cardModel.RequestDelete();
RequestDelete();
}

public void OnDrop(CardStack cardStack)
{
cardStack.RequestInsert(0, Id);
Discard();
RequestDelete();
}

public static CardModel CreateDrag(PointerEventData eventData, GameObject gameObject, Transform transform,
Expand Down Expand Up @@ -423,7 +433,7 @@ protected override void PostDragPlayable(PointerEventData eventData)
{
var dropTargetCardModel = DropTarget.GetComponent<CardModel>();

var shouldDiscard = false;
var shouldDeleteOnDropTarget = false;
if (Visibility.blocksRaycasts && ParentCardZone != null && ParentCardZone.Type == CardZoneType.Area
|| PlaceHolderCardZone != null && PlaceHolderCardZone.Type == CardZoneType.Area
|| dropTargetCardModel != null && dropTargetCardModel.ParentCardZone != null &&
Expand All @@ -435,13 +445,15 @@ protected override void PostDragPlayable(PointerEventData eventData)
if (isPointerOverDropTarget)
{
DropTarget.OnDrop(eventData);
shouldDiscard = PlaySettings.AutoStackCards || dropTargetCardModel == null;
shouldDeleteOnDropTarget = PlaySettings.AutoStackCards || dropTargetCardModel == null;
Debug.LogWarning("shouldDeleteOnDropTarget: " + shouldDeleteOnDropTarget);
}
}

if (shouldDiscard)
if (shouldDeleteOnDropTarget)
{
Discard();
Debug.Log("PostDragPlayable::shouldDeleteOnDropTarget");
RequestDelete();
return;
}

Expand All @@ -465,7 +477,10 @@ protected override void PostDragPlayable(PointerEventData eventData)
if (PlaceHolder != null)
IsMovingToPlaceHolder = true;
else if (ParentCardZone == null)
Discard();
{
Debug.LogWarning($"PostDragPlayable Lost PlaceHolder and ParentCardZone for {gameObject.name}!");
RecoverLostPlaceholder();
}
else if (ParentCardZone.Type == CardZoneType.Area)
SnapToGrid();
}
Expand All @@ -484,7 +499,7 @@ protected override void ActOnDrag()

public override void SnapToGrid()
{
if (ToDiscard)
if (ToDelete)
return;

var gridPosition = CalculateGridPosition();
Expand Down Expand Up @@ -514,8 +529,8 @@ public override void SnapToGrid()
PlayController.Instance.CreateCardStack(PlayController.DefaultStackName,
cards, siblingCardModel.Position, siblingCardModel.Rotation,
!siblingCardModel.IsFacedown);
siblingCardModel.Discard();
Discard();
siblingCardModel.RequestDelete();
RequestDelete();
return;
}

Expand All @@ -524,7 +539,7 @@ public override void SnapToGrid()
continue;

siblingCardStack.OnDrop(this);
Discard();
RequestDelete();
return;
}
}
Expand Down Expand Up @@ -628,9 +643,8 @@ private void FinishMovingToPlaceHolder()
{
if (PlaceHolder == null)
{
Debug.Log($"Discarding {gameObject.name} MoveToPlaceHolder");
Discard();
return;
Debug.LogWarning($"FinishMovingToPlaceHolder Lost PlaceHolder for {gameObject.name}!");
RecoverLostPlaceholder();
}

var previousParentCardZone = ParentCardZone;
Expand All @@ -648,6 +662,19 @@ private void FinishMovingToPlaceHolder()
IsMovingToPlaceHolder = false;
}

private void RecoverLostPlaceholder()
{
if (PlayController.Instance == null)
{
RequestDelete();
return;
}

PlaceHolderCardZone = PlayController.Instance.playAreaCardZone;
if (!Vector2.zero.Equals(Position))
PlaceHolder.localPosition = Position;
}

private void ParentToCanvas(Vector3 targetPosition)
{
Debug.Log($"ParentToCanvas {gameObject.name}");
Expand All @@ -674,7 +701,7 @@ private void ParentToCanvas(Vector3 targetPosition)
CreateDrag(CurrentPointerEventData, gameObject, transform, Value, IsFacedown, PlaceHolderCardZone);

if (IsSpawned)
DespawnAndDestroyServerRpc();
DeleteServerRpc();
else
Destroy(gameObject);
}
Expand Down Expand Up @@ -715,27 +742,9 @@ public void OnChangeIsFacedown(bool oldValue, bool newValue)
Value.UnregisterDisplay(this);
}

public void PromptDiscard()
{
CardGameManager.Instance.Messenger.Prompt(DiscardPrompt, Discard);
}

private void Discard()
{
Debug.Log($"Discarding {gameObject.name}");
ToDiscard = true;
if (IsSpawned)
DespawnAndDestroyServerRpc();
else
Destroy(gameObject);
}

[ServerRpc(RequireOwnership = false)]
private void DespawnAndDestroyServerRpc()
public override void PromptDelete()
{
Debug.Log($"DespawnAndDestroyServerRpc {gameObject.name}");
MyNetworkObject.Despawn();
Destroy(gameObject);
CardGameManager.Instance.Messenger.Prompt(DeletePrompt, RequestDelete);
}

public override void OnDestroy()
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Cgs/CardGameView/Multiplayer/CardZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public void OnAdd(CardModel cardModel)
if (Type == CardZoneType.Area)
cardModel.SnapToGrid();

if (cardModel.ToDiscard)
if (cardModel.ToDelete)
return;

foreach (var onAddCardDelegate in OnAddCardActions)
Expand Down
42 changes: 28 additions & 14 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CgsNetPlayable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,21 @@ public GameObject Container

public Vector2 Position
{
get => IsSpawned ? _positionNetworkVariable.Value : transform.localPosition;
get => IsSpawned ? _positionNetworkVariable.Value : _position;
set
{
transform.localPosition = value;
_position = value;
transform.localPosition = _position;
if (!IsSpawned)
return;
if (IsServer)
_positionNetworkVariable.Value = transform.localPosition;
_positionNetworkVariable.Value = _position;
else
RequestUpdatePosition(transform.localPosition);
RequestUpdatePosition(_position);
}
}

private Vector2 _position = Vector2.zero;
private NetworkVariable<Vector2> _positionNetworkVariable;

public Quaternion Rotation
Expand Down Expand Up @@ -117,7 +119,7 @@ public Quaternion Rotation
private float _disownedTime;
private Vector2 _previousPosition;

public bool ToDiscard { get; protected set; }
public bool ToDelete { get; protected set; }

public virtual string ViewValue => "<Playable:Value>";

Expand Down Expand Up @@ -188,10 +190,16 @@ public override void OnNetworkSpawn()
ParentTo(Container == null ? PlayController.Instance.playAreaCardZone.transform : Container.transform);

if (IsServer && !Vector2.zero.Equals(transform.localPosition))
_positionNetworkVariable.Value = transform.localPosition;
{
_position = transform.localPosition;
_positionNetworkVariable.Value = _position;
}

if (!Vector2.zero.Equals(Position))
transform.localPosition = Position;
{
_position = Position;
transform.localPosition = _position;
}

if (IsServer && !Quaternion.identity.Equals(transform.localRotation))
_rotationNetworkVariable.Value = transform.localRotation;
Expand Down Expand Up @@ -392,7 +400,7 @@ public void OnEndDrag(PointerEventData eventData)

PostDragPlayable(eventData);

if (IsSpawned && IsOwner && !ToDiscard)
if (IsSpawned && IsOwner && !ToDelete)
RemoveOwnershipServerRpc();
}

Expand Down Expand Up @@ -454,19 +462,21 @@ protected virtual void UpdatePosition()
var rectTransform = (RectTransform) transform;
rectTransform.position = targetPosition;
rectTransform.SetAsLastSibling();
_position = rectTransform.localPosition;

if (IsOwner)
RequestUpdatePosition(rectTransform.localPosition);
RequestUpdatePosition(_position);
}

public virtual void SnapToGrid()
{
var rectTransform = (RectTransform) transform;
var gridPosition = CalculateGridPosition();
rectTransform.position = gridPosition;
_position = rectTransform.localPosition;

if (IsOwner)
RequestUpdatePosition(rectTransform.localPosition);
RequestUpdatePosition(_position);
}

protected Vector2 CalculateGridPosition()
Expand Down Expand Up @@ -532,7 +542,8 @@ private void UpdatePositionServerRpc(Vector2 position)
[PublicAPI]
public void OnChangePosition(Vector2 oldValue, Vector2 newValue)
{
transform.localPosition = newValue;
_position = newValue;
transform.localPosition = _position;
}

private void RequestUpdateRotation(Quaternion rotation)
Expand Down Expand Up @@ -564,22 +575,25 @@ public void HideHighlightClientRpc()
HighlightMode = HighlightMode.Off;
}

public void PromptDelete()
public virtual void PromptDelete()
{
CardGameManager.Instance.Messenger.Prompt(DeletePrompt, RequestDelete);
}

protected void RequestDelete()
public void RequestDelete()
{
Debug.Log($"RequestDelete {gameObject.name}");
ToDelete = true;
if (IsSpawned)
DeleteServerRpc();
else
Destroy(gameObject);
}

[ServerRpc(RequireOwnership = false)]
private void DeleteServerRpc()
protected void DeleteServerRpc()
{
Debug.Log($"DeleteServerRpc {gameObject.name}");
MyNetworkObject.Despawn();
Destroy(gameObject);
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Cgs/CardGameView/Viewer/CardActionPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public void Discard()

public static void Discard(CardModel cardModel)
{
cardModel.PromptDiscard();
cardModel.PromptDelete();
}

public void Hide()
Expand Down
1 change: 1 addition & 0 deletions Assets/Scripts/Cgs/CardGameView/Viewer/StackViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public void Sync(int handIndex, CardZone cardZone, Text nameText, Text countText
public void OnDrop(CardModel cardModel)
{
AddCard(cardModel.Value);
cardModel.RequestDelete();
}

public void AddCard(UnityCard card)
Expand Down

0 comments on commit 5cf3ffe

Please sign in to comment.