DragDropMatchingItem.cs for stickers 2d (on click stickers) Simplified version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
public class DragDropMatchingItem : MonoBehaviour | |
{ | |
private bool startDraging;//start draging flag | |
private bool clickedOnDest;//clicked on a destination flag | |
private GameObject currentMatchingItemSource;//current matching item | |
private GameObject currentMatchingItemDest;//current matching item | |
private bool matchDone;//match done flag | |
private Vector3 clickPoint;//click point | |
private Vector3 offset;//offset vector | |
public AudioClip youWin; | |
public AudioSource audioSource;//audio source | |
public Animator youWinAnimator;//you win animator | |
//Three Parameter to handle click on source and destinaiton | |
private Vector3 screenPoint; | |
public bool IsSourceSelected; | |
public bool IsTargetSelected; | |
void Start () | |
{ | |
if (audioSource == null) { | |
audioSource = Camera.main.GetComponent<AudioSource> ();//get the audio source | |
} | |
} | |
// Update is called once per frame | |
void Update () | |
{ | |
if (matchDone) { | |
return; | |
} | |
CheckMatchDone ();//check matching | |
if (matchDone) { | |
audioSource.Stop (); | |
//When the entire Match Done | |
if (youWinAnimator != null) { | |
audioSource.clip = youWin; | |
audioSource.Play ();//play win audio clip | |
youWinAnimator.SetBool ("isRunning", true);//show you win on the screen | |
} | |
} | |
if (Input.GetMouseButtonDown (0)) { | |
RayCast2D (Input.mousePosition); | |
} else if (Input.GetMouseButtonUp (0)) { | |
//empty | |
} | |
} | |
//2d screen raycast | |
private void RayCast2D (Vector3 pos) | |
{ | |
pos = Camera.main.ScreenToWorldPoint (pos);//get click position | |
RaycastHit2D rayCastHit2D = Physics2D.Raycast (pos, Vector2.zero); | |
if (rayCastHit2D.collider == null) { | |
return; | |
} | |
startDraging = true;//trigger start dragging | |
if (currentMatchingItemSource != null) { | |
Vector3 curScreenPoint = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); | |
Vector3 curPosition = Camera.main.ScreenToWorldPoint (curScreenPoint) + offset; | |
currentMatchingItemSource.transform.position = curPosition; | |
} | |
if (rayCastHit2D.collider.tag == "MatchingItem") { | |
//check if source is selected | |
if (rayCastHit2D.collider.GetComponent<MatchingItem> ().itemType == MatchingItem.ItemType.SOURCE) { | |
Debug.Log (rayCastHit2D.collider.tag); | |
Debug.Log (rayCastHit2D.collider.name + "1"); | |
IsSourceSelected = true; | |
IsTargetSelected = false; | |
currentMatchingItemSource = (GameObject)rayCastHit2D.transform.gameObject; | |
currentMatchingItemSource.GetComponent<Collider2D> ().enabled = true;//enable the collider for the current matching item | |
currentMatchingItemSource.GetComponent<FollowTarget> ().isRunning = true;//let the current matching item follows the target | |
audioSource.clip = currentMatchingItemSource.GetComponent<MatchingItem> ().dragAudioClip; | |
audioSource.Play ();//play drop audio clip | |
screenPoint = Camera.main.WorldToScreenPoint(currentMatchingItemSource.transform.position); | |
offset = currentMatchingItemSource.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); | |
//check if target is selected and if its matching the source | |
} else if (rayCastHit2D.collider.GetComponent<MatchingItem> ().itemType == MatchingItem.ItemType.DESTINATION) { | |
IsTargetSelected = true; | |
Debug.Log (rayCastHit2D.collider.tag); | |
Debug.Log (rayCastHit2D.collider.name + "2"); | |
currentMatchingItemDest = (GameObject)rayCastHit2D.transform.gameObject; | |
if (IsSourceSelected == true && IsTargetSelected == true) { | |
if (currentMatchingItemSource.GetComponent<MatchingItem> ().id == rayCastHit2D.collider.GetComponent<MatchingItem> ().id) { | |
Debug.Log ("Match"); | |
//correct match | |
clickedOnDest = true;//trigger clickedOnDest | |
startDraging = false;//reset startDragging flag | |
rayCastHit2D.collider.GetComponent<MatchingItem> ().connected = true;//trigger connected flag for the destination | |
currentMatchingItemDest.GetComponent<MatchingItem> ().connected = true;//trigger connected flag for the source | |
currentMatchingItemDest.GetComponent<SpriteRenderer> ().sprite = currentMatchingItemSource.GetComponent<SpriteRenderer> ().sprite; | |
audioSource.clip = currentMatchingItemSource.GetComponent<MatchingItem> ().dropAudioClip; | |
audioSource.Play ();//play drop audio clip | |
currentMatchingItemSource.SetActive (false);//hide the source | |
IsSourceSelected = false; | |
IsTargetSelected = false; | |
} else { | |
Debug.Log ("No Match"); | |
} | |
} | |
} else { | |
//Nothing | |
} | |
} | |
} | |
//Check if the matching for the entire matching items is done or not. | |
private void CheckMatchDone () | |
{ | |
MatchingItem [] matchingItems = (MatchingItem[])GameObject.FindObjectsOfType (typeof(MatchingItem)); | |
matchDone = true; | |
foreach (MatchingItem item in matchingItems) { | |
if (!item.connected) { | |
matchDone = false; | |
return; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment