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

public class Shot : MonoBehaviour {
    float timer = 5f;
    float TTL = 1f;
    public Rigidbody2D mainRigidBody;

    public bool simulateShot = false;
    bool shotSimulation = false;

    public float shotSpeed = 5f;

    public float shotDirection = 1;

    void Awake()
    {
        if (this.tag == "Shot")
        {
            Observer.shotHandler += OnShot;
        }
        else if (this.tag == "GrumpShot")
        {
            Observer.grumpShotHandler += OnShot;
        }
    }

    void OnShot(Vector3 firePosition)
    {
        timer = 0f;
        mainRigidBody.velocity = Vector3.zero;
        this.transform.position = firePosition;
        this.gameObject.SetActive(true);
    }

    void Update()
    {
        if (simulateShot != shotSimulation)
        {
            simulateShot = shotSimulation;
            OnShot(new Vector3(3,-1,0));
        }
    }
	
	// Update is called once per frame
	void FixedUpdate () {
		if (timer < TTL)
        {
            mainRigidBody.velocity = new Vector3(shotSpeed * shotDirection, 0f, 0f);
            timer += Time.deltaTime;
            Observer.OnShotBar(timer);
            if (timer >= TTL)
            {
                this.gameObject.SetActive(false);
                mainRigidBody.velocity = Vector3.zero;
            }
        }
	}
}
