153 lines
3.9 KiB
C#
153 lines
3.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using DragonLi.Frame;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
|
|
public class StunGun : Launcher
|
|
{
|
|
public float maxDistance = 50f;
|
|
public LayerMask hitMask;
|
|
|
|
[SyncVar] private float _damage;
|
|
[SyncVar] private float _curAtkTime;
|
|
|
|
public int score;
|
|
|
|
private RaycastHit _hit;
|
|
|
|
private void Start()
|
|
{
|
|
type = GunType.DianJiGun;
|
|
GunInfo gunInfo = GameManager.Ins.GunInfos[type][1];
|
|
|
|
shootRate = gunInfo.ShootRate;
|
|
recoil = gunInfo.Recoil;
|
|
userTime = gunInfo.Time;
|
|
curUserTime = gunInfo.Time;
|
|
waitBulletTime = gunInfo.CoolDown;
|
|
_damage = gunInfo.Damage;
|
|
score = gunInfo.Score;
|
|
bullet_prefab.SetActive(false);
|
|
|
|
if (isOwned)
|
|
{
|
|
if (hand == HandType.Left)
|
|
MRInput.Ins.RegisterHoldPressLeftTrigger(ClickLeftTrigger);
|
|
else
|
|
MRInput.Ins.RegisterHoldPressRightTrigger(ClickRightTrigger);
|
|
|
|
MRInput.Ins.RegisterUpPressRightTrigger(UpRightTrigger);
|
|
}
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
MRInput.Ins.UnregisterHoldPressRightTrigger(ClickRightTrigger);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!isOwned) return;
|
|
|
|
// 手部跟随
|
|
if (hand == HandType.Left)
|
|
{
|
|
transform.position = GameLocal.Ins.self.LeftHand.position;
|
|
transform.rotation = GameLocal.Ins.self.LeftHand.rotation;
|
|
}
|
|
else
|
|
{
|
|
transform.position = GameLocal.Ins.self.RightHand.position;
|
|
transform.rotation = GameLocal.Ins.self.RightHand.rotation;
|
|
}
|
|
}
|
|
|
|
// ================= 按键事件(客户端) ================
|
|
[Client] private void ClickLeftTrigger() => CmdShoot();
|
|
[Client] private void ClickRightTrigger() => CmdShoot();
|
|
[Client] private void UpRightTrigger() => CmdUpShoot();
|
|
|
|
// ====================== 开始射击 ======================
|
|
|
|
[Command]
|
|
private void CmdShoot()
|
|
{
|
|
// 让所有客户端显示激光
|
|
RpcShowLaser();
|
|
|
|
// 服务器射线检测
|
|
Ray ray = new Ray(bulletPoint.position, bulletPoint.forward);
|
|
|
|
if (Physics.Raycast(ray, out _hit, maxDistance, hitMask))
|
|
{
|
|
_curAtkTime -= Time.deltaTime;
|
|
if (_curAtkTime <= 0)
|
|
{
|
|
ServerDealDamage();
|
|
_curAtkTime = 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_curAtkTime = 0;
|
|
}
|
|
}
|
|
|
|
// ====================== 停止射击 ======================
|
|
|
|
[Command]
|
|
private void CmdUpShoot()
|
|
{
|
|
_curAtkTime = 0;
|
|
RpcHideLaser();
|
|
}
|
|
|
|
// ====================== 客户端渲染 ======================
|
|
|
|
[ClientRpc]
|
|
private void RpcShowLaser()
|
|
{
|
|
bullet_prefab.SetActive(true);
|
|
}
|
|
|
|
[ClientRpc]
|
|
private void RpcHideLaser()
|
|
{
|
|
bullet_prefab.SetActive(false);
|
|
}
|
|
|
|
// ====================== 服务器伤害计算 ======================
|
|
|
|
[Server]
|
|
private void ServerDealDamage()
|
|
{
|
|
if(_hit.collider==null)
|
|
return;
|
|
IDamagable damagable = _hit.collider.GetComponent<IDamagable>();
|
|
|
|
if (damagable == null) return;
|
|
|
|
Player targetPlayer = _hit.collider.GetComponent<Player>();
|
|
|
|
if (targetPlayer != null && targetPlayer.teamType == team)
|
|
return;
|
|
|
|
damagable.ApplyDamage(_damage, id, _hit.transform,score);
|
|
|
|
// 伤害飘字(客户端执行)
|
|
if (targetPlayer != null)
|
|
{
|
|
bool crit = Vector3.Distance(transform.position, targetPlayer.helmet.position) < 1.5f;
|
|
RpcShowDamageNumber(targetPlayer.helmet.position + new Vector3(0, 1, 0), _damage, crit);
|
|
}
|
|
}
|
|
|
|
// 飘字由客户端显示
|
|
private void RpcShowDamageNumber(Vector3 pos, float dmg, bool crit)
|
|
{
|
|
GameManager.Ins.RpcShowDamageNumber(dmg, pos, crit);
|
|
}
|
|
}
|