| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
- /// <summary>
- /// 弓箭手状态枚举
- /// </summary>
- public enum ArmBowState
- {
- none,
- /// <summary>待机 </summary>
- idel,
- /// <summary>准备 </summary>
- prepare,
- /// <summary>瞄准 </summary>
- ain,
- /// <summary>射击 </summary>
- launch,
- }
- /// <summary>
- /// 播放动画命常量
- /// </summary>
- public class ArmBowAnimConstName
- {
- public const string IDEL = "dajian";
- public const string PREPARE = "lagong";
- public const string LAUNCH = "fire";
- }
- /// <summary>
- /// 弓箭手控制器类
- /// </summary>
- public class LauncherCtrl : MonoBehaviour
- {
- public static LauncherCtrl instance;
- [SerializeField] GameObject go_Bullet;
- public Transform crossHair; //测试 瞄准得目标点
- /// <summary>
- /// 枪的观察相机
- /// </summary>
- public Camera launcherCamera;
- /// <summary>
- /// 枪模型
- /// </summary>
- public MeshRenderer go_GunModel;
- /// <summary>
- /// 枪口位置
- /// </summary>
- public GameObject gunPoint;
- ArmBowState armBowState = ArmBowState.none;
- int bulletCount; //可发射得子弹数量
- //触摸检测器
- JC.Unity.TouchChecker touchChecker = new JC.Unity.TouchChecker();
- //触摸模式开关
- private static bool _isTouchMode = true;
- private bool isOnUIEnter = false;
- public static bool isTouchMode
- {
- get
- {
- // if (CommonConfig.isReleaseVersion) return false;
- return _isTouchMode;
- }
- set
- {
- _isTouchMode = value;
- }
- }
- //左右转动范围限制
- float[] limitRangeRotateY = { -40, 40 };
- //上下转动范围限制
- float[] limitRangeRotateX = { -25, 15 };
- //本地欧拉角记录值
- Vector3 localEulerAngles;
- /// <summary>准心图片 </summary>
- public Image img_Anim;
- private Vector2 vec_AinPos = new Vector2(10000, 10000);
- /// <summary>是否显示准心 </summary>
- public bool isShowAni = true;
- void Awake()
- {
- instance = this;
- //this.transform.localPosition = new Vector3(0.0865f, -1.692f, -0.1f);
- ////设置播放次数 ,不循环,只播放一次。(可以在当个动画模型里面修改,也可以代码同意修改播放器设置)
- //anim_Arrow.wrapMode = WrapMode.Once;
- //anim_Bow.wrapMode = WrapMode.Once;
- if (launcherCamera == null)
- {
- launcherCamera = transform.parent.GetComponent<Camera>();
- }
- if (gunPoint == null || go_GunModel == null)
- {
- go_GunModel = transform.Find("qiang").GetComponent<MeshRenderer>(); ;
- gunPoint = transform.Find("qiang/GunPoint").gameObject;
- }
- if (go_Bullet == null )
- {
- go_Bullet = transform.Find("qiang/GunPoint/Bullet").gameObject;
- }
- localEulerAngles = transform.localEulerAngles;
- touchChecker.onBegan += delegate (Touch t, bool isOnUI)
- {
- // Debug.Log(isOnUI);
- this.isOnUIEnter = isOnUI;
- if (isOnUI) return;
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, true });
- };
- touchChecker.onMoved += delegate (Touch t, bool isOnUI)
- {
- // Debug.Log(isOnUI);
- // if (banLogic) return;
- // if (isOnUI) return;
- if (isOnUIEnter) return;
- //触摸控制镜头和拉弓射箭
- this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - t.deltaPosition.y * Time.deltaTime * 5, limitRangeRotateX[0], limitRangeRotateX[1]);
- this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + t.deltaPosition.x * Time.deltaTime * 5, limitRangeRotateY[0], limitRangeRotateY[1]);
- this.transform.localEulerAngles = this.localEulerAngles;
- };
- touchChecker.onEnded += delegate (Touch t, bool isOnUI)
- {
- // if (banLogic) return;
- this.isOnUIEnter = false;
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, false });
- if (!isOnUI)
- Shoot();// armBow.ADS_fire();
- };
- img_Anim = transform.Find("AniCanvas/Image_Ani").GetComponent<Image>();
- }
- #region add by JC
- void Start()
- {
- if (AimHandler.ins) AimHandler.ins.onGameRotationUpdate += OnGameRotationUpdate;
- if (ShootCheck.ins) ShootCheck.ins.onShoot += OnGameShoot;
- }
- void OnDestroy()
- {
- if (AimHandler.ins) AimHandler.ins.onGameRotationUpdate -= OnGameRotationUpdate;
- if (ShootCheck.ins) ShootCheck.ins.onShoot -= OnGameShoot;
- }
- void OnGameRotationUpdate(Quaternion quat)
- {
- Vector3 angles = quat.eulerAngles;
- angles.x = angles.x > 180 ? angles.x - 360 : angles.x;
- angles.y = angles.y > 180 ? angles.y - 360 : angles.y;
- angles.x = Mathf.Clamp(angles.x, limitRangeRotateX[0], limitRangeRotateX[1]);
- angles.y = Mathf.Clamp(angles.y, limitRangeRotateY[0], limitRangeRotateY[1]);
- this.transform.localEulerAngles = angles;
- }
- void OnGameShoot(float speed)
- {
- this.Shoot();
- }
- #endregion
- public void SetArrowCount( int count)
- {
- bulletCount = count;
- }
- /// <summary>
- /// 准备射击
- /// </summary>
- public void ReadyShoot()
- {
- //如果不是空状态,直接跳出
- if (armBowState != ArmBowState.none)
- {
- return;
- }
- armBowState = ArmBowState.idel;
- }
- public void ReadyShoot(int count)
- {
- SetArrowCount(count);
- armBowState = ArmBowState.ain;
- }
- public void ShowGunModel(bool value)
- {
- go_GunModel.enabled = value;
- }
- public void ShowAni(bool value)
- {
- isShowAni = value;
- img_Anim.gameObject.SetActive(isShowAni);
- }
- /// <summary>
- /// 射击
- /// </summary>
- public void Shoot()
- {
- if (bulletCount <= 0)
- {
- return;
- }
- //有其他地方调用,判断一下是否瞄准状态下
- //if (armBowState != ArmBowState.ain)
- //{
- // return;
- //}
- //armBowState = ArmBowState.launch;
- GameObject arw = GameObject.Instantiate(go_Bullet);
- arw.transform.SetParent(go_Bullet.transform.parent);
- arw.transform.localPosition = go_Bullet.transform.localPosition;
- arw.transform.localRotation = go_Bullet.transform.localRotation;
- arw.transform.localScale = go_Bullet.transform.localScale;
- arw.SetActive(true);
- BulletCtrl arwCtrl = arw.AddComponent<BulletCtrl>();
- arw.transform.SetParent(null);
- //箭矢发射的目标以及方向
- arwCtrl.PlayShoot(gunPoint);
- AudioMgr.instance.PlaySound();
- //子弹数量
- bulletCount--;
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2 , bulletCount });
- }
- /// <summary>
- /// 结束射击
- /// </summary>
- public void EndShoot()
- {
- armBowState = ArmBowState.none;
- }
- private void Update()
- {
- //简单的有限状态机
- switch (armBowState)
- {
- case ArmBowState.idel:
- break;
- case ArmBowState.prepare:
- break;
- case ArmBowState.ain:
- AniState();
- break;
- case ArmBowState.launch:
- break;
- default:
- break;
- }
- }
- /// <summary>
- /// 瞄准状态,可以开启 允许移动摄像机,点击发射等功能
- /// </summary>
- private void AniState()
- {
- if (bulletCount <= 0)
- return;
- if (Application.platform == RuntimePlatform.WindowsEditor)
- {
- if (Input.GetMouseButton(0))
- {
- if (isOnUIEnter) return;
- //鼠标控制镜头和拉弓射箭
- this.localEulerAngles.x = Mathf.Clamp(this.localEulerAngles.x - 2f * Input.GetAxis("Mouse Y"), limitRangeRotateX[0], limitRangeRotateX[1]);
- this.localEulerAngles.y = Mathf.Clamp(this.localEulerAngles.y + 2f * Input.GetAxis("Mouse X"), limitRangeRotateY[0], limitRangeRotateY[1]);
- this.transform.localEulerAngles = this.localEulerAngles;
- }
- if (Input.GetMouseButtonDown(0))
- {
- isOnUIEnter = EventSystem.current.IsPointerOverGameObject();
- if (isOnUIEnter) return;
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3 ,true });
- // print("按下鼠标");
- }
- if (Input.GetMouseButtonUp(0))
- {
- isOnUIEnter = false;
- PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 3, false });
- if (EventSystem.current.IsPointerOverGameObject())
- {
- //Debug.Log("点击到UI");
- }
- else
- {
- Shoot();
- }
- }
-
- }
- else if (isTouchMode)
- {
- touchChecker.Update();
- }
- //开启准心
- if (isShowAni)
- {
- Vector2 uguiPos = Vector2.zero;
- Ray ray = new Ray(gunPoint.transform.position, transform.TransformDirection(Vector3.forward));
- Vector3 viewportPos = launcherCamera.WorldToViewportPoint(ray.GetPoint(GameInstantiateData.Instance.int_CameraToCastleDis ));
- uguiPos.x = (viewportPos.x - 0.5f) * 1280; // canvasRtm.sizeDelta.x;
- uguiPos.y = (viewportPos.y - 0.5f) * 720;// canvasRtm.sizeDelta.y;
- img_Anim.transform.localPosition = uguiPos;
- }
- }
- /// <summary>
- /// 测试使用
- /// </summary>
- private void OnDrawGizmos()
- {
- //Debug.DrawRay(launcherCamera.transform.position, transform.TransformDirection(Vector3.forward) * 200, Color.green);//绘制射线
- //Debug.DrawRay(gunPoint.transform.position, transform.TransformDirection(Vector3.forward) * 200, Color.green);//绘制射线
- }
- }
|