NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Bullet/ViewBullet.cs

159 lines
4.4 KiB
C#
Raw Normal View History

2023-08-22 19:08:45 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
2023-12-13 16:20:41 +08:00
using Gameplay.Unit;
using Gameplay.Unit.Data;
2023-08-22 19:08:45 +08:00
using Gameplay.Vehicle.Impl;
2023-10-19 15:00:19 +08:00
using PhxhSDK;
2023-08-22 19:08:45 +08:00
namespace Gameplay.Bullet
{
using UnityEngine;
using Character;
using LTGame;
using cfg.BulletCfg;
using Level;
using Framework;
using Common;
public class ViewBullet
{
public GameObject rootObj;
2023-10-19 15:00:19 +08:00
public readonly GameUnit sender;
2023-08-22 19:08:45 +08:00
public GameUnit lockUnit;
public int targetCellIndex;
2023-10-19 15:00:19 +08:00
public readonly DataBullet configData;
2023-08-22 19:08:45 +08:00
public bool needRemove = false;
private NStateMachine<BaseBulletState> _fsm;
2023-10-19 15:00:19 +08:00
public readonly Map map;
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
public ViewBullet(DataBullet configData, GameUnit sender)
2023-08-22 19:08:45 +08:00
{
this.configData = configData;
map = LevelManager.Instance.CurrentLevel.Map;
2023-10-19 15:00:19 +08:00
this.sender = sender;
2023-08-22 19:08:45 +08:00
2023-10-19 15:00:19 +08:00
rootObj = new GameObject();
2023-08-22 19:08:45 +08:00
rootObj.transform.SetParent(BulletManager.instance.rootObj.transform);
var firePoint = _GetFireTrans();
rootObj.transform.position = firePoint.position;
// 这里使用sender的旋转
rootObj.transform.rotation = sender.Node.GameObject.transform.rotation;
_InitFsm();
}
private Transform _GetFireTrans()
{
switch (sender.gameunitType)
{
case EGameUnitType.Character:
var character = sender as Character;
return character.weapon.fireTrans;
case EGameUnitType.Vehicle:
var normalVehicle = sender as NormalVehicle;
return normalVehicle.battery.fireTrans;
}
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("暂未支持的类型:{0}", sender.gameunitType);
2023-08-22 19:08:45 +08:00
return null;
}
public void Init()
{
_CreateView();
}
2024-02-21 15:22:32 +08:00
public static string GetPrefabPath(string fileName)
2024-02-20 13:09:53 +08:00
{
return $"Assets/Art_Out/Manual/Bullets/{fileName}.prefab";
2024-02-20 13:09:53 +08:00
}
2023-08-22 19:08:45 +08:00
private async void _CreateView()
{
2024-02-21 15:22:32 +08:00
var prefabPath = GetPrefabPath(configData.PrefabPath);
2024-02-20 13:09:53 +08:00
BulletManager.instance.RecordLoadCount(prefabPath);
2023-10-19 15:00:19 +08:00
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(prefabPath);
2023-08-22 19:08:45 +08:00
if (sampleObj == null)
{
2023-10-19 15:00:19 +08:00
DebugUtil.LogError("加载子弹资源失败:{0}", prefabPath);
2023-08-22 19:08:45 +08:00
return;
}
if (rootObj == null)
{
// 已经销毁了
return;
2023-10-19 15:00:19 +08:00
}
2024-02-20 13:09:53 +08:00
var bulletObj = Object.Instantiate(sampleObj, rootObj.transform);
bulletObj.SetActive(false);
2023-08-22 19:08:45 +08:00
if (rootObj == null)
{
// 已经销毁了
bulletObj.Destroy();
return;
2023-10-19 15:00:19 +08:00
}
2023-11-02 19:53:39 +08:00
// bulletObj.transform.SetParent(rootObj.transform);
2023-08-22 19:08:45 +08:00
bulletObj.transform.ResetLocals();
rootObj.SetLayerEx(rootObj.layer);
bulletObj.SetActive(true);
2023-08-22 19:08:45 +08:00
}
private void _InitFsm()
{
_fsm = new NStateMachine<BaseBulletState>();
_fsm.Add(new BulletStateDestroy(this));
}
public void LockPos(int cellIndex)
{
targetCellIndex = cellIndex;
_fsm.Add(new BulletStateLockPosFly(this));
_fsm.Add(new BulletStatePosBoom(this));
_fsm.ChangeState(EBulletState.LockPosFly);
}
public void LockUnit(GameUnit lockUnit)
{
this.lockUnit = lockUnit;
_fsm.Add(new BulletStateUnitLockFly(this));
_fsm.Add(new BulletStateDamageUnit(this));
_fsm.ChangeState(EBulletState.UnitLockFly);
}
public void LogicUpdate(float dt)
{
if (needRemove) return;
_fsm.LogicUpdate(dt);
}
public void Destroy()
{
2023-10-19 15:00:19 +08:00
// 播放销毁音效
var audioId = configData.HitAudioId;
2024-09-06 18:35:09 +08:00
if (!string.IsNullOrEmpty(audioId))
2023-10-19 15:00:19 +08:00
{
2024-09-06 18:35:09 +08:00
AudioManager.Instance.PlayAudio(audioId, AudioCriManager.EPlayType.Sound, rootObj);
2023-10-19 15:00:19 +08:00
}
2023-08-22 19:08:45 +08:00
// 销毁obj
if (rootObj != null)
{
rootObj.Destroy();
rootObj = null;
}
}
}
}