NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Weapon/ViewWeapon.cs

186 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using AOT.PostProcess.BlackArea;
using Code.Scripts.Gameplay.Weapon;
using Cysharp.Threading.Tasks;
using Gameplay.Area;
using Gameplay.Bullet.NormalAttack;
using Gameplay.Performance;
using Gameplay.Pool;
using Gameplay.PostProcess.BlackArea;
using Gameplay.Unit;
using Gameplay.Unit.Data;
namespace Gameplay.Weapon
{
using cfg.WeaponCfg;
using Character;
using Framework;
using Common;
using Effect;
using UnityEngine;
public class ViewWeapon
{
public readonly Character owner;
public readonly DataWeapon rawConfig;
public readonly GameObject rootObj;
public readonly Transform fireTrans;
public readonly WeaponAnim weaponAnim;
public readonly Transform weaponBone;
public ViewWeapon(Character owner)
{
this.owner = owner;
var weaponId = owner.runtimeData.configData.weaponId;
rawConfig = TableManager.Instance.Tables.WeaponConfig.Get(weaponId);
// 找到骨骼
var parentPointName = rawConfig.ParentPointName;
if (string.IsNullOrEmpty(parentPointName))
{
DebugUtil.LogError("武器配置错误: {0}, 没有ParentPointName", weaponId);
parentPointName = "Grip_point01";
}
var bone = owner.Skeleton.GetBone(parentPointName);
if (bone == null)
{
owner.LogError("找不到骨骼:" + rawConfig.ParentPointName);
return;
}
weaponBone = bone.transform;
// 绑定武器
if (bone.transform.childCount == 0)
{
this.owner.LogError($"当前角色 {owner.runtimeData.configData.nameId} 武器节点下未正确挂载武器");
// 未挂载武器直接使用角色的trans作为fire点
fireTrans = this.owner.RawAnimator.transform;
return;
}
var findTrans = bone.transform.GetChild(0);
rootObj = findTrans.gameObject;
weaponAnim = new WeaponAnim(rootObj, owner);
// 美术约定从该节点取fire点
fireTrans = owner.Skeleton.GetBone("Fire");
if (fireTrans == null)
{
DebugUtil.LogError($"当前角色 {owner.runtimeData.configData.nameId} 没有Fire节点, 使用角色的trans作为fire点");
fireTrans = owner.RawAnimator.transform;
}
// _lines = new List<WeaponLine>();
}
public void OpenFire(GameUnit enemy)
{
if (owner.commonData.troopId != ETroopsId.Self)
{
BlackAreaManager.instance.LightUpCell(owner.transData.cellIndex,
CodeDefine.Fight.SHOOT_LIGHT_UP_TIME);
}
var groundEffectId = GLConfig.Inst.data.GroundFireEffectId;
if (groundEffectId > 0 && rawConfig.FireGroundEffect)
{
var firePos = fireTrans.position;
firePos.y = 0.1f;
var rot = owner.Node.GameObject.transform.rotation;
EffectManager.instance.PlayEffectById(groundEffectId, firePos, rot);
}
if (rawConfig.FireAudioId.Count > 0)
{
var fireAudioId = rawConfig.FireAudioId[Random.Range(0, rawConfig.FireAudioId.Count)];
AudioManager.Instance.PlayAudio(fireAudioId, fireTrans.position);
}
var fireEffectId = rawConfig.FireEffectId;
if (fireEffectId > 0)
{
var rot = owner.Node.GameObject.transform.rotation;
EffectManager.instance.PlayEffectById(fireEffectId, fireTrans.position, rot, fireTrans);
}
}
public void PlayMatchFireAnim()
{
if (weaponAnim == null) return;
weaponAnim.PlayAnim(rawConfig.FireAnimName);
}
public void PlayMatchReloadAnim()
{
if (weaponAnim == null) return;
weaponAnim.PlayAnim(rawConfig.ReleadAnimName);
}
public static void ShowHitEffect(GameUnit enemy,
bool isCritical,
int criticalHitEffectId,
int hitEffectId,
Transform senderTrans)
{
var effectId = isCritical ? criticalHitEffectId : hitEffectId;
if (effectId <= 0)
return;
var hitPoints = NormalAttackHelper.GetPoints(enemy);
if (hitPoints == null)
{
return;
}
var points = isCritical ? hitPoints.m_CriticalPoints : hitPoints.m_OtherPoints;
var randomPoint = points[Random.Range(0, points.Length)];
var sender = senderTrans;
var senderPos = sender.position;
var position = randomPoint.position;
var toSenderPos = senderPos - position;
toSenderPos.y = 0;
var angle = Vector3.SignedAngle(Vector3.forward, toSenderPos, Vector3.up);
var rot = Quaternion.Euler(0, angle + 180, 0);
var effectObj = EffectManager.instance.PlayEffectById(effectId, position, rot);
// effectObj.transform.LookAt(fireTrans, Vector3.up);
}
public void ViewUpdate(float dt)
{
}
public void Hide()
{
if (weaponBone == null) return;
weaponBone.localScale = Vector3.zero;
weaponBone.gameObject.SetActive(false);
var childBone = weaponBone.transform.GetChild(0);
if (childBone != null)
{
childBone.transform.localScale = Vector3.zero;
childBone.gameObject.SetActive(false);
}
}
public void Show()
{
if (weaponBone == null) return;
weaponBone.localScale = Vector3.one;
weaponBone.gameObject.SetActive(true);
if (weaponBone.transform.childCount != 0)
{
var childBone = weaponBone.transform.GetChild(0);
childBone.transform.localScale = Vector3.one;
childBone.gameObject.SetActive(true);
}
}
}
}