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

public class Cannonball : MonoBehaviour
{
    public float speed = 10f;

    public bool hasHitObstacle { get; private set; } = false;
    public bool hasHitStackNode { get; private set; } = false;

    void Awake()
    {
        if (Time.timeScale != 0)
        {
            GetComponent<Movable>().SetDirection(Vector3.forward, speed, false);
            Destroy(this.gameObject, 1f);
        }
        else
        {
            Destroy(gameObject);
        }
    }

    private void Start()
    {
        GameEvents.instance.onNextLevelEvent += SelfDestruct;
    }

    public void HitObstacle()
    {
        hasHitObstacle = true;
    }

    public void HitNode()
    {
        hasHitStackNode = true;
    }

    public void SelfDestruct()
    {
        if(!hasHitStackNode && this != null)
            Destroy(gameObject);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag.Equals("Cannonball") && !hasHitObstacle)
        {
            Destroy(gameObject);
        }
    }
}
