DoorScript.cs 1.51 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
using UnityEngine;
using System.Collections;

public class DoorScript : MonoBehaviour {

    public enum State
    {
        closed,
        open,
        locked
    }

    public KeyColor doorColor;
    public State state;
    private Collider2D collider;

    void Start()
    {
        collider = GetComponent<Collider2D>();
    }

    // Update is called once per frame
    void Update()
    {
        if(state == State.closed)
        {
            //change sprite to be a closed door sprite
            collider.enabled = true;
        }
        if(state == State.locked)
        {
            //change sprite to be a locked door sprite
            collider.enabled = true;
        }
        if(state == State.open)
        {
            //change sprite to be an open door sprite
            collider.enabled = false;
        }
    }

    void unlockDoor()
    {
        if(state == State.locked)
        {
            state = State.open;
        }
        Debug.Log("Door Unlocked");
    }

Ashley's avatar
Ashley committed
51
    public void OnCollisionEnter2D(Collision2D coll)
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    {
        if(coll.gameObject.tag == "Player")
        {
            if (state == State.locked)
            {
                KeyScript[] keyScripts = coll.gameObject.GetComponents<KeyScript>();
                foreach (KeyScript key in keyScripts)
                {
                    if (key.color == doorColor)
                    {
                        unlockDoor();
                        break;
                    }
                }
            }
        }
        
    }
}