/Assignment2Submission/Client/Unity Scripts/Ball.cs

https://bitbucket.org/MalcolmH/assignment2networking · C# · 145 lines · 105 code · 12 blank · 28 comment · 20 complexity · 868f6dec6091aed77c0bd5453aa50fb5 MD5 · raw file

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using UnityEngine.SceneManagement;
  6. using System.Runtime.InteropServices;
  7. public class Ball : MonoBehaviour
  8. {
  9. private Vector3 velocity;
  10. private int scoreL, scoreR = 0;
  11. public Text scoreLText, scoreRText, winText;
  12. private float speed = 5f;
  13. // Use this for initialization
  14. void Start ()
  15. {
  16. velocity = Vector3.Normalize(new Vector3(speed, speed, 0f)) * speed;
  17. }
  18. public static Vector3 StringToVector3(string sVector)
  19. {
  20. // Remove the parentheses
  21. if (sVector.StartsWith("(") && sVector.EndsWith(")"))
  22. {
  23. sVector = sVector.Substring(1, sVector.Length - 2);
  24. }
  25. // split the items
  26. string[] sArray = sVector.Split(',');
  27. // store as a Vector3
  28. Vector3 result = new Vector3(
  29. float.Parse(sArray[0]),
  30. float.Parse(sArray[1]),
  31. float.Parse(sArray[2]));
  32. return result;
  33. }
  34. // Update is called once per frame
  35. void Update ()
  36. {
  37. transform.position += velocity * Time.deltaTime;
  38. if (scoreL > 10)
  39. {
  40. velocity = Vector3.zero;
  41. transform.position = new Vector3(0f, 1f, 1f);
  42. StartCoroutine(WinLeft());
  43. }
  44. else if (scoreR > 10)
  45. {
  46. velocity = Vector3.zero;
  47. transform.position = new Vector3(0f, 1f, 1f);
  48. StartCoroutine(WinRight());
  49. }
  50. /*string recvUdp = Marshal.PtrToStringAnsi(Client.RecvMsg(1));
  51. if (recvUdp.StartsWith("B"))
  52. {
  53. recvUdp = recvUdp.Substring(1);
  54. velocity = StringToVector3(recvUdp);
  55. }*/
  56. }
  57. IEnumerator WinRight ()
  58. {
  59. winText.text = "Right Player Wins!";
  60. yield return new WaitForSeconds(2f * Time.deltaTime);
  61. winText.text = " ";
  62. yield return new WaitForSeconds(2f * Time.deltaTime);
  63. winText.text = "Right Player Wins!";
  64. yield return new WaitForSeconds(2f * Time.deltaTime);
  65. winText.text = " ";
  66. yield return new WaitForSeconds(2f * Time.deltaTime);
  67. winText.text = "Right Player Wins!";
  68. yield return new WaitForSeconds(2f * Time.deltaTime);
  69. winText.text = " ";
  70. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  71. /*speed = 5f;
  72. scoreL = 0;
  73. scoreLText.text = scoreL.ToString();
  74. scoreR = 0;
  75. scoreRText.text = scoreR.ToString();
  76. velocity = Vector3.Normalize(new Vector3(speed, speed, 0f)) * speed;*/
  77. }
  78. IEnumerator WinLeft()
  79. {
  80. winText.text = "Left Player Wins!";
  81. yield return new WaitForSeconds(2f * Time.deltaTime);
  82. winText.text = " ";
  83. yield return new WaitForSeconds(2f * Time.deltaTime);
  84. winText.text = "Left Player Wins!";
  85. yield return new WaitForSeconds(2f * Time.deltaTime);
  86. winText.text = " ";
  87. yield return new WaitForSeconds(2f * Time.deltaTime);
  88. winText.text = "Left Player Wins!";
  89. yield return new WaitForSeconds(2f * Time.deltaTime);
  90. winText.text = " ";
  91. SceneManager.LoadScene(SceneManager.GetActiveScene().name);
  92. /*speed = 5f;
  93. scoreL = 0;
  94. scoreLText.text = scoreL.ToString();
  95. scoreR = 0;
  96. scoreRText.text = scoreR.ToString();
  97. velocity = Vector3.Normalize(new Vector3(speed, speed, 0f)) * speed;*/
  98. }
  99. void OnCollisionEnter (Collision other)
  100. {
  101. if (other.gameObject.name == "WallT" || other.gameObject.name == "WallB")
  102. {
  103. velocity.y = -velocity.y;
  104. }
  105. else if (other.gameObject.name == "WallL")
  106. {
  107. scoreR += 1;
  108. scoreRText.text = scoreR.ToString();
  109. speed = 5f;
  110. velocity = Vector3.Normalize(new Vector3(speed, 0f, 0f)) * speed;
  111. transform.position = new Vector3(-6.5f, GameObject.Find("PaddleL").transform.position.y, 0f);
  112. }
  113. else if (other.gameObject.name == "WallR")
  114. {
  115. scoreL += 1;
  116. scoreLText.text = scoreL.ToString();
  117. speed = 5f;
  118. velocity = Vector3.Normalize(new Vector3(-speed, 0f, 0f)) * speed;
  119. transform.position = new Vector3(6.5f, GameObject.Find("PaddleR").transform.position.y, 0f);
  120. }
  121. else if (other.gameObject.name == "PaddleL" || other.gameObject.name == "PaddleR")
  122. {
  123. //speed += 1f;
  124. velocity.x = -velocity.x;
  125. velocity.y = (transform.position.y - other.transform.position.y) * speed;
  126. //while (velocity.y == 0f)
  127. //{
  128. // velocity.y = Random.Range(-0.1f, 0.1f);
  129. //}
  130. velocity = Vector3.Normalize(velocity) * speed;
  131. }
  132. }
  133. }