using UnityEngine;
using System.Collections.Generic;

[RequireComponent(typeof(BulletPool))]
public class Gun : MonoBehaviour  {
	
	public Transform shotPoint1;
	public Transform shotPoint2;
	public float shoot_interval=0.05f;
	BulletPool bulletPool;
	AircraftController ac;

	bool shot_swap;

	void Start() {
		bulletPool = GetComponent<BulletPool>();
		ac = GetComponent<AircraftController>();
	}
	public void Fire() {
		GenBullet(shot_swap?shotPoint1:shotPoint2);
		shot_swap =  !shot_swap;
	}

	void GenBullet(Transform shotPoint) {
		GameObject b = bulletPool.Get ();
		b.transform.position = shotPoint.position;
		b.transform.rotation = shotPoint.rotation;
		BulletNoFX b_script = b.GetComponent<BulletNoFX>();
		b_script.direction = shotPoint.forward;
	}
	float shoot_timer;
	void Update() {
		shoot_timer+= Time.deltaTime;
		if ( ac) ac.shooting = false;
		if (Input.GetMouseButton(0) && (ac==null?true:ac.started) && shoot_timer >= shoot_interval ) {
			Fire ();
			shoot_timer = 0;
			if (ac) ac.shooting = true;
		}
	}
}
