Files
DefendNJ/Assets/_DefendNJ/Scripts/Guns/NewGun/Gun2.cs

187 lines
5.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using DarkTonic.MasterAudio;
using DragonLi.Core;
using DragonLi.Frame;
using Mirror;
using UnityEngine;
using UnityEngine.XR;
//快速枪(喷火枪)
public class Gun2 : Launcher
{
public GameObject shot;
public float flameRange = 5f; // 火焰最大距离
public float flameAngle = 45f; // 火焰锥形夹角
private float damageInterval = 0.5f; // 每次伤害间隔
private float lastDamageTime = 0f; // 玩家所在的层
public AudioSource shotSource;
public override void Start()
{
base.Start();
// 主机自身
if (isServer && isClient && isOwned)
{
type = GunType.FireGun;
AiInfo gunInfo = GameManager.Ins.AiInfos[(int)type];
shootRate = gunInfo.FiringRate;
recoil = 100;
// if (hand == HandType.Left)
// {
// MRInput.Ins.RegisterHoldPressLeftTrigger(ClickLeftTrigger);
// }
// else if (hand == HandType.Right)
// {
// MRInput.Ins.RegisterHoldPressRightTrigger(ClickLeftTrigger);
// }
}
// 主机其他
else if (isServer && isClient)
{
type = GunType.FireGun;
AiInfo gunInfo = GameManager.Ins.AiInfos[(int)type];
shootRate = gunInfo.FiringRate;
recoil = 100;
}
// 从机自身
else if (isClient && isOwned)
{
// if (hand == HandType.Left)
// {
// MRInput.Ins.RegisterHoldPressLeftTrigger(ClickLeftTrigger);
// }
// else if (hand == HandType.Right)
// {
// MRInput.Ins.RegisterHoldPressRightTrigger(ClickLeftTrigger);
// }
}
shot.SetActive(false);
bullet_attack = GameManager.Ins.BulletInfos[(BulletType)type].Damage;
shotSource.enabled = false;
}
public void Update()
{
if (!isOwned)
{
return;
}
// 左手
if (hand == HandType.Left)
{
transform.position = GameLocal.Ins.self.LeftHand.position;
transform.rotation = GameLocal.Ins.self.LeftHand.rotation;
}
// 右手
else if (hand == HandType.Right)
{
transform.position = GameLocal.Ins.self.RightHand.position;
transform.rotation = GameLocal.Ins.self.RightHand.rotation;
}
bool isTrigger = false;
bool isGrip = false;
rightHandDevice.TryGetFeatureValue(CommonUsages.triggerButton, out isTrigger);
rightHandDevice.TryGetFeatureValue(CommonUsages.gripButton, out isGrip);
#if UNITY_EDITOR
if (Input.GetMouseButton(0))
{
isTrigger = true;
}
#endif
if ((isTrigger||isGrip)&&!GameLocal.Ins.self.isDie&& GameManager.Ins.isStart)
{
CmdShot();
}
else
{
CmdCloseShot();
}
}
[Command]
public void CmdShot()
{
MRInput.Ins.VibrateRightController(0.4F, 50, 300);
if (GameManager.Ins.gameState == GameState.Playing)
{
if (bullet_amount>0)
{
bullet_amount-=Time.deltaTime;
EventDispatcher.TriggerEvent("UserGun", bullet_amount);
}
}
// 获取范围内所有碰撞体
Collider[] hits = Physics.OverlapSphere(bulletPoint.position, flameRange);
foreach (var hit in hits)
{
if (hit.CompareTag("Enemy"))
{
Vector3 dirToTarget = (hit.transform.position - bulletPoint.position).normalized;
float angle = Vector3.Angle(bulletPoint.forward, dirToTarget);
// 判断是否在喷火器的锥形范围内
if (angle < flameAngle * 0.5f)
{
// 控制伤害间隔
if (Time.time - lastDamageTime >= damageInterval)
{
ApplyDamage(hit.transform); // 敌人受伤
}
}
}
}
if (Time.time - lastDamageTime >= damageInterval)
{
lastDamageTime = Time.time;
}
Debug.LogError("剩余子弹数量:"+bullet_amount);
SetShot(true);
// 武器寿命逻辑
if (bullet_amount<=0)
{
bullet_amount = 0;
GameLocal.Ins.self.DelWeapon();
}
}
[Command]
public void CmdCloseShot()
{
shot.SetActive(false);
SetShot(false);
StopAudio();
}
[ClientRpc]
public void StopAudio()
{
MasterAudio.StopAllSoundsOfTransform(transform);
}
[ClientRpc]
public void SetShot(bool active)
{
shot.SetActive(active);
shotSource.enabled = active;
}
[Server]
private void ApplyDamage(Transform target)
{
IDamagable component = target.GetComponent<IDamagable>();
if (component != null)
{
component.ApplyDamage(bullet_attack, curOwnerIndex, transform);
}
}
}