using System;
using System.Collections.Generic;
using DragonLi.Core;
using Mirror;
using UnityEngine;
using UnityEngine.XR;
///
/// 手类型
///
public enum HandType
{
Left,
Right
}
///
/// 手状态
///
public enum HandState
{
///
/// 空手
///
Empty,
///
/// 有武器
///
HaveWeapon
}
public class Player : NetworkRoomPlayer
{
public Transform LeftHand;
public Transform RightHand;
public Transform Aim;
public Collider Collider;
public LayerMask AimLayer;
#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 PistolPre;
#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;
}
GameManager.Ins.players.Add(gameObject);
}
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;
}
}
}
[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.GameStart();
}
}
#region 枪械系统
///
/// 创建武器
///
///
///
[Server]
public GameObject CreateWeapon(GunType type)
{
GameObject gun = null;
switch (type)
{
case GunType.Pistol:
gun = Instantiate(PistolPre);
break;
}
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)
{
Launcher find = null;
for (int i = 0; i < Guns.Count; i++)
{
for (int j = 0; j < Guns[i].Length; j++)
{
Launcher launcher = Guns[i][j];
if (launcher != null && launcher.type == gunType)
{
find = launcher;
break;
}
}
if (find != null)
{
break;
}
}
if (find != null)
{
Debug.Log("添加弹药 - " + gunType);
find.StuffBullet(amount);
Swith2TargetGun(gunType);
}
else
{
Debug.Log("增加新武器 - " + gunType);
Launcher left = null;
Launcher right = null;
switch (gunType)
{
case GunType.Pistol:
left = LoadGun(HandType.Left, gunType);
right = LoadGun(HandType.Right, gunType);
break;
}
Launcher[] launchers = { left, right };
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("无武器");
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++)
{
for (int j = 0; j < Guns[i].Length; j++)
{
Launcher launcher = Guns[i][j];
if (launcher != null && launcher.type == type)
{
find = i;
break;
}
}
if (find != -1)
{
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 (leftGun != null)
{
leftGun.gameObject.SetActive(false);
NetworkServer.UnSpawn(leftGun.gameObject);
leftGun = null;
}
if (rightGun != null)
{
rightGun.gameObject.SetActive(false);
NetworkServer.UnSpawn(rightGun.gameObject);
rightGun = null;
}
for (int i = 0; i < Guns.Count; i++)
{
if (i == NowGunIndex)
{
Launcher l1 = Guns[i][0];
Launcher l2 = Guns[i][1];
if (l1 != null)
{
leftGun = l1.transform;
leftGun.gameObject.SetActive(true);
NetworkServer.Spawn(l1.gameObject, gameObject);
}
if (l2 != null)
{
rightGun = l2.transform;
rightGun.gameObject.SetActive(true);
NetworkServer.Spawn(l2.gameObject, gameObject);
}
}
}
}
///
/// 删除武器
///
[Command(requiresAuthority = false)]
public void DelWeapon()
{
bool change = false;
for (int i = 0; i < Guns.Count; i++)
{
for (int j = 0; j < Guns[i].Length; j++)
{
Launcher launcher = Guns[i][j];
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();
}
#endregion
}