BowCamera.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. /* 弓的相机 */
  5. public class BowCamera : MonoBehaviour
  6. {
  7. //相机组件
  8. Camera _cameraComp;
  9. Camera cameraComp {
  10. get {
  11. if (!_cameraComp) _cameraComp = GetComponent<Camera>();
  12. return _cameraComp;
  13. }
  14. }
  15. //控制的手臂弓
  16. ArmBow _armBow;
  17. ArmBow armBow {
  18. get {
  19. if (!_armBow) _armBow = GetComponentInChildren<ArmBow>();
  20. return _armBow;
  21. }
  22. }
  23. //本地欧拉角记录值
  24. Vector3 localEulerAngles;
  25. //触摸检测器
  26. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  27. //触摸模式开关
  28. private static bool _isTouchMode = true;
  29. public static bool isTouchMode {
  30. get {
  31. if (CommonConfig.isReleaseVersion) return false;
  32. return _isTouchMode;
  33. }
  34. set {
  35. _isTouchMode = value;
  36. }
  37. }
  38. //左右转动范围限制
  39. float[] limitRangeRotateY = {-80, 80};
  40. //上下转动范围限制
  41. float[] limitRangeRotateX = {-80, 80};
  42. //禁止逻辑,只用于同步状态和渲染,目前用于联机
  43. [NonSerialized] public bool banLogic = false;
  44. private static BowCamera _ins;
  45. public static BowCamera ins {
  46. get {
  47. if (!_ins) {
  48. _ins = GameObject.FindObjectOfType<BowCamera>();
  49. }
  50. return _ins;
  51. }
  52. }
  53. void Awake() {
  54. _ins = this;
  55. localEulerAngles = transform.localEulerAngles;
  56. RecordDefaultCameraFieldOfView();
  57. if (UserSettings.ins.bowCameraFixed && !CommonConfig.isReleaseVersion) {
  58. bowCameraFixed = new BowCameraFixed(this);
  59. }
  60. }
  61. void Start() {
  62. touchChecker.onMoved += delegate(Touch t, bool isOnUI) {
  63. if (banLogic) return;
  64. if (isOnUI) return;
  65. //触摸控制镜头和拉弓射箭
  66. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  67. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  68. this.transform.localEulerAngles = this.localEulerAngles;
  69. };
  70. touchChecker.onEnded += delegate(Touch t, bool isOnUI) {
  71. if (banLogic) return;
  72. if (!isOnUI) armBow.ADS_fire();
  73. };
  74. }
  75. void Update()
  76. {
  77. //更新相机视野
  78. if (cameraComp && !banCameraFieldOfView) {
  79. cameraComp.fieldOfView = cameraFieldOfView;
  80. }
  81. if (banLogic) return;
  82. //满足以下条件则阻止控制输入
  83. if (GameMgr.ins.gameOver) {
  84. return;
  85. }
  86. if (GameMgr.debugInEditor) {
  87. //鼠标控制镜头和拉弓射箭
  88. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  89. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  90. this.transform.localEulerAngles = this.localEulerAngles;
  91. if (EventSystem.current.IsPointerOverGameObject()) return;
  92. }
  93. else if (isTouchMode) {
  94. touchChecker.Update();
  95. } else {
  96. if (SB_EventSystem.ins && SB_EventSystem.ins.simulateMouseIsAwaked) return;
  97. //需要-镜头看向九轴姿态虚拟节点
  98. needLookAtPoint = true;
  99. }
  100. }
  101. //需要-镜头看向九轴姿态虚拟节点?
  102. bool needLookAtPoint = false;
  103. //镜头旋转转换比值
  104. float[] _bowRotateConvertRate = null;
  105. float bowRotateConvertRate {
  106. get {
  107. if (_bowRotateConvertRate == null) {
  108. _bowRotateConvertRate = new float[]{ UserSettings.ins.bowRotateConvert.GetRate() };
  109. }
  110. return _bowRotateConvertRate[0];
  111. }
  112. }
  113. void LateUpdate() {
  114. if (needLookAtPoint) {
  115. needLookAtPoint = false;
  116. //镜头看向九轴姿态虚拟节点
  117. this.transform.LookAt(CameraToLook.ins.point);
  118. if (!CommonConfig.isReleaseVersion) {
  119. //镜头旋转比值转换
  120. Vector3 localAngles = this.transform.localEulerAngles;
  121. localAngles.x = Mathf.Clamp((localAngles.x > 180 ? localAngles.x - 360 : localAngles.x) * bowRotateConvertRate,
  122. limitRangeRotateX[0], limitRangeRotateX[1]);
  123. localAngles.y = Mathf.Clamp((localAngles.y > 180 ? localAngles.y - 360 : localAngles.y) * bowRotateConvertRate,
  124. limitRangeRotateY[0], limitRangeRotateY[1]);
  125. if (bowCameraFixed != null) {
  126. localAngles = bowCameraFixed.LimitBowAngle(localAngles);
  127. }
  128. this.transform.localEulerAngles = localAngles;
  129. }
  130. }
  131. onAfterLateUpdate?.Invoke();
  132. }
  133. Action onAfterLateUpdate;
  134. //---------------相机视野的相关操作---------------------
  135. //视野值记录
  136. float cameraFieldOfView = 60;
  137. [NonSerialized] public float defaultCameraFieldOfView = 60;
  138. private void RecordDefaultCameraFieldOfView() {
  139. defaultCameraFieldOfView = cameraComp.fieldOfView;
  140. cameraFieldOfView = defaultCameraFieldOfView;
  141. }
  142. //禁止相机视野改变
  143. [NonSerialized] public bool banCameraFieldOfView = false;
  144. public void SetCameraFieldOfView(float value) {
  145. cameraComp.fieldOfView = value;
  146. }
  147. public void SetCameraFieldOfViewRecord(float value) {
  148. cameraFieldOfView = value;
  149. }
  150. //拉弓时的相机视野值变化
  151. public void updateFollowPullBow() {
  152. // if (cameraFieldOfView > 40) {
  153. // cameraFieldOfView -= 20 * Time.deltaTime;
  154. // } else {
  155. // cameraFieldOfView = 40;
  156. // }
  157. }
  158. //松开拉弓时的相机视野值变化
  159. public void updateGiveUpPullBow() {
  160. // if (cameraFieldOfView < 60) {
  161. // cameraFieldOfView += 20 * Time.deltaTime;
  162. // } else {
  163. // cameraFieldOfView = 60;
  164. // }
  165. }
  166. // 2022-04-28
  167. // ------ 添加固定镜头选项后,新增的API ------
  168. bool isArrowFollowing = false;
  169. public void SetArrowFollowing(bool value) {
  170. isArrowFollowing = value;
  171. cameraComp.enabled = !isArrowFollowing;
  172. AutoSwitchCamera();
  173. }
  174. bool isScaleAimDisplaying = false;
  175. public void SetScaleAimDisplaying(bool value) {
  176. isScaleAimDisplaying = value;
  177. AutoSwitchCamera();
  178. }
  179. void AutoSwitchCamera() {
  180. if (bowCameraFixed == null) {
  181. cameraComp.enabled = !isArrowFollowing;
  182. } else {
  183. bowCameraFixed.gameObject.SetActive(!isScaleAimDisplaying && !isArrowFollowing);
  184. cameraComp.enabled = isScaleAimDisplaying && !isArrowFollowing;
  185. }
  186. }
  187. public Camera GetRenderCamera() {
  188. if (bowCameraFixed == null) {
  189. return cameraComp;
  190. } else {
  191. if (bowCameraFixed.gameObject.activeSelf) {
  192. return bowCameraFixed.camera;
  193. } else {
  194. return cameraComp;
  195. }
  196. }
  197. }
  198. public BowCameraFixed bowCameraFixed = null;
  199. public class BowCameraFixed {
  200. public GameObject gameObject;
  201. public Transform transform;
  202. public Camera camera;
  203. private UnityStandardAssets.ImageEffects.Blur blur;
  204. //bowCameraComponent
  205. BowCamera bowCamera;
  206. private Camera targetCamera;
  207. private UnityStandardAssets.ImageEffects.Blur targetBlur;
  208. public BowCameraFixed(BowCamera bowCamera) {
  209. this.bowCamera = bowCamera;
  210. gameObject = new GameObject("BowCameraFixed");
  211. transform = gameObject.transform;
  212. //复制Camera组件
  213. targetCamera = bowCamera.cameraComp;
  214. camera = gameObject.AddComponent<Camera>();
  215. camera.tag = "MainCamera";
  216. camera.clearFlags = targetCamera.clearFlags;
  217. camera.backgroundColor = targetCamera.backgroundColor;
  218. camera.cullingMask = targetCamera.cullingMask;
  219. camera.fieldOfView = targetCamera.fieldOfView;
  220. camera.nearClipPlane = targetCamera.nearClipPlane;
  221. camera.depth = targetCamera.depth + 0.1f;
  222. camera.renderingPath = targetCamera.renderingPath;
  223. //复制Blur组件
  224. targetBlur = bowCamera.GetComponent<UnityStandardAssets.ImageEffects.Blur>();
  225. if (targetBlur) {
  226. blur = gameObject.AddComponent<UnityStandardAssets.ImageEffects.Blur>();
  227. blur.enabled = targetBlur.enabled;
  228. blur.blurShader = targetBlur.blurShader;
  229. blur.blurSpread = targetBlur.blurSpread;
  230. blur.iterations = targetBlur.iterations;
  231. }
  232. //设置Transform属性
  233. transform.parent = bowCamera.transform.parent;
  234. transform.localPosition = bowCamera.transform.localPosition;
  235. transform.localScale = bowCamera.transform.localScale;
  236. transform.localRotation = Quaternion.identity;
  237. //监听和驱动LateUpdate
  238. bowCamera.onAfterLateUpdate += LateUpdate;
  239. //切换镜头
  240. bowCamera.AutoSwitchCamera();
  241. InitForLimitBound();
  242. }
  243. void LateUpdate() {
  244. if (gameObject.activeSelf) {
  245. CrossHair.ins.UpdatePostionWhenFixedCamera();
  246. }
  247. if (blur) {
  248. blur.enabled = targetBlur.enabled;
  249. if (blur.enabled) {
  250. blur.blurShader = targetBlur.blurShader;
  251. blur.blurSpread = targetBlur.blurSpread;
  252. blur.iterations = targetBlur.iterations;
  253. }
  254. }
  255. }
  256. //边界限制
  257. float[] rangeRotateY = {-80, 80};
  258. float rangeRotateX = 25;
  259. Vector3 vecF;
  260. Vector3 vecU;
  261. public int outBoundIndex = -1; //-1为未出界
  262. void InitForLimitBound() {
  263. for (int i = (int)rangeRotateY[0]; i < 0; i++) {
  264. Vector3 pos = transform.position + Quaternion.AngleAxis(i, Vector3.up) * transform.forward;
  265. pos = camera.WorldToViewportPoint(pos);
  266. if (pos.x >= 0) {
  267. rangeRotateY[0] = i;
  268. rangeRotateY[1] = -i - 4;
  269. break;
  270. }
  271. }
  272. rangeRotateX = camera.fieldOfView / 2;
  273. vecF = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.forward;
  274. vecU = Quaternion.AngleAxis(rangeRotateX, Vector3.right) * Vector3.up;
  275. }
  276. public Vector3 LimitBowAngle(Vector3 outAngle) {
  277. float angleY = outAngle.y;
  278. float angleX = outAngle.x;
  279. outAngle.y = Mathf.Clamp(angleY, rangeRotateY[0], rangeRotateY[1]);
  280. Vector3 vec = Quaternion.AngleAxis(outAngle.y, vecU) * vecF;
  281. float rx = (float)(Math.Asin(vec.y) / Math.PI * 180) * (angleX < 0 ? 1 : -1);
  282. if (angleY < rangeRotateY[0]) outBoundIndex = 0;
  283. else if (angleY > rangeRotateY[1]) outBoundIndex = 1;
  284. else if (angleX < -rangeRotateX) outBoundIndex = 2;
  285. else if (angleX > rangeRotateX) outBoundIndex = 3;
  286. else outBoundIndex = -1;
  287. if (Mathf.Abs(angleX) > Mathf.Abs(rx)) {
  288. outAngle.x = rx;
  289. }
  290. //因为Vector3是struct,传递给函数是值传递而非引用传递
  291. return outAngle;
  292. }
  293. }
  294. }