643 lines
19 KiB
C#
643 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using DragonLi.Core;
|
|
using DragonLi.Frame;
|
|
using Mirror;
|
|
using UnityEngine;
|
|
using UnityEngine.XR;
|
|
namespace Valheim
|
|
{
|
|
public class Player : NetworkRoomPlayer
|
|
{
|
|
public Transform LeftHand;
|
|
public Transform RightHand;
|
|
|
|
#region 武器
|
|
public GameObject rightHandPre;
|
|
public GameObject leftHandPre;
|
|
public GameObject hammerPre;
|
|
public GameObject fireStaffPre;
|
|
public GameObject magicStaffPre;
|
|
|
|
public GameObject PathGuidePre;
|
|
|
|
public Transform leaderPoint;
|
|
|
|
public GuideArrowPath pathGuide;//引导路径
|
|
|
|
/// <summary>
|
|
/// 手上的武器
|
|
/// </summary>
|
|
private List<Weapon[]> Weapons = new List<Weapon[]>();
|
|
|
|
/// <summary>
|
|
/// 双手状态
|
|
/// </summary>
|
|
[NonSerialized]
|
|
public HandState NowHandState = HandState.Empty;
|
|
|
|
/// <summary>
|
|
/// 现在手上的武器
|
|
/// </summary>
|
|
[NonSerialized]
|
|
public int NowWeaponIndex = -1;
|
|
|
|
private Transform left;
|
|
private Transform right;
|
|
#endregion
|
|
|
|
public InputDevice leftHandDevice;
|
|
private InputDevice rightHandDevice;
|
|
|
|
public bool isClickSwith = false;
|
|
|
|
public Rigidbody Rigidbody;
|
|
|
|
private int frameCount = 0;
|
|
|
|
public int InAreaId = -1;
|
|
|
|
public new void Start()
|
|
{
|
|
base.Start();
|
|
|
|
leftHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.LeftHand);
|
|
rightHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
|
|
|
|
if (isLocalPlayer)
|
|
{
|
|
Rigidbody.isKinematic = true;
|
|
GameInit.Ins.self = this;
|
|
GameManager.Ins.AddTeamCmd(index);
|
|
//PickUpWeapon(WeaponType.LoveStuff, -999);
|
|
//攻击力
|
|
PickUpWeapon(WeaponType.Hand, -999,50);
|
|
CreatePathGuide();
|
|
}
|
|
else if (isServer)
|
|
{
|
|
Rigidbody.isKinematic = false;
|
|
}
|
|
}
|
|
|
|
private Vector3 TriggerPoint;
|
|
|
|
public bool isInAnyArea = false;
|
|
|
|
public void Update()
|
|
{
|
|
if (isLocalPlayer)
|
|
{
|
|
if (Input.GetKeyDown(KeyCode.F))
|
|
{
|
|
//秒杀所有小怪
|
|
GameManager.Ins.KillAllEnemy();
|
|
}
|
|
|
|
// 同步相机-身体
|
|
if (GameInit.Ins.MRCamera != null)
|
|
{
|
|
transform.position = GameInit.Ins.MRCamera.transform.position;
|
|
transform.rotation = GameInit.Ins.MRCamera.transform.rotation;
|
|
}
|
|
#if UNITY_EDITOR
|
|
// 切换武器
|
|
if (Input.GetKeyDown(KeyCode.Alpha1))
|
|
{
|
|
Switch2BeforeWeaponCmd();
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Alpha2))
|
|
{
|
|
Switch2NextWeaponCmd();
|
|
}
|
|
if (Input.GetKeyDown(KeyCode.Escape))
|
|
{
|
|
ExitPanel.Show();
|
|
}
|
|
#endif
|
|
|
|
#if !UNITY_EDITOR && UNITY_ANDROID && (PICO||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;
|
|
}
|
|
if (rightHandDevice != null)
|
|
{
|
|
Vector2 value;
|
|
rightHandDevice.TryGetFeatureValue(CommonUsages.primary2DAxis, out value);
|
|
if (value.magnitude > 0)
|
|
{
|
|
// 根据轴的值判断摇杆朝向
|
|
if (!isClickSwith && value.x < 0)
|
|
{
|
|
// 摇杆朝左
|
|
isClickSwith = true;
|
|
Switch2BeforeWeaponCmd();
|
|
|
|
}
|
|
else if (!isClickSwith && value.x > 0)
|
|
{
|
|
// 摇杆朝右
|
|
isClickSwith = true;
|
|
Switch2NextWeaponCmd();
|
|
}
|
|
}
|
|
else if (value.magnitude == 0)
|
|
{
|
|
isClickSwith = false;
|
|
}
|
|
if (value.Equals(Vector2.zero))
|
|
{
|
|
isClickSwith = false;
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
if (isServer)
|
|
{
|
|
// 判断在区域内还是外
|
|
if (GameManager.Ins.AreaList.Count > 0)
|
|
{
|
|
// InAreaId = -1;
|
|
// foreach (BattleArea battleArea in GameManager.Ins.AreaList.Values)
|
|
// {
|
|
// if (GameManager.Ins.CheckPointInArea(battleArea.AreaId, transform.position))
|
|
// {
|
|
// InAreaId = battleArea.AreaId;
|
|
// break;
|
|
// }
|
|
// }
|
|
}
|
|
|
|
// 区域内督战
|
|
if (InAreaId != -1)
|
|
{
|
|
frameCount++;
|
|
// Debug.Log("order Pet1");
|
|
GameManager.Ins.OverseerPetInArea(index);
|
|
if (frameCount % 10 == 0)
|
|
{
|
|
}
|
|
}
|
|
// 区域外督战boss
|
|
else if (
|
|
InAreaId == -1 &&
|
|
GameManager.Ins.fishGiant != null &&
|
|
GameManager.Ins.fishGiant.bossState == BossState.Idle
|
|
)
|
|
{
|
|
frameCount++;
|
|
GameManager.Ins.OverseerPet2Boss(index);
|
|
}
|
|
else
|
|
{
|
|
GameManager.Ins.OriginRetreat(index);
|
|
}
|
|
}
|
|
}
|
|
|
|
[ServerCallback]
|
|
public void OnTriggerEnter(Collider other)
|
|
{
|
|
if (isServer)
|
|
{
|
|
// 进入区域
|
|
if (other.tag == "BattleArea")
|
|
{
|
|
// other.gameObject.SetActive(false);
|
|
// InAreaId = other.GetComponent<BattleArea>().AreaId;
|
|
// ClosePathGuide();
|
|
// //触发区域怪物
|
|
// StartCoroutine(GameManager.Ins.CreateBattleAreaEnemy());
|
|
}
|
|
// 捕捉野生宠物
|
|
if (other.tag == "PetCatch")
|
|
{
|
|
Pet pet = other.transform.parent.GetComponent<Pet>();
|
|
if (pet != null)
|
|
{
|
|
GameManager.Ins.Try2CapturePet(index, pet.id);
|
|
}
|
|
}
|
|
if (other.tag == "Door")
|
|
{
|
|
GameManager.Ins.ShowPetWorld(other.gameObject);
|
|
}
|
|
if (other.tag == "InitDoor")
|
|
{
|
|
if(GameManager.Ins.isPlayGame)
|
|
return;
|
|
GameManager.Ins.CreateMap();
|
|
GameManager.Ins.StartGame();
|
|
NetworkServer.Destroy(other.gameObject);
|
|
}
|
|
}
|
|
}
|
|
public void ExitArea()
|
|
{
|
|
InAreaId = -1;
|
|
//Debug.Log(string.Format("player{0}离开区域{1}", index, areaId));
|
|
GameManager.Ins.OriginRetreat(index);
|
|
ShowPathGuide();
|
|
GameManager.Ins.ShowBattleArea();
|
|
}
|
|
|
|
public override void OnGUI()
|
|
{
|
|
if (!showRoomGUI)
|
|
return;
|
|
NetworkRoomManager room = NetworkManager.singleton as NetworkRoomManager;
|
|
if (room)
|
|
{
|
|
if (!room.showRoomGUI)
|
|
return;
|
|
|
|
DrawPlayerReadyState();
|
|
}
|
|
}
|
|
|
|
#region 武器系统
|
|
|
|
/// <summary>
|
|
/// 创建武器
|
|
/// </summary>
|
|
/// <param name="type"></param>
|
|
/// <returns></returns>
|
|
[Server]
|
|
public GameObject CreateWeapon(WeaponType type, HandType handType)
|
|
{
|
|
GameObject weapon = null;
|
|
switch (type)
|
|
{
|
|
case WeaponType.Hand:
|
|
if (handType == HandType.Left)
|
|
{
|
|
weapon = Instantiate(leftHandPre);
|
|
}
|
|
else if (handType == HandType.Right)
|
|
{
|
|
weapon = Instantiate(rightHandPre);
|
|
}
|
|
break;
|
|
case WeaponType.Hammer:
|
|
weapon= Instantiate(hammerPre);
|
|
break;
|
|
case WeaponType.FireStaff:
|
|
weapon = Instantiate(fireStaffPre);
|
|
break;
|
|
case WeaponType.MagicStaff:
|
|
weapon = Instantiate(magicStaffPre);
|
|
break;
|
|
}
|
|
return weapon;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 装载武器
|
|
/// </summary>
|
|
/// <param name="handType"></param>
|
|
/// <param name="weaponType"></param>
|
|
/// <returns></returns>
|
|
public Weapon LoadWeapon(HandType handType, WeaponType weaponType,int curAtk)
|
|
{
|
|
GameObject weapon = CreateWeapon(weaponType,handType);
|
|
Weapon launcher = weapon.GetComponent<Weapon>();
|
|
launcher.hand = handType;
|
|
launcher.type = weaponType;
|
|
launcher.playerId = index;
|
|
launcher.atk = curAtk;
|
|
return launcher;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拾取武器
|
|
/// </summary>
|
|
/// <param name="weaponType"></param>
|
|
/// <param name="owner"></param>
|
|
[Command(requiresAuthority = false)]
|
|
public void PickUpWeapon(WeaponType weaponType, int amount,int atk)
|
|
{
|
|
Weapon find = null;
|
|
for (int i = 0; i < Weapons.Count; i++)
|
|
{
|
|
for (int j = 0; j < Weapons[i].Length; j++)
|
|
{
|
|
Weapon 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);
|
|
Weapon left = null;
|
|
Weapon right = null;
|
|
switch (weaponType)
|
|
{
|
|
case WeaponType.Hand:
|
|
right = LoadWeapon(HandType.Right, weaponType,atk);
|
|
right.StuffBullet(amount);
|
|
left = LoadWeapon(HandType.Left, weaponType,atk);
|
|
left.StuffBullet(amount);
|
|
break;
|
|
case WeaponType.Hammer:
|
|
case WeaponType.FireStaff:
|
|
case WeaponType.MagicStaff:
|
|
right = LoadWeapon(HandType.Right, weaponType,atk);
|
|
right.StuffBullet(amount);
|
|
break;
|
|
}
|
|
Weapon[] weapons = { left, right };
|
|
Weapons.Add(weapons);
|
|
Swith2TargetWeapon(weaponType);
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 切换到下个武器
|
|
/// </summary>
|
|
[Command]
|
|
public void Switch2NextWeaponCmd()
|
|
{
|
|
Switch2NextWeapon();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到上个武器
|
|
/// </summary>
|
|
[Command]
|
|
public void Switch2BeforeWeaponCmd()
|
|
{
|
|
Switch2BeforeWeapon();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到下个武器
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到上个武器
|
|
/// </summary>
|
|
[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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换到指定武器
|
|
/// </summary>
|
|
[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++)
|
|
{
|
|
Weapon launcher = Weapons[i][j];
|
|
if (launcher != null && launcher.type == type)
|
|
{
|
|
find = i;
|
|
break;
|
|
}
|
|
}
|
|
if (find != -1)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (find != -1)
|
|
{
|
|
NowWeaponIndex = find;
|
|
}
|
|
|
|
UpdateWeapons();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 武器更新
|
|
/// </summary>
|
|
[Server]
|
|
public void UpdateWeapons()
|
|
{
|
|
if (left != null)
|
|
{
|
|
left.gameObject.SetActive(false);
|
|
left.gameObject.GetComponent<Weapon>().ResetWeapon();
|
|
NetworkServer.UnSpawn(left.gameObject);
|
|
left = null;
|
|
}
|
|
if (right != null)
|
|
{
|
|
right.gameObject.SetActive(false);
|
|
right.gameObject.GetComponent<Weapon>().ResetWeapon();
|
|
NetworkServer.UnSpawn(right.gameObject);
|
|
right = null;
|
|
}
|
|
|
|
for (int i = 0; i < Weapons.Count; i++)
|
|
{
|
|
if (i == NowWeaponIndex)
|
|
{
|
|
Weapon l1 = Weapons[i][0];
|
|
Weapon l2 = Weapons[i][1];
|
|
if (l1 != null)
|
|
{
|
|
left = l1.transform;
|
|
left.gameObject.SetActive(true);
|
|
NetworkServer.Spawn(l1.gameObject, gameObject);
|
|
}
|
|
if (l2 != null)
|
|
{
|
|
right = l2.transform;
|
|
right.gameObject.SetActive(true);
|
|
NetworkServer.Spawn(l2.gameObject, gameObject);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void UserWeapon(int weaponId)
|
|
{
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 删除武器
|
|
/// </summary>
|
|
[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++)
|
|
{
|
|
Weapon 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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 切换为空手
|
|
/// </summary>
|
|
[Server]
|
|
public void Switch2Empty()
|
|
{
|
|
NowHandState = HandState.Empty;
|
|
NowWeaponIndex = -1;
|
|
UpdateWeapons();
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 引导系统
|
|
public void CreatePathGuide()
|
|
{
|
|
var pathGuideObj = Instantiate(PathGuidePre);
|
|
pathGuide = pathGuideObj.GetComponent<GuideArrowPath>();
|
|
pathGuide.ClosePath();
|
|
}
|
|
|
|
public void ShowPathGuide()
|
|
{
|
|
pathGuide.ShowPath();
|
|
var battleAreaInfos = GameManager.Ins.AreaList.Values;
|
|
float minDistance = float.MaxValue;
|
|
BattleArea curInfo = null;
|
|
foreach (var area in battleAreaInfos)
|
|
{
|
|
|
|
// var path= pathGuide.GetToTargetDis(new Vector3(transform.position.x,0,transform.position.z), new Vector3(area.transform.position.x,0,area.transform.position.z));
|
|
// if (!Mathf.Approximately(path, -1)&& path < minDistance)
|
|
// {
|
|
// curInfo = area;
|
|
// minDistance = path;
|
|
// }
|
|
}
|
|
|
|
for (int i = battleAreaInfos.Count-1; i >=0 ; i--)
|
|
{
|
|
if(battleAreaInfos.ToList()[i].isWin)
|
|
continue;
|
|
curInfo = battleAreaInfos.ToList()[i];
|
|
}
|
|
|
|
if (curInfo != null)
|
|
{
|
|
pathGuide.SetPath(new Vector3(transform.position.x,0,transform.position.z),new Vector3(curInfo.transform.position.x,0,curInfo.transform.position.z));
|
|
GameManager.Ins.curBattleAreaIndex=curInfo.AreaId;
|
|
}
|
|
else
|
|
{
|
|
GameManager.Ins.curBattleAreaIndex = -1;
|
|
ClosePathGuide();
|
|
}
|
|
}
|
|
|
|
public void ClosePathGuide()
|
|
{
|
|
pathGuide.ClosePath();
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
|
|
|