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

public class SailSpeed : MonoBehaviour {

	float newSpeed;
	public float speed;
	Transform thisTF;


	void Start () {
		thisTF = GetComponent<Transform> ();
	}

	[SerializeField] AudioClip hitAndBurn;
	[SerializeField] AudioClip hit;

	void OnTriggerEnter(Collider col)
	{
		if(col.tag == "Rock")
		{
			particle.Play();
			GetComponent<AudioSource> ().PlayOneShot (hitAndBurn);
			scrapeTime = Time.time + 2f;
		}
	}

	void OnCollisionEnter()
	{
		GetComponent<AudioSource> ().PlayOneShot (hit);
	}
	bool scraping;
	float scrapeTime;
	[SerializeField] ParticleSystem particle;
	void Update()
	{
		scraping = (scrapeTime > Time.time);
		if (scraping) {
			AddSpeed (thisTF.forward);
		}
	}

	public void AddSpeed (Vector3 direction) 
	{
		float addition = Vector3.Distance (thisTF.forward, direction.normalized);
		if ((1.4f - addition) > 0f)
			newSpeed += 1.4f - addition;
	}

	void LateUpdate()
	{
		speed = newSpeed;
		newSpeed = 0f;
	}

}
