}// property public Board(int rows, int columns) { _rows = rows; _columns = columns; _cards = new Card[rows][]; for (int row=0; row<rows; row++){ _cards [row] = new Card[columns]; //for (int col=0; col<columns; col++){ // _cards [row] [col] = null; //}// for }// for }// Card
}// GetCardAt public void SetCardAt(int row, int column, Card card){ _cards [Mathf.Clamp (row, 0, _rows)] [Mathf.Clamp (column, 0, _columns)] = card; }// SetCardAt
}// CreateInfrastructure protected CardView AddCardView(int player, Card card){ CardView cv = AddCardView (player, card.Color, card.Value); cv.CardModel = card; cv.CardModel.Position = player; return cv; }// AddCard
}// SpawnCard protected CardView SpawnCard(Card cardModel){ GameObject card = GameObject.Instantiate (PrefabCard); CardView view = card.GetComponent<CardView> (); view.CardModel = cardModel; return view; }// SpawnCard
}// ValidateRoutine_BesidesLast protected bool ValidateRoutine_NotBesidesLast(Card played, Card onboard, int column, int row){ return !ValidateRoutine_BesidesLast (played, onboard, column, row); }// ValidateRoutine_NotBesidesLast
}// FixedUpdate protected void LateUpdate() { if (Input.GetKeyDown(KeyCode.Space) && _keys.TryPress(KeyCode.Space)) { BeginMatch (); }// fi // Show board cards. if (_selectedCard == null && Input.GetKey (KeyCode.Return)){// && _keys.TryPress (KeyCode.Return)) { if (!_camPosBoardActive) { _camPosBoardActive = true; _camPosStashed = _camPosCurrent; ApplyCamPosition (CamPosBoard, TimeCamFlightBoard); }// fi } else { if (_camPosBoardActive) { ApplyCamPosition (_camPosStashed, TimeCamFlightBoard); _camPosBoardActive = false; }// fi }// fi // Check if card is targeted. if (_matchRunning){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Camera.main.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0f)); RaycastHit hitinfo; //Vector3 hit = Vector3.zero; bool interaction = false; if (Physics.Raycast(ray, out hitinfo, 1<<LayerMask.NameToLayer("Card"))){ /* if ((hitinfo.collider.gameObject.layer & LayerMask.NameToLayer("Card")) != 0){ Debug.Log("Hit card=" + hitinfo.collider.gameObject.name); } else { Debug.Log("Hit noncard with objectname=" + hitinfo.collider.gameObject.name); }// fi */ /* //if (Physics.Raycast(ray, out hitinfo, 100.0f)){ hit = hitinfo.point; if (hit.y < MaxDepth) hit.y = MaxDepth; this.transform.position = hit; */ if ( ((hitinfo.collider.gameObject.layer & LayerMask.NameToLayer("Card")) != 0) ){ // Unhighlight previously highlighted object first. if (_interacttarget != null && _interacttarget != hitinfo.collider.gameObject) { if (_highlightable != null){ _highlightable.Unhighlight(); _highlightable = null; }// fi _interacttarget = null; }// fi // Ensure that player can only select cards of own deck. CardView cv = hitinfo.collider.gameObject.GetComponent<CardView> (); if (cv == null && hitinfo.collider.gameObject.name.CompareTo ("Number") == 0) { cv = hitinfo.collider.gameObject.transform.parent.gameObject.GetComponent<CardView> (); }// fi //if (cv == null) { // DbgOut.LogError ("Failed to get CardView from object=" + hitinfo.collider.gameObject.name); //} else if (cv.CardModel == null){ // DbgOut.LogError ("CardView has null CardModel; object=" + hitinfo.collider.gameObject.name); //}// fi if ((cv != null && cv.CardModel != null) && ((_selectedCard == null && cv.CardModel.Position == _match.PlayerAction) || (_selectedCard != null && _selectedTarget == null && cv.CardModel.Position == Card.POSITION_BOARD && DoCheckCardFits(_selectedCardModel, cv)))){ _interacttarget = hitinfo.collider.gameObject; CardView cv2 = _interacttarget.GetComponent<CardView> (); if (cv2 == null && _interacttarget.name.CompareTo ("Number") == 0) _interacttarget = _interacttarget.transform.parent.gameObject; _highlightable = _interacttarget.GetComponent<Highlightable>(); if (_highlightable != null && _highlightable.enabled){ interaction = true; _highlightable.Highlight(); }// fi }// fi }// fi }// fi if (!interaction) { if (_highlightable != null) { _highlightable.Unhighlight (); _highlightable = null; }// fi _interacttarget = null; } else { if (Input.GetMouseButtonDown(0)){ // Check if player selects card. if (_selectedCard == null) { _selectedCard = _interacttarget; CardView cvSelected = _selectedCard.GetComponent<CardView> (); _selectedCardModel = cvSelected.CardModel; DoSelectCard (); _camPosStashed = _camPosCurrent; ApplyCamPosition (CamPosBoard, TimeCamFlightBoard); } else if (_selectedTarget == null){ _selectedTarget = _interacttarget; DoPlayCard (); }// fi }// fi }// fi if (Input.GetMouseButtonDown (1)) { if (_selectedCard != null) { DoUnselectCard (); _selectedCard = null; ApplyCamPosition (_camPosStashed, TimeCamFlightBoard); }// fi }// fi }// fi #region debug //if (Input.GetKeyDown(KeyCode.X) && _keys.TryPress(KeyCode.X)) { // EndTurn(); //}// fi #endregion debug }// LateUpdate
}// ValidateRoutine_SameOddEven protected bool ValidateRoutine_MixedOddEven(Card played, Card onboard, int column, int row){ return !ValidateRoutine_SameOddEven(played, onboard, column, row); }// ValidateRoutine_MixedOddEven
}// ValidateRoutine_MixedOddEven protected bool ValidateRoutine_BesidesLast(Card played, Card onboard, int column, int row){ int diffX = Mathf.Abs (_lastBoardColumn - column); int diffY = Mathf.Abs (_lastBoardRow - row); return ( (diffX==1 && diffY<2) || (diffX==0 && diffY == 1) ); }// ValidateRoutine_BesidesLast
}// ValidateRoutine_RedBlue protected bool ValidateRoutine_GreenBlue(Card played, Card onboard, int column, int row){ return (onboard.Color.CompareTo(CardColor.Green)==0) || (onboard.Color.CompareTo(CardColor.Blue)==0); }// ValidateRoutine_GreenBlue
}// ValidateRoutine_GreenBlue protected bool ValidateRoutine_SameOddEven(Card played, Card onboard, int column, int row){ return (((played.Value + onboard.Value) % 2) == 0); }// ValidateRoutine_SameOddEven
}// ValidateRoutine_Common protected bool ValidateRoutine_YellowRed(Card played, Card onboard, int column, int row){ return (onboard.Color.CompareTo(CardColor.Yellow)==0) || (onboard.Color.CompareTo(CardColor.Red)==0); }// ValidateRoutine_YellowRed
}// CheckMatchEnd #region Routine validators protected bool ValidateRoutine_Common(Card played, Card onboard, int column, int row){ return (onboard.Color.CompareTo(played.Color)==0) || (onboard.Color.CompareTo(played.Color)!=0 && (onboard.Value <= played.Value)); }// ValidateRoutine_Common
}// DoUnselectCard protected void DoPlayCard(){ DbgOut.Log ("Playing card."); CardView cvplayed = _selectedCard.GetComponent<CardView>(); CardView cvtarget = _selectedTarget.GetComponent<CardView>(); // Hide the temporary selection view. DoUnselectCard (); // Remember the last played card. _lastPlayedCard = cvplayed.CardModel; _lastBoardCard = cvtarget.CardModel; _lastBoardColumn = cvtarget.Column; _lastBoardRow = cvtarget.Row; // Return card from board to reserve stack. DbgOut.Log ("Returning card to reserve; card=" + cvtarget.CardModel.ToString ()); _match.PlayDeck.ReturnCard (cvtarget.CardModel); // Place it on the board. cvtarget.CardModel = cvplayed.CardModel; cvtarget.CardModel.Position = Card.POSITION_BOARD; DbgOut.Log ("Placing card on board; removing from player hand; card=" + cvplayed.CardModel.ToString ()); // Remove card from player hand. Player player = _match.GetPlayer (_match.PlayerAction); player.Cards.Remove (cvplayed.CardModel); cvplayed.CardModel = null; cvplayed.gameObject.SetActive (false); //Renderer rend = cvplayed.gameObject.GetComponent<Renderer> (); //rend.enabled = false; if (CheckMatchEnd ()) { EndMatch (); } else { EndTurn (); }// fi }// DoPlayCard
}// CheckPlayerCanPlay protected bool DoCheckCardFits(Card selected, CardView cv){ return ValidateRoutine_Common(selected, cv.CardModel, cv.Column, cv.Row) && (_match.GetPlayer(_match.PlayerAction).Routine.Validator(selected, cv.CardModel, cv.Column, cv.Row)) ; }// DoCheckCardFits
}// DrawCard public void ReturnCard(Card card){ card.Position = Card.POSITION_RESERVE; _cardsInReserve.Add (card); _cardsInPlay.Remove (card); }// ReturnCard