using JetBrains.Annotations;
using UnityEngine;

public class Ship : MonoBehaviour
{
    [SerializeField] private float speedX;
    [SerializeField] private float resetX;
    [SerializeField] private int score;
    [SerializeField] private GameObject explosion;

    private float shipWidth;

    private GameManager game;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        game = FindFirstObjectByType<GameManager>();

        shipWidth = GetComponent<SpriteRenderer>().bounds.size.x;

        transform.position = new Vector3(resetX, Random.Range(-4f, 4f), transform.position.z);
    }

    // Update is called once per frame
    void Update()
    {
        if(transform.position.x + (shipWidth / 2f) < 0)
        {
            transform.position = new Vector3(resetX, Random.Range(-4f, 4f), transform.position.z);
            game.AddScore(-score);
        }

        transform.position = transform.position + new Vector3(speedX * Time.deltaTime, 0f, 0f);
    }


    private void OnMouseDown()
    {
        Instantiate(explosion, transform.position, Quaternion.identity);

        transform.position = new Vector3(resetX, Random.Range(-4f, 4f), transform.position.z);

        game.AddScore(score);
    }

}
