using UnityEngine;
using System.Collections;

public class Droplet_S : MonoBehaviour {
	
	LightColour col;
	public void SetColour(LightColour col){
		this.col = col;
		GameObject fab = new GameObject();
		foreach(Transform child in transform) 
			fab = child.gameObject;
		
		ColourChanger cc = fab.GetComponent(typeof(ColourChanger)) as ColourChanger;		
		cc.SelectTexture(col);
	}
	public AudioClip jump;
	public AudioClip death;
	public LightColour GetColour(){return col;}
	
	float strength = 1f;
	
	void HitByLaser(Laser_S hittingLaser){
		strength += 0.04f;
		transform.localScale *= 1.0001f;
		
		
		LightColour resi_col = hittingLaser.GetCol();
		
		if(resi_col.Equals(col)){
			AIDirector.DecreaseAlive();
			Instantiate(References.dieEffectObj, transform.position, Quaternion.identity) ;
			GameObject effect = Instantiate(References.soundEffectObj, transform.position, Quaternion.identity) as GameObject;
			SoundEffect_S effect_script = effect.GetComponent(typeof(SoundEffect_S)) as SoundEffect_S;
			effect_script.SetEffect(death);
			effect_script.SetVolume(5f);
			GUIScript.AddScore(30);
			Destroy(gameObject);
		}else{
		
			Vector3 resi_dir = hittingLaser.GetDir();

			
			Vector3 resi_hit = hittingLaser.GetHit();
			
			LightColour new_col = ColourCalc.MixColours(resi_col, col);
			

			
			GameObject laser = Instantiate(References.laserObj, resi_hit, Quaternion.LookRotation(resi_dir)) as GameObject;
			Laser_S laser_script = laser.GetComponent(typeof(Laser_S)) as Laser_S;
			laser_script.SetColour(new_col);
			laser_script.SetLifetime(2f);
			laser_script.SetSource(gameObject, false);
		}
		
		
	}
	bool sendToAir = false;
	public void AirPush(){
		sendToAir = true;
	}
	
	
	

		
			
		
	Vector3 center = new Vector3(989,288,1018);
	public GameObject player;	
	bool grounded = false;
	float speed = 10f;
	float jumpSpeed = 30f;
	float gravity = 20.0f;
	private Vector3 moveDirection = Vector3.zero;
	float rotationSpeed = 3f;
	
	float playerChaseDist = 50;
	
	void Start(){
		player = References.playerObj;
	}
	
	
	void FixedUpdate() {
		if (grounded) {
			Vector3  target;
			float dist = Vector3.Distance(player.transform.position, transform.position);
			if(dist < playerChaseDist && dist < Vector3.Distance(center, transform.position))
				target = player.transform.position;
			else
				target = center;
			
			Vector3 direction = target - transform.position; 
			direction.y = 0; 
			transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); 
			transform.eulerAngles = new Vector3(0, transform.eulerAngles.y, 0); 
			Vector3 forward = transform.TransformDirection(Vector3.forward); 
		    float speedModifier = Vector3.Dot(forward, direction.normalized); 
		    speedModifier = Mathf.Clamp01(speedModifier); 
		    moveDirection = forward * speed * speedModifier; 
			
			if (sendToAir) {
				Instantiate(References.jumpEffectObj, transform.position, Quaternion.identity);
				sendToAir = false;
				moveDirection.y = jumpSpeed;
				audio.clip = jump;
				audio.Play();
			}
			
			
			
			
			
		}
		moveDirection.y -= gravity * Time.deltaTime;
		CharacterController controller = gameObject.GetComponent(typeof(CharacterController)) as CharacterController;
		CollisionFlags flags = controller.Move(moveDirection * Time.deltaTime);
		grounded = (flags & CollisionFlags.CollidedBelow) != 0;		
	}
	float myDamage = 2f;
	
	public void EnterFlower(Flower_S flower){
		flower.TakeDamage(myDamage * strength);
		AIDirector.DecreaseAlive();
		Destroy(gameObject);
	}
	
	public void EnterPlayer(PlayerTouch_S player){
		player.TakeDamage(myDamage * strength);
		AIDirector.DecreaseAlive();
		Destroy(gameObject);
	}
	
	public void LightColorPaddle(){
		References.SetFlashingColor(col.GetColourAsString());
	}
	
}
