﻿using UnityEngine;
using System.Collections;

public class SingleBoid : MonoBehaviour
{
	/* パブリックなフィールド（別クラスから参照可能） */
	public Vector3 pos, vel; //位置・速度
	public float vision_space;	//視界距離
	public float neighbor_space; //接触距離	

	/* プライベートなフィールド */
	private Rigidbody rb;						 //剛体オブジェクト
	private float xmax,xmin,ymax,ymin,zmax,zmin; //空間の境界
	private float speedmax;						 //速さの最大値


	void Start ()
	{
		speedmax = BoidManager.speedmax; 		
		rb = this.GetComponent<Rigidbody> ();

		this.SetBorder ();			//飛翔空間の決定
		this.SetRandomPosition ();  //初期位置の決定
		this.SetRandomVelocity ();	//初期速度の決定

	}

	void Update ()
	{	//位置と速度の更新
		pos = this.transform.position;
		vel = this.rb.linearVelocity;

		Rebound ();				//境界判定		　
		LimitVelocity ();		//速度制限
		SetGravity();			//重力の設定

		RotateBoid(); //速度に応じた回転：2026年より追加
	}


	//ボイドが境界を出たら, 速度を反転させる. 
	//現バージョンでは高さのみ判定
	private void Rebound(){
		// if ((pos.x > xmax && vel.x > 0) || (pos.x < xmin && vel.x < 0)) {
		// 	rb.velocity = new Vector3 (-vel.x, vel.y, vel.z);
		// 	this.vel = this.rb.velocity;
		// }

		// if ((pos.y > ymax && vel.y>0) || (pos.y < ymin && vel.y<0)){
		// 	rb.velocity = new Vector3 (vel.x, -vel.y, vel.z);
		// 	this.vel = this.rb.velocity;

		// }

		// if ((pos.z > zmax && vel.z>0) || (pos.z < zmin && vel.z<0)) {
		// 	rb.velocity = new Vector3 (vel.x, vel.y, -vel.z);
		// 	this.vel = this.rb.velocity;
		// }

		if ((pos.y > ymax && vel.y>0)) {
			rb.linearVelocity = new Vector3 (vel.x, -vel.y, vel.z);
			this.vel = this.rb.linearVelocity;
		}


	}

	//速度の制限
	private void LimitVelocity(){
		if(this.vel.magnitude > speedmax){
			this.vel = (speedmax / this.vel.magnitude) * this.vel;
			this.rb.linearVelocity = this.vel;
		}
	}	

	//重力の設定（3Dモードの時、重力を作用させない）
	private void SetGravity(){

		if (BoidManager.mode_2d){
			this.rb.useGravity = true;
		}else{
			this.rb.useGravity = false;
		}
	}

	//境界線の決定
	public void SetBorder(){

		xmax = 0.5f * BoidManager.flyspace;		
		xmin = -0.5f *  BoidManager.flyspace;
		ymin = 0f;
		ymax = BoidManager.flyheight;
		zmax = 0.5f *  BoidManager.flyspace;
		zmin = -0.5f *  BoidManager.flyspace;

	}

	//位置をランダムに決定
	public void SetRandomPosition(){
		// float rz = zmin + (zmax - zmin) * Random.value;
		// float rx = xmin + (xmax - xmin) * Random.value;

		float ry = ymin + 0.9f * (ymax - ymin) * Random.value;

		//XZ平面の仮想円周上にランダムな点を打つ。
		float rad = Random.value * 2.0f * Mathf.PI;
		float rx = 0.9f * Random.value * xmax * Mathf.Cos(rad);
		float rz = 0.9f * Random.value * zmax * Mathf.Sin(rad);

		this.transform.position = new Vector3 (rx, ry, rz);
		this.pos = this.transform.position;

	}

	//速度をランダムに決定
	public void SetRandomVelocity(){
				
		float vx = -speedmax + 2f * speedmax * Random.value;
		float vy = -speedmax + 2f * speedmax * Random.value;
		float vz = -speedmax + 2f * speedmax * Random.value;

		// rb.velocity = new Vector3 (vx, vy, vz);
		rb.linearVelocity = new Vector3 (vx, 0, vz);
		this.vel = this.rb.linearVelocity;

	}
		
	//速度の設定
	public void SetVelocity(Vector3 v){
		this.rb.linearVelocity = v;
		this.vel = this.rb.linearVelocity;
	}


	//視界距離の設定
	public void SetVisionSpace(float vs){
		this.vision_space = vs;
	}

	//接触距離の設定
	public void SetNeighborSpace(float ns){
		this.neighbor_space = ns;
	}

	// ボイドを速度方向に応じて転がす（2026年授業より）
	private void RotateBoid()
	{
		Vector3 horizontalVel = new Vector3(vel.x, 0, vel.z);

		if (horizontalVel.sqrMagnitude < 0.0001f) return;

		float radius = 0.5f; // Sphereの半径。Scaleが1なら0.5
		Vector3 axis = Vector3.Cross(Vector3.up, horizontalVel.normalized);

		float angle = horizontalVel.magnitude / radius * Mathf.Rad2Deg * Time.deltaTime;

		transform.Rotate(axis, angle, Space.World);
	}


}
