using System.Net.Http.Headers; using DragonLi.Core; using DragonLi.Frame; using MechanicalAge; using Mirror; using System; using System.Collections; using System.Collections.Generic; using System.Linq; using Unity.VisualScripting; using UnityEngine; using UnityEngine.UI; using UnityEngine.XR; using XPlugin.Data.JsonLiteDB; using static XPlugin.Data.JsonLiteDB.JsonLiteDB; public class Player : NetworkRoomPlayer, IDamagable { [Header("身体")] public Transform LeftHand; public Transform RightHand; public Collider colliderSelf; public Transform ownerTempHand; /// /// 最大血量 /// public float maxHp = 10000; private float currentHp; public float Health { get { return currentHp; } set { currentHp = value; } } public InputDevice leftHandDevice; private InputDevice rightHandDevice; [Header("武器预制体")] public GameObject DoubleGunPre; public GameObject LargeGunPre; public GameObject SuperGunPre; public GameObject RocketGunPre; public GameObject ShotGunPre; public GameObject GrenadeGunPre; public GameObject TempHandPre; /// /// 手上的武器 /// private List Weapons = new List(); /// /// 双手状态 /// public HandState NowHandState = HandState.Empty; /// /// 现在手上的武器 /// public int NowWeaponIndex = -1; /// /// 切换之前的武器 /// public int lastNoWeaponIndex = -1; public WeaponType lastNoWeaponType; private Transform left; private Transform right; [SyncVar] public int playerId; public StatisticManager statisticManager; public MRNetworkManager mRNetworkManager; [SyncVar] public float atkBuff = 0.5f; [Header("角色BUFF状态")] public bool isAtkBuff = false; public bool isDeeBuff = false; public GameObject owner; public bool isClickSwith = false; private bool debugSwitch = false; public override void OnStartServer() { base.OnStartServer(); } [ClientRpc] public void SetIsAtkBuff(bool value) { GameManager.Ins.buffAtk = value ? 1 : 0; isAtkBuff = value; } [ClientRpc] public void SetIsDeeBuff(bool value) { GameManager.Ins.buffDef = value ? 1 : 0; isDeeBuff = value; } public new void Start() { base.Start(); #if UNITY_ANDROID && PICO leftHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand); rightHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand); #endif Health = maxHp; if (isLocalPlayer) { GameInit.Ins.self = this; StatisticManager.Ins.playerIndex = index; CreateHand(); PickUpWeapon(WeaponType.DoubleGun, -999); } if (isServer) { colliderSelf.enabled = true; } EventDispatcher.TriggerEvent("GetHand", LeftHand, RightHand); } public void SetBlood(float num) { Health += num; EventDispatcher.TriggerEvent("HpChange", currentHp, maxHp); } //修改处:在 Player 类中添加这个字段来存储 AI 控制器的引用 private AIController aiController; //只再服务器执行 [ServerCallback] public void OnTriggerEnter(Collider other) { if (isServer) { CustomEvent.Trigger(GameInit.Ins.Event.gameObject, "PlayerTrigger", other); // 血包 if (other.tag == "BloodBag") { AudioManager.Ins?.SoundPlayOneShot("shiqu", false); StatisticManager.Ins?.AddGetPropAmount(index); BloodBag bloodBagScript = other.transform.GetComponent(); bloodBagScript.ColliderBloodBag(); float hpAdd = bloodBagScript.hpAdd; if (currentHp + hpAdd <= maxHp) { SetBlood(hpAdd); } else { SetBlood(maxHp - currentHp); } } if (other.tag == "AtkBuff") { AudioManager.Ins?.SoundPlayOneShot("shiqu", false); StatisticManager.Ins?.AddGetPropAmount(index); AtkBuff atkBuffScript = other.transform.GetComponent(); atkBuffScript.ColliderAtkBuff(); SetIsAtkBuff(true); CoroutineTaskManager.Instance.WaitSecondTodo(() => { SetIsAtkBuff(false); }, 10.0f); } if (other.tag == "DeeBuff") { AudioManager.Ins?.SoundPlayOneShot("shiqu", false); StatisticManager.Ins?.AddGetPropAmount(index); AtkBuff atkBuffScript = other.transform.GetComponent(); atkBuffScript.ColliderAtkBuff(); SetIsDeeBuff(true); CoroutineTaskManager.Instance.WaitSecondTodo(() => { SetIsDeeBuff(false); }, 10.0f); } // 武器道具 if (other.tag == "WeaponProp") { Debug.Log("触碰武器道具"); WeaponProp prop = other.transform.GetComponent(); prop.Collider(); PickUpWeapon(prop.weaponType, prop.amount); StatisticManager.Ins?.AddGetPropAmount(index); } if (other.tag == "GameStartPoint") { GameManager.Ins?.ShowLandmark(0, 1); NetworkServer.Destroy(other.gameObject); StoryManager.Ins.CreateStoryItem(); //修改处:注释下面一行 //TempInfo tempInfo = GameManager.Ins.tempDescInfos[2]; // for (int i = 0; i < tempInfo.Positions.Length; i++) // { // GameManager.Ins.GenerateTurret(tempInfo.Positions[i], tempInfo.Scales[i], tempInfo.Angles[i]); // } Debug.Log("触碰光柱"); //GameManager.Ins.GenerateDoor(); GameManager.Ins.GameOK(); //修改处:移除直接开始的协程调用 //StartCoroutine(StoryManager.Ins?.MyCoroutine()); //修改处:获取 AI 控制器并注册事件 if (GameManager.Ins.aiCharacter != null) { aiController = GameManager.Ins.aiCharacter.GetComponent(); if (aiController != null) { aiController.OnIntroductionComplete += StartGameAfterAIIntroduction; } } //修改处:注释进入游戏 //GameManager.Ins.HudMessage(0); //修改处:}前注释 //CoroutineTaskManager.Instance.WaitSecondTodo(() => //{ // AudioManager.Ins?.SoundPlay("bgm2", true); //}, 10.0f); //CoroutineTaskManager.Instance.WaitSecondTodo(() => //{ // AudioManager.Ins?.SoundPlay("bgm3", true); //}, 30.0f); } // if (other.tag == "Story1") // { // //进入门 关闭大门 // GameManager.Ins.HudMessage(0); // other.gameObject.GetComponent().enabled = false; // StartCoroutine(StoryManager.Ins?.MyCoroutine()); // CoroutineTaskManager.Instance.WaitSecondTodo(() => // { // NetworkServer.Destroy(other.gameObject); // }, 4.0f); // CoroutineTaskManager.Instance.WaitSecondTodo(() => // { // AudioManager.Ins?.SoundPlay("bgm2", true); // }, 10.0f); // // CoroutineTaskManager.Instance.WaitSecondTodo(() => // { // AudioManager.Ins?.SoundPlay("bgm3", true); // }, 30.0f); // } if (other.tag == "EnergyPump") { AudioManager.Ins?.SoundPlayOneShot("TouchEnergyPump", false); EnergyPump energyPumpScript = other.transform.GetComponent(); energyPumpScript.ShowLandMask(); EventDispatcher.TriggerEvent("NextTrriger"); energyPumpScript.ColliderEnergyPump(); } } } //修改处:添加新的方法处理AI介绍完成后的游戏开始 [Server] private void StartGameAfterAIIntroduction() { Debug.Log("AI介绍完成,开始游戏讲解"); //修改:添加设置AI介绍完成标志 GameManager.Ins.RpcSetAIIntroductionComplete(true); //修改:触发教学音频和UI GameManager.Ins.HudMessage(0); //启动游戏讲解协程 StartCoroutine(StoryManager.Ins.MyCoroutine()); // 延迟播放背景音乐 CoroutineTaskManager.Instance.WaitSecondTodo(() => { AudioManager.Ins?.SoundPlay("bgm2", true); }, 10f); CoroutineTaskManager.Instance.WaitSecondTodo(() => { AudioManager.Ins?.SoundPlay("bgm3", true); }, 30f); // 取消事件注册,避免重复调用 if (aiController != null) { aiController.OnIntroductionComplete -= StartGameAfterAIIntroduction; } } private bool IsAvlie = true; public float time = 0; public float OnReceiveDamage(float damage, object info, Transform _sender) { var curDamage = GameManager.Ins.buffDef > 0 ? damage / GameManager.Ins.buffDef : damage; SetBlood(-curDamage); if (currentHp <= 0) { GameManager.Ins.GameStart = false; GameInit.Ins.UIHudDie.SetActive(true); Switch2Empty(); MonoSingleton.Instance.WaitSecondTodo(() => { IsAvlie = false; }, 2f, this); MonoSingleton.Instance.WaitSecondTodo(() => { GameInit.Ins.UIHudDie.SetActive(false); GameInit.Ins.AliveUI.SetActive(true); Swith2TargetWeapon(lastNoWeaponType); GameManager.Ins.GameStart = true; currentHp = maxHp; IsAvlie = true; GameInit.Ins.restartCountDownText.text = ""; time = 0; }, 5.5f, this); } if (currentHp > 0) { GameInit.Ins.HitUI.SetActive(true); MonoSingleton.Instance.WaitSecondTodo(() => { GameInit.Ins.HitUI.SetActive(false); }, 1f, this); } return currentHp; } [Command] public void CmdPlaySwitchSound() { GameManager.Ins.PlaySwitchGunSound(); } public void Update() { if (!isLocalPlayer) { return; } for (int i = 0; i < Weapons.Count; i++) { if (i == NowWeaponIndex) { Launcher l1 = Weapons[i][0]; Launcher l2 = Weapons[i][1]; if (l1 != null) { TriggerEventAmount(Weapons[i][0].bullet_amount); } if (l2 != null) { TriggerEventAmount(Weapons[i][1].bullet_amount); } } } if (IsAvlie == false) { time += Time.deltaTime; if (time >= 0.8f) { GameInit.Ins.restartCountDownText.text = "3"; } if (time >= 2.0f) { GameInit.Ins.restartCountDownText.text = "2"; } if (time >= 3.0f) { GameInit.Ins.restartCountDownText.text = "1"; } } // 同步相机-身体 if (GameInit.Ins.MRCamera != null) { transform.position = GameInit.Ins.MRCamera.transform.position; transform.rotation = GameInit.Ins.MRCamera.transform.rotation; } #if UNITY_EDITOR // 切换武器 if (isLocalPlayer && Input.GetKeyDown(KeyCode.Tab) && GameManager.Ins.GameStart) { Switch2NextWeaponCmd(); } if (isLocalPlayer && Input.GetKeyDown(KeyCode.LeftAlt) && GameManager.Ins.GameStart) { Switch2BeforeWeaponCmd(); } // 创建能量罩 if (isLocalPlayer && Input.GetKeyDown(KeyCode.F)) { GameManager.Ins.CreateShield(transform.position); } // 创建11 if (isLocalPlayer && Input.GetKeyDown(KeyCode.V)) { GameManager.Ins.CreateAtkBuff(transform.position + new Vector3(1, 0, 0)); } // 秒杀 if (isLocalPlayer && Input.GetKeyDown(KeyCode.K)) { for (int i = 0; i < GameManager.Ins.enemyList.Count; i++) { if (GameManager.Ins.enemyList[i] != null) { GameManager.Ins.enemyList[i].GetComponent().ApplyDamage(100000000000, true, null); } } } // M打开debug if (isLocalPlayer && Input.GetKeyDown(KeyCode.M)) { debugSwitch = !debugSwitch; if (debugSwitch) { DebugPanel.Ins.Show(); } else { DebugPanel.Ins.Hide(); } } // z切换空手 if (isLocalPlayer && Input.GetKeyDown(KeyCode.Z)) { Switch2EmptyCmd(); } #endif #if !UNITY_EDITOR && UNITY_ANDROID && PICO if (rightHandDevice != null) { Vector2 value; rightHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out value); if (value.magnitude > 0) { // 根据轴的值判断摇杆朝向 if (!isClickSwith && value.x < 0 && GameManager.Ins.GameStart) { // 摇杆朝左 isClickSwith = true; GameManager.Ins.PlaySwitchGunSound(); Switch2BeforeWeaponCmd(); } else if (!isClickSwith && value.x > 0 && GameManager.Ins.GameStart) { // 摇杆朝右 isClickSwith = true; GameManager.Ins.PlaySwitchGunSound(); Switch2NextWeaponCmd(); } } else if (value.magnitude == 0) { isClickSwith = false; } if (value.Equals(Vector2.zero)) { isClickSwith = false; } } // 呼出debug if (leftHandDevice != null && rightHandDevice != null) { bool leftValue; leftHandDevice.TryGetFeatureValue(CommonUsages.secondaryButton, out leftValue); bool rightValue; rightHandDevice.TryGetFeatureValue(CommonUsages.secondaryButton, out rightValue); if (leftValue && rightValue) { debugSwitch = !debugSwitch; if (debugSwitch) { DebugPanel.Ins.Show(); } else { DebugPanel.Ins.Hide(); } } } // 同步手柄 if (GameInit.Ins.MRLeftControl != null) { LeftHand.transform.position = GameInit.Ins.MRLeftControl.position; LeftHand.transform.rotation = GameInit.Ins.MRLeftControl.rotation; } if (GameInit.Ins.MRRightControl != null) { RightHand.transform.position = GameInit.Ins.MRRightControl.position; RightHand.transform.rotation = GameInit.Ins.MRRightControl.rotation; } #endif #if !UNITY_EDITOR && UNITY_ANDROID && VIVE // 同步手柄 if (GameInit.Ins.MRLeftControl != null) { LeftHand.transform.position = GameInit.Ins.MRLeftControl.position; LeftHand.transform.rotation = GameInit.Ins.MRLeftControl.rotation; } if (GameInit.Ins.MRRightControl != null) { RightHand.transform.position = GameInit.Ins.MRRightControl.position; RightHand.transform.rotation = GameInit.Ins.MRRightControl.rotation; } #endif } public override void OnGUI() { if (!showRoomGUI) return; NetworkRoomManager room = NetworkManager.singleton as NetworkRoomManager; if (room) { if (!room.showRoomGUI) return; // DrawPlayerReadyState(); // DrawPlayerReadyButton(); } } void DrawPlayerReadyState() { GUILayout.BeginArea(new Rect(20f + (index * 100), 200f, 90f, 130f)); GUILayout.Label($"Player [{index + 1}]"); if (readyToBegin) GUILayout.Label("Ready"); else GUILayout.Label("Not Ready"); // if (((isServer && index > 0) || isServerOnly) && GUILayout.Button("REMOVE")) // { // // This button only shows on the Host for all players other than the Host // // Host and Players can't remove themselves (stop the client instead) // // Host can kick a Player this way. // GetComponent().connectionToClient.Disconnect(); // } GUILayout.EndArea(); } void DrawPlayerReadyButton() { if (NetworkClient.active && isLocalPlayer) { GUILayout.BeginArea(new Rect(20f, 300f, 120f, 20f)); if (readyToBegin) { if (GUILayout.Button("Cancel")) CmdChangeReadyState(false); } else { if (GUILayout.Button("Ready")) CmdChangeReadyState(true); } GUILayout.EndArea(); } } #region 武器系统 /// /// 创建武器 /// /// /// [Server] public GameObject CreateWeapon(WeaponType type) { GameObject gun = null; switch (type) { case WeaponType.DoubleGun: gun = Instantiate(DoubleGunPre); break; case WeaponType.LargeGun: gun = Instantiate(LargeGunPre); break; case WeaponType.SuperGun: gun = Instantiate(SuperGunPre); break; case WeaponType.RocketGun: gun = Instantiate(RocketGunPre); break; case WeaponType.ShotGun: gun = Instantiate(ShotGunPre); break; case WeaponType.GrenadeGun: gun = Instantiate(GrenadeGunPre); break; } return gun; } /// /// 装载武器 /// /// /// /// public Launcher LoadWeapon(HandType handType, WeaponType weaponType) { GameObject gun = CreateWeapon(weaponType); Launcher launcher = gun.GetComponent(); launcher.hand = handType; launcher.type = weaponType; return launcher; } [TargetRpc] public void TriggerEvent(WeaponType weaponType) { EventDispatcher.TriggerEvent("ChangeWeaponIcon", weaponType, 0); } [TargetRpc] public void TriggerEventAmount(float amount) { if (amount == -999) amount = 0; EventDispatcher.TriggerEvent("ShowBulletAmount", amount); } /// /// 拾取武器 /// /// /// [Command(requiresAuthority = false)] public void PickUpWeapon(WeaponType weaponType, int amount) { TriggerEvent(weaponType); Launcher find = null; for (int i = 0; i < Weapons.Count; i++) { for (int j = 0; j < Weapons[i].Length; j++) { Launcher launcher = Weapons[i][j]; if (launcher != null && launcher.type == weaponType) { find = launcher; break; } } if (find != null) { break; } } if (find != null) { Debug.Log("添加弹药 - " + weaponType); find.StuffBullet(amount); Swith2TargetWeapon(weaponType); } else { Debug.Log("增加新武器 - " + weaponType); Launcher left = null; Launcher right = null; switch (weaponType) { case WeaponType.DoubleGun: left = LoadWeapon(HandType.Left, weaponType); left.StuffBullet(-999); right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(-999); break; case WeaponType.LargeGun: right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(amount); break; case WeaponType.RocketGun: right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(amount); break; case WeaponType.ShotGun: right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(amount); break; case WeaponType.GrenadeGun: right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(amount); break; case WeaponType.SuperGun: right = LoadWeapon(HandType.Right, weaponType); right.StuffBullet(amount); break; } Launcher[] launchers = { left, right }; Weapons.Add(launchers); Swith2TargetWeapon(weaponType); } } /// /// 切换到下个武器 /// [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 (Weapons.Count <= 0) { Debug.Log("无武器"); return; } NowWeaponIndex++; if (NowWeaponIndex > Weapons.Count - 1) { NowWeaponIndex = 0; } UpdateWeapons(); } /// /// 切换到上个武器 /// [Server] public void Switch2BeforeWeapon() { Debug.Log("切换到下个武器"); // 空手 if (NowHandState == HandState.Empty) { Debug.Log("空手->可有武器状态"); NowHandState = HandState.HaveWeapon; } // 无武器 if (Weapons.Count <= 0) { Debug.Log("无武器"); return; } NowWeaponIndex--; if (NowWeaponIndex < 0) { NowWeaponIndex = Weapons.Count - 1; } UpdateWeapons(); } /// /// 切换到指定武器 /// [Server] public void Swith2TargetWeapon(WeaponType type) { Debug.Log("切换到指定武器"); int find = -1; for (int i = 0; i < Weapons.Count; i++) { for (int j = 0; j < Weapons[i].Length; j++) { Launcher launcher = Weapons[i][j]; if (launcher != null && launcher.type == type) { find = i; break; } } if (find != -1) { break; } } if (find != -1) { NowWeaponIndex = find; } UpdateWeapons(); } [Command] public void EntertTurret(NetworkIdentity turret) { turret.AssignClientAuthority(connectionToClient); } [Command] public void ExitTurret(NetworkIdentity turret) { turret.RemoveClientAuthority(); } /// /// 武器更新 /// [Server] public void UpdateWeapons() { CmdPlaySwitchSound(); if (left != null) { left.gameObject.SetActive(false); NetworkServer.UnSpawn(left.gameObject); left = null; } if (right != null) { right.gameObject.SetActive(false); NetworkServer.UnSpawn(right.gameObject); right = null; } for (int i = 0; i < Weapons.Count; i++) { if (i == NowWeaponIndex) { Launcher l1 = Weapons[i][0]; Launcher l2 = Weapons[i][1]; if (l1 != null) { left = l1.transform; left.gameObject.SetActive(true); NetworkServer.Spawn(l1.gameObject, gameObject); //EventDispatcher.TriggerEvent("ChangeWeaponIcon", Weapons[i][0].type, 0); TriggerEvent(Weapons[i][0].type); } if (l2 != null) { right = l2.transform; right.gameObject.SetActive(true); NetworkServer.Spawn(l2.gameObject, gameObject); TriggerEvent(Weapons[i][1].type); //EventDispatcher.TriggerEvent("ChangeWeaponIcon", Weapons[i][1].type, 0); } } } } [Command(requiresAuthority = false)] public void CreateHand() { Debug.Log("创建手"); GameObject tempHandLeft = Instantiate(TempHandPre); NetworkServer.Spawn(tempHandLeft, gameObject); tempHandLeft.GetComponent().hand = HandType.Left; GameObject tempHandRight = Instantiate(TempHandPre); NetworkServer.Spawn(tempHandRight, gameObject); tempHandRight.GetComponent().hand = HandType.Right; ownerTempHand = tempHandRight.transform; } /// /// 删除武器 /// [Command(requiresAuthority = false)] public void DelWeapon() { bool change = false; for (int i = 0; i < Weapons.Count; i++) { for (int j = 0; j < Weapons[i].Length; j++) { Launcher launcher = Weapons[i][j]; if (launcher != null && launcher.bullet_amount <= 0 && launcher.bullet_amount != -999) { change = true; Weapons[i] = null; break; } } } if (change) { Weapons.RemoveAll(x => x == null); Switch2NextWeapon(); } } [Command] public void Switch2EmptyCmd() { Switch2Empty(); } /// /// 切换为空手 /// [Server] public void Switch2Empty() { if (NowWeaponIndex >= 0) { for (int i = 0; i < Weapons[NowWeaponIndex].Length; i++) { if (Weapons[NowWeaponIndex][i] != null) { lastNoWeaponType = Weapons[NowWeaponIndex][i].GetComponent().type; } } } NowHandState = HandState.Empty; NowWeaponIndex = -1; UpdateWeapons(); } [TargetRpc] public void ApplyDamage(float value, object info, Transform _sender) { OnReceiveDamage(value, info, _sender); } #endregion // 修改处:在 OnDestroy 方法中添加清理代码 private void OnDestroy() { // 取消事件注册 if (aiController != null) { aiController.OnIntroductionComplete -= StartGameAfterAIIntroduction; } } }