using UnityEngine;
using System.Collections;

public class MusicProjectile : Projectile
{
    public PlayerMovement playerScript;

    public float WaveAmplitude;
    public float WaveFrequency = 1.0f;
    public float MoveSpeedTilesPerSecond;
    float fWaveTimer;
    float fStartingYValue;
    float fXDelta;

	// Use this for initialization
	void Start () 
    {
	
	}
	
	// Update is called once per frame
	public override void Update () 
    {
        fRemainingLifetime -= Time.smoothDeltaTime;
        if (fRemainingLifetime < 0.0f)
        {
            Destroy(gameObject);
            return;
        }

        fWaveTimer += Time.smoothDeltaTime;
        if (fWaveTimer > WaveFrequency)
            fWaveTimer -= WaveFrequency;

        Vector3 vNewPosition = transform.position;
        vNewPosition.x += fXDelta * Time.smoothDeltaTime;
        vNewPosition.y = fStartingYValue + Mathf.Sin(6.28f * (fWaveTimer/ WaveFrequency)) * WaveAmplitude;

        transform.position = vNewPosition;

	}

    public override void Launch(Vector2 vDirection)
    {
        fRemainingLifetime = MaxLifetime;
        fStartingYValue = transform.position.y;
        fXDelta = vDirection.x * MoveSpeedTilesPerSecond;

    }

    protected virtual bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if (go != null)
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (playerScript == null && !GetPlayerScript())
            return;

        if (other.gameObject == playerScript.gameObject)
        {
            playerScript.ChangeFrustration(-4.0f);
            Destroy(gameObject);

            playerScript.SaySomething("I'm soothed! Oh no!", 1.0f);
        }
    }
}
