LauncherCtrl.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// 弓箭手状态枚举
  8. /// </summary>
  9. public enum ArmBowState
  10. {
  11. none,
  12. /// <summary>待机 </summary>
  13. idel,
  14. /// <summary>准备 </summary>
  15. prepare,
  16. /// <summary>瞄准 </summary>
  17. ain,
  18. /// <summary>射击 </summary>
  19. launch,
  20. }
  21. /// <summary>
  22. /// 播放动画命常量
  23. /// </summary>
  24. public class ArmBowAnimConstName
  25. {
  26. public const string IDEL = "dajian";
  27. public const string PREPARE = "lagong";
  28. public const string LAUNCH = "fire";
  29. }
  30. /// <summary>
  31. /// 弓箭手控制器类
  32. /// </summary>
  33. public class LauncherCtrl : MonoBehaviour
  34. {
  35. public static LauncherCtrl instance;
  36. [SerializeField] GameObject go_Bullet;
  37. public Transform crossHair; //测试 瞄准得目标点
  38. /// <summary>
  39. /// 枪的观察相机
  40. /// </summary>
  41. public Camera launcherCamera;
  42. /// <summary>
  43. /// 枪模型
  44. /// </summary>
  45. public MeshRenderer go_GunModel;
  46. /// <summary>
  47. /// 枪口位置
  48. /// </summary>
  49. public GameObject gunPoint;
  50. ArmBowState armBowState = ArmBowState.none;
  51. int bulletCount; //可发射得子弹数量
  52. //触摸检测器
  53. JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
  54. //触摸模式开关
  55. private static bool _isTouchMode = true;
  56. private bool isOnUIEnter = false;
  57. public static bool isTouchMode
  58. {
  59. get
  60. {
  61. // if (CommonConfig.isReleaseVersion) return false;
  62. return _isTouchMode;
  63. }
  64. set
  65. {
  66. _isTouchMode = value;
  67. }
  68. }
  69. //左右转动范围限制
  70. float[] limitRangeRotateY = { -40, 40 };
  71. //上下转动范围限制
  72. float[] limitRangeRotateX = { -25, 15 };
  73. //本地欧拉角记录值
  74. Vector3 localEulerAngles;
  75. /// <summary>准心图片 </summary>
  76. public Image img_Anim;
  77. private Vector2 vec_AinPos = new Vector2(10000, 10000);
  78. /// <summary>是否显示准心 </summary>
  79. public bool isShowAni = true;
  80. void Awake()
  81. {
  82. instance = this;
  83. //this.transform.localPosition = new Vector3(0.0865f, -1.692f, -0.1f);
  84. ////设置播放次数 ,不循环,只播放一次。(可以在当个动画模型里面修改,也可以代码同意修改播放器设置)
  85. //anim_Arrow.wrapMode = WrapMode.Once;
  86. //anim_Bow.wrapMode = WrapMode.Once;
  87. if (launcherCamera == null)
  88. {
  89. launcherCamera = transform.parent.GetComponent<Camera>();
  90. }
  91. if (gunPoint == null || go_GunModel == null)
  92. {
  93. go_GunModel = transform.Find("qiang").GetComponent<MeshRenderer>(); ;
  94. gunPoint = transform.Find("qiang/GunPoint").gameObject;
  95. }
  96. if (go_Bullet == null )
  97. {
  98. go_Bullet = transform.Find("qiang/GunPoint/Bullet").gameObject;
  99. }
  100. localEulerAngles = transform.localEulerAngles;
  101. touchChecker.onBegan += delegate (Touch t, bool isOnUI)
  102. {
  103. // Debug.Log(isOnUI);
  104. this.isOnUIEnter = isOnUI;
  105. if (isOnUI) return;
  106. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, true });
  107. };
  108. touchChecker.onMoved += delegate (Touch t, bool isOnUI)
  109. {
  110. // Debug.Log(isOnUI);
  111. // if (banLogic) return;
  112. // if (isOnUI) return;
  113. if (isOnUIEnter) return;
  114. //触摸控制镜头和拉弓射箭
  115. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
  116. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
  117. this.transform.localEulerAngles = this.localEulerAngles;
  118. };
  119. touchChecker.onEnded += delegate (Touch t, bool isOnUI)
  120. {
  121. // if (banLogic) return;
  122. this.isOnUIEnter = false;
  123. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, false });
  124. if (!isOnUI)
  125. Shoot();// armBow.ADS_fire();
  126. };
  127. img_Anim = transform.Find("AniCanvas/Image_Ani").GetComponent<Image>();
  128. }
  129. #region add by JC
  130. void Start()
  131. {
  132. if (AimHandler.ins) AimHandler.ins.onGameRotationUpdate += OnGameRotationUpdate;
  133. if (ShootCheck.ins) ShootCheck.ins.onShoot += OnGameShoot;
  134. }
  135. void OnDestroy()
  136. {
  137. if (AimHandler.ins) AimHandler.ins.onGameRotationUpdate -= OnGameRotationUpdate;
  138. if (ShootCheck.ins) ShootCheck.ins.onShoot -= OnGameShoot;
  139. }
  140. void OnGameRotationUpdate(Quaternion quat)
  141. {
  142. Vector3 angles = quat.eulerAngles;
  143. angles.x = angles.x > 180 ? angles.x - 360 : angles.x;
  144. angles.y = angles.y > 180 ? angles.y - 360 : angles.y;
  145. angles.x = Mathf.Clamp(angles.x, limitRangeRotateX[0], limitRangeRotateX[1]);
  146. angles.y = Mathf.Clamp(angles.y, limitRangeRotateY[0], limitRangeRotateY[1]);
  147. this.transform.localEulerAngles = angles;
  148. }
  149. void OnGameShoot(float speed)
  150. {
  151. this.Shoot();
  152. }
  153. #endregion
  154. public void SetArrowCount( int count)
  155. {
  156. bulletCount = count;
  157. }
  158. /// <summary>
  159. /// 准备射击
  160. /// </summary>
  161. public void ReadyShoot()
  162. {
  163. //如果不是空状态,直接跳出
  164. if (armBowState != ArmBowState.none)
  165. {
  166. return;
  167. }
  168. armBowState = ArmBowState.idel;
  169. }
  170. public void ReadyShoot(int count)
  171. {
  172. SetArrowCount(count);
  173. armBowState = ArmBowState.ain;
  174. }
  175. public void ShowGunModel(bool value)
  176. {
  177. go_GunModel.enabled = value;
  178. }
  179. public void ShowAni(bool value)
  180. {
  181. isShowAni = value;
  182. img_Anim.gameObject.SetActive(isShowAni);
  183. }
  184. /// <summary>
  185. /// 射击
  186. /// </summary>
  187. public void Shoot()
  188. {
  189. if (bulletCount <= 0)
  190. {
  191. return;
  192. }
  193. //有其他地方调用,判断一下是否瞄准状态下
  194. //if (armBowState != ArmBowState.ain)
  195. //{
  196. // return;
  197. //}
  198. //armBowState = ArmBowState.launch;
  199. GameObject arw = GameObject.Instantiate(go_Bullet);
  200. arw.transform.SetParent(go_Bullet.transform.parent);
  201. arw.transform.localPosition = go_Bullet.transform.localPosition;
  202. arw.transform.localRotation = go_Bullet.transform.localRotation;
  203. arw.transform.localScale = go_Bullet.transform.localScale;
  204. arw.SetActive(true);
  205. BulletCtrl arwCtrl = arw.AddComponent<BulletCtrl>();
  206. arw.transform.SetParent(null);
  207. //箭矢发射的目标以及方向
  208. arwCtrl.PlayShoot(gunPoint);
  209. AudioMgr.instance.PlaySound();
  210. //子弹数量
  211. bulletCount--;
  212. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2 , bulletCount });
  213. }
  214. /// <summary>
  215. /// 结束射击
  216. /// </summary>
  217. public void EndShoot()
  218. {
  219. armBowState = ArmBowState.none;
  220. }
  221. private void Update()
  222. {
  223. //简单的有限状态机
  224. switch (armBowState)
  225. {
  226. case ArmBowState.idel:
  227. break;
  228. case ArmBowState.prepare:
  229. break;
  230. case ArmBowState.ain:
  231. AniState();
  232. break;
  233. case ArmBowState.launch:
  234. break;
  235. default:
  236. break;
  237. }
  238. }
  239. /// <summary>
  240. /// 瞄准状态,可以开启 允许移动摄像机,点击发射等功能
  241. /// </summary>
  242. private void AniState()
  243. {
  244. if (bulletCount <= 0)
  245. return;
  246. if (Application.platform == RuntimePlatform.WindowsEditor)
  247. {
  248. if (Input.GetMouseButton(0))
  249. {
  250. if (isOnUIEnter) return;
  251. //鼠标控制镜头和拉弓射箭
  252. this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
  253. this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
  254. this.transform.localEulerAngles = this.localEulerAngles;
  255. }
  256. if (Input.GetMouseButtonDown(0))
  257. {
  258. isOnUIEnter = EventSystem.current.IsPointerOverGameObject();
  259. if (isOnUIEnter) return;
  260. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3 ,true });
  261. // print("按下鼠标");
  262. }
  263. if (Input.GetMouseButtonUp(0))
  264. {
  265. isOnUIEnter = false;
  266. PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, false });
  267. if (EventSystem.current.IsPointerOverGameObject())
  268. {
  269. //Debug.Log("点击到UI");
  270. }
  271. else
  272. {
  273. Shoot();
  274. }
  275. }
  276. }
  277. else if (isTouchMode)
  278. {
  279. touchChecker.Update();
  280. }
  281. //开启准心
  282. if (isShowAni)
  283. {
  284. Vector2 uguiPos = Vector2.zero;
  285. Ray ray = new Ray(gunPoint.transform.position, transform.TransformDirection(Vector3.forward));
  286. Vector3 viewportPos = launcherCamera.WorldToViewportPoint(ray.GetPoint(GameInstantiateData.Instance.int_CameraToCastleDis ));
  287. uguiPos.x = (viewportPos.x - 0.5f) * 1280; // canvasRtm.sizeDelta.x;
  288. uguiPos.y = (viewportPos.y - 0.5f) * 720;// canvasRtm.sizeDelta.y;
  289. img_Anim.transform.localPosition = uguiPos;
  290. }
  291. }
  292. /// <summary>
  293. /// 测试使用
  294. /// </summary>
  295. private void OnDrawGizmos()
  296. {
  297. //Debug.DrawRay(launcherCamera.transform.position, transform.TransformDirection(Vector3.forward) * 200, Color.green);//绘制射线
  298. //Debug.DrawRay(gunPoint.transform.position, transform.TransformDirection(Vector3.forward) * 200, Color.green);//绘制射线
  299. }
  300. }