Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dropping Stacks on Cards or other Stacks #112

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

3 changes: 2 additions & 1 deletion Assets/Scripts/Cgs/CardGameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,8 @@ private IEnumerator Start()
yield return null;

Debug.Log("CardGameManager::Start:GameReady");
GameReady();
if (Current == null || Current == UnityCardGame.UnityInvalid || Current.Id.Equals(Tags.StandardPlayingCardsDirectoryName))
GameReady();
}
#endif

Expand Down
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
Loading