using UnityEngine;
using System.Collections;

public class Engine : BaseShipComponent 
{
	private float ThrustPower = 10.0f;

	public ParticleSystem[] particleSystems;
	bool emit = false;

	void Start()
	{
		particleSystems = GetComponentsInChildren<ParticleSystem> ();
		SetParticleEmission(false);
		emit = false;
	}

	void Update()
	{
		if (inputController.playersActiveKey.Length == 0 || buildController.Building)
			return;

		if (inputController.playersActiveKey [(int) PlayerID]) {
			thisRigidbody.AddForce (thisTransform.up * ThrustPower, ForceMode2D.Force);

			if(!emit)
			{
				SetParticleEmission(true);
				PlayAudio();
			}
		} else {
			if(emit)
			{
				SetParticleEmission(false);
				StopAudio();
			}
		}
	}

	void SetParticleEmission(bool newState)
	{
		foreach(ParticleSystem p in particleSystems)
		{
			p.enableEmission = newState;
		}
				emit = newState;
	}
}
