Skip to content

Commit

Permalink
Card Zone fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmfinol committed Sep 29, 2024
1 parent 536ba44 commit c0078c7
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 107 deletions.
6 changes: 3 additions & 3 deletions Assets/Scripts/Cgs/CardGameView/CardDropArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private void OnTriggerEnter2D(Collider2D other)
{
var cardModel = other.GetComponent<CardModel>();
if (cardModel == null || cardModel.ParentCardZone == null ||
cardModel.ParentCardZone.type != CardZoneType.Area)
cardModel.ParentCardZone.Type != CardZoneType.Area)
return;

cardModel.DropTarget = this;
Expand All @@ -53,8 +53,8 @@ public void OnDrop(PointerEventData eventData)
{
var cardModel = CardModel.GetPointerDrag(eventData);
if (cardModel == null
|| cardModel.ParentCardZone != null && cardModel.ParentCardZone.type != CardZoneType.Area
|| cardModel.PlaceHolderCardZone != null && cardModel.PlaceHolderCardZone.type != CardZoneType.Area)
|| cardModel.ParentCardZone != null && cardModel.ParentCardZone.Type != CardZoneType.Area
|| cardModel.PlaceHolderCardZone != null && cardModel.PlaceHolderCardZone.Type != CardZoneType.Area)
return;

var drawerViewer = DropHandler as DrawerViewer;
Expand Down
22 changes: 11 additions & 11 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CardModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ protected override void OnStartPlayable()
if (PlayController.Instance != null &&
PlayController.Instance.playAreaCardZone.transform == transform.parent)
{
PlayController.SetPlayActions(this);
PlayController.SetPlayAreaActions(this);
var cardDropArea = gameObject.GetOrAddComponent<CardDropArea>();
cardDropArea.isBlocker = true;
cardDropArea.DropHandler = this;
Expand Down Expand Up @@ -424,10 +424,10 @@ protected override void PostDragPlayable(PointerEventData eventData)
var dropTargetCardModel = DropTarget.GetComponent<CardModel>();

var shouldDiscard = false;
if (Visibility.blocksRaycasts && ParentCardZone != null && ParentCardZone.type == CardZoneType.Area
|| PlaceHolderCardZone != null && PlaceHolderCardZone.type == CardZoneType.Area
if (Visibility.blocksRaycasts && ParentCardZone != null && ParentCardZone.Type == CardZoneType.Area
|| PlaceHolderCardZone != null && PlaceHolderCardZone.Type == CardZoneType.Area
|| dropTargetCardModel != null && dropTargetCardModel.ParentCardZone != null &&
dropTargetCardModel.ParentCardZone.type == CardZoneType.Area)
dropTargetCardModel.ParentCardZone.Type == CardZoneType.Area)
{
var hits = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventData, hits);
Expand All @@ -446,7 +446,7 @@ protected override void PostDragPlayable(PointerEventData eventData)
}

if (dropTargetCardModel != null && dropTargetCardModel.ParentCardZone != null &&
dropTargetCardModel.ParentCardZone.type == CardZoneType.Area)
dropTargetCardModel.ParentCardZone.Type == CardZoneType.Area)
{
PlaceHolderCardZone = dropTargetCardModel.ParentCardZone;
PlaceHolderCardZone.UpdateLayout(PlaceHolder, transform.position);
Expand All @@ -466,7 +466,7 @@ protected override void PostDragPlayable(PointerEventData eventData)
IsMovingToPlaceHolder = true;
else if (ParentCardZone == null)
Discard();
else if (ParentCardZone.type == CardZoneType.Area)
else if (ParentCardZone.Type == CardZoneType.Area)
SnapToGrid();
}

Expand Down Expand Up @@ -580,25 +580,25 @@ private void UpdateCardZonePosition(Vector2 targetPosition)
if (cardZone == null || LacksOwnership)
return;

if (!cardZone.DoesImmediatelyRelease && cardZone.type is CardZoneType.Vertical or CardZoneType.Horizontal)
if (!cardZone.DoesImmediatelyRelease && cardZone.Type is CardZoneType.Vertical or CardZoneType.Horizontal)
cardZone.UpdateScrollRect(CurrentDragPhase, CurrentPointerEventData);
else if (!IsStatic)
cardZone.UpdateLayout(transform as RectTransform, targetPosition);

if (IsStatic)
return;

if (cardZone.type == CardZoneType.Area)
if (cardZone.Type == CardZoneType.Area)
transform.SetAsLastSibling();

var zoneCorners = new Vector3[4];
((RectTransform) cardZone.transform).GetWorldCorners(zoneCorners);
var isOutYBounds = targetPosition.y < zoneCorners[0].y || targetPosition.y > zoneCorners[1].y;
var isOutXBounds = targetPosition.x < zoneCorners[0].x || targetPosition.y > zoneCorners[2].x;
if ((cardZone.DoesImmediatelyRelease && !IsProcessingSecondaryDragAction)
|| (cardZone.type == CardZoneType.Vertical && isOutXBounds)
|| (cardZone.type == CardZoneType.Horizontal && isOutYBounds)
|| (cardZone.type == CardZoneType.Area && PlaceHolder != null &&
|| (cardZone.Type == CardZoneType.Vertical && isOutXBounds)
|| (cardZone.Type == CardZoneType.Horizontal && isOutYBounds)
|| (cardZone.Type == CardZoneType.Area && PlaceHolder != null &&
PlaceHolder.parent != transform.parent))
ParentToCanvas(targetPosition);
}
Expand Down
63 changes: 34 additions & 29 deletions Assets/Scripts/Cgs/CardGameView/Multiplayer/CardZone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,20 @@ protected override void OnAwakePlayable()

protected override void OnNetworkSpawnPlayable()
{
if (CardZoneType.Area.Equals(Type))
if (CardZoneType.Area.Equals(type))
type = (CardZoneType) _typeNetworkVariable.Value;
if (Vector2.zero.Equals(_size))
_size = _sizeNetworkVariable.Value;
if (FacePreference.Any.Equals(_facePreference))
_facePreference = (FacePreference) _faceNetworkVariable.Value;
if (CardAction.Move.Equals(_cardAction))
_cardAction = (CardAction) _actionNetworkVariable.Value;
}

protected override void OnStartPlayable()
{
if (PlayController.Instance == null ||
PlayController.Instance.playAreaCardZone.transform != transform.parent)
return;

var rectTransform = (RectTransform) transform;
Expand Down Expand Up @@ -141,7 +154,6 @@ protected override void OnNetworkSpawnPlayable()
if (layoutGroup != null)
layoutGroup.spacing = -225f;

type = Type;
allowsFlip = true;
allowsRotation = true;
scrollRectContainer = PlayController.Instance.playArea;
Expand All @@ -150,27 +162,20 @@ protected override void OnNetworkSpawnPlayable()
if (_faceNetworkVariable.Value != (int) _facePreference && (int) _facePreference != 0)
_faceNetworkVariable.Value = (int) _facePreference;

switch (DefaultFace)
{
case FacePreference.Any:
OnAddCardActions.Add(PlayController.OnAddCardModel);
break;
case FacePreference.Down:
OnAddCardActions.Add(PlayController.OnAddCardModelFaceDown);
break;
case FacePreference.Up:
OnAddCardActions.Add(PlayController.OnAddCardModelFaceUp);
break;
default:
OnAddCardActions.Add(PlayController.OnAddCardModel);
break;
}

if (_actionNetworkVariable.Value != (int) _cardAction && (int) _cardAction != 0)
_actionNetworkVariable.Value = (int) _cardAction;

var defaultAction = CardActionPanel.CardActionDictionary[DefaultAction];
OnAddCardActions.Add((_, cardModel) => cardModel.DefaultAction = defaultAction);
OnAddCardActions.Add((cardZone, cardModel) =>
{
cardModel.IsFacedown = cardZone.DefaultFace switch
{
FacePreference.Down => true,
FacePreference.Up => false,
_ => cardModel.IsFacedown
};
cardModel.DefaultAction = CardActionPanel.CardActionDictionary[cardZone.DefaultAction];
cardModel.SecondaryDragAction = cardModel.UpdateParentCardZoneScrollRect;
});

StartCoroutine(WaitToAddMoveCardToServer());
}
Expand All @@ -185,11 +190,11 @@ private IEnumerator WaitToAddMoveCardToServer()
protected override void OnPointerEnterPlayable(PointerEventData eventData)
{
var cardModel = CardModel.GetPointerDrag(eventData);
if (cardModel != null && (type != CardZoneType.Area || cardModel.transform.parent != transform) &&
if (cardModel != null && (Type != CardZoneType.Area || cardModel.transform.parent != transform) &&
!cardModel.IsStatic)
cardModel.PlaceHolderCardZone = this;

if (type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
if (Type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
scrollRect.OnPointerEnter(eventData);
}

Expand All @@ -201,7 +206,7 @@ protected override void OnPointerExitPlayable(PointerEventData eventData)

OnLayout?.Invoke();

if (type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
if (Type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
scrollRect.OnPointerExit(eventData);
}

Expand All @@ -227,23 +232,23 @@ protected override void OnDeselectPlayable(BaseEventData eventData)

protected override void OnBeginDragPlayable(PointerEventData eventData)
{
if (type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
if (Type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
scrollRect.OnBeginDrag(eventData);
else if (scrollRectContainer != null)
scrollRectContainer.OnBeginDrag(eventData);
}

protected override void OnDragPlayable(PointerEventData eventData)
{
if (type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
if (Type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
scrollRect.OnDrag(eventData);
else if (scrollRectContainer != null)
scrollRectContainer.OnDrag(eventData);
}

protected override void OnEndDragPlayable(PointerEventData eventData)
{
if (type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
if (Type == CardZoneType.Area && scrollRectContainer is RotateZoomableScrollRect scrollRect)
scrollRect.OnEndDrag(eventData);
else if (scrollRectContainer != null)
scrollRectContainer.OnEndDrag(eventData);
Expand All @@ -264,7 +269,7 @@ public void OnAdd(CardModel cardModel)
if (cardModel == null)
return;

if (type == CardZoneType.Area)
if (Type == CardZoneType.Area)
cardModel.SnapToGrid();

if (cardModel.ToDiscard)
Expand All @@ -288,14 +293,14 @@ public void UpdateLayout(RectTransform child, Vector2 targetPosition)
if (child == null)
return;

switch (type)
switch (Type)
{
case CardZoneType.Vertical:
case CardZoneType.Horizontal:
var newSiblingIndex = transform.childCount;
for (var i = 0; i < transform.childCount; i++)
{
if (type == CardZoneType.Vertical
if (Type == CardZoneType.Vertical
? targetPosition.y < transform.GetChild(i).position.y
: targetPosition.x > transform.GetChild(i).position.x)
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ protected virtual void OnEndDragPlayable(PointerEventData eventData)

protected virtual void PostDragPlayable(PointerEventData eventData)
{
if (ParentCardZone != null && ParentCardZone.type == CardZoneType.Area)
if (ParentCardZone != null && ParentCardZone.Type == CardZoneType.Area)
SnapToGrid();
}

Expand Down
2 changes: 0 additions & 2 deletions Assets/Scripts/Cgs/CardGameView/Viewer/CardActionPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Cgs.CardGameView.Viewer

public class CardActionPanel : MonoBehaviour
{
public const float PositionOffsetAmount = 250f;

public static IReadOnlyDictionary<FinolDigital.Cgs.CardGameDef.CardAction, CardAction> CardActionDictionary =>
_cardActionDictionary ??= new Dictionary<FinolDigital.Cgs.CardGameDef.CardAction, CardAction>
{
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scripts/Cgs/Decks/DeckEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ private void Consolidate()
private void AddCardZone()
{
var cardZone = Instantiate(cardZonePrefab, layoutContent).GetOrAddComponent<CardZone>();
cardZone.type = CardZoneType.Vertical;
cardZone.Type = CardZoneType.Vertical;
cardZone.scrollRectContainer = scrollRect;
cardZone.DoesImmediatelyRelease = true;
cardZone.OnLayout = Consolidate;
Expand Down
7 changes: 4 additions & 3 deletions Assets/Scripts/Cgs/Play/Multiplayer/CgsNetPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ public void MoveCardToServer(CardZone cardZone, CardModel cardModel)
var isFacedown = cardModel.IsFacedown && !cardModel.Value.IsBackFaceCard;

if (cardZone.IsSpawned)
SpawnCardInZoneServerRpc(cardZone.gameObject, cardModel.Id, position, rotation, isFacedown);
SpawnCardInZoneServerRpc(cardZone.gameObject, cardModel.Id, position, rotation, isFacedown,
cardZone.DefaultAction.ToString());
else
SpawnCardInPlayAreaServerRpc(cardModel.Id, position, rotation, isFacedown);

Expand All @@ -539,9 +540,9 @@ public void MoveCardToServer(CardZone cardZone, CardModel cardModel)
[ServerRpc]
// ReSharper disable once MemberCanBeMadeStatic.Local
private void SpawnCardInZoneServerRpc(NetworkObjectReference container, string cardId, Vector3 position,
Quaternion rotation, bool isFacedown)
Quaternion rotation, bool isFacedown, string defaultAction)
{
PlayController.Instance.CreateCardModel(container, cardId, position, rotation, isFacedown);
PlayController.Instance.CreateCardModel(container, cardId, position, rotation, isFacedown, defaultAction);
}

[ServerRpc]
Expand Down
Loading

0 comments on commit c0078c7

Please sign in to comment.