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

159 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Gameplay.Unit;
using Gameplay.Unit.Data;
using Gameplay.Vehicle.Impl;
using PhxhSDK;
namespace Gameplay.Bullet
{
using UnityEngine;
using Character;
using LTGame;
using cfg.BulletCfg;
using Level;
using Framework;
using Common;
public class ViewBullet
{
public GameObject rootObj;
public readonly GameUnit sender;
public GameUnit lockUnit;
public int targetCellIndex;
public readonly DataBullet configData;
public bool needRemove = false;
private NStateMachine<BaseBulletState> _fsm;
public readonly Map map;
public ViewBullet(DataBullet configData, GameUnit sender)
{
this.configData = configData;
map = LevelManager.Instance.CurrentLevel.Map;
this.sender = sender;
rootObj = new GameObject();
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;
}
DebugUtil.LogError("暂未支持的类型:{0}", sender.gameunitType);
return null;
}
public void Init()
{
_CreateView();
}
public static string GetPrefabPath(string fileName)
{
return $"Assets/Art/Gfx/Character/{fileName}.prefab";
}
private async void _CreateView()
{
var prefabPath = GetPrefabPath(configData.PrefabPath);
BulletManager.instance.RecordLoadCount(prefabPath);
var sampleObj = await AssetManager.Instance.LoadAssetAsync<GameObject>(prefabPath);
if (sampleObj == null)
{
DebugUtil.LogError("加载子弹资源失败:{0}", prefabPath);
return;
}
if (rootObj == null)
{
// 已经销毁了
return;
}
var bulletObj = Object.Instantiate(sampleObj, rootObj.transform);
bulletObj.SetActive(false);
if (rootObj == null)
{
// 已经销毁了
bulletObj.Destroy();
return;
}
// bulletObj.transform.SetParent(rootObj.transform);
bulletObj.transform.ResetLocals();
rootObj.SetLayerEx(rootObj.layer);
bulletObj.SetActive(true);
}
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()
{
// 播放销毁音效
var audioId = configData.HitAudioId;
if (!string.IsNullOrEmpty(audioId))
{
AudioManager.Instance.PlayAudio(audioId, AudioCriManager.EPlayType.Sound, rootObj);
}
// 销毁obj
if (rootObj != null)
{
rootObj.Destroy();
rootObj = null;
}
}
}
}