using FalcoEngine;
using System;
using System.Collections;
using System.Collections.Generic;

public class camera_s : MonoBehaviour
{
	public float camera_speed;
	private float rot;
	private float rot2;
	public Prefab ball_shot;
	private GameObject aim;
	private aim_s aim_s;
	private GameObject tower;
	private float add_x;
	private float add_z;
	private float angle;
	private int create_delay;
	public Prefab sound_shot;
	private controller_s controller_s;
	
	void Start()
	{
		controller_s=GameObject.Find("controller").GetComponent<controller_s>();
		Cursor.locked = true;
		Cursor.visible=false;
		aim_s=GameObject.Find("aim").GetComponent<aim_s>();
		tower=GameObject.Find("tower_center");
		
		rot=90;
	}
	
	void FixedUpdate()
	{
		if(controller_s.game_end==0)
		{
			rot+=Input.cursorDirection.x*camera_speed;
			rot2+=Input.cursorDirection.y*camera_speed;
			
			if(rot2>85)
			{
				rot2=85;
			}
			
			if(rot2<-50)
			{
				rot2=-50;
			}
			
			transform.rotation=Quaternion.Euler(rot2,-rot,0);
			
			float add_x=Mathf.Cos(angle*Mathf.Deg2Rad)*10;
			float add_z=Mathf.Sin(angle*Mathf.Deg2Rad)*10;
			
			transform.position=tower.transform.position;
			transform.position+=new Vector3(add_x,5,add_z);
			
			if(Input.GetKey(ScanCode.A))
			{
				rot+=1;
				angle+=1f;
			}

			if(Input.GetKey(ScanCode.D))
			{ 
				rot-=1;
				angle-=1f;
			}
			
			if(create_delay>0)
			{
				create_delay--;
			}
		}
	}
	
	void Update()
	{	
		if(Input.GetKeyDown(ScanCode.Escape))
		{
			Cursor.locked = false;
			Cursor.visible=true;
			FalcoEngine.Application.Quit();
		}
		
		if(controller_s.game_end==0)
		{
			if(Input.GetMouseButtonDown(0))
			{
				if(create_delay==0)
				{
					create_delay=50;
					GameObject new_ball_shot=ball_shot.Instantiate(transform.position,transform.rotation);
					ball_shot_s ball_shot_s=new_ball_shot.GetComponent<ball_shot_s>();
					ball_shot_s.set_color(aim_s.color_int);
					aim_s.new_color();
					
					sound_shot.Instantiate(transform.position,transform.rotation);
				}
			}
		}
	}
}
