Files
Zombie/Assets/_Zombie/Scripts/Guns/NewGun/Gun3.cs
2025-08-30 18:18:34 +08:00

170 lines
4.3 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 Gun3 : Launcher
{
public GameObject shot;
[SyncVar]
private int goFrame = 0;
private int frame = 0;
public AudioSource audio;
public float damageInterval = 0.2f; // 每次伤害间隔
private float lastDamageTime = 0f;
public override void Start()
{
base.Start();
// 主机自身
if (isServer && isClient && isOwned)
{
type = GunType.LaserGun;
GunInfo gunInfo = GameManager.Ins.GunInfos[type];
shootRate = gunInfo.ShootRate;
recoil = gunInfo.Recoil;
}
// 主机其他
else if (isServer && isClient)
{
type = GunType.LaserGun;
GunInfo gunInfo = GameManager.Ins.GunInfos[type];
shootRate = gunInfo.ShootRate;
recoil = gunInfo.Recoil;
}
shot.SetActive(false);
bullet_attack = GameManager.Ins.BulletInfos[(BulletType)type].Damage;
audio.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;
}
else
{
isTrigger = false;
}
if(isTrigger||isGrip)
{
CmdShot();
}
else
{
CmdCloseShot();
}
}
[Command]
public void CmdShot()
{
frame++;
Debug.Log(frame);
if (frame >= 70)
{
if (GameManager.Ins.gameState == GameState.Playing)
{
if (bullet_amount>0)
{
bullet_amount-=Time.deltaTime;
EventDispatcher.TriggerEvent("UserGun", bullet_amount);
}
}
RaycastHit raycast;
Ray ray = new Ray();
ray.origin = bulletPoint.position;
ray.direction = bulletPoint.forward;
if (Physics.Raycast(ray, out raycast))
{
float len = Vector3.Distance(bulletPoint.position, raycast.point);
float time = len / 20f;
if (raycast.transform.tag == "Enemy")
{
// 控制伤害间隔
if (Time.time - lastDamageTime >= damageInterval)
{
ApplyDamage(raycast.transform); // 敌人受伤
}
}
}
SetShot(true);
if (Time.time - lastDamageTime >= damageInterval)
{
lastDamageTime = Time.time;
}
}
Debug.LogError("剩余子弹数量:"+bullet_amount);
// 武器寿命逻辑
if (bullet_amount<=0)
{
bullet_amount = 0;
GameLocal.Ins.self.DelWeapon();
}
}
[Command]
public void CmdCloseShot()
{
frame = 0;
shot.SetActive(false);
SetShot(false);
//StopAudio();
}
[ClientRpc]
public void StopAudio()
{
audio.enabled = false;
}
[ClientRpc]
public void SetShot(bool active)
{
shot.SetActive(active);
audio.enabled = active;
}
[Server]
private void ApplyDamage(Transform target)
{
IDamagable component = target.GetComponent<IDamagable>();
if (component != null)
{
component.ApplyDamage(bullet_attack, curOwnerIndex, transform);
}
}
}