using System; using System.Collections.Generic; using DragonLi.Core; using DragonLi.Frame; using Mirror; using UnityEngine; using UnityEngine.XR; using Random = UnityEngine.Random; /// /// 手类型 /// public enum HandType { Left, Right } /// /// 手状态 /// public enum HandState { /// /// 空手 /// Empty, /// /// 有武器 /// HaveWeapon } public class Player : NetworkRoomPlayer,IDamagable { public Transform LeftHand; public Transform RightHand; public Transform Aim; public Collider Collider; public LayerMask AimLayer; [SyncVar] public bool isDie; private bool IsAlive = true; private float time = 0; [Header("玩家最大血量")] public float maxHp = 150; private float currentHp; public float Health { get => currentHp; set => currentHp = value; } #if UNITY_EDITOR [DisplayOnly] #endif public Vector3 AimVec = Vector3.zero; #region 枪械参数 /// /// 双手状态 /// public HandState NowHandState = HandState.Empty; /// /// 手上的枪械 /// private List Guns = new List(); /// /// 现在手上的武器 /// public int NowGunIndex = -1; /// /// 左手上的枪 /// private Transform leftGun; /// /// 右手上的枪 /// private Transform rightGun; public GameObject[] guns; #endregion public new void Start() { base.Start(); if (isLocalPlayer) { GameLocal.Ins.self = this; Collider.enabled = false; } if (isServer) { GameManager.Ins.AddData(index); Collider.enabled = true; } currentHp = maxHp; GameManager.Ins.players.Add(index,this); IsAlive = true; } public void SetBlood(float num) { currentHp += num; EventDispatcher.TriggerEvent("HpChange", currentHp, maxHp); } public float OnReceiveDamage(float damage, object info, Transform _sender) { float curDamage = damage; SetBlood(-curDamage); #if !UNITY_EDITOR && UNITY_ANDROID && PICO if (_sender != null) { Vector3 forward = transform.forward; Vector3 directionFormHit=(transform.position-_sender.position).normalized; float angle = Vector3.Angle(forward, directionFormHit); int index = Random.Range(0, 4); if(TrueGearEffectManager.Ins!=null) TrueGearEffectManager.Ins.OnHit(angle > 90,index,false); } #endif if (currentHp <= 0 && IsAlive) { IsAlive = false; isDie = true; GameLocal.Ins.DieUI.SetActive(true); GameManager.Ins.PlaySound2DRPC("1.35"); MonoSingleton.Instance.WaitSecondTodo(() => { IsAlive = true; isDie = false; GameLocal.Ins.DieUI.SetActive(false); currentHp = maxHp; EventDispatcher.TriggerEvent("HpChange", currentHp, maxHp); time = 0f; }, 5f, this); } else if (currentHp > 0) { GameLocal.Ins.HitUI.SetActive(true); MonoSingleton.Instance.WaitSecondTodo(() => GameLocal.Ins.HitUI.SetActive(false), 1f, this); } return currentHp; } public void Update() { Transform mainCamera = GameLocal.Ins.MRCamera.transform; Transform aim = GameLocal.Ins.Aim.transform; Transform leftControl = GameLocal.Ins.MRLeftControl; Transform rightControl = GameLocal.Ins.MRRightControl; if (isLocalPlayer) { // 同步相机 transform.position = mainCamera.position; transform.rotation = mainCamera.rotation; // 同步注视点 Aim.position = aim.position; Aim.rotation = aim.rotation; #if !UNITY_EDITOR && UNITY_ANDROID && PICO // 同步手柄 LeftHand.transform.position = leftControl.position; LeftHand.transform.rotation = leftControl.rotation; RightHand.transform.position = rightControl.position; RightHand.transform.rotation = rightControl.rotation; #endif // 秒杀当前场上的怪 if (Input.GetKeyDown(KeyCode.Q)) { GameManager.Ins.DamageAllEnemy(); } } if (isServer) { // 发射射线 if (Physics.Raycast(transform.position, transform.forward, out RaycastHit handlingHit, 20f, AimLayer)) { AimVec = handlingHit.point; } else { AimVec = Aim.position; } } } [TargetRpc] public void ApplyDamage(float value, object info, Transform _sender) { OnReceiveDamage(value, info, _sender); } [ServerCallback] public void OnTriggerEnter(Collider other) { // 开始游戏 if (other.tag == "Door") { Debug.Log("触碰开始"); other.gameObject.GetComponent().enabled = false; other.gameObject.SetActive(false); NetworkServer.Destroy(other.gameObject); GameManager.Ins.StopGuide(); GameManager.Ins.GameStart(); } if (other.tag == "WeaponProp") { WeaponProp prop = other.transform.GetComponent(); if(NowGunIndex==(int)prop.weaponType) return; PickUpGun(prop.weaponType, prop.amount); } // if (other.tag == "ItemProp") // { // ItemProp prop = other.transform.GetComponent(); // if (prop.itemPropType == ItemPropType.Hp) // { // GetProp(prop); // GameManager.Ins.DesItemProp(prop.gameObject); // } // } } [TargetRpc] public void GetProp(ItemProp prop) { SetBlood(prop.addHpIndex); } #region 枪械系统 /// /// 创建武器 /// /// /// [Server] public GameObject CreateWeapon(GunType type) { GameObject gun = null; gun= Instantiate(guns[(int)type]); return gun; } /// /// 装载武器 /// /// /// /// [Server] public Launcher LoadGun(HandType handType, GunType gunType) { GameObject gun = CreateWeapon(gunType); Launcher launcher = gun.GetComponent(); launcher.hand = handType; launcher.type = gunType; return launcher; } [TargetRpc] public void TriggerEvent(GunType gunType) { EventDispatcher.TriggerEvent("ChangeWeaponIcon", gunType, 0); } [TargetRpc] public void TriggerEventAmount(float amount) { if (amount == -999) amount = 0; EventDispatcher.TriggerEvent("ShowBulletAmount", amount); } /// /// 拾取枪械 /// /// /// [Command(requiresAuthority = false)] public void PickUpGun(GunType gunType, int amount) { Debug.Log("创建武器"); Launcher find = null; for (int i = 0; i < Guns.Count; i++) { Launcher launcher = Guns[i]; if (launcher != null && launcher.type == gunType) { find = launcher; break; } } if (find != null) { Debug.Log("添加弹药 - " + gunType); find.StuffBullet(amount); Swith2TargetGun(gunType); } else { Debug.Log("增加新武器 - " + gunType); Launcher right = null; right = LoadGun(HandType.Right, gunType); Launcher launchers = right; launchers.SetBulletAmount(amount,index); Guns.Add(launchers); Swith2TargetGun(gunType); } } /// /// 切换到下个武器 /// [Command] public void Switch2NextWeaponCmd() { Switch2NextWeapon(); } [Command] public void Switch2BeforeWeaponCmd() { Switch2BeforeWeapon(); } /// /// 切换到下个武器 /// [Server] public void Switch2NextWeapon() { Debug.Log("切换到下个武器"); // 空手 if (NowHandState == HandState.Empty) { Debug.Log("空手->可有武器状态"); NowHandState = HandState.HaveWeapon; } // 无武器 if (Guns.Count <= 0) { Debug.Log("无武器"); PickUpGun(GunType.Pistol,-999); return; } NowGunIndex++; if (NowGunIndex > Guns.Count - 1) { NowGunIndex = 0; } UpdateWeapons(); } /// /// 切换到上个武器 /// [Server] public void Switch2BeforeWeapon() { Debug.Log("切换到下个武器"); // 空手 if (NowHandState == HandState.Empty) { Debug.Log("空手->可有武器状态"); NowHandState = HandState.HaveWeapon; } // 无武器 if (Guns.Count <= 0) { Debug.Log("无武器"); return; } NowGunIndex--; if (NowGunIndex < 0) { NowGunIndex = Guns.Count - 1; } UpdateWeapons(); } /// /// 切换到指定枪械 /// [Server] public void Swith2TargetGun(GunType type) { Debug.Log("切换到指定枪械"); int find = -1; for (int i = 0; i < Guns.Count; i++) { Launcher launcher = Guns[i]; if (launcher != null && launcher.type == type) { find = i; break; } } if (find != -1) { NowGunIndex = find; } UpdateWeapons(); } [Command] public void EntertTurret(NetworkIdentity turret) { turret.AssignClientAuthority(connectionToClient); } [Command] public void ExitTurret(NetworkIdentity turret) { turret.RemoveClientAuthority(); } /// /// 武器更新 /// [Server] public void UpdateWeapons() { if (rightGun != null) { rightGun.gameObject.SetActive(false); NetworkServer.Destroy(rightGun.gameObject); rightGun = null; } for (int i = 0; i < Guns.Count; i++) { if (i == NowGunIndex) { Launcher l1 = Guns[i]; if (l1 != null) { rightGun = l1.transform; rightGun.gameObject.SetActive(true); NetworkServer.Spawn(l1.gameObject, gameObject); } } } EventDispatcher.TriggerEvent("ChangeGunIcon", Guns[NowGunIndex].bullet_amount); } /// /// 删除武器 /// [Command(requiresAuthority = false)] public void DelWeapon() { bool change = false; for (int i = 0; i < Guns.Count; i++) { Launcher launcher = Guns[i]; if (launcher != null && launcher.bullet_amount <= 0 && launcher.bullet_amount != -999) { change = true; Guns[i] = null; break; } } if (change) { Guns.RemoveAll(x => x == null); Switch2NextWeapon(); } } [Command] public void Switch2EmptyCmd() { Switch2Empty(); } /// /// 切换为空手 /// [Server] public void Switch2Empty() { NowHandState = HandState.Empty; NowGunIndex = -1; UpdateWeapons(); } public Launcher CurUserGun() { return Guns[NowGunIndex]; } #endregion }