using UnityEngine;

public class Engine : MonoBehaviour {
	public float EngineTractionAsk=1.6f;
	public float PerkSpeed=30;
    public string DEBUG;
	
	public enum EngineGear
	{
		Gas,
		Electric,
		Reverse
	}

    public EngineGear Gear;
    public float ThrottleFactor;
    public float TargetSpeed;

    private Rigidbody2D rbody;
    private Tire[] tires;
	
	void Start ()
    {
		Gear = EngineGear.Gas;
		rbody = gameObject.GetComponentInChildren<Rigidbody2D>();
        tires = GetComponentsInChildren<Tire>();
	}
	
	void Update ()
    {
        DEBUG = "spd = " + rbody.velocity.magnitude;
		float power=0;
        
		switch(Gear)
		{
			case EngineGear.Gas:
				power = ThrottleFactor * EngineTractionAsk;
			break;
			case EngineGear.Electric:
				if(rbody.velocity.magnitude < PerkSpeed)
					power = ThrottleFactor * EngineTractionAsk / 1.2f;
				else
					power = ThrottleFactor * EngineTractionAsk / 3f;
			break;
			case EngineGear.Reverse:
				if(rbody.velocity.magnitude < PerkSpeed*0.8f)
					power = ThrottleFactor * EngineTractionAsk / -2f;
				else
					power = ThrottleFactor * EngineTractionAsk / -4f;
			break;
		}

        power = ThrottleFactor * EngineTractionAsk;		
		foreach(Tire t in tires)
			if(t.Powered)
				t.CurrentPower = power;
	}
}
