/*
 * +================================================================================+
 *      __  ___  ______________  __  __  ______________   ____
 *     / / / / |/ /  _/_  __/\ \/ / / / / /_  __/  _/ /  / __/
 *    / /_/ /    // /  / /    \  / / /_/ / / / _/ // /___\ \
 *    \____/_/|_/___/ /_/     /_/  \____/ /_/ /___/____/___/
 *
 * +================================================================================+
 *
 *    "THE BEER-WARE LICENSE"
 *    -----------------------
 *
 *    As long as you retain this notice you can do whatever you want with this
 *    stuff. However it is expressly forbidden to sell or commercially distribute
 *    Unity Utils or parts of it apart from your games.
 *    If you meet any of the authors some day, and you think this stuff is
 *    worth it, you can buy them a beer in return.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *    THE SOFTWARE.
 *
 * +================================================================================+
 *
 *    Authors:
 *    David Krummenacher (dave@timbookedtwo.com)
 *
 */

using UnityEngine;
using System.Collections.Generic;

namespace Utils.Behaviours {

	/// <summary>
	/// Destroy game objects based on a trigger.
	/// </summary>
	public class DestroyGameObjectTrigger : MonoBehaviour {

		[Tooltip("The game objects that trigger.")]
		/// <summary>
		/// The game objects that trigger.
		/// </summary>
		public List<GameObject> gameObjectsThatTrigger;
		[Tooltip("The game objects to destroy.")]
		/// <summary>
		/// The game objects to destroy.
		/// </summary>
		public List<GameObject> gameObjectsToDestroy;
		[Tooltip("Determines wheter the script destroys itself.")]
		/// <summary>
		/// Determines wheter the script destroys itself.
		/// </summary>
		public bool DestroySelf = true;

		void OnTriggerEnter(Collider collider) {
			// If collider is one of the objects that can trigger
			if (gameObjectsThatTrigger.Contains(collider.gameObject)) {
				// Destroy game objects
				foreach(GameObject go in gameObjectsToDestroy) {
					if (go != null) Destroy(go);
				}
	
				// Destroy yourself or clear the list with the now inexistent game objects
				if (DestroySelf) Destroy(gameObject);
				else gameObjectsToDestroy.Clear();
			}
		}
	}
}
