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

public class Movement : MonoBehaviour
{

    public enum MoveDirection
    {
        MOVE_BACKWARD = -1,
        MOVE_STOP = 0,
        MOVE_FORWARD = 1
    };

    [Header("Values")]
    public float maximumSpeed = 5;
    public MoveDirection currentDirection = MoveDirection.MOVE_STOP;

    float speed = 0;
    float acc = 10f;

    [Header("Objects")]
    public GameObject frontwheel;
    public GameObject backwheel;
    public PoweredObject frontPower;
    public PoweredObject backPower;
    // Use this for initialization
    void Start()
    {
        frontPower = frontwheel.GetComponent<PoweredObject>();
        backPower = backwheel.GetComponent<PoweredObject>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        speed += GetDirection() * Time.deltaTime * acc;

        speed = Mathf.Min(speed, maximumSpeed);
        speed = Mathf.Max(speed, -maximumSpeed);

        Vector2 targetPosition = transform.position + (transform.right * GetDirection() * maximumSpeed);
        transform.position = Vector2.MoveTowards(transform.position, targetPosition, Time.fixedDeltaTime * Mathf.Abs(speed));
    }

    int GetDirection()
    {
        int direction = (int)currentDirection;
        if (transform.localScale.x < 0)
        {
            direction *= -1;
        }

        return direction;
    }

    public void ChangeDirection(MoveDirection direction)
    {
        if (direction != currentDirection)
        {
            currentDirection = direction;
            speed = 0;
        }

    }
}
