using UnityEngine;
using System.Collections;

public enum ObjectStatus { 
    Empty = 0,
    Picked = 1,
    Used = 2
}

public class PlayerControl : MonoBehaviour {
    public Transform playerSprite;
    public float jumpStrength;
    public float speed;
    public float maxLeft;
    public float maxRight;
    public float goingUpStrength;
    public float pushStrength;
    public GameObject messageBox;
    public TextMesh messageLine1,
        messageLine2, messageLine3;
    public Transform bathroom;
    public Transform bedroom;
    public Transform secretroom;
    public Sprite[] slotlist;
    public SpriteRenderer[] slotcontainer;
    public Transform buttonLeft, buttonRight, buttonAction;
    public Transform buttonSlot1, buttonSlot2, buttonSlot3, buttonSlot4;
    public Transform pauseScreen;
    public Camera secondCamera;

    private bool _isfaceleft;
    private float oriZPos;
    private bool _isgrounded;
    private bool _isgoingup;
    private bool _isgoingdown;
    private bool _isbuttonupactive;
    private bool _isbuttondownactive;
    private bool _isjumpallowed;
    private bool _iswalking;
    private bool _isbebekcompleted;
    private bool _isjaketcompleted;

    private Transform stair;
    private Transform currentRoom;
    private Transform pushablething;
    private Transform sofa;
    private Transform sofaatas;
    private Transform cupboard;
    private Transform facedObject;

    private Animator animator;

    private bool[] messageStatus;
    private ObjectStatus[] objectStatus;
    private int[] objectSlot;
    private Vector3 worldPos;

    private Vector3 posisiPintuDapurKamarMandi = new Vector3(14.60003f, 105.115f, 0);
    private Vector3 posisiKamarMandiDapur = new Vector3(-4.050008f, 7.134999f, 0f);
    private Vector3 posisiBacaKamarTidur = new Vector3(3.287546f, 155.115f, 0);
    private Vector3 posisiKamarTidurBaca = new Vector3(6.418943f, 14.285f, 0);
    private Vector3 posisiGudangSecret = new Vector3(8.180013f, 205.115f, 0);
    private Vector3 posisiSecretGudang = new Vector3(10.57238f, 21.435f, 0);

    private static readonly int Kunci1 = 0,
        Pintu1 = 1, 
        Kunci2 = 2,
        Pintu2 = 3,
        Ikan = 4,
        Kunci3 = 5,
        Pintu3 = 6,
        Bebek = 7,
        Kunci4 = 8,
        Pintu4 = 9,
        Kucing = 10,
        Gunting = 11,
        Jaket = 12,
        KunciLemari = 13,
        KunciEmas = 14,
        Kunci5 = 15,
        Pintu5 = 16;
    //define ndk kene

    public bool IsJaketCompleted
    {
        get
        {
            return _isjaketcompleted;
        }
        set
        {
            _isjaketcompleted = value;
        }
    }

    public bool IsBebekCompleted
    {
        get
        {
            return _isbebekcompleted;
        }
        set
        {
            _isbebekcompleted = value;
        }
    }

    public bool IsWalking
    {
        get
        {
            return _iswalking;
        }
        set
        {
            if (_iswalking != value)
            {
                _iswalking = value;
                animator.SetBool("iswalking", _iswalking);
                if (_iswalking)
                {
                    audio.Play();
                }
                else
                {
                    audio.Stop();
                }
            }
        }
    }

    public bool IsJumpAllowed
    {
        get
        {
            return _isjumpallowed;
        }
        set
        {
            _isjumpallowed = value;
            if (_isjumpallowed)
            {
                animator.SetBool("isjumping", false);
            }
        }
    }

    public bool IsButtonDownActive
    {
        get
        {
            return _isbuttondownactive;
        }
        set
        {
            _isbuttondownactive = value;
            stair.GetComponent<Stairs>().buttonDown.gameObject.SetActive(_isbuttondownactive);
            if (_isbuttondownactive)
            {
                stair.GetComponent<Collider2D>().isTrigger = true;
            }
        }
    }

    public bool IsButtonUpActive
    {
        get
        {
            return _isbuttonupactive;
        }
        set
        {
            _isbuttonupactive = value;
            stair.GetComponent<Stairs>().buttonUp.gameObject.SetActive(_isbuttonupactive);
            if (_isbuttonupactive)
            {
                stair.GetComponent<Collider2D>().isTrigger = true;
            }
        }
    }

    public bool IsGoingDown
    {
        get
        {
            return _isgoingdown;
        }
        set
        {
            _isgoingdown = value;
        }
    }

    public bool IsGoingUp
    {
        get
        {
            return _isgoingup;
        }
        set
        {
            _isgoingup = value;
        }
    }

    public bool IsGrounded
    {
        get
        {
            return _isgrounded;
        }
        set
        {
            this._isgrounded = value;
            if (_isgrounded)
            {
                animator.SetBool("isjumping", false);
            }
        }
    }
    
    public bool IsFaceLeft {
        get
        {
            return _isfaceleft;
        }
        set
        {
            if (_isfaceleft != value)
            {
                Vector3 scale = playerSprite.transform.localScale;
                scale.x *= -1;
                playerSprite.transform.localScale = scale;
                _isfaceleft = value;
            }
        }
    }

    void showMessage(int index)
    {

        switch (index)
        { 
            case 0:
                if (objectStatus[Kunci1] != ObjectStatus.Picked)
                {
                    messageLine1.text = "The door is locked,";
                    messageLine2.text = "find the key to";
                    messageLine3.text = "open the door!";
                }
                else
                {
                    return;
                }
                break;
            case 1:
                if (messageStatus[0])
                {
                    messageLine1.text = "I see the key on";
                    messageLine2.text = "the top of cupboard,";
                    messageLine3.text = "I can jump here";
                }
                else
                {
                    return;
                }
                break;
            case 2:
                if (messageStatus[1])
                {
                    messageLine1.text = "Jump again to the";
                    messageLine2.text = "cupboard!";
                    messageLine3.text = " ";
                }
                else
                {
                    return;
                }
                break;
            case 3:
                messageLine1.text = " ";
                messageLine2.text = "Pick it!";
                messageLine3.text = " ";
                break;
            case 4:
                messageLine1.text = "Select the key to open";
                messageLine2.text = "the door!";
                messageLine3.text = "";
                break;
        }
        messageBox.SetActive(false);
        messageStatus[index] = true;
        messageBox.SetActive(true);
    }

    ObjectStatus getObjectStatus(int objectIndex)
    {
        return objectStatus[objectIndex];
    }

    void setStatus(int objectIndex, ObjectStatus status)
    {
        objectStatus[objectIndex] = status;
        if (status == ObjectStatus.Picked)
        {
            animator.SetTrigger("pick");
            addToObjectSlot(objectIndex);
        }
        else if (status == ObjectStatus.Used)
        {
            removeFromObjectSlot(objectIndex);
        }
    }

	void Start () {
        animator = playerSprite.GetComponent<Animator>();
        messageStatus = new bool[25];
        objectStatus = new ObjectStatus[25];
        objectSlot = new int[4];
        for (int i = 0; i < 4; i++)
        {
            objectSlot[i] = -1;
        }
        //Keterangan
        //0 : kunci room 1
        IsFaceLeft = false;
        oriZPos = this.transform.position.z;
        IsGrounded = true;
        IsJumpAllowed = true;
        IsGoingUp = false;
        IsBebekCompleted = false;
	}

    void addToObjectSlot(int index)
    {
        for (int i = 0; i < 4; i++)
        {
            if (objectSlot[i] == -1)
            {
                objectSlot[i] = index;
                slotcontainer[i].sprite = slotlist[index];
                break;
            }
        }
    }

    void removeFromObjectSlot(int index)
    {
        for (int i = 0; i < 4; i++)
        {
            if (objectSlot[i] == index)
            {
                objectSlot[i] = -1;
                slotcontainer[i].sprite = null;
                break;
            }
        }
    }

    void OnCollisionEnter2D(Collision2D collision) {
        if (collision.transform.tag.Equals("Ground"))
        {
            IsGrounded = true;
            currentRoom = collision.transform;
        }
        if (collision.transform.tag.Equals("Pushable"))
        {
            pushablething = collision.transform;
        }
        if (collision.transform.name.Equals("Sofa"))
        {
            sofa = collision.transform;
            if ((transform.position.y < 0.9f) || (secretroom.gameObject.activeSelf && transform.position.y < 206.0f))
            {
                showMessage(1);
                sofa.GetComponent<Collider2D>().isTrigger = true;
            }
            else
            {
                showMessage(2);
                sofa.GetComponent<Collider2D>().isTrigger = false;
                IsGrounded = true;
            }
        }
        if (collision.transform.name.Equals("Cupboard"))
        {
            cupboard = collision.transform;
            if (transform.position.y < 2.1f)
            {
                cupboard.GetComponent<Collider2D>().isTrigger = true;
            }
            else
            {
                cupboard.GetComponent<Collider2D>().isTrigger = false;
            }
        }
        else if (collision.transform.name.Equals("Pintu1"))
        {
            showMessage(0);
        }
        else if (collision.transform.name.Equals("SofaAtas"))
        { 
            if (transform.position.y < 1.75f)
            {
                sofaatas.GetComponent<Collider2D>().isTrigger = true;
            }
            else
            {
                sofaatas.GetComponent<Collider2D>().isTrigger = false;
            }
        }
        else if (collision.transform.name.Equals("PushArea"))
        {
            collision.transform.transform.parent.collider2D.isTrigger = true;
            collision.transform.transform.parent.rigidbody2D.isKinematic = true;
        }
        IsJumpAllowed = true;
    }

    void OnCollisionExit2D(Collision2D collision)
    {
        if (collision.transform.tag.Equals("Pushable"))
        {
            pushablething = null;
        }
        if (collision.transform.name.Equals("Cupboard"))
        {
            cupboard.GetComponent<Collider2D>().isTrigger = true;
        }
        IsJumpAllowed = false;
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.transform.name.Equals("StairSteper"))
        {
            stair = collision.transform.parent;
            IsButtonUpActive = true;
            if (currentRoom != null && IsGoingDown)
            {
                IsGoingDown = false;
                rigidbody2D.velocity = new Vector2(0, 0);
                //currentRoom.GetComponent<Collider2D>().isTrigger = true;
            }
        }
        else if (collision.transform.name.Equals("StairStoper"))
        {
            stair = collision.transform.parent;
            IsButtonDownActive = true;
            rigidbody2D.velocity = new Vector2(0, 0);
            if (IsGoingUp)
            {
                IsGoingUp = false;
                currentRoom.GetComponent<Collider2D>().isTrigger = false;
            }
        }
        if (collision.transform.tag.Equals("Ground"))
        {
            currentRoom = collision.transform;
        }
        if (collision.transform.name.Equals("Sofa"))
        {
            if (transform.position.y > 0.85f || (secretroom.gameObject.activeSelf && transform.position.y > 206.0f))
            {
                showMessage(2);
                sofa.GetComponent<Collider2D>().isTrigger = false;
                IsGrounded = true;
                IsJumpAllowed = true;
            }
            else
            {
                showMessage(1);
            }
        }
        if (collision.transform.name.Equals("SofaAtas"))
        {
            sofaatas = collision.transform;
            if (transform.position.y > 1.75f)
            {
                sofaatas.GetComponent<Collider2D>().isTrigger = false;
                IsGrounded = true;
                IsJumpAllowed = true;
            }
        }
        if (collision.transform.name.Equals("Cupboard"))
        {
            cupboard = collision.transform;
            if (transform.position.y > 3.9f)
            {
                cupboard.GetComponent<Collider2D>().isTrigger = false;
                IsJumpAllowed = true;
            }
            else
            {
                cupboard.GetComponent<Collider2D>().isTrigger = true;
            }
        }
        if (collision.transform.name.Equals("Kunci1"))
        {
            showMessage(3);
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Pintu1"))
        {
            if (objectStatus[Kunci1] == ObjectStatus.Picked)
            {
                showMessage(4);
            }
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("tv"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("lemaridibuka"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Saklar"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("LemariKunci"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("lemaridibuka2"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Pintu2"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Kulkas"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("LemariDapur"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Kunci3"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Pintu3"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("PintuKamarMandi"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Curtain"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Kunci4"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Pintu4"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("PintuKamar"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Cat"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("KunciLemari"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("LemariJaket")) 
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("PushArea"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("TumpukanKardus"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("PintuSecret"))
        {
            if (!(facedObject != null && facedObject.transform.name.Equals("TumpukanKardus")))
            {
                facedObject = collision.transform;
            }
        }
        else if (collision.transform.name.Equals("PintuSecret2"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("KunciEmas"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Kunci5"))
        {
            facedObject = collision.transform;
        }
        else if (collision.transform.name.Equals("Pintu5"))
        {
            facedObject = collision.transform;
        }
        //trigger ndk kene
    }

    void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.transform.name.Equals("StairSteper"))
        {
            IsButtonUpActive = false;
        }
        else if (collision.transform.name.Equals("StairStoper"))
        {
            IsButtonDownActive = false;
        }
        else if (collision.transform.name.Equals("PushArea"))
        {
            collision.transform.transform.parent.collider2D.isTrigger = true;
            collision.transform.transform.parent.rigidbody2D.isKinematic = true;
        }
        if (collision.transform.Equals(facedObject))
        {
            facedObject = null;
        }
    }

    private void DoGoingDown()
    {
        if (stair.GetComponent<Stairs>().isOnLeft)
        {
            IsFaceLeft = false;
        }
        else
        {
            IsFaceLeft = true;
        }
        IsGoingDown = true;
        rigidbody2D.AddForce(Vector2.zero);
        IsButtonDownActive = false;
        stair.GetComponent<Collider2D>().isTrigger = false;
        currentRoom.GetComponent<Collider2D>().isTrigger = true;
    }

    private void DoGoingUp()
    {
        if (stair.GetComponent<Stairs>().isOnLeft)
        {
            IsFaceLeft = true;
        }
        else
        {
            IsFaceLeft = false;
        }
        IsGoingUp = true;
        rigidbody2D.AddForce(Vector2.zero);
        stair.GetComponent<Collider2D>().isTrigger = false;
        IsButtonUpActive = false;
        stair.GetComponent<Collider2D>().isTrigger = false;
    }

    private Vector3 pos;

    private void normalizeZ()
    {
        if (this.transform.position.z != oriZPos)
        {
            pos = this.transform.position;
            pos.z = oriZPos;
            this.transform.position = pos;
        }
    }

    void checkingSlot(int slotIndex)
    {
        if (facedObject != null)
        {
            if (facedObject.name.Equals("Pintu1") &&
            objectStatus[Kunci1] == ObjectStatus.Picked &&
            objectSlot[slotIndex] == Kunci1)
            {
                setStatus(Kunci1, ObjectStatus.Used);
                setStatus(Pintu1, ObjectStatus.Used);
                Collider2D[] collider = facedObject.GetComponents<Collider2D>();
                foreach (Collider2D c in collider)
                {
                    c.enabled = false;
                }
                facedObject.GetComponent<Animation>().enabled = true;
                facedObject.gameObject.SetActive(false);
                facedObject.gameObject.SetActive(true);
            }
            else if (facedObject.name.Equals("Pintu2") &&
                objectStatus[Kunci2] == ObjectStatus.Picked &&
                objectSlot[slotIndex] == Kunci2)
            {
                setStatus(Kunci2, ObjectStatus.Used);
                setStatus(Pintu2, ObjectStatus.Used);
                Collider2D[] collider = facedObject.GetComponents<Collider2D>();
                foreach (Collider2D c in collider)
                {
                    c.enabled = false;
                }
                facedObject.GetComponent<Animation>().enabled = true;
                facedObject.gameObject.SetActive(false);
                facedObject.gameObject.SetActive(true);
            }
            else if (facedObject.name.Equals("Pintu3") &&
                objectStatus[Kunci3] == ObjectStatus.Picked &&
                objectSlot[slotIndex] == Kunci3)
            {
                setStatus(Kunci3, ObjectStatus.Used);
                setStatus(Pintu3, ObjectStatus.Used);
                bathroom.gameObject.SetActive(true);
                this.transform.position = posisiPintuDapurKamarMandi;
            }
            else if (facedObject.name.Equals("Pintu4") &&
                objectStatus[Kunci4] == ObjectStatus.Picked &&
                objectSlot[slotIndex] == Kunci4)
            {
                setStatus(Kunci4, ObjectStatus.Used);
                setStatus(Pintu4, ObjectStatus.Used);
                bedroom.gameObject.SetActive(true);
                this.transform.position = posisiBacaKamarTidur;
            }
            else if (facedObject.name.Equals("Cat") &&
                objectStatus[Ikan] == ObjectStatus.Picked &&
                objectSlot[slotIndex] == Ikan)
            {
                setStatus(Ikan, ObjectStatus.Used);
                setStatus(Kucing, ObjectStatus.Used);
                facedObject.GetComponent<Animator>().SetTrigger("walk");
            }
            else if (facedObject.name.Equals("LemariJaket") &&
                objectStatus[KunciLemari] == ObjectStatus.Picked &&
                objectSlot[slotIndex] == KunciLemari)
            {
                setStatus(KunciLemari, ObjectStatus.Used);
                facedObject.GetComponent<Animator>().SetTrigger("buka");
            }
            else if (facedObject.name.Equals("Pintu5") &&
            objectStatus[Kunci5] == ObjectStatus.Picked &&
            objectSlot[slotIndex] == Kunci5)
            {
                setStatus(Kunci5, ObjectStatus.Used);
                setStatus(Pintu5, ObjectStatus.Used);
                Collider2D[] collider = facedObject.GetComponents<Collider2D>();
                foreach (Collider2D c in collider)
                {
                    c.enabled = false;
                }
                facedObject.GetComponent<Animation>().enabled = true;
                facedObject.gameObject.SetActive(false);
                facedObject.gameObject.SetActive(true);
            }
            //slot ndk kene
        }
    }



	void Update ()
    {
        #region keluar tekan escape atau tombol back
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (pauseScreen.gameObject.activeSelf)
            {
                pauseScreen.gameObject.SetActive(false);
            }
            else
            {
                pauseScreen.gameObject.SetActive(true);
            }
        }
#endregion
        if (!IsGoingUp && !IsGoingDown)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                if (!IsFaceLeft)
                {
                    IsFaceLeft = true;
                }
                if (this.transform.position.x > maxLeft)
                {
                    this.transform.Translate(-speed, 0, 0);
                    IsWalking = true;
                    if (pushablething != null && IsGrounded)
                    {
                        rigidbody2D.AddForce(new Vector2(-pushStrength, 0));
                    }
                    normalizeZ();
                }
            }
            else if (Input.GetKey(KeyCode.RightArrow))
            {
                if (IsFaceLeft)
                {
                    IsFaceLeft = false;
                }
                if (this.transform.position.x < maxRight)
                {
                    this.transform.Translate(speed, 0, 0);
                    IsWalking = true;
                    if (pushablething != null && IsGrounded)
                    {
                        rigidbody2D.AddForce(new Vector2(pushStrength, 0));
                    }
                    normalizeZ();
                }
            }
            else
            {
                IsWalking = false;
            }
            if (Input.GetKeyDown(KeyCode.Alpha1))
            {
                checkingSlot(0);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha2))
            {
                checkingSlot(1);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha3))
            {
                checkingSlot(2);
            }
            else if (Input.GetKeyDown(KeyCode.Alpha4))
            {
                checkingSlot(3);
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                if (IsButtonUpActive)
                {
                    DoGoingUp();
                }
                else if (IsButtonDownActive)
                {
                    DoGoingDown();
                }
                else if (facedObject != null && (IsGrounded || IsJumpAllowed))
                {
                    if (facedObject.name.Equals("Kunci1"))
                    {
                        setStatus(Kunci1, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    else if (facedObject.name.Equals("tv"))
                    {
                        facedObject.GetComponent<Animator>().SetTrigger("tvnyala");
                    }
                    else if (facedObject.name.Equals("lemaridibuka"))
                    {
                        facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
                    }
                    else if (facedObject.name.Equals("Saklar"))
                    {
                        facedObject.GetComponent<Animator>().SetTrigger("ganti");
                    }
                    else if (facedObject.name.Equals("LemariKunci"))
                    {
                        if (facedObject.GetChild(0).gameObject.activeSelf)
                        {
                            setStatus(Kunci2, ObjectStatus.Picked);
                            Destroy(facedObject.GetChild(0).gameObject);
                        }
                        facedObject.GetComponent<Animator>().SetTrigger("buka");
                    }
                    else if (facedObject.name.Equals("lemaridibuka2"))
                    {
                        facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
                    }
                    else if (facedObject.name.Equals("Kulkas"))
                    {
                        if (facedObject.childCount > 0)
                        {
                            if (facedObject.GetChild(0).gameObject.activeSelf)
                            {
                                setStatus(Ikan, ObjectStatus.Picked);
                                Destroy(facedObject.GetChild(0).gameObject);
                            }
                        }
                        facedObject.GetComponent<Animator>().SetTrigger("bukakulkas");
                    }
                    else if (facedObject.name.Equals("LemariDapur"))
                    {
                        facedObject.GetComponent<Animator>().SetTrigger("bukalemari");
                    }
                    else if (facedObject.name.Equals("Kunci3"))
                    {
                        setStatus(Kunci3, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    else if (facedObject.name.Equals("PintuKamarMandi"))
                    {
                        bathroom.gameObject.SetActive(false);
                        this.transform.position = posisiKamarMandiDapur;
                    }
                    else if (facedObject.name.Equals("Pintu3") && objectStatus[Kunci3] == ObjectStatus.Used)
                    {
                        bathroom.gameObject.SetActive(true);
                        this.transform.position = posisiPintuDapurKamarMandi;
                    }
                    else if (facedObject.name.Equals("Curtain"))
                    {
                        if (facedObject.childCount > 0)
                        {
                            if (facedObject.GetChild(0).gameObject.activeSelf)
                            {
                                IsBebekCompleted = true;
                                animator.SetTrigger("pick");
                                Destroy(facedObject.GetChild(0).gameObject);
                            }
                        }
                        facedObject.GetComponent<Animator>().SetTrigger("bukacurtain");
                    }
                    else if (facedObject.name.Equals("Kunci4"))
                    {
                        setStatus(Kunci4, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    else if (facedObject.name.Equals("Pintu4") && objectStatus[Kunci4] == ObjectStatus.Used)
                    {
                        bedroom.gameObject.SetActive(true);
                        this.transform.position = posisiBacaKamarTidur;
                    }
                    else if (facedObject.name.Equals("PintuKamar"))
                    {
                        bedroom.gameObject.SetActive(false);
                        this.transform.position = posisiKamarTidurBaca;
                    }
                    else if (facedObject.name.Equals("KunciLemari"))
                    {
                        setStatus(KunciLemari, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    else if (facedObject.name.Equals("LemariJaket"))
                    {
                        if (facedObject.childCount > 0)
                        {
                            if (facedObject.GetChild(0).gameObject.activeSelf)
                            {
                                IsJaketCompleted = true;
                                animator.SetTrigger("pick");
                                Destroy(facedObject.GetChild(0).gameObject);
                            }
                        }
                    }
                    else if (facedObject.name.Equals("PintuSecret"))
                    {
                        secretroom.gameObject.SetActive(true);
                        this.transform.position = posisiGudangSecret;
                    }
                    else if (facedObject.name.Equals("PintuSecret2"))
                    {
                        secretroom.gameObject.SetActive(false);
                        this.transform.position = posisiSecretGudang;
                    }
                    else if (facedObject.name.Equals("KunciEmas"))
                    {
                        setStatus(KunciEmas, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    else if (facedObject.name.Equals("Kunci5"))
                    {
                        setStatus(Kunci5, ObjectStatus.Picked);
                        Destroy(facedObject.gameObject);
                    }
                    //action ndk kene
                }
                else if (IsJumpAllowed || IsGrounded)
                {
                    animator.SetBool("isjumping", true);
                    rigidbody2D.AddForce(new Vector2(0, jumpStrength));
                    IsGrounded = false;
                }
            }
            else if (Input.GetKey(KeyCode.Space))
            {
                if (facedObject != null)
                {
                    if (facedObject.name.Equals("PushArea"))
                    {
                        animator.SetBool("ispushing", true);
                        facedObject.transform.parent.collider2D.isTrigger = false;
                        facedObject.transform.parent.rigidbody2D.isKinematic = false;
                    }
                }
            }
            else if (Input.GetKeyUp(KeyCode.Space))
            {
                if (animator.GetBool("ispushing"))
                {
                    animator.SetBool("ispushing", false);
                }
            }
        }
        else if(IsGoingUp)
        {
            if (IsFaceLeft)
            {
                rigidbody2D.velocity = new Vector2(-goingUpStrength, 0);
            }
            else
            {
                rigidbody2D.velocity = new Vector2(goingUpStrength, 0);
            }
            IsWalking = true;
        }
        else if (IsGoingDown)
        {
            if (IsFaceLeft)
            {
                rigidbody2D.velocity = new Vector2(-(goingUpStrength / 4), -(goingUpStrength / 4));
            }
            else
            {
                rigidbody2D.velocity = new Vector2((goingUpStrength / 4), -(goingUpStrength / 4));
            }
            IsWalking = true;
        }
//#if UNITY_ANDROID
//        #region keluar tekan escape atau tombol back
//        if (Input.GetKeyDown(KeyCode.Escape))
//        {
//            Application.Quit();
//        }
//        #endregion
//        if (!IsGoingUp && !IsGoingDown)
//        {
//            if (Input.GetMouseButton(0))
//            {
//                Touch[] touches = Input.touches;
//                int length = Input.touchCount;
//                for (int i = 0; i < length; i++)
//                {
//                    worldPos = secondCamera.ScreenToWorldPoint(touches[i].position);
//                    if (buttonLeft.collider2D.OverlapPoint(worldPos))
//                    {
//                        if (!IsFaceLeft)
//                        {
//                            IsFaceLeft = true;
//                        }
//                        if (this.transform.position.x > maxLeft)
//                        {
//                            this.transform.Translate(-speed, 0, 0);
//                            IsWalking = true;
//                            if (pushablething != null && IsGrounded)
//                            {
//                                rigidbody2D.AddForce(new Vector2(-pushStrength, 0));
//                            }
//                            normalizeZ();
//                        }
//                    }
//                    else if (buttonRight.collider2D.OverlapPoint(worldPos))
//                    {
//                        if (IsFaceLeft)
//                        {
//                            IsFaceLeft = false;
//                        }
//                        if (this.transform.position.x < maxRight)
//                        {
//                            this.transform.Translate(speed, 0, 0);
//                            IsWalking = true;
//                            if (pushablething != null && IsGrounded)
//                            {
//                                rigidbody2D.AddForce(new Vector2(pushStrength, 0));
//                            }
//                            normalizeZ();
//                        }
//                    }
//                    if (buttonAction.collider2D.OverlapPoint(worldPos))
//                    {
//                        if (IsButtonUpActive)
//                        {
//                            DoGoingUp();
//                        }
//                        else if (IsButtonDownActive)
//                        {
//                            DoGoingDown();
//                        }
//                        else if (facedObject != null && (IsGrounded || IsJumpAllowed))
//                        {
//                            if (facedObject.name.Equals("Kunci1"))
//                            {
//                                setStatus(Kunci1, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            else if (facedObject.name.Equals("tv"))
//                            {
//                                facedObject.GetComponent<Animator>().SetTrigger("tvnyala");
//                            }
//                            else if (facedObject.name.Equals("lemaridibuka"))
//                            {
//                                facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
//                            }
//                            else if (facedObject.name.Equals("Saklar"))
//                            {
//                                facedObject.GetComponent<Animator>().SetTrigger("ganti");
//                            }
//                            else if (facedObject.name.Equals("LemariKunci"))
//                            {
//                                if (facedObject.GetChild(0).gameObject.activeSelf)
//                                {
//                                    setStatus(Kunci2, ObjectStatus.Picked);
//                                    Destroy(facedObject.GetChild(0).gameObject);
//                                }
//                                facedObject.GetComponent<Animator>().SetTrigger("buka");
//                            }
//                            else if (facedObject.name.Equals("lemaridibuka2"))
//                            {
//                                facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
//                            }
//                            else if (facedObject.name.Equals("Kulkas"))
//                            {
//                                if (facedObject.childCount > 0)
//                                {
//                                    if (facedObject.GetChild(0).gameObject.activeSelf)
//                                    {
//                                        setStatus(Ikan, ObjectStatus.Picked);
//                                        Destroy(facedObject.GetChild(0).gameObject);
//                                    }
//                                }
//                                facedObject.GetComponent<Animator>().SetTrigger("bukakulkas");
//                            }
//                            else if (facedObject.name.Equals("LemariDapur"))
//                            {
//                                facedObject.GetComponent<Animator>().SetTrigger("bukalemari");
//                            }
//                            else if (facedObject.name.Equals("Kunci3"))
//                            {
//                                setStatus(Kunci3, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            else if (facedObject.name.Equals("PintuKamarMandi"))
//                            {
//                                bathroom.gameObject.SetActive(false);
//                                this.transform.position = posisiKamarMandiDapur;
//                            }
//                            else if (facedObject.name.Equals("Pintu3") && objectStatus[Kunci3] == ObjectStatus.Used)
//                            {
//                                bathroom.gameObject.SetActive(true);
//                                this.transform.position = posisiPintuDapurKamarMandi;
//                            }
//                            else if (facedObject.name.Equals("Curtain"))
//                            {
//                                if (facedObject.childCount > 0)
//                                {
//                                    if (facedObject.GetChild(0).gameObject.activeSelf)
//                                    {
//                                        IsBebekCompleted = true;
//                                        animator.SetTrigger("pick");
//                                        Destroy(facedObject.GetChild(0).gameObject);
//                                    }
//                                }
//                                facedObject.GetComponent<Animator>().SetTrigger("bukacurtain");
//                            }
//                            else if (facedObject.name.Equals("Kunci4"))
//                            {
//                                setStatus(Kunci4, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            else if (facedObject.name.Equals("Pintu4") && objectStatus[Kunci4] == ObjectStatus.Used)
//                            {
//                                bedroom.gameObject.SetActive(true);
//                                this.transform.position = posisiBacaKamarTidur;
//                            }
//                            else if (facedObject.name.Equals("PintuKamar"))
//                            {
//                                bedroom.gameObject.SetActive(false);
//                                this.transform.position = posisiKamarTidurBaca;
//                            }
//                            else if (facedObject.name.Equals("KunciLemari"))
//                            {
//                                setStatus(KunciLemari, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            else if (facedObject.name.Equals("LemariJaket"))
//                            {
//                                if (facedObject.childCount > 0)
//                                {
//                                    if (facedObject.GetChild(0).gameObject.activeSelf)
//                                    {
//                                        IsJaketCompleted = true;
//                                        animator.SetTrigger("pick");
//                                        Destroy(facedObject.GetChild(0).gameObject);
//                                    }
//                                }
//                            }
//                            else if (facedObject.name.Equals("PintuSecret"))
//                            {
//                                secretroom.gameObject.SetActive(true);
//                                this.transform.position = posisiGudangSecret;
//                            }
//                            else if (facedObject.name.Equals("PintuSecret2"))
//                            {
//                                secretroom.gameObject.SetActive(false);
//                                this.transform.position = posisiSecretGudang;
//                            }
//                            else if (facedObject.name.Equals("KunciEmas"))
//                            {
//                                setStatus(KunciEmas, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            else if (facedObject.name.Equals("Kunci5"))
//                            {
//                                setStatus(Kunci5, ObjectStatus.Picked);
//                                Destroy(facedObject.gameObject);
//                            }
//                            //action ndk kene
//                        }
//                        else if (IsJumpAllowed || IsGrounded)
//                        {
//                            animator.SetBool("isjumping", true);
//                            rigidbody2D.AddForce(new Vector2(0, jumpStrength));
//                            IsGrounded = false;
//                        }
//                    }
//                }
//            }
//            else
//            {
//                IsWalking = false;
//            }
//            bool spaceondown = false;
//            if (Input.GetMouseButtonDown(0))
//            { 
//                Touch[] touches = Input.touches;
//                int length = Input.touchCount;
//                for (int i = 0; i < length; i++)
//                {
//                    worldPos = secondCamera.ScreenToWorldPoint(touches[i].position);
//                    if (buttonSlot1.collider2D.OverlapPoint(worldPos))
//                    {
//                        checkingSlot(0);
//                    }
//                    else if (buttonSlot2.collider2D.OverlapPoint(worldPos))
//                    {
//                        checkingSlot(1);
//                    }
//                    else if (buttonSlot3.collider2D.OverlapPoint(worldPos))
//                    {
//                        checkingSlot(2);
//                    }
//                    else if (buttonSlot4.collider2D.OverlapPoint(worldPos))
//                    {
//                        checkingSlot(3);
//                    }
//                    //if (buttonAction.collider2D.OverlapPoint(worldPos))
//                    //{
//                    //    spaceondown = true;
//                    //    if (IsButtonUpActive)
//                    //    {
//                    //        DoGoingUp();
//                    //    }
//                    //    else if (IsButtonDownActive)
//                    //    {
//                    //        DoGoingDown();
//                    //    }
//                    //    else if (facedObject != null && (IsGrounded || IsJumpAllowed))
//                    //    {
//                    //        if (facedObject.name.Equals("Kunci1"))
//                    //        {
//                    //            setStatus(Kunci1, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        else if (facedObject.name.Equals("tv"))
//                    //        {
//                    //            facedObject.GetComponent<Animator>().SetTrigger("tvnyala");
//                    //        }
//                    //        else if (facedObject.name.Equals("lemaridibuka"))
//                    //        {
//                    //            facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
//                    //        }
//                    //        else if (facedObject.name.Equals("Saklar"))
//                    //        {
//                    //            facedObject.GetComponent<Animator>().SetTrigger("ganti");
//                    //        }
//                    //        else if (facedObject.name.Equals("LemariKunci"))
//                    //        {
//                    //            if (facedObject.GetChild(0).gameObject.activeSelf)
//                    //            {
//                    //                setStatus(Kunci2, ObjectStatus.Picked);
//                    //                Destroy(facedObject.GetChild(0).gameObject);
//                    //            }
//                    //            facedObject.GetComponent<Animator>().SetTrigger("buka");
//                    //        }
//                    //        else if (facedObject.name.Equals("lemaridibuka2"))
//                    //        {
//                    //            facedObject.GetComponent<Animator>().SetTrigger("bukalaci");
//                    //        }
//                    //        else if (facedObject.name.Equals("Kulkas"))
//                    //        {
//                    //            if (facedObject.childCount > 0)
//                    //            {
//                    //                if (facedObject.GetChild(0).gameObject.activeSelf)
//                    //                {
//                    //                    setStatus(Ikan, ObjectStatus.Picked);
//                    //                    Destroy(facedObject.GetChild(0).gameObject);
//                    //                }
//                    //            }
//                    //            facedObject.GetComponent<Animator>().SetTrigger("bukakulkas");
//                    //        }
//                    //        else if (facedObject.name.Equals("LemariDapur"))
//                    //        {
//                    //            facedObject.GetComponent<Animator>().SetTrigger("bukalemari");
//                    //        }
//                    //        else if (facedObject.name.Equals("Kunci3"))
//                    //        {
//                    //            setStatus(Kunci3, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        else if (facedObject.name.Equals("PintuKamarMandi"))
//                    //        {
//                    //            bathroom.gameObject.SetActive(false);
//                    //            this.transform.position = posisiKamarMandiDapur;
//                    //        }
//                    //        else if (facedObject.name.Equals("Pintu3") && objectStatus[Kunci3] == ObjectStatus.Used)
//                    //        {
//                    //            bathroom.gameObject.SetActive(true);
//                    //            this.transform.position = posisiPintuDapurKamarMandi;
//                    //        }
//                    //        else if (facedObject.name.Equals("Curtain"))
//                    //        {
//                    //            if (facedObject.childCount > 0)
//                    //            {
//                    //                if (facedObject.GetChild(0).gameObject.activeSelf)
//                    //                {
//                    //                    IsBebekCompleted = true;
//                    //                    animator.SetTrigger("pick");
//                    //                    Destroy(facedObject.GetChild(0).gameObject);
//                    //                }
//                    //            }
//                    //            facedObject.GetComponent<Animator>().SetTrigger("bukacurtain");
//                    //        }
//                    //        else if (facedObject.name.Equals("Kunci4"))
//                    //        {
//                    //            setStatus(Kunci4, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        else if (facedObject.name.Equals("Pintu4") && objectStatus[Kunci4] == ObjectStatus.Used)
//                    //        {
//                    //            bedroom.gameObject.SetActive(true);
//                    //            this.transform.position = posisiBacaKamarTidur;
//                    //        }
//                    //        else if (facedObject.name.Equals("PintuKamar"))
//                    //        {
//                    //            bedroom.gameObject.SetActive(false);
//                    //            this.transform.position = posisiKamarTidurBaca;
//                    //        }
//                    //        else if (facedObject.name.Equals("KunciLemari"))
//                    //        {
//                    //            setStatus(KunciLemari, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        else if (facedObject.name.Equals("LemariJaket"))
//                    //        {
//                    //            if (facedObject.childCount > 0)
//                    //            {
//                    //                if (facedObject.GetChild(0).gameObject.activeSelf)
//                    //                {
//                    //                    IsJaketCompleted = true;
//                    //                    animator.SetTrigger("pick");
//                    //                    Destroy(facedObject.GetChild(0).gameObject);
//                    //                }
//                    //            }
//                    //        }
//                    //        else if (facedObject.name.Equals("PintuSecret"))
//                    //        {
//                    //            secretroom.gameObject.SetActive(true);
//                    //            this.transform.position = posisiGudangSecret;
//                    //        }
//                    //        else if (facedObject.name.Equals("PintuSecret2"))
//                    //        {
//                    //            secretroom.gameObject.SetActive(false);
//                    //            this.transform.position = posisiSecretGudang;
//                    //        }
//                    //        else if (facedObject.name.Equals("KunciEmas"))
//                    //        {
//                    //            setStatus(KunciEmas, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        else if (facedObject.name.Equals("Kunci5"))
//                    //        {
//                    //            setStatus(Kunci5, ObjectStatus.Picked);
//                    //            Destroy(facedObject.gameObject);
//                    //        }
//                    //        //action ndk kene
//                    //    }
//                    //    else if (IsJumpAllowed || IsGrounded)
//                    //    {
//                    //        animator.SetBool("isjumping", true);
//                    //        rigidbody2D.AddForce(new Vector2(0, jumpStrength));
//                    //        IsGrounded = false;
//                    //    }
//                    //}
//                }
//            }
//            bool spaceonhold = false;
//            if (!spaceondown)
//            {
//                if (Input.GetMouseButton(0))
//                { 
//                    Touch[] touches = Input.touches;
//                    int length = Input.touchCount;
//                    for (int i = 0; i < length; i++)
//                    {
//                        worldPos = secondCamera.ScreenToWorldPoint(touches[i].position);
//                        if (buttonAction.collider2D.OverlapPoint(worldPos))
//                        {
//                            spaceonhold = true;
//                            if (facedObject != null)
//                            {
//                                if (facedObject.name.Equals("PushArea"))
//                                {
//                                    animator.SetBool("ispushing", true);
//                                    facedObject.transform.parent.collider2D.isTrigger = false;
//                                    facedObject.transform.parent.rigidbody2D.isKinematic = false;
//                                }
//                            }
//                        }
//                    }
//                }
//            }
//            if (!spaceonhold)
//            {
//                if (Input.GetMouseButtonUp(0))
//                {
//                    Touch[] touches = Input.touches;
//                    int length = Input.touchCount;
//                    for (int i = 0; i < length; i++)
//                    {
//                        worldPos = secondCamera.ScreenToWorldPoint(touches[i].position);
//                        if (buttonAction.collider2D.OverlapPoint(worldPos))
//                        {
//                            if (animator.GetBool("ispushing"))
//                            {
//                                animator.SetBool("ispushing", false);
//                            }
//                        }
//                    }
//                }
//            }
//        }
//        else if(IsGoingUp)
//        {
//            if (IsFaceLeft)
//            {
//                rigidbody2D.velocity = new Vector2(-goingUpStrength, 0);
//            }
//            else
//            {
//                rigidbody2D.velocity = new Vector2(goingUpStrength, 0);
//            }
//            IsWalking = true;
//        }
//        else if (IsGoingDown)
//        {
//            if (IsFaceLeft)
//            {
//                rigidbody2D.velocity = new Vector2(-(goingUpStrength / 4), -(goingUpStrength / 4));
//            }
//            else
//            {
//                rigidbody2D.velocity = new Vector2((goingUpStrength / 4), -(goingUpStrength / 4));
//            }
//            IsWalking = true;
//        }
//#endif
	}
}
