184 lines
5.1 KiB
C#
184 lines
5.1 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;
|
|
GunInfo gunInfo = GameManager.Ins.GunInfos[type];
|
|
shootRate = gunInfo.ShootRate;
|
|
recoil = gunInfo.Recoil;
|
|
// if (hand == HandType.Left)
|
|
// {
|
|
// MRInput.Ins.RegisterHoldPressLeftTrigger(ClickLeftTrigger);
|
|
// }
|
|
// else if (hand == HandType.Right)
|
|
// {
|
|
// MRInput.Ins.RegisterHoldPressRightTrigger(ClickLeftTrigger);
|
|
// }
|
|
}
|
|
// 主机其他
|
|
else if (isServer && isClient)
|
|
{
|
|
type = GunType.FireGun;
|
|
GunInfo gunInfo = GameManager.Ins.GunInfos[type];
|
|
shootRate = gunInfo.ShootRate;
|
|
recoil = gunInfo.Recoil;
|
|
}
|
|
// 从机自身
|
|
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 (Input.GetMouseButton(0))
|
|
{
|
|
isTrigger = true;
|
|
}
|
|
if(isTrigger||isGrip)
|
|
{
|
|
CmdShot();
|
|
}
|
|
else
|
|
{
|
|
CmdCloseShot();
|
|
}
|
|
}
|
|
|
|
[Command]
|
|
public void CmdShot()
|
|
{
|
|
if(!GameLocal.Ins.self.IsAlive)
|
|
return;
|
|
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);
|
|
}
|
|
}
|
|
}
|