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

public class ball_shot_s : MonoBehaviour
{
	public string color;
	private int color_int;
	
	public GameObject red;
	public GameObject green;
	public GameObject blue;
	public GameObject purple;
	public GameObject yellow;
	
	private GameObject[] all_child=new GameObject[5];
	private GameObject my_camera;
	private Rigidbody my_rigid;
	
	public Prefab ball;
	private controller_s controller_s;
	private int del;
	private int one;
	
	void Start()
	{
		my_rigid=GetComponent<Rigidbody>();
		my_camera=GameObject.Find("Camera");
		my_rigid.linearVelocity=transform.forward*60f;
		controller_s=GameObject.Find("controller").GetComponent<controller_s>();
	}
	
	public void set_color(int value)
	{
		all_child[0]=red;
		all_child[1]=green;
		all_child[2]=blue;
		all_child[3]=purple;
		all_child[4]=yellow;
		
		for(int i=0;i<5;i++)
		{
			all_child[i].enabled=false;
		}
		
		color_int=value;
		
		all_child[value].enabled=true;
	}
	
	void FixedUpdate()
	{
		del++;
		
		if(del==300)
		{
			Destroy(gameObject);
		}
		
		if(controller_s.collapse==0)
		{
			my_rigid.linearVelocity=transform.forward*8f;
		}
		
		ball_s[] all_balls=FindObjectsOfType<ball_s>();
		
		for(int i=0;i<all_balls.Length;i++)
		{
			if(Vector3.Distance(transform.position,all_balls[i].transform.position)<1)
			{
				Destroy(gameObject);
				if(one==0)
				{
					one=1;
					int index=all_balls[i].index;
					GameObject new_ball=ball.Instantiate(all_balls[i].transform.position,Quaternion.Euler(0,0,0));
					ball_s new_ball_s=new_ball.GetComponent<ball_s>();
					new_ball_s.pos=all_balls[i].pos;
					new_ball_s.index=index+1;
					new_ball_s.set_color(color_int);
					
					for(int i2=0;i2<all_balls.Length;i2++)
					{
						Debug.Log(index.ToString()+"/"+all_balls[i].index.ToString());
						if(all_balls[i2].index>=index)
						{
							all_balls[i2].index+=2;
							all_balls[i2].pos-=0.03f;
						}
					}
				}
			}
		}
	}

}
