using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwitchScript : MonoBehaviour {

    float timer = 0f;
    public AudioSource SwitchSound;
    public bool switched = false;

    // Use this for initialization 
    void Start () {
	}
	
	// Update is called once per frame
	void Update () {
        if (!switched)
        {
            timer += Time.deltaTime;
            Vector3 p = this.transform.position;
            p.x = Mathf.Abs(Mathf.Cos(timer)) * 6.3f;
            p.y = Mathf.Abs(Mathf.Sin(timer)) * 5.5f - 2;
            this.transform.position = p;
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (switched) return; // already done

        if (collision.gameObject.tag == "Shot" || collision.gameObject.GetComponent<GuyScript>())
        { // can only be triggered by a guy or a shot

            GameObject[] gameObjects;
            gameObjects = GameObject.FindGameObjectsWithTag("Door");
            gameObjects[0].GetComponent<DoorScript>().TriggerDoor();
            SwitchSound.Play();
            switched = true;

            this.GetComponent<Collider2D>().enabled = false;
        }

    }
}
