Files
valheim/Assets/_Valheim/Scripts/Weapons/IceStaff.cs
2025-07-18 18:11:39 +08:00

375 lines
10 KiB
C#

using System;
using DarkTonic.MasterAudio;
using DragonLi.Core;
using Mirror;
using UnityEngine;
using UnityEngine.XR;
using Valheim;
public class IceStaff : Weapon
{
public Transform Top;
[Header("最远距离")]
public float MaxDistance = 5f;
[Header("技能施放图层")]
public LayerMask HitLayer;
[Header("敲击图层")]
public LayerMask AttackHitLayer;
[Header("范围预制体")]
public GameObject AreaPre;
[Header("冰雪预制体")]
public GameObject IcePre;
public Animator animator;
public BoxCollider boxCollider;
#if UNITY_EDITOR
[DisplayOnly]
#endif
[SyncVar]
public bool IsIceing = false;
public GameObject IceEffect;
public Transform Bottom;
/// <summary>
/// 是否攻击到目标
/// </summary>
#if UNITY_EDITOR
[DisplayOnly]
#endif
[NonSerialized]
public bool IsAttacking = false;
private InputDevice _rightHandDevice;
private bool _isClick = false;
private GameObject _areaIns;
private int _frame = 0;
[Header("声音")]
/// <summary>
/// 蓄力音效
/// </summary>
[SoundGroup] public string chargedEnergSound;
public void Start()
{
if (boxCollider != null)
boxCollider.enabled = false;
#if !UNITY_EDITOR
_rightHandDevice = InputDevices.GetDeviceAtXRNode(XRNode.RightHand);
#endif
}
private float timer;
private float interval=0.5f;
public new void Update()
{
base.Update();
if (isServer)
{
if(GameManager.Ins.isGuide)
return;
if (type == WeaponType.Hand || type == WeaponType.Hammer)
{
KnockAttack();
}
timer += Time.deltaTime;
}
if (isOwned)
{
#if UNITY_EDITOR
// 一直按
if (Input.GetKey(KeyCode.Q) && !IsIceing)
{
if (GameManager.Ins.isPlayPetWorld)
{
//Charge();
}
}
// 按下
if (Input.GetKeyDown(KeyCode.Q) && !IsIceing)
{
if (GameManager.Ins.isPlayPetWorld)
{
// 显示蓄力粒子
ShowEffect();
//MasterAudio.PlaySound3DFollowTransform(chargedEnergSound, transform);
}
else
{
GameManager.Ins.SelectCard();
}
UserWeapon(true);
}
// 抬起
if (Input.GetKeyUp(KeyCode.Q) && !IsIceing)
{
if (GameManager.Ins.isPlayPetWorld)
{
_frame = 0;
HideEffect();
//MasterAudio.StopAllOfSound(chargedEnergSound);
if (_areaIns != null && !IsIceing)
{
_areaIns.SetActive(false);
}
}
UserWeapon(false);
}
#else
// bool isTrigger = false;
// if (_rightHandDevice != null)
// {
// _rightHandDevice.TryGetFeatureValue(CommonUsages.triggerButton, out isTrigger);
// if (isTrigger && !_isClick)
// {
// _isClick = true;
// }
// else if (!isTrigger)
// {
// _isClick = false;
// }
// }
// // 一直按
// if (isTrigger && !IsIceing)
// {
// if (GameManager.Ins.isPlayPetWorld)
// {
// //Charge();
// }
// }
// // 按下
// if (_isClick && !IsIceing)
// {
// if (GameManager.Ins.isPlayPetWorld)
// {
// // 显示蓄力粒子
// //ShowEffect();
// //MasterAudio.PlaySound3DFollowTransform(chargedEnergSound, transform);
// }
// else
// {
// GameManager.Ins.SelectCard();
// }
// UserWeapon(true);
// }
// // 抬起
// if (!_isClick && !IsIceing)
// {
// if (GameManager.Ins.isPlayPetWorld)
// {
// _frame = 0;
// //HideEffect();
// //MasterAudio.StopAllOfSound(chargedEnergSound);
// if (_areaIns != null && !IsIceing)
// {
// _areaIns.SetActive(false);
// }
// }
// UserWeapon(false);
// }
#endif
}
}
/// <summary>
/// 敲击
/// </summary>
[Server]
public void KnockAttack()
{
if (Physics.Raycast(Top.position, Top.forward, out RaycastHit handlingHit, 1F, AttackHitLayer))
{
Enemy enemy = handlingHit.collider.GetComponent<Enemy>();
if (enemy != null && !IsAttacking)
{
// Debug.Log("TO 攻击到目标");
IsAttacking = true;
enemy.KnockTakeDamage(atk, handlingHit.point);
enemy.PlayTakeDamageAnim();
UserWeapon(true);
if (timer >= interval)
{
PlayAudio("锤子打人音效");
timer = 0;
}
}
}
else
{
IsAttacking = false;
// Debug.Log("TO 没有目标");
}
Vector3 origin = Top.position;
Vector3 direction = Top.forward;
Debug.DrawRay(origin, direction * 1F, Color.blue);
}
[ClientRpc]
public void RpcShowEffect()
{
if(IceEffect==null)
return;
if (!IceEffect.activeSelf)
{
IceEffect.SetActive(true);
}
}
[ClientRpc]
public void RpcHideEffect()
{
if(IceEffect==null)
return;
if (IceEffect.activeSelf)
{
IceEffect.SetActive(false);
}
}
public override void ResetWeapon()
{
base.ResetWeapon();
if (_areaIns != null && _areaIns.activeSelf)
{
_areaIns.SetActive(false);
}
}
public override void UserWeapon(bool isDown)
{
switch (type)
{
case WeaponType.Hand:
animator.SetBool("isHold",isDown);
break;
case WeaponType.Hammer:
base.UserWeapon(isDown);
break;
case WeaponType.FireStaff:
if (isDown)
{
base.UserWeapon(true);
GameManager.Ins.CreateBullet(-1,Top.position,Top,1,atk,null);
}
break;
case WeaponType.MagicStaff:
if (isDown)
{
base.UserWeapon(true);
GameManager.Ins.CreateBullet(-2,Top.position,Top,1,atk,null);
}
break;
}
if (bullet_amount <= 0&& type!=WeaponType.Hand)
{
bullet_amount = 0;
if(animator!=null)
animator.SetBool("isBoom",true);
Top.gameObject.SetActive(false);
boxCollider.gameObject.transform.parent = null;
boxCollider.gameObject.transform.GetComponent<Rigidbody>().isKinematic = false;
boxCollider.gameObject.transform.GetComponent<Rigidbody>().useGravity = true;
boxCollider.gameObject.transform.eulerAngles = new Vector3(0, 0, 0);
boxCollider.enabled = true;
CloseWeapon();
CoroutineTaskManager.Instance.WaitSecondTodo(() =>
{
Destroy(animator.gameObject);
}, 5);
}
}
public void CloseWeapon()
{
GameManager.Ins.DelItem(playerId);
}
/// <summary>
/// 蓄力
/// </summary>
public void Charge()
{
// RaycastHit handlingHit;
// if (Physics.Raycast(Top.position, Top.forward, out handlingHit, MaxDistance, HitLayer))
// {
// if (_areaIns == null)
// {
// _areaIns = Instantiate(AreaPre);
// }
// Vector3 tempTransform = handlingHit.point;
// tempTransform.y = 0.04F;
// _areaIns.transform.position = tempTransform;
// _areaIns.SetActive(true);
// }
// _frame++;
// if (_frame >= 60 * 3)
// {
// if (_areaIns != null && _areaIns.activeSelf)
// {
// _frame = 0;
// MasterAudio.StopAllOfSound(chargedEnergSound);
// Debug.Log("施法!!!");
// CreateIce(_areaIns.transform.position);
// }
// }
}
[Command]
public void ShowEffect()
{
//RpcShowEffect();
}
[Command]
public void HideEffect()
{
//RpcHideEffect();
}
[Command]
public void CreateIce(Vector3 pos)
{
// IsIceing = true;
// GameObject Ice = Instantiate(IcePre);
// NetworkServer.Spawn(Ice);
// Ice.transform.position = pos;
// Ice.SetActive(true);
// CoroutineTaskManager.Instance.WaitSecondTodo(() =>
// {
// RpcHide();
// Ice.SetActive(false);
// HideEffect();
// NetworkServer.Destroy(Ice);
// IsIceing = false;
// }, 8);
}
[TargetRpc]
public void RpcHide()
{
if (_areaIns != null)
{
_areaIns.SetActive(false);
}
}
public void PlayAudio(string clipName)
{
MasterAudio.StopAllSoundsOfTransform(animator.transform);
if(clipName!="")
MasterAudio.PlaySound3DAtTransform(clipName, animator.transform);
}
}