Esempio n. 1
0
	IEnumerator WaitAndShotCue(CueController cueController)
	{
		shotIsStarted = true;

		yield return new WaitForSeconds(0.5f);

		targetPoint = Vector3.zero;
		targetBallController = null;
		haveBallTarget = false;
		haveWallTarget = false;
		float ballHitAngle = 0.0f;
		float wallHitAngle = 0.0f;

		holleDistance = 0.0f;
		ballDistance = 0.0f;
		allDistance = 40.0f;
		realHitAngle = 0.1f;
		bool haveFirsHit = false;
		if (targetsAI && targetsAI.targets != null)
		{
			foreach (Target target in targetsAI.targets) {
				foreach (BallController ballController in cueController.ballControllers) {
					if (ballController == cueController.ballController) {
						continue;
					}
					if (ballController.isBlack && !cueController.gameManager.afterOtherRemainedBlackBall) {
						continue;
					} 
					if (!cueController.gameManager.tableIsOpened &&
                        (cueController.gameManager.ballType == ballController.ballType || cueController.gameManager.otherProfileNew.checkBallCard(ballController.id))) {
						continue;
					}

					Vector3 direction = (target.transform.position - ballController.transform.position).normalized;
					Vector3 origin = ballController.transform.position + 2.0f * cueController.ballRadius * direction;
					Vector3 checkPoint = ballController.transform.position - 1.99f * cueController.ballRadius * direction;
					if (!haveBallTarget && !haveWallTarget) {
						targetPoint = checkPoint;
					}

					RaycastHit targetHit;

					if (Physics.SphereCast (origin, 0.99f * cueController.ballRadius, direction, out targetHit, cueController.wallAndBallMask | cueController.mainBallMask)) {
						if (targetHit.collider.GetComponent<BallController> ()) {
							continue;
						}

						if (targetHit.collider.GetComponent<HolleController> ()) {
							cueBallStartPosition = cueController.ballController.transform.position;

							Vector3 cueBallMoveOrient = (checkPoint - ballController.transform.position).normalized;
							Ray cueBallMoveRay = new Ray (checkPoint + 0.04f * cueController.ballRadius * cueBallMoveOrient, cueBallMoveOrient);
							Ray checkOtherBallRay = new Ray (checkPoint + 2.0f * cueController.ballRadius * Vector3.up, -Vector3.up);

							Transform StartOrMoveCube = cueController.isFirsTime ? cueController.StartCube : cueController.MoveCube;
							Vector3 point = checkPoint + cueController.ballRadius * cueBallMoveOrient;
							if (VectorOperator.sphereInCube (point, cueController.ballRadius, StartOrMoveCube) && cueController.cueFSMController.moveInTable
								&& !Physics.SphereCast (cueBallMoveRay, 0.99f * cueController.ballRadius, 2.0f * cueController.ballRadius, cueController.wallAndBallMask)
								&& !Physics.SphereCast (checkOtherBallRay, 1.1f * cueController.ballRadius, 4.0f * cueController.ballRadius, cueController.ballMask)) {
								//Can move the main (cue ) ball
								Debug.Log ("cueBallStartPosition " + cueBallStartPosition);
								haveFirsHit = true;
								cueBallStartPosition = point;
								targetBallController = ballController;
								Vector3 cueBallOrient = (checkPoint - cueBallStartPosition).normalized;
								float distance = Vector3.Distance (cueBallStartPosition, checkPoint);
								float currentHitAngle = Vector3.Dot (cueBallOrient, direction);

								CheckSetCueBallPivot (cueController, 0.5f * currentHitAngle * Vector3.down);
								holleDistance = Vector3.Distance (ballController.transform.position, target.transform.position);
								ballDistance = distance;
								allDistance = holleDistance + ballDistance;
								ballHitAngle = currentHitAngle;
								realHitAngle = ballHitAngle;
								targetPoint = checkPoint;
								haveBallTarget = true;
								targetBallController = ballController;
								break;

							} else {
					
								Vector3 cueBallOrient = (checkPoint - cueBallStartPosition).normalized;

								float distance = Vector3.Distance (cueBallStartPosition, checkPoint);

								Ray ray = new Ray (cueBallStartPosition, cueBallOrient);
								if (!Physics.SphereCast (ray, 0.99f * cueController.ballRadius, distance - 0.02f * cueController.ballRadius, cueController.wallAndBallMask)) {
									float currentHitAngle = Vector3.Dot (cueBallOrient, direction);
									if (currentHitAngle > ballHitAngle) {
										//Can throw the target ball
										CheckSetCueBallPivot (cueController, 0.5f * currentHitAngle * Vector3.down);
										holleDistance = Vector3.Distance (ballController.transform.position, target.transform.position);
										ballDistance = distance;
										allDistance = holleDistance + ballDistance;
										ballHitAngle = currentHitAngle;
										realHitAngle = ballHitAngle;
										targetPoint = checkPoint;
										haveBallTarget = true;
										targetBallController = ballController;
									} 
								} else
								if (MenuControllerGenerator.controller.AISkill == 3 && !haveBallTarget && CheckAllWalls (cueController, cueController.ballRadius, cueBallStartPosition, checkPoint, ballController, ref targetPoint, ref wallHitAngle)) {
									//Can throw the target ball using the walls
									//Debug.LogWarning("have Wall Target");
								}
							}
						}
					}
					if (MenuControllerGenerator.controller.AISkill == 1) {
						cueRotation.SetLookRotation ((targetPoint + 0.2f * cueController.ballRadius * Random.onUnitSphere - cueController.cuePivot.position).normalized);
					} else {
						cueRotation.SetLookRotation ((targetPoint - cueController.cuePivot.position).normalized);
					}
				}

				if (!cueController.gameManager.isFirstShot) {
					cueController.cuePivot.rotation = cueRotation;
				} else {
					cueController.cuePivot.localRotation = Quaternion.Euler (0.093f, 0.675f, 0.0f);
				}
				if (haveFirsHit) {
					break;
				}
		
				yield return null;

			}

			if (cueController.cueFSMController.moveInTable && targetBallController) {
				cueController.ballController.GetComponent<Rigidbody> ().isKinematic = true;
				cueController.ballController.GetComponent<Rigidbody> ().position += 3.0f * cueController.ballRadius * Vector3.up;
				yield return new WaitForFixedUpdate ();
				cueController.ballController.GetComponent<Rigidbody> ().position = cueBallStartPosition + 3.0f * cueController.ballRadius * Vector3.up;
				yield return new WaitForFixedUpdate ();
				cueController.ballController.transform.position = cueBallStartPosition;
				cueController.ballController.GetComponent<Rigidbody> ().isKinematic = false;
				yield return new WaitForSeconds (0.5f);
				if (!cueController.gameManager.isFirstShot) {
					cueController.cuePivot.LookAt (targetPoint);
				}			
				yield return new WaitForFixedUpdate ();
			}
			yield return StartCoroutine (StretchCue (cueController));


			cueController.cueForceValue = Mathf.Clamp01 ((allDistance / 40.0f) * (1.0f / Mathf.Clamp (realHitAngle, 0.3f, 1.0f)));
			if (!cueController.gameManager.isFirstShot) {
				cueController.cueForceValue *= 0.85f;
			}
			cueController.OnDrawLinesAndSphere ();
			cueController.CheckShotCue ();
			shotIsStarted = false;
		}
	}