/*================================================================
Product:    GGJ-2017
Developer:  Onur Tanrıkulu
Company:    Onur Tanrikulu
Date:       22/01/2017 13:22

Copyright (c) 2017 Onur Tanrikulu. All rights reserved.
================================================================*/

using UnityEngine;

public class DoorKeyController : MonoBehaviour
{
   #region Fields
    public WallController wallController;
    private float animateDelay;
    private SpriteRenderer spriteRenderer;
    private Sprite[] sprites;
    private int spriteId;
   #endregion
 
   #region Constructors
   // code here
   #endregion
 
   #region Properties
   // code here
   #endregion
 
   #region Methods
    private void OnTriggerEnter2D(Collider2D collider)
    {
        GameObject go = collider.gameObject;

        if (go.tag.Equals("Box"))
        {
            wallController.Open();
            spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 0);
        }
    }

    private void OnTriggerExit2D(Collider2D collider)
    {
        GameObject go = collider.gameObject;

        if (go.tag.Equals("Box"))
        {
            wallController.Close();
            spriteRenderer.color = new Color(spriteRenderer.color.r, spriteRenderer.color.g, spriteRenderer.color.b, 1);
        }
    }

    private void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        sprites = Resources.LoadAll<Sprite>("Sprites/doorKey");
    }

    private void Update()
    {          
        float dt = Time.deltaTime;
        animateDelay += dt;

        if (animateDelay > 1 / 60)
        {
            animateDelay = 0;
            spriteRenderer.sprite = sprites[spriteId];

            if (spriteId == sprites.Length - 1)
            {
                spriteId = 0;
            }
            else
            {
                spriteId++;
            }
        }
    }
   #endregion
}
