Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
4.7 kB
1
Indexable
Never
using System.Collections;
using UnityEngine;

class StuffMover : MonoBehaviour
{
    //Setting

    [SerializeField] float minZ;
    [SerializeField] float maxZ;
    [SerializeField] float heightOnDrag;

    private bool dragging = false;
    private float distance;

    //References
    [SerializeField] GameObject shadow;
    [SerializeField] GameObject shadowPlace;
    [SerializeField] GameObject fixCube;
    [SerializeField] GameObject countText;

    Rigidbody rb;
    LevelManager levelManager;


    //bool
    public bool isPlaced;
    public bool isMoveable = true;
    public bool isIdleShadow;
    public bool isFake;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
        shadow.gameObject.SetActive(false);

        levelManager = FindObjectOfType<LevelManager>();
        countText.gameObject.SetActive(false);
    }

    void OnMouseDown()
    {
        if (isMoveable)
        {

            shadow.gameObject.SetActive(true);
            distance = Vector3.Distance(transform.position, Camera.main.transform.position);
            dragging = true;
            //GetComponent<DotweenAnimation>().SetTween();
            GetComponent<Outline>().enabled = true;
            GetComponent<BoxCollider>().enabled = false;
            transform.rotation = Quaternion.identity;
            rb.freezeRotation = true;
            countText.gameObject.SetActive(true);
            shadow.transform.GetComponent<BoxCollider>().enabled = false;
            fixCube.gameObject.SetActive(false);
        }
        
    }

    void OnMouseUp()
    {
        if (isMoveable)
        {
            

            if (shadow.transform.position.z > minZ && shadow.transform.position.z < maxZ && !isPlaced && !isFake)
            {
                fixCube.gameObject.SetActive(false);
                //Clear
                isPlaced = true;
                
                isMoveable = false;
                shadow.gameObject.SetActive(true);
                shadowPlace.gameObject.SetActive(false);

                levelManager.objectCount++;
                levelManager.CheckObjectCount();
                countText.gameObject.SetActive(true);
                
                rb.useGravity = true;
                GetComponent<BoxCollider>().isTrigger = true;
                fixCube.transform.GetComponent<BoxCollider>().isTrigger = true;

                shadow.transform.GetComponent<BoxCollider>().enabled = true;               
                shadow.transform.GetComponent<Rigidbody>().useGravity = true;
            }

            if (!isPlaced)
            {
                shadow.gameObject.SetActive(false);
                rb.freezeRotation = false;
                countText.gameObject.SetActive(false);

                rb.useGravity = true;
                
            }

            dragging = false;

            shadow.transform.GetComponent<Rigidbody>().useGravity = true;
            GetComponent<Outline>().enabled = false;
            GetComponent<BoxCollider>().enabled = true;
            fixCube.gameObject.SetActive(true);

        }
    }
    
    void Update()
    {
        if (isMoveable)
        {
            if (dragging)
            {

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                Vector3 rayPoint = ray.GetPoint(distance);

                transform.position = Vector3.Lerp(transform.position, new Vector3(rayPoint.x, heightOnDrag, rayPoint.z), 0.3f);

                shadow.transform.position = new Vector3(shadow.transform.position.x, transform.position.y, transform.position.z);

                if (shadow.transform.position.z > minZ && shadow.transform.position.z < maxZ && !isPlaced && !isFake)
                {
                    GetComponent<OutlineClear>().enabled = true;
                }
                else
                {
                    GetComponent<OutlineClear>().enabled = false;
                }

                rb.useGravity = false;
                shadow.transform.GetComponent<Rigidbody>().useGravity = false;
            }
        }

        
    }



    public void AnimNow()
    {
        if (!isIdleShadow && shadow.transform.GetComponent<DotweenAnimation>() != null)
        {
            shadow.transform.GetComponent<DotweenAnimation>().SetTween();

        }
    }



    private void OnTriggerEnter(Collider other)
    {
        if (other.transform.name.StartsWith("Floor"))
        {
            rb.useGravity = false;
            rb.isKinematic = true;
            transform.position = new Vector3(transform.position.x, transform.position.y+0.05f, transform.position.z);

        }
    }
}