Advertisement
Guest User

Untitled

a guest
Dec 6th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class Ball : MonoBehaviour
  6. {
  7. public static Ball _instance;
  8. private GameObject _attachedGameObject;
  9. private const float _TIME_TO_RESET = 2.0f;
  10. private bool _hittable = true;
  11.  
  12. // Use this for initialization
  13. void Start()
  14. {
  15. _instance = this;
  16. print(GameManager._controlledPlayer);
  17. StartCoroutine(UpdateBallPos());
  18. }
  19.  
  20. IEnumerator UpdateBallPos()
  21. {
  22.  
  23. while (true)
  24. {
  25. yield return new WaitForSeconds(0.02f);
  26. if (GameManager._instance._gameStarted)
  27. {
  28. break;
  29. }
  30. }
  31.  
  32. if (GameManager._controlledPlayer == 0)
  33. {
  34. while (true)
  35. {
  36. yield return new WaitForSeconds(GameManager._timeBetweenNetworkMessages);
  37. if (transform.parent != null)
  38. {
  39. FakeNet._instance.Send(
  40. NetworkHandler.MessageType.StateOfBall.ToString() + "%" +
  41. (transform.parent.transform.position.x - transform.position.x) + " " +
  42. (transform.parent.transform.position.y - transform.position.y) + " " +
  43. transform.parent.GetComponent<PlayerLogic>()._playerID);
  44. }
  45. }
  46. }
  47. }
  48.  
  49. // Update is called once per frame
  50. void Update()
  51. {
  52.  
  53. }
  54.  
  55. IEnumerator ResetBallState()
  56. {
  57. yield return new WaitForSeconds(_TIME_TO_RESET);
  58. _hittable = true;
  59. }
  60.  
  61. public static void ReceiveBallInfo(string pData)
  62. {
  63. float xDiffMessage = float.Parse(pData.Split(' ')[0]);
  64. float yDiffMessage = float.Parse(pData.Split(' ')[1]);
  65. int index = int.Parse(pData.Split(' ')[2]);
  66.  
  67. if (index == 1)
  68. index = 0;
  69. else
  70. index = 1;
  71.  
  72. Vector3 pos = new Vector3(GameManager._instance._players[index].transform.position.x - xDiffMessage,
  73. GameManager._instance._players[index].transform.position.y - yDiffMessage);
  74.  
  75. _instance.transform.position = pos;
  76.  
  77. _instance.transform.parent = GameManager._instance._players[index].transform;
  78. }
  79.  
  80. void OnCollisionEnter2D(Collision2D pCol)
  81. {
  82. if (_hittable == false)
  83. return;
  84.  
  85. _hittable = false;
  86.  
  87. if (pCol.gameObject.GetComponent<PlayerLogic>() == null)
  88. return;
  89.  
  90. _instance.transform.SetParent(pCol.gameObject.transform);
  91. _instance._attachedGameObject = pCol.gameObject;
  92. StartCoroutine(ResetBallState());
  93. }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement