Skip to content

Commit

Permalink
Add stack dropping
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Nov 24, 2023
1 parent 2c00cfe commit b196ff2
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 53 deletions.
69 changes: 67 additions & 2 deletions Assets/Prefabs/CardGameView/Multiplayer/Card Stack.prefab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 18 additions & 15 deletions Assets/Prefabs/CardGameView/Multiplayer/Token.prefab

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CardModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace Cgs.CardGameView.Multiplayer
{
public class CardModel : CgsNetPlayable, ICardDisplay, ICardDropHandler
public class CardModel : CgsNetPlayable, ICardDisplay, ICardDropHandler, IStackDropHandler
{
public const string DropErrorMessage = "Error: Card dropped on Card outside of play area!";

Expand Down Expand Up @@ -148,9 +148,6 @@ private void SetIsNameVisible(bool value)
private Image View => _view ??= GetComponent<Image>();
private Image _view;

private CanvasGroup Visibility => _visibility ??= GetComponent<CanvasGroup>();
private CanvasGroup _visibility;

public override void OnNetworkSpawn()
{
PlayController.SetPlayActions(this);
Expand All @@ -175,6 +172,9 @@ protected override void OnStartPlayable()
var cardDropArea = gameObject.GetOrAddComponent<CardDropArea>();
cardDropArea.isBlocker = true;
cardDropArea.DropHandler = this;

var stackDropArea = gameObject.GetOrAddComponent<StackDropArea>();
stackDropArea.DropHandler = this;
}

var cardSize = new Vector2(CardGameManager.Current.CardSize.X, CardGameManager.Current.CardSize.Y);
Expand Down Expand Up @@ -325,6 +325,12 @@ public void OnDrop(CardModel cardModel)
Discard();
}

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

public static CardModel CreateDrag(PointerEventData eventData, GameObject gameObject, Transform transform,
UnityCard value, bool isFacedown, CardZone placeHolderCardZone = null)
{
Expand Down
35 changes: 30 additions & 5 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CardStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void Shuffle<T>(this IList<T> list)
}

[RequireComponent(typeof(CardDropArea))]
public class CardStack : CgsNetPlayable, ICardDisplay, ICardDropHandler
public class CardStack : CgsNetPlayable, ICardDisplay, ICardDropHandler, IStackDropHandler
{
private const float DragHoldTime = 0.5f;
public const string ShuffleText = "Shuffled!";
Expand All @@ -61,6 +61,12 @@ public class CardStack : CgsNetPlayable, ICardDisplay, ICardDropHandler
CurrentPointerEventData.button != PointerEventData.InputButton.Right
|| PointerPositions.Count > 1;

private bool IsDraggingStack => HoldTime >= DragHoldTime || CurrentPointerEventData is
{button: PointerEventData.InputButton.Middle};

protected override bool IsProcessingSecondaryDragAction =>
!IsDraggingStack && base.IsProcessingSecondaryDragAction;

public GameObject stackViewerPrefab;
public GameObject cardModelPrefab;

Expand Down Expand Up @@ -152,6 +158,7 @@ protected override void OnStartPlayable()
{
ParentToPlayAreaContent();
GetComponent<CardDropArea>().DropHandler = this;
GetComponent<StackDropArea>().DropHandler = this;

var rectTransform = (RectTransform) transform;
var cardSize = new Vector2(CardGameManager.Current.CardSize.X, CardGameManager.Current.CardSize.Y);
Expand Down Expand Up @@ -240,6 +247,12 @@ protected override void OnEndDragPlayable(PointerEventData eventData)
{
if (!IsDraggingCard && !LacksOwnership)
ActOnDrag();
Visibility.blocksRaycasts = true;
}

public static CardStack GetPointerDrag(PointerEventData eventData)
{
return eventData.pointerDrag == null ? null : eventData.pointerDrag.GetComponent<CardStack>();
}

private void DragCard(PointerEventData eventData)
Expand Down Expand Up @@ -295,7 +308,7 @@ private void OnCardsUpdated(NetworkListEvent<CgsNetString> changeEvent)
}

if (changeEvent.Type.Equals(NetworkListEvent<CgsNetString>.EventType.Insert) &&
changeEvent.Index == _cardIds.Count -1)
changeEvent.Index == _cardIds.Count - 1)
{
if (CardGameManager.Current.Cards.TryGetValue(_cardIds[^2], out var previous))
previous.UnregisterDisplay(this);
Expand All @@ -312,13 +325,25 @@ private void OnCardsUpdated(NetworkListEvent<CgsNetString> changeEvent)
public void OnDrop(CardModel cardModel)
{
cardModel.PlaceHolderCardZone = null;
RequestInsert(Cards.Count, cardModel.Id);
}

public void OnDrop(CardStack cardStack)
{
for (var i = _cardIds.Count - 1; i >= 0; i--)
cardStack.RequestInsert(0, _cardIds[i]);
RequestDelete();
}

public void RequestInsert(int index, string cardId)
{
if (LacksOwnership)
CgsNetManager.Instance.LocalPlayer.RequestInsert(gameObject, Cards.Count, cardModel.Id);
CgsNetManager.Instance.LocalPlayer.RequestInsert(gameObject, index, cardId);
else
Insert(Cards.Count, cardModel.Id);
OwnerInsert(index, cardId);
}

public void Insert(int index, string cardId)
public void OwnerInsert(int index, string cardId)
{
Debug.Log($"CardStack: {name} insert {cardId} at {index} of {_cardIds.Count}");
_cardIds.Insert(index, cardId);
Expand Down
Loading

0 comments on commit b196ff2

Please sign in to comment.