using UnityEngine;

public class GameItem : MonoBehaviour
{
	public float xx;
	public float yy;
	public Velocity velocity; // Current speed
	public float lastRenderTime;
	public Animator animator;

	private float destroyTimer = -1f;

	public GameItem()
	{
		this.velocity = new Velocity(0, 0);
	}

	public Vector2 position
	{
		set
		{
			transform.position = new Vector3(value.x, value.y, transform.position.z);
		}

		get => new Vector2(transform.position.x, transform.position.y);
	}

	protected virtual void Update()
	{
		if (destroyTimer >= 0f)
			destroyTimer += Time.deltaTime;

		if (destroyTimer > 1f)
			Destroy(gameObject);
	}

	public virtual void Explode(Vector2? pos)
	{
		// Activate timer to give the explosion animation enough time (1 second) to play before the GameObject is destroyed (see Update)
		destroyTimer = 0f;

		if (pos != null)
		{
			this.velocity = new Velocity(0, 0);
			this.position = (Vector2) pos;
		}

		// Show explosion animation
		animator.SetBool("explode", true);
	}

	public virtual void Explode()
	{
		this.Explode(null);
	}
}
