Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using UnityEngine;
- using System.Collections;
- public class Ball : MonoBehaviour
- {
- public static Ball _instance;
- private GameObject _attachedGameObject;
- private const float _TIME_TO_RESET = 2.0f;
- private bool _hittable = true;
- // Use this for initialization
- void Start()
- {
- _instance = this;
- print(GameManager._controlledPlayer);
- StartCoroutine(UpdateBallPos());
- }
- IEnumerator UpdateBallPos()
- {
- while (true)
- {
- yield return new WaitForSeconds(0.02f);
- if (GameManager._instance._gameStarted)
- {
- break;
- }
- }
- if (GameManager._controlledPlayer == 0)
- {
- while (true)
- {
- yield return new WaitForSeconds(GameManager._timeBetweenNetworkMessages);
- if (transform.parent != null)
- {
- FakeNet._instance.Send(
- NetworkHandler.MessageType.StateOfBall.ToString() + "%" +
- (transform.parent.transform.position.x - transform.position.x) + " " +
- (transform.parent.transform.position.y - transform.position.y) + " " +
- transform.parent.GetComponent<PlayerLogic>()._playerID);
- }
- }
- }
- }
- // Update is called once per frame
- void Update()
- {
- }
- IEnumerator ResetBallState()
- {
- yield return new WaitForSeconds(_TIME_TO_RESET);
- _hittable = true;
- }
- public static void ReceiveBallInfo(string pData)
- {
- float xDiffMessage = float.Parse(pData.Split(' ')[0]);
- float yDiffMessage = float.Parse(pData.Split(' ')[1]);
- int index = int.Parse(pData.Split(' ')[2]);
- if (index == 1)
- index = 0;
- else
- index = 1;
- Vector3 pos = new Vector3(GameManager._instance._players[index].transform.position.x - xDiffMessage,
- GameManager._instance._players[index].transform.position.y - yDiffMessage);
- _instance.transform.position = pos;
- _instance.transform.parent = GameManager._instance._players[index].transform;
- }
- void OnCollisionEnter2D(Collision2D pCol)
- {
- if (_hittable == false)
- return;
- _hittable = false;
- if (pCol.gameObject.GetComponent<PlayerLogic>() == null)
- return;
- _instance.transform.SetParent(pCol.gameObject.transform);
- _instance._attachedGameObject = pCol.gameObject;
- StartCoroutine(ResetBallState());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement