using UnityEngine;

public class BrokenLadder : MonoBehaviour {

    Ray ray;
    RaycastHit hit;

    Collider myCollider;

    private const int barreauxAPlacer = 3;
    public static int barreauxPlaces = 0;

    void Start() {
        myCollider = GetComponent<Collider>();
    }

    void Update() {
        if (PlayerBehaviour.IsInLadder()) {
            ray = Camera.main.ScreenPointToRay(new Vector2(Screen.width / 2, Screen.height / 2));
            if (Physics.Raycast(ray, out hit) && hit.collider != myCollider) {
                PlayerBehaviour.isInLadder = false;
            }
        }
    }

    void OnCollisionEnter(Collision col) {
        if (col.gameObject.tag == "Player" && barreauxPlaces == barreauxAPlacer) {
            PlayerBehaviour.SetLadder(true);
        }
    }

    void OnCollisionExit(Collision col) {
        if (col.gameObject.tag == "Player" && barreauxPlaces == barreauxAPlacer) {
            PlayerBehaviour.SetLadder(false);
        }
    }
}