using UnityEngine;

namespace ScrambleKit.Move
{
    public class MoveTracer : MonoBehaviour
    {

        public Transform PipPrefab;
        public KeyCode ToggleKey = KeyCode.C;
        public bool StartActive = true;

        private Rigidbody2D rb;
        private bool on;

        void Update()
        {
            if (Input.GetKeyDown(ToggleKey))
                on = !on;
        }

        void FixedUpdate()
        {
            if (on)
                PlacePip();
        }

        private void PlacePip()
        {
            if (PipPrefab == null || !IsMoving())
                return;

            Transform pip = GameObject.Instantiate(PipPrefab);
            pip.position = transform.position;
        }

        private bool IsMoving()
        {
            return (rb == null || rb.velocity.magnitude > 0.001f);
        }

        void Start()
        {
            on = StartActive;
            rb = GetComponent<Rigidbody2D>();
        }
    }
}