using UnityEngine;
using System.Collections;
using Gamelogic.Grids2;

public class RenderSplat : Render
{
    private GridPoint2 start;
    private GridPoint2 target;
    private Transform shotPrefab;
    private Transform hitPrefab;

    private Transform shot;
    private Vector2 startPoint;
    private Vector2 endPoint;

    public RenderSplat(GridPoint2 start, GridPoint2 target, Transform shotPrefab, Transform hitPrefab)
    {
        this.start = start;
        this.target = target;
        this.shotPrefab = shotPrefab;
        this.hitPrefab = hitPrefab;

        renderDuration = 0.15f;
        nextRenderDelay = 0.016f;

        startPoint = GameMap.Cell[start].transform.position;
        endPoint = GameMap.Cell[target].transform.position;
    }

    public override void Initialize()
    {
        Vector2 muzzlePos = startPoint + 0.12f * (endPoint - startPoint).normalized;

        shot = GameObject.Instantiate(shotPrefab);
        shot.position = muzzlePos;
    }

    override public void RenderProgress(float progressFactor)
    {
        if (shot == null)
            return;

        // y = -(2x - 1)^2 + 1
        Vector2 height = new Vector2(0, 0.16f*(-Mathf.Pow(2 * progressFactor - 1, 2) + 1));
        shot.position = height + startPoint + (endPoint - startPoint) * progressFactor;

        if (progressFactor == 1)
        {
            GameObject.Destroy(shot.gameObject);
            Explode();
        }
    }

    private void Explode()
    {
        Transform exp = SpawnActor.SpawnPrefabAt(hitPrefab, target);
    }
}
