SmartBowController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using AppUI.Bluetooth;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. namespace WildAttack
  7. {
  8. public class SmartBowController : MonoBehaviour
  9. {
  10. public static SmartBowController Instance;
  11. void Start()
  12. {
  13. Instance = this;
  14. SimulateMouseController.ins?.RemoveOpenLocker("NotGame");
  15. StartCoroutine(SetOutBoundChecker());
  16. }
  17. public void onInit() {
  18. if (GameMananger.GetInstance().CurrentGameType == GameTypeEnum.SinglePlayer)
  19. {
  20. if (SmartBowDeviceHub.ins?.Aim?.bRuning9Axis() == true)
  21. {
  22. CameraToLook.ins.onParseRotation += OnRotationUpdate;
  23. }
  24. else
  25. {
  26. InfraredDemo.infraredCameraHelper.OnPositionUpdate += OnScreenPointUpdate;
  27. }
  28. }
  29. else
  30. InfraredDemo.infraredCameraHelper.OnPositionUpdate2 += OnScreenPointsUpdate;
  31. }
  32. IEnumerator SetOutBoundChecker()
  33. {
  34. yield return null;
  35. CrossHairOutBoundChecker1 outBoundChecker = Instantiate(Resources.Load<GameObject>("Prefabs/CrossHairOutBoundChecker1")).GetComponent<CrossHairOutBoundChecker1>();
  36. outBoundChecker.FetchOutBoundIndex = () => GameMananger.GetInstance().outBoundIndex;
  37. }
  38. void OnDestroy()
  39. {
  40. if (Instance == this) Instance = null;
  41. if (GameMananger.GetInstance().CurrentGameType == GameTypeEnum.SinglePlayer)
  42. {
  43. if (SmartBowDeviceHub.ins?.Aim?.bRuning9Axis() == true)
  44. {
  45. CameraToLook.ins.onParseRotation -= OnRotationUpdate;
  46. }
  47. else
  48. {
  49. InfraredDemo.infraredCameraHelper.OnPositionUpdate -= OnScreenPointUpdate;
  50. }
  51. }
  52. else
  53. InfraredDemo.infraredCameraHelper.OnPositionUpdate2 -= OnScreenPointsUpdate;
  54. }
  55. public void OnRotationUpdate(Quaternion rotation)
  56. {
  57. if (SB_EventSystem.ins.simulateMouseIsAwaked) return;
  58. GameMananger.GetInstance().OnRotationUpdate(rotation);
  59. }
  60. public void OnScreenPointUpdate(Vector2 point, Vector2 cameraLocation)
  61. {
  62. if (SB_EventSystem.ins.simulateMouseIsAwaked) return;
  63. GameMananger.GetInstance().OnScreenPointUpdate(point);
  64. }
  65. public void OnScreenPointsUpdate(Vector2[] points, Vector2[] cameraLocations)
  66. {
  67. if (points == null || cameraLocations == null) return;
  68. if (SB_EventSystem.ins.simulateMouseIsAwaked) return;
  69. for (int i = 0; i < points.Length; i++)
  70. {
  71. Vector2 point = points[i];
  72. PlayerTypeEnum playerType = (i == 0) ? PlayerTypeEnum.First : PlayerTypeEnum.Second;
  73. // 传给 GameManager 逻辑(准星控制)
  74. GameMananger.GetInstance()?.OnScreenPointUpdate(point, playerType);
  75. }
  76. }
  77. float _lastShootTime = 0;
  78. public void OnShooting(float speed)
  79. {
  80. if (Time.time == 0) return;
  81. if (Time.realtimeSinceStartup - _lastShootTime < 1) return;
  82. _lastShootTime = Time.realtimeSinceStartup;
  83. GameMananger.GetInstance().OnModuleShooting(speed);
  84. }
  85. float _lastShootTime_2P = 0;
  86. public void OnShooting2P(float speed)
  87. {
  88. if (Time.time == 0) return;
  89. if (Time.realtimeSinceStartup - _lastShootTime_2P < 1) return;
  90. _lastShootTime_2P = Time.realtimeSinceStartup;
  91. GameMananger.GetInstance().OnModuleShooting(speed,PlayerTypeEnum.Second);
  92. }
  93. public bool IsBluetoothModuleInited()
  94. {
  95. return SmartBowDeviceHub.ins && SmartBowDeviceHub.ins.IsConnected(BluetoothPlayer.FIRST_PLAYER);
  96. }
  97. }
  98. }