void Card(GameObject card) { if (card.GetComponent <CardFace>().selectable) { if (selected == null || selected != card) { selected = card; originPosition = selected.transform.position; CardColorYellow(selected); //ignore by raycast if they are being moveds selected.layer = 2; } else if (selected == card) { int foundationIndx = solitaire.IntoAllFoundations(selected); if (selected.transform.childCount == 0 && foundationIndx == -1) { //when the same card clicked twice //put the card into fountains if possible //otherwise put the card into available free cell //don't move the card if it already in the free cell int freeCellIndx = solitaire.FreeCell(""); if (freeCellIndx != -1) { ICommand command = new FreeCellCommand(freeCellIndx, selected, originPosition); CommandInvoker.AddCommand(command); } } else if (foundationIndx != -1) { ICommand command = new FoundationCommand(selected, originPosition, foundationIndx); CommandInvoker.AddCommand(command); } selected.layer = 0; selected = null; } } else if (selected != null) { CardColorWhite(selected); selected = null; //warn the player that this card is not selectable } }
void MouseInput() { if (Input.GetMouseButtonDown(0)) { if (selected != null) { ResetCard(selected); } //get origin mouse position for drag calculation mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)); RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit) { if (hit.collider.CompareTag("Card")) { Card(hit.collider.gameObject); } if (hit.collider.CompareTag("Foundation")) { selected = null; } } else { selected = null; } } //when dragging, card moves with mouse if (Input.GetMouseButton(0)) { float deltaX = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)).x - mousePosition.x; float deltaY = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)).y - mousePosition.y; if (selected != null) { selected.transform.position = new Vector3(originPosition.x + deltaX, originPosition.y + deltaY, -0.35f); } } if (Input.GetMouseButtonUp(0)) { if (selected != null && selected.transform.position != originPosition) { Debug.Log("mouse up"); mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0f)); RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector2.zero); if (hit) { //when card drag to another card if (hit.collider.CompareTag("Card")) { Debug.Log(hit.collider.name); if (Stackable(selected, hit.collider.gameObject)) { ICommand command = new StackCommand(selected, hit.collider.gameObject, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } //when card drag to foundation area if (hit.collider.CompareTag("Foundation")) { int foundationIndx = solitaire.IntoAllFoundations(selected); if (foundationIndx == -1) { ResetCard(selected); } else { ICommand command = new FoundationCommand(selected, originPosition, foundationIndx); CommandInvoker.AddCommand(command); } CardColorWhite(selected); selected = null; } //when card drag to an empty cascade if (hit.collider.CompareTag("Empty Cascade")) { //check if the cascade is really empty if (hit.collider.transform.childCount == 0) { ICommand command = new EmptyCascadeCommand(selected, hit.collider.gameObject, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } //when card drag to free cell if (hit.collider.CompareTag("Free Cell")) { if (selected.transform.childCount == 0) { int freeCellIndx = hit.collider.transform.GetSiblingIndex(); if (solitaire.freeCells[freeCellIndx] == "") { ////remove from the free cell if the card is in it ICommand command = new FreeCellCommand(freeCellIndx, selected, originPosition); CommandInvoker.AddCommand(command); CardColorWhite(selected); selected.layer = 0; selected = null; } } } } else { ResetCard(selected); //selected = null; } if (selected != null) { ResetCard(selected, false); } } } }