Advertisement
Guest User

ThrowBall

a guest
Dec 15th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.83 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using TMPro;
  5.  
  6. public class ThrowBall : MonoBehaviour
  7. {
  8.  
  9. #region Private_Variables
  10. private static ThrowBall throwPaperInstance;
  11. private float factor = 230.0f;
  12. private float startTime;
  13. public Vector3 startPos;
  14. private GameObject currentBall;
  15. private bool isGettingDirection = false;
  16. private bool isGameStart = true;
  17. public bool isCurveThrow = false;
  18. private Vector3 lastPos;
  19. private Vector3 lastBallPosition = Vector3.forward;
  20. private bool isStartRotate = false;
  21. List<Vector3> ballPos = new List<Vector3>();
  22. List<float> ballTime = new List<float>();
  23.  
  24. // Use this for initialization
  25. float lastAngel = 0f;
  26. public int angleDirection = 0;
  27. float rotationSpeed = 2f;
  28. float totalX = 0f, totalY = 0f;
  29. int dLeft = 0, dRight = 0;
  30. #endregion
  31.  
  32. #region Public_Variables
  33.  
  34. public Vector3 minThrow;
  35. public Vector3 maxThrow;
  36. //public GameObject ball;
  37. private Transform currentBallChild;
  38. private float dist;
  39. private Vector3 v3Offset;
  40. private Plane plane;
  41. private bool ObjectMouseDown = false;
  42. public GameObject linkedObject;
  43. public Transform target;
  44. public ParticleSystem curveParticle;
  45. public Camera cam;
  46. public GameObject _gameSoloManager;
  47. public float distance;
  48. public float down;
  49.  
  50.  
  51.  
  52.  
  53. int isBaseBallEquip;
  54. int isTennisBallEquip;
  55. int isBilliardEquip;
  56.  
  57. public GameObject[] ballModels;
  58.  
  59.  
  60. public int ballSelectorNumber;
  61.  
  62.  
  63. #endregion
  64.  
  65. #region Unity_Callbacks
  66. void Awake()
  67. {
  68. throwPaperInstance = this;
  69. }
  70.  
  71. void Start()
  72. {
  73.  
  74. StartCoroutine(GetBallCoroutine());
  75.  
  76. isBaseBallEquip = PlayerPrefs.GetInt("isBaseBallEquip");
  77. isTennisBallEquip = PlayerPrefs.GetInt("isTennisBallEquip");
  78. isBilliardEquip = PlayerPrefs.GetInt("isBilliardEquip");
  79.  
  80. ballSelectorNumber = 0;
  81.  
  82. if (isBaseBallEquip == 1)
  83. {
  84. ballSelectorNumber = 1;
  85. }
  86. if (isTennisBallEquip == 1)
  87. {
  88. ballSelectorNumber = 2;
  89. }
  90. if (isBilliardEquip == 1)
  91. {
  92. ballSelectorNumber = 3;
  93. }
  94. }
  95.  
  96. void Update()
  97. {
  98.  
  99. if (currentBall)
  100. {
  101. transform.position = currentBall.transform.position;
  102.  
  103. }
  104.  
  105. if (currentBall && isGettingDirection)
  106. {
  107. //Debug.Log ("M:" + Input.mousePosition);
  108.  
  109. transform.position = currentBall.transform.position;
  110.  
  111.  
  112. float angle = 0f;
  113.  
  114. if (currentBallChild != null)
  115. {
  116. Vector3 mouse_pos = Input.mousePosition;
  117. Vector3 player_pos = currentBallChild.transform.position;
  118.  
  119. mouse_pos.x = mouse_pos.x - player_pos.x;
  120. mouse_pos.y = mouse_pos.y - player_pos.y;
  121.  
  122. angle = Mathf.Atan2(mouse_pos.y, mouse_pos.x) * Mathf.Rad2Deg;
  123.  
  124. }
  125.  
  126. if (Vector3.Distance(currentBall.transform.position, lastBallPosition) > 0f)
  127. {
  128.  
  129. if (!isStartRotate)
  130. {
  131. Invoke("ResetPoint", 0.01f);
  132. }
  133. else
  134. {
  135. Vector3 dir = (currentBall.transform.position - lastBallPosition);
  136.  
  137. if (dir.x > dir.y)
  138. {
  139. totalX += 0.12f;
  140. }
  141. else
  142. {
  143. if (dir.x != dir.y)
  144. {
  145. totalY += 0.12f;
  146. }
  147. }
  148.  
  149. if (totalX >= 2 && totalY >= 2)
  150. {
  151. isCurveThrow = true;
  152. curveParticle.gameObject.SetActive(true);
  153. if (curveParticle.isStopped)
  154. {
  155. curveParticle.Play();
  156. }
  157.  
  158. curveParticle.transform.position = currentBall.transform.position;
  159. }
  160. }
  161.  
  162. isStartRotate = true;
  163. if (angle > 0)
  164. {
  165. if (lastAngel >= angle)
  166. {
  167. dRight++;
  168. }
  169. else
  170. {
  171. dLeft++;
  172. }
  173. }
  174. else
  175. {
  176. if (lastAngel >= angle)
  177. {
  178. dRight++;
  179. }
  180. else
  181. {
  182. dLeft++;
  183. }
  184. }
  185.  
  186. if (dLeft < dRight)
  187. {
  188. angleDirection = 1;
  189. if (currentBallChild != null)
  190. currentBallChild.transform.Rotate(new Vector3(0, 0, 40f));
  191. }
  192. else
  193. {
  194. angleDirection = -1;
  195. if (currentBallChild != null)
  196. currentBallChild.transform.Rotate(new Vector3(0, 0, -40f));
  197. }
  198.  
  199. }
  200. else
  201. {
  202.  
  203. if (isStartRotate == true)
  204. {
  205. StartCoroutine(StopRotation(angleDirection, 0.5f));
  206. }
  207. }
  208.  
  209. lastAngel = angle;
  210. lastBallPosition = currentBall.transform.position;
  211. }
  212.  
  213. }
  214.  
  215. private void ResetPoint()
  216. {
  217.  
  218. if (!isStartRotate)
  219. {
  220. totalX = 0f;
  221. totalY = 0f;
  222. dLeft = 0;
  223. dRight = 0;
  224. isCurveThrow = false;
  225.  
  226. startPos = currentBall.transform.position;
  227.  
  228.  
  229. ballPos.Clear();
  230.  
  231. ballTime.Clear();
  232. ballTime.Add(Time.time);
  233.  
  234. ballPos.Add(currentBall.transform.position);
  235.  
  236. curveParticle.Stop();
  237. curveParticle.gameObject.SetActive(false);
  238.  
  239. ThrowBall.Instance.isCurveThrow = false;
  240. }
  241. }
  242.  
  243.  
  244.  
  245. IEnumerator rotate(Quaternion from, Quaternion to, float t)
  246. {
  247. float rate = 1.0f / t;
  248. float i = 0f;
  249. while (i < 1.0f)
  250. {
  251. i += rate * Time.deltaTime;
  252. currentBallChild.localRotation = Quaternion.Lerp(from, to, i);
  253. yield return 0;
  254. }
  255. }
  256.  
  257. IEnumerator StopRotation(int direction, float t)
  258. {
  259.  
  260. isStartRotate = false;
  261.  
  262.  
  263. float rate = 1.0f / t;
  264. float i = 0f;
  265. while (i < 1.0f)
  266. {
  267. i += rate * Time.deltaTime;
  268. if (!isStartRotate)
  269. {
  270. if (direction == 1)
  271. {
  272. if (currentBallChild != null)
  273. currentBallChild.transform.Rotate(new Vector3(0, 0, 40f) * (Mathf.Lerp(rotationSpeed * totalX, 0, i)) * Time.deltaTime);
  274. }
  275. else
  276. {
  277. if (currentBallChild != null)
  278. currentBallChild.transform.Rotate(new Vector3(0, 0, -40f) * (Mathf.Lerp(rotationSpeed * totalX, 0, i)) * Time.deltaTime);
  279. }
  280. }
  281.  
  282. yield return 0;
  283. }
  284.  
  285. if (!isStartRotate)
  286. {
  287.  
  288. totalX = 0f;
  289. totalY = 0f;
  290. dLeft = 0;
  291. dRight = 0;
  292. isCurveThrow = false;
  293. rotationSpeed = 100f;
  294.  
  295. curveParticle.Stop();
  296. curveParticle.gameObject.SetActive(false);
  297. }
  298. }
  299.  
  300. void OnMouseDown()
  301. {
  302. if (currentBall == null)
  303. return;
  304. plane.SetNormalAndPosition(Camera.main.transform.forward, currentBall.transform.position);
  305. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  306. float dist;
  307. plane.Raycast(ray, out dist);
  308. v3Offset = currentBall.transform.position - ray.GetPoint(dist);
  309. ObjectMouseDown = true;
  310.  
  311.  
  312.  
  313. if (!currentBall || !isGameStart)
  314. return;
  315.  
  316.  
  317. ballPos.Clear();
  318.  
  319. ballTime.Clear();
  320. ballTime.Add(Time.time);
  321.  
  322. ballPos.Add(currentBall.transform.position);
  323.  
  324. totalX = 0f;
  325. totalY = 0f;
  326.  
  327. dLeft = 0;
  328. dRight = 0;
  329.  
  330. isCurveThrow = false;
  331.  
  332. isGettingDirection = true;
  333. currentBall.SendMessage("SetIsThrowed", true, SendMessageOptions.RequireReceiver);
  334.  
  335. currentBallChild = currentBall.transform.Find("Ball");
  336.  
  337.  
  338. StartCoroutine(GettingDirection());
  339.  
  340. }
  341.  
  342. IEnumerator GettingDirection()
  343. {
  344. while (isGettingDirection)
  345. {
  346. startTime = Time.time;
  347. lastPos = startPos;
  348. startPos = currentBall.transform.position;
  349. startPos.z = currentBall.transform.position.z - cam.transform.rotation.y;
  350. yield return new WaitForSeconds(0.01f);
  351.  
  352. }
  353. }
  354.  
  355. //drag pokeball
  356. void OnMouseDrag()
  357. {
  358. if (ObjectMouseDown == true)
  359. {
  360.  
  361. if (!currentBall)
  362. return;
  363.  
  364. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  365. float dist;
  366. plane.Raycast(ray, out dist);
  367. Vector3 v3Pos = ray.GetPoint(dist);
  368. v3Pos.z = currentBall.transform.position.z;
  369. v3Offset.z = 0;
  370. currentBall.transform.position = v3Pos + v3Offset;
  371.  
  372.  
  373.  
  374.  
  375. if (ballPos.Count > 0)
  376. {
  377.  
  378.  
  379. if (ballPos.Count <= 4)
  380. {
  381. if (Vector3.Distance(currentBall.transform.position, ballPos[ballPos.Count - 1]) >= 0.01f)
  382. {
  383. ballTime.Add(Time.time);
  384. ballPos.Add(currentBall.transform.position);
  385.  
  386. }
  387. }
  388. else
  389. {
  390. if (Vector3.Distance(currentBall.transform.position, ballPos[ballPos.Count - 1]) >= 0.01f)
  391. {
  392. ballTime.RemoveAt(0);
  393. ballPos.RemoveAt(0);
  394. ballTime.Add(Time.time);
  395. ballPos.Add(currentBall.transform.position);
  396. }
  397. }
  398.  
  399. }
  400. else
  401. {
  402. ballPos.Add(currentBall.transform.position);
  403. }
  404.  
  405. if (linkedObject != null)
  406. {
  407. linkedObject.transform.position = v3Pos + v3Offset;
  408. }
  409. }
  410. }
  411.  
  412. void OnMouseOut()
  413. {
  414. ObjectMouseDown = false;
  415. }
  416.  
  417. //Throw pokeball when mouse up from ball
  418.  
  419. void OnMouseUp()
  420. {
  421.  
  422.  
  423.  
  424. curveParticle.Stop();
  425. curveParticle.gameObject.SetActive(false);
  426. isGettingDirection = false;
  427.  
  428. if (!currentBall || !isGameStart)
  429. return;
  430.  
  431.  
  432. var endPos = Input.mousePosition;
  433. endPos.z = currentBall.transform.position.z + Camera.main.transform.rotation.y;
  434.  
  435. Debug.Log("EndPostion: " + endPos) ;
  436. Debug.Log("EndPostion Z: " + endPos.z);
  437. Debug.Log("Currentball pos: " + currentBall.transform.position.z);
  438.  
  439.  
  440.  
  441. int ballPositionIndex = ballPos.Count - 2;
  442.  
  443. if (ballPositionIndex < 0)
  444. ballPositionIndex = 0;
  445.  
  446. Vector3 force = currentBall.transform.position - ballPos[ballPositionIndex];
  447.  
  448. if (Vector3.Distance(lastPos, startPos) <= 0.0f)
  449. {
  450. currentBall.SendMessage("ResetBall", SendMessageOptions.RequireReceiver);
  451. return;
  452. }
  453.  
  454. //if downside
  455. if (currentBall.transform.position.y <= ballPos[ballPositionIndex].y)
  456. {
  457. currentBall.SendMessage("ResetBall", SendMessageOptions.RequireReceiver);
  458.  
  459. return;
  460. }
  461.  
  462. //if not swipe
  463. if (force.magnitude < 0.02f)
  464. {
  465. currentBall.SendMessage("ResetBall", SendMessageOptions.RequireReceiver);
  466. return;
  467. }
  468.  
  469. force.z = force.magnitude;
  470. force /= (Time.time - ballTime[ballPositionIndex]);
  471. force.y /= 2f;
  472. force.x /= 2f;
  473.  
  474. force.x = Mathf.Clamp(force.x, minThrow.x, maxThrow.x);
  475. force.y = Mathf.Clamp(force.y, minThrow.y, maxThrow.y);
  476. force.z = Mathf.Clamp(force.z, minThrow.z, maxThrow.z);
  477.  
  478. //send message ball was thrown
  479. currentBall.SendMessage("SetIsThrowed", true, SendMessageOptions.RequireReceiver);
  480. _gameSoloManager.GetComponent<GameSoloManager>().BallThrowed();
  481.  
  482.  
  483. if (isCurveThrow)
  484. {
  485. force.z -= 0.1f;
  486. if (angleDirection == 1)
  487. {
  488. if (force.z < 2.3f)
  489. force.z = 2.3f;
  490. currentBall.SendMessage("SetCurve", -0.2);
  491. }
  492. else
  493. {
  494. if (force.z < 2.3f)
  495. force.z = 2.3f;
  496.  
  497. currentBall.SendMessage("SetCurve", 0.2);
  498. }
  499. }
  500. else
  501. {
  502. if (force.z < 2.3f)
  503. force.z = 2.3f;
  504. }
  505.  
  506. //get rigidbody
  507. Rigidbody ballRigidbody = currentBall.GetComponent<Rigidbody>();
  508. //enable collider
  509. currentBall.GetComponent<Collider>().enabled = true;
  510. ballRigidbody.useGravity = true;
  511.  
  512. ballRigidbody.AddForce(force * factor);
  513.  
  514. StartCoroutine(GetBallCoroutine());
  515.  
  516. currentBall = null;
  517.  
  518. }
  519.  
  520.  
  521.  
  522.  
  523. #endregion
  524.  
  525. #region Private_Methods
  526. #endregion
  527.  
  528. #region Public_Methods
  529.  
  530. /// <summary>
  531. /// Gets the new ball.
  532. /// </summary>
  533. public void GetNewBall()
  534. {
  535. if (currentBall != null)
  536. Destroy(currentBall);
  537. currentBall = Instantiate(ballModels[ballSelectorNumber], ballModels[ballSelectorNumber].transform.position, Quaternion.identity) as GameObject;
  538. //disable collider
  539. currentBall.GetComponent<Collider>().enabled = false;
  540. }
  541. #endregion
  542.  
  543. #region Properties
  544. /// <summary>
  545. /// Gets the instance.
  546. /// </summary>
  547. /// <value>The instance.</value>
  548. public static ThrowBall Instance
  549. {
  550. get
  551. {
  552. return throwPaperInstance;
  553. }
  554. }
  555.  
  556. /// <summary>
  557. /// Gets or sets a value indicating whether this instance is getting direction.
  558. /// </summary>
  559. /// <value><c>true</c> if this instance is getting direction; otherwise, <c>false</c>.</value>
  560. public bool IsGettingDirection
  561. {
  562. get
  563. {
  564. return isGettingDirection;
  565. }
  566. set
  567. {
  568. isGettingDirection = value;
  569. }
  570. }
  571.  
  572. /// <summary>
  573. /// Gets or sets a value indicating whether this instance is game start.
  574. /// </summary>
  575. /// <value><c>true</c> if this instance is game start; otherwise, <c>false</c>.</value>
  576. public bool IsGameStart
  577. {
  578. get
  579. {
  580. return isGameStart;
  581. }
  582. set
  583. {
  584. isGameStart = value;
  585. }
  586. }
  587.  
  588.  
  589. #endregion
  590.  
  591. #region Coroutine
  592.  
  593. /// <summary>
  594. /// Gets the ball coroutine.
  595. /// </summary>
  596. /// <returns>The ball coroutine.</returns>
  597. IEnumerator GetBallCoroutine()
  598. {
  599. yield return new WaitForSeconds(2.0f);
  600.  
  601. if (currentBall != null)
  602. Destroy(currentBall);
  603.  
  604. currentBall = Instantiate(ballModels[ballSelectorNumber], ballModels[ballSelectorNumber].transform.position, Quaternion.identity) as GameObject;
  605. //disable collider
  606. currentBall.GetComponent<Collider>().enabled = false;
  607.  
  608. }
  609. #endregion
  610.  
  611. #region RPC
  612. #endregion
  613. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement