Drag

 avatar
unknown
plain_text
20 days ago
4.0 kB
3
Indexable
using Unity.VisualScripting;
using UnityEngine;

public class DragAndDrop : MonoBehaviour
{
    public GameObject targetPos;
    

    private bool moving;
    private GameObject objectToMove;
    private float startPosX;
    private float startPosY;
    public SpawnComanda spawnComanda;

    public SpriteRenderer ingredientSprite;

    public PointHandler pointHandler;

    void Update()
    {
        if (moving && objectToMove != null)
        {
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            objectToMove.transform.position = new Vector3(
                mousePos.x - startPosX,
                mousePos.y - startPosY,
                objectToMove.transform.position.z
            );
        }
    }

    private void OnMouseDown()
    {
        if (Input.GetMouseButtonDown(0))
        {
            // Create a clone at the original's position and rotation, preserving its parent
            GameObject clone = Instantiate(this.gameObject, this.transform.position, this.transform.rotation, this.transform.parent);
            // Remove the DragAndDrop script from the clone to avoid duplicate behavior
            Destroy(clone.GetComponent<DragAndDrop>());
            objectToMove = clone;

            // Calculate the offset in world space
            Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            startPosX = mousePos.x - clone.transform.position.x;
            startPosY = mousePos.y - clone.transform.position.y;
            moving = true;
        }
    }

    private void OnMouseUp()
    {
        moving = false;
        if (objectToMove != null)
        {
            // Check if the clone is close to the caldero in world space
            if (Mathf.Abs(objectToMove.transform.position.x - targetPos.transform.position.x) <= 2f &&
                Mathf.Abs(objectToMove.transform.position.y - targetPos.transform.position.y) <= 1.5f)
            {
                // Snap the clone to the caldero's position and deactivate it
                objectToMove.transform.position = targetPos.transform.position;
                objectToMove.SetActive(false);
                // Uncomment to add points if needed
                // GameObject.Find("PointHandler").GetComponent<ganar>().AddPoints();

                for (int i = 0; i < 4; i++){
                    if(spawnComanda.currentActiveComandaCorrectIngredients[spawnComanda.currentActiveComanda][i] == this.gameObject.tag) {
                        if (spawnComanda.ingredientIsCorrect[i] == false){
                            spawnComanda.ingredientIsCorrect[i] = true;
                            SetColor(Color.green); // Ingrediente correcto
        


                            i =4;
                        }
                        else{
                            DoubleIngredient();
                            i=4;
                        }
                    }
                    else if (i == 3){
                
                        IncorrectIngredient();
                    }
                }
            }
            else
            {
                // Destroy the clone if not dropped correctly
                Destroy(objectToMove);
            }
            // Clear the reference
            objectToMove = null;
        }
    }
      void IncorrectIngredient(){
        pointHandler.score = pointHandler.score - 20;
        Debug.Log("Ingrediente incorrecto");
        SetColor(Color.red); // Color rojo para ingrediente incorrecto
    }
    void DoubleIngredient(){
        pointHandler.score = pointHandler.score - 10;
        Debug.Log("Ingrediente duplicado");
        SetColor(Color.yellow); // Color amarillo para ingrediente duplicado

    }

    private void SetColor(Color newColor)
    {
        if (ingredientSprite != null)
        {
            ingredientSprite.color = newColor;
        }
    }




}
Editor is loading...
Leave a Comment