view EasyBilliardsGame/Assets/Scripts/smartballscript.cs @ 2:fdab88fc2cb9

add game projects
author Yuta ANSE <e135745@ie.u-ryukyu.ac.jp>
date Fri, 17 Jul 2015 23:14:45 +0900
parents
children
line wrap: on
line source

using UnityEngine;
using System.Collections;

public class smartballscript : MonoBehaviour {
//	public static int n = 1;
	float power = 0f;
	bool flg = true;
	GameObject[] cubes = new GameObject[5];
//	Vector3[] moves = new Vector3[5];

	// Use this for initialization
	void Start () {
//		GameObject[] goals = GameObject.FindGameObjectsWithTag ("goal");
//		int n = 0;
//		foreach(GameObject obj in goals){
//			obj.GetComponent<Renderer> ().material.shader = 
//				Shader.Find ("Transparent/Diffuse");
//			obj.GetComponent<Renderer>().material.color = 
//				new Color(0f, 0.15f * n, 1f - 0.15f * n++, 0.5f);
//		}
//		GameObject[] ob_cubes = GameObject.FindGameObjectsWithTag ("ob_cube");
//		foreach (GameObject obj in ob_cubes) {
//			Vector3 move = obj.transform.position;
//			AnimationClip clip = new AnimationClip ();
//			Keyframe[] keysX = new Keyframe[2];
//			keysX [0] = new Keyframe (0f, move.x - 3);
//			keysX [1] = new Keyframe (2f, move.x + 3);
//			AnimationCurve curveX = new AnimationCurve (keysX);
//			clip.SetCurve ("", typeof(Transform), "localPosition.x", curveX);
//			clip.wrapMode = WrapMode.PingPong;
//
//			Keyframe[] keysY = new Keyframe[2];
//			keysY [0] = new Keyframe (0f, move.y);
//			keysY [1] = new Keyframe (2f, move.y);
//			AnimationCurve curveY = new AnimationCurve (keysY);
//			clip.SetCurve ("", typeof(Transform), "localPosition.y", curveY);
//
//			Keyframe[] keysZ = new Keyframe[2];
//			keysZ [0] = new Keyframe (0f, move.z);
//			keysZ [1] = new Keyframe (2f, move.z);
//			AnimationCurve curveZ = new AnimationCurve (keysZ);
//			clip.SetCurve ("", typeof(Transform), "localPosition.z", curveZ);
//
//			obj.GetComponent<Animation>().AddClip (clip, "clip1");
//			obj.GetComponent<Animation>().Play ("clip1");
//		}
//		moves [0] = new Vector3 (0f, 1f, 0f);
//		moves [1] = new Vector3 (-3f, 1f, 5f);
//		moves [2] = new Vector3 (3f, 1f, 5f);
//		moves [3] = new Vector3 (-3f, 1f, 5f);
//		moves [4] = new Vector3 (3f, 1f, -3f);
//		for (int i = 0; i < 5; i++){
//			cubes[i] = GameObject.Find("BoardCube" + i);
//			Vector3 move = cubes[i].transform.position;
//			AnimationClip clip = new AnimationClip();
//			Keyframe[] keysX = new Keyframe[2];
//			keysX[0] = new Keyframe(0f, move.x - 3);
//			keysX[1] = new Keyframe(i + 1f,move.x + 3);
//			AnimationCurve curveX = new AnimationCurve(keysX);
//			clip.SetCurve("", typeof(Transform), "localPosition.x", curveX);
//			clip.wrapMode = WrapMode.PingPong;
//
//			Keyframe[] keysY = new Keyframe[2];
//			keysY[0] = new Keyframe(0f, move.y);
//			keysY[1] = new Keyframe(i + 1f, move.y);
//			AnimationCurve curveY = new AnimationCurve(keysY);
//			clip.SetCurve("", typeof(Transform), "localPosition.y", curveY);
//
//			Keyframe[] keysZ = new Keyframe[2];
//			keysZ[0] = new Keyframe(0f, move.z);
//			keysZ[1] = new Keyframe(i + 1f, move.z);
//			AnimationCurve curveZ = new AnimationCurve(keysZ);
//			clip.SetCurve("", typeof(Transform), "localPosition.z", curveZ);
//
//			cubes[i].GetComponent<Animation>().AddClip(clip, "clip1");
//			cubes[i].GetComponent<Animation>().Play("clip1");
//		}
	}
	
	// Update is called once per frame
	void Update () {
		MoveCube ();
		GetComponent<Rigidbody> ().AddForce (0f, 0f, -10f);
		if (flg){
			if (Input.GetKey (KeyCode.Space)) {
				power += 0.01f;
				if (power > 1f) {
					power = 1f;
				}
				GetComponent<Renderer> ().material.color = new Color (1f, power, 0f);
			}
		}
		if (Input.GetKeyUp(KeyCode.Space)){
			GetComponent<Rigidbody> ().AddForce (new Vector3 (0f, 0f, power * 2500f));
			power = 0f;
			GetComponent<Renderer> ().material.color = Color.red;
			flg = false;
		}
		if((transform.position.y > 10)||(transform.position.y < -10)){
			GetComponent<Rigidbody> ().velocity = Vector3.zero;
			transform.position = new Vector3 (9f, 0.5f, -13f);
		}
	}


	void OnCollisionEnter(Collision collision){
		if (collision.gameObject.tag == "ob_cube") {
			Behaviour b = (Behaviour)collision.gameObject.GetComponent ("Halo");
			b.enabled = true;
		}
		if (collision.gameObject.name == "Cube (3)") {
//			transform.position = new Vector3 (9f, 0.5f, -13f);
			flg = true;
//			Application.LoadLevel ("Sence1");
		}
		if (collision.gameObject.name == "Cube (13)") {
			flg = true;
		}
	}

	void OnCollisionExit(Collision collision){
		if (collision.gameObject.tag == "ob_cube"){
			Behaviour b = (Behaviour)collision.gameObject.GetComponent ("Halo");
			b.enabled = false;
			Vector3 v = GetComponent<Rigidbody> ().velocity;
			if (v.magnitude < 15){
				v *= 2.0f;
				if (v.magnitude < 5) {
					v *= 2.0f;
				}
				GetComponent<Rigidbody>().velocity = v;
		}
	}
		if (collision.gameObject.tag == "ob_wall"){
			Vector3 v = GetComponent<Rigidbody> ().velocity;
			if (v.magnitude < 20){
				v *= 1.5f;
				if (v.magnitude < 5){
					v *= 2.0f;
				}
				GetComponent<Rigidbody>().velocity = v;
			}
		}
	}

	void MoveCube(){
		GameObject[] ob_cubes = GameObject.FindGameObjectsWithTag ("ob_cube");
		foreach (GameObject obj in ob_cubes) {
			if (obj.gameObject.name != "BoardCube5") {
				if (obj.gameObject.name == "BoardCube0") {
					obj.transform.Rotate (new Vector3 (0f, -7f, 0f));
				} else {
					obj.transform.Rotate (new Vector3 (0f, 7f, 0f));
				}
			}
		}
//		for(int i = 0; i < 5; i++){
//			cubes [i].transform.Rotate (new Vector3 (0f, 2.5f, 0f));
//		}
	}

	void OnTriggerEnter(Collider collider){
		if (collider.gameObject.tag == "goal"){
		GetComponent<Rigidbody> ().velocity = Vector3.zero;
			GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
//			if (collider.name == "Cylinder0") {
//				Application.LoadLevel ("50");
//			}if (collider.name == "Cylinder1") {
//				Application.LoadLevel ("200");
//			}if (collider.name == "Cylinder2") {
//				Application.LoadLevel ("400");
//			}if (collider.name == "Cylinder3") {
//				Application.LoadLevel ("300");
//			}if (collider.name == "Cylinder4") {
//				Application.LoadLevel ("500");
//			}if (collider.name == "Cylinder5") {
//				Application.LoadLevel ("100");
//			}
//			GameObject[] objs = GameObject.FindGameObjectsWithTag ("goal");
//			int n = 1;
//			foreach(GameObject obj in objs){
//			for (int i = 0; i < 5; i++){
//				GameObject obj = GameObject.Find ("Cylinder" + i);
//				if (obj == collider.gameObject) {
//					GameObject.Find ("GUI Text").GetComponent<GUIText> ().text = 
//						"point:" + (n * 100);
//				n *= 100;
//					ParticleSystem ps = (ParticleSystem)GameObject.Find("Cylinder" + i).
//						GetComponent ("ParticleSystem");
//				}
//				n++;
//			}
		}
	}
	void OnTriggerExit(Collider collider){
		if (collider.name == "Cylinder0") {
			Application.LoadLevel ("50");
		}if (collider.name == "Cylinder1") {
			Application.LoadLevel ("200");
		}if (collider.name == "Cylinder2") {
			Application.LoadLevel ("400");
		}if (collider.name == "Cylinder3") {
			Application.LoadLevel ("300");
		}if (collider.name == "Cylinder4") {
			Application.LoadLevel ("500");
		}if (collider.name == "Cylinder5") {
			Application.LoadLevel ("100");
		}
	}
}