public CardElement GetLastCardInStack() { if (!m_NextInStack) { return(this); } else { return(m_NextInStack.GetLastCardInStack()); } }
// Update is called once per frame private void Update() { if (!m_IsGameStart) { return; } if (m_GameResult != Solitaire.GameResult.Still) { Logger.Instance.PrintLog(Common.DEBUG_TAG, "Game END !!!"); return; } Vector3 mousePos = Input.mousePosition; //Left mouse clicked if (Input.GetMouseButtonDown(0)) { if (EventSystem.current.IsPointerOverGameObject()) { return; } Logger.Instance.PrintLog(Common.DEBUG_TAG, "GameController MouseButton down"); RaycastHit2D hit = Physics2D.Raycast(m_MainCamera.ScreenToWorldPoint(mousePos), Vector2.zero); if (hit) { switch (hit.collider.tag) { case "Card": { CardElement card = hit.collider.gameObject.GetComponent <CardElement>(); //No selected and card not in top position if (!m_CurrentSelected && (card.position & (Solitaire.CardPosition.Top1 | Solitaire.CardPosition.Top2 | Solitaire.CardPosition.Top3 | Solitaire.CardPosition.Top4)) == 0) { if (((card.position & Solitaire.CardPosition.Draw) > 0) && card.CardValue != m_DeckCards[m_GameData.currentDrawCard].CardValue) { return; } m_CurrentSelected = card; m_CurrentSelected.IsSelected = true; m_PrevClickedTime = Time.time; break; } //Card in Top position if ((card.position & (Solitaire.CardPosition.Top1 | Solitaire.CardPosition.Top2 | Solitaire.CardPosition.Top3 | Solitaire.CardPosition.Top4)) > 0) { if (CanStack(m_CurrentSelected.CardValue, card.CardValue, true) && !m_CurrentSelected.IsInStack()) { StackToCard(card, true); return; } } //Card in Bottom position if ((card.position & (Solitaire.CardPosition.Bottom1 | Solitaire.CardPosition.Bottom2 | Solitaire.CardPosition.Bottom3 | Solitaire.CardPosition.Bottom4 | Solitaire.CardPosition.Bottom5 | Solitaire.CardPosition.Bottom6 | Solitaire.CardPosition.Bottom7)) > 0) { if (CanStack(m_CurrentSelected.CardValue, card.CardValue)) { StackToCard(card, false); return; } else { m_CurrentSelected.IsSelected = false; m_CurrentSelected = card; m_CurrentSelected.IsSelected = true; m_PrevClickedTime = Time.time; return; } } //Card in draw position if ((card.position & Solitaire.CardPosition.Draw) > 0) { if (card.CardValue == m_DeckCards[m_GameData.currentDrawCard].CardValue) { m_CurrentSelected.IsSelected = false; m_CurrentSelected = card; m_CurrentSelected.IsSelected = true; m_PrevClickedTime = Time.time; return; } } /* * if(!selected && (card.position & (CardPosition.Top1 | CardPosition.Top2 | CardPostion.Top3 | CardPositon.Top4)) == 0) * if((card.positon & CardPosition.Draw) > 0 && card.CardValue != m_DeckCards[m_GameData.currentDrawCard].CardValue) * return * * selected = card * selected.IsSelect = true * return * * if(card.position & (CardPosition.Top1 | CardPosition.Top2 | CardPostion.Top3 | CardPositon.Top4) > 0) * if(!selected) * return * * if(CanStack(selected.CardValue, card.CardValue) && !selected.IsInStack()) * move selected to card position * remove seleteced from current ListCard * add selected to TopCards * selected.IsSelect = false * set selected = null * CheckWinCondition() * return * * if((card.position & (CardPosition.Bottom1 | CardPosition.Bottom2 | CardPosition.Bottom3 | CardPosition.Bottom4 | CardPosition.Bottom5 | CardPosition.Bottom6 | CardPosition.Bottom7)) > 0) * if(!selected) * selected = card * selected.IsSelect = true * return * * if(CanStack(selected.CardValue, card.CardValue)) * move selected to card position * remove seleteced from current ListCard * add selected to TopCards * selected.IsSelect = false * set selected = null * return * * * if((card.position & CardPosition.Draw) > 0) * if(!selected && (card.CardValue == m_MainData.currentDrawCard.CardValue)) * selected = card * selected.IsSelect = true * return */ break; } case "Bottom": { if (!m_CurrentSelected) { return; } ushort selectedCardValue = (ushort)Utilities.Instance.ExtractBit(m_CurrentSelected.CardValue, 12, 1); if (selectedCardValue == (ushort)Solitaire.CardValue.King) { StackToPosition(hit.collider.gameObject, false); return; } /* * if(!selected) * return * * selectedValue = Utilities.Instance.ExtractBit(selected, 12, 1); * if(selectedValue == CardValue.King) * move selected to bottom position * remove selected from current ListCard * add selected to BottomCards * set selected = null */ break; } case "Top": { if (!m_CurrentSelected) { return; } ushort selectedCardValue = (ushort)Utilities.Instance.ExtractBit(m_CurrentSelected.CardValue, 12, 1); if (selectedCardValue == (ushort)Solitaire.CardValue.Ace) { StackToPosition(hit.collider.gameObject, true); return; } /* * if(!selected) * return * * selectedValue = Utilities.Instance.ExtractBit(selected, 12, 1); * if(selectedValue == CardValue.King) * move selected to top position * remove selected from current ListCard * add selected to TopCards * set selected = null */ break; } default: { if (m_CurrentSelected) { m_CurrentSelected.IsSelected = false; m_CurrentSelected = null; m_PrevClickedTime = 0.0f; } break; } } } } //Left mouse held down if (Input.GetMouseButton(0)) { Logger.Instance.PrintLog(Common.DEBUG_TAG, "GameController MouseButton drag"); Logger.Instance.PrintLog(Common.DEBUG_TAG, "time spent = " + (Time.time - m_PrevClickedTime)); //TODO: more testing to find the magic number :< if (m_CurrentSelected && (Time.time - m_PrevClickedTime) >= Common.MIN_DRAGGING_TIME) { Vector3 cardNewPos = Utilities.Instance.GetWorldPosition2D(m_MainCamera, mousePos); cardNewPos.z = -Common.DRAGGING_Z; m_CurrentSelected.IsDragging = true; m_CurrentSelected.OnCardDrag(cardNewPos); } /* * * if(selected) * Vector3 cardNewPos = Utilities.Instance.GetWorldPosition2D(mousePos) * selected.IsDragging = true; * selected.OnCardDrag(cardNewPos) */ } //Left mouse release if (Input.GetMouseButtonUp(0)) { Logger.Instance.PrintLog(Common.DEBUG_TAG, "GameController MouseButton up"); if (m_CurrentSelected && m_CurrentSelected.IsDragging) { switch (m_CurrentSelected.CollidedTag) { case 1: //Card { if (!m_CurrentSelected.Collided) { return; } CardElement card = m_CurrentSelected.Collided.GetComponent <CardElement>(); //Card in Top position if ((card.position & (Solitaire.CardPosition.Top1 | Solitaire.CardPosition.Top2 | Solitaire.CardPosition.Top3 | Solitaire.CardPosition.Top4)) > 0) { if (CanStack(m_CurrentSelected.CardValue, card.CardValue, true) && !m_CurrentSelected.IsInStack()) { StackToCard(card, true); return; } } //Card in Bottom position if ((card.position & (Solitaire.CardPosition.Bottom1 | Solitaire.CardPosition.Bottom2 | Solitaire.CardPosition.Bottom3 | Solitaire.CardPosition.Bottom4 | Solitaire.CardPosition.Bottom5 | Solitaire.CardPosition.Bottom6 | Solitaire.CardPosition.Bottom7)) > 0) { CardElement lastCardInStack = card.GetLastCardInStack(); if (CanStack(m_CurrentSelected.CardValue, lastCardInStack.CardValue)) { StackToCard(lastCardInStack, false); return; } //else //{ // m_CurrentSelected.IsSelected = false; // m_CurrentSelected = card; // m_CurrentSelected.IsSelected = true; // return; //} } break; } case 2: //Top { if (!m_CurrentSelected) { return; } ushort selectedCardValue = (ushort)Utilities.Instance.ExtractBit(m_CurrentSelected.CardValue, 12, 1); if (selectedCardValue == (ushort)Solitaire.CardValue.Ace) { StackToPosition(m_CurrentSelected.Collided, true); return; } break; } case 3: //Bottom { if (!m_CurrentSelected) { return; } ushort selectedCardValue = (ushort)Utilities.Instance.ExtractBit(m_CurrentSelected.CardValue, 12, 1); if (selectedCardValue == (ushort)Solitaire.CardValue.King) { StackToPosition(m_CurrentSelected.Collided, false); return; } break; } } m_PrevClickedTime = 0.0f; m_CurrentSelected.ResetCardPosition(); m_CurrentSelected = null; } /* * if(selected && IsDragging) * switch(selected.CollidedTag) * case 1: //Card * CardElement card = selected.Collided.GetComponent<CardElement>() * if(card.position & (CardPosition.Top1 | CardPosition.Top2 | CardPostion.Top3 | CardPositon.Top4) > 0) * if(!selected) * return * * if(CanStack(selected.CardValue, card.CardValue) && !selected.IsInStack()) * move selected to card position * remove seleteced from current ListCard * add selected to TopCards * selected.IsSelect = false * set selected = null * CheckWinCondition() * return * * if((card.position & (CardPosition.Bottom1 | CardPosition.Bottom2 | CardPosition.Bottom3 | CardPosition.Bottom4 | CardPosition.Bottom5 | CardPosition.Bottom6 | CardPosition.Bottom7)) > 0) * if(!selected) * selected = card * selected.IsSelect = true * return * * if(CanStack(selected.CardValue, card.CardValue)) * move selected to card position * remove seleteced from current ListCard * add selected to TopCards * selected.IsSelect = false * set selected = null * return * break; * * case 2: //Top * selectedValue = Utilities.Instance.ExtractBit(selected, 12, 1); * if(selectedValue == CardValue.King) * move selected to top position * remove selected from current ListCard * add selected to TopCards * set selected = null * return * break * * case 3: //Bottom * selectedValue = Utilities.Instance.ExtractBit(selected, 12, 1); * if(selectedValue == CardValue.King) * move selected to bottom position * remove selected from current ListCard * add selected to BottomCards * set selected = null * return * break * * selected.ResetCardPosition() * selected = null */ } }