Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

  1. namespace Oculus.Platform.Samples.VrHoops
  2. {
  3. using UnityEngine;
  4. using System.Collections;
  5. // This component handles network coordination for the moving backboard.
  6. // Although there is randomness in the next direction, the movement is
  7. // otherwise completely predictable, much like a moving platform or door,
  8. // thus we only need to send occasional updates. If the position of the
  9. // backboard is not correct, the GoalMover will gradually nudge it in the
  10. // correct direction until the local and remote motion is synchronized.
  11. public class P2PNetworkGoal : MonoBehaviour
  12. {
  13. // cached reference to the associated GoalMover component
  14. private GoalMover m_goal;
  15. // the last move direction we sent to remote clients
  16. private Vector3 m_lastSentMoveDirection;
  17. private bool m_sendUpdates;
  18. public bool SendUpdates
  19. {
  20. set { m_sendUpdates = value; }
  21. }
  22. void Awake()
  23. {
  24. m_goal = gameObject.GetComponent<GoalMover>();
  25. }
  26. void FixedUpdate ()
  27. {
  28. // since the backboard's movement is deterministic, we don't need to send position
  29. // updates constantly, just when the move direction changes
  30. if (m_sendUpdates && m_goal.MoveDirection != m_lastSentMoveDirection)
  31. {
  32. SendBackboardUpdate();
  33. }
  34. }
  35. public void SendBackboardUpdate()
  36. {
  37. m_lastSentMoveDirection = m_goal.MoveDirection;
  38. float time = Time.realtimeSinceStartup;
  39. PlatformManager.P2P.SendBackboardUpdate(
  40. time, transform.localPosition,
  41. m_goal.MoveDirection, m_goal.NextMoveDirection);
  42. }
  43. // message from the remote player with new transforms
  44. public void RemoteBackboardUpdate(float remoteTime, Vector3 pos, Vector3 moveDir, Vector3 nextMoveDir)
  45. {
  46. // interpolate the position forward since the backboard would have moved over
  47. // the time it took to send the message
  48. float time = Time.realtimeSinceStartup;
  49. float numMissedSteps = (time - remoteTime) / Time.fixedDeltaTime;
  50. m_goal.ExpectedPosition = pos + (Mathf.Round(numMissedSteps) * moveDir);
  51. m_goal.MoveDirection = moveDir;
  52. m_goal.NextMoveDirection = nextMoveDir;
  53. }
  54. }
  55. }