using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
///
/// 弓箭手状态枚举
///
public enum ArmBowState
{
none,
/// 待机
idel,
/// 准备
prepare,
/// 瞄准
ain,
/// 射击
launch,
}
///
/// 播放动画命常量
///
public class ArmBowAnimConstName
{
public const string IDEL = "dajian";
public const string PREPARE = "lagong";
public const string LAUNCH = "fire";
}
///
/// 弓箭手控制器类
///
public class LauncherCtrl : MonoBehaviour
{
public static LauncherCtrl instance;
[SerializeField] GameObject go_Bullet;
public Transform crossHair; //测试 瞄准得目标点
///
/// 枪的观察相机
///
public Camera launcherCamera;
///
/// 枪模型
///
public MeshRenderer go_GunModel;
///
/// 枪口位置
///
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;
/// 准心图片
public Image img_Anim;
private Vector2 vec_AinPos = new Vector2(10000, 10000);
/// 是否显示准心
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();
}
if (gunPoint == null || go_GunModel == null)
{
go_GunModel = transform.Find("qiang").GetComponent(); ;
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();
}
#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;
}
///
/// 准备射击
///
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);
}
///
/// 射击
///
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();
arw.transform.SetParent(null);
//箭矢发射的目标以及方向
arwCtrl.PlayShoot(gunPoint);
AudioMgr.instance.PlaySound();
//子弹数量
bulletCount--;
PinBallUIManager.instance.BroadcastUI(new object[] { "GameView", true, 2 , bulletCount });
}
///
/// 结束射击
///
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;
}
}
///
/// 瞄准状态,可以开启 允许移动摄像机,点击发射等功能
///
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;
}
}
///
/// 测试使用
///
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);//绘制射线
}
}