464 lines
11 KiB
C#
464 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using DragonLi.Core;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
|
|
/// <summary>
|
|
/// 手类型
|
|
/// </summary>
|
|
public enum HandType
|
|
{
|
|
Left,
|
|
Right
|
|
}
|
|
|
|
/// <summary>
|
|
/// 手状态
|
|
/// </summary>
|
|
public enum HandState
|
|
{
|
|
/// <summary>
|
|
/// 空手
|
|
/// </summary>
|
|
Empty,
|
|
/// <summary>
|
|
/// 有武器
|
|
/// </summary>
|
|
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 枪械参数
|
|
|
|
/// <summary>
|
|
/// 双手状态
|
|
/// </summary>
|
|
public HandState NowHandState = HandState.Empty;
|
|
|
|
/// <summary>
|
|
/// 手上的枪械
|
|
/// </summary>
|
|
private List<Launcher[]> Guns = new List<Launcher[]>();
|
|
|
|
/// <summary>
|
|
/// 现在手上的武器
|
|
/// </summary>
|
|
public int NowGunIndex = -1;
|
|
|
|
/// <summary>
|
|
/// 左手上的枪
|
|
/// </summary>
|
|
private Transform leftGun;
|
|
|
|
/// <summary>
|
|
/// 右手上的枪
|
|
/// </summary>
|
|
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<Collider>().enabled = false;
|
|
other.gameObject.SetActive(false);
|
|
NetworkServer.Destroy(other.gameObject);
|
|
GameManager.Ins.GameStart();
|
|
}
|
|
}
|
|
|
|
#region 枪械系统
|
|
|
|
/// <summary>
|
|
/// 创建武器
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
[Server]
|
|
public GameObject CreateWeapon(GunType type)
|
|
{
|
|
GameObject gun = null;
|
|
switch (type)
|
|
{
|
|
case GunType.Pistol:
|
|
gun = Instantiate(PistolPre);
|
|
break;
|
|
}
|
|
return gun;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 装载武器
|
|
/// </summary>
|
|
/// <param name="handType"></param>
|
|
/// <param name="gunType"></param>
|
|
/// <returns></returns>
|
|
[Server]
|
|
public Launcher LoadGun(HandType handType, GunType gunType)
|
|
{
|
|
GameObject gun = CreateWeapon(gunType);
|
|
Launcher launcher = gun.GetComponent<Launcher>();
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拾取枪械
|
|
/// </summary>
|
|
/// <param name="gunType"></param>
|
|
/// <param name="owner"></param>
|
|
[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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到下个武器
|
|
/// </summary>
|
|
[Command]
|
|
public void Switch2NextWeaponCmd()
|
|
{
|
|
Switch2NextWeapon();
|
|
}
|
|
[Command]
|
|
public void Switch2BeforeWeaponCmd()
|
|
{
|
|
Switch2BeforeWeapon();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到下个武器
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到上个武器
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到指定枪械
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 武器更新
|
|
/// </summary>
|
|
[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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除武器
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换为空手
|
|
/// </summary>
|
|
[Server]
|
|
public void Switch2Empty()
|
|
{
|
|
NowHandState = HandState.Empty;
|
|
NowGunIndex = -1;
|
|
UpdateWeapons();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
|