130 lines
3.8 KiB
C#
130 lines
3.8 KiB
C#
using Framework;
|
|
using Gameplay.Unit;
|
|
using Gameplay.Vehicle.Impl;
|
|
using LTGame;
|
|
namespace Gameplay.Vehicle
|
|
{
|
|
|
|
using UnityEngine;
|
|
|
|
public class BaseBattery
|
|
{
|
|
|
|
public readonly NormalVehicle owner;
|
|
|
|
public GameObject rootObj;
|
|
|
|
public float remainShootTime;
|
|
public bool isShootReady
|
|
{
|
|
get
|
|
{
|
|
return remainShootTime <= 0;
|
|
}
|
|
}
|
|
|
|
private Transform _fireTrans;
|
|
public Transform fireTrans
|
|
{
|
|
get
|
|
{
|
|
return _fireTrans;
|
|
}
|
|
}
|
|
|
|
public readonly cfg.WeaponCfg.DataWeapon rawWeaponConfig;
|
|
|
|
public int remainBulletCount;
|
|
|
|
public GameUnit lockEnemy;
|
|
|
|
private NStateMachine<BaseBaterryState> _fsm;
|
|
|
|
/// <summary>
|
|
/// 炮台动画比较简单,直接使用Animator
|
|
/// </summary>
|
|
public Animator rawAnim;
|
|
|
|
public BaseBattery(NormalVehicle owner)
|
|
{
|
|
this.owner = owner;
|
|
remainShootTime = 0f;
|
|
remainBulletCount = this.owner.fightData.maxBulletCount;
|
|
rawWeaponConfig = TableManager.Instance.Tables.WeaponConfig.Get(owner.configData.weaponId);
|
|
}
|
|
|
|
private void _InitFsm()
|
|
{
|
|
_fsm = new NStateMachine<BaseBaterryState>();
|
|
_fsm.Add(new BatteryStateIdle(this));
|
|
_fsm.Add(new BatteryStateShoot(this));
|
|
_fsm.Add(new BatteryStateReload(this));
|
|
_fsm.Add(new BatteryStateDestroyed(this));
|
|
|
|
_fsm.ChangeState(EBatteryState.Idle);
|
|
}
|
|
|
|
private void _BindView()
|
|
{
|
|
var ownerObj = owner.Node.GameObject;
|
|
var collector = new ComponentCollector(ownerObj);
|
|
var skeleton = new Skeleton(collector.Transforms, ownerObj.transform);
|
|
var parentTrans = skeleton.GetBone("Battery_point01");
|
|
rootObj = parentTrans.GetChild(0).GetChild(0).gameObject;
|
|
_fireTrans = skeleton.GetBone("Fire");
|
|
|
|
rawAnim = parentTrans.GetComponentInChildren<Animator>();
|
|
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
_BindView();
|
|
_InitFsm();
|
|
}
|
|
|
|
public bool RotateTower(GameUnit targetEnemy, float dt)
|
|
{
|
|
var turnSpeed = owner.configData.batteryTurnSpeed;
|
|
var turnDt = turnSpeed * dt;
|
|
var isMatchAngle = false;
|
|
if (targetEnemy == null || targetEnemy.isDisposed)
|
|
{
|
|
// 炮台回正
|
|
var localYaw = rootObj.transform.localEulerAngles.y;
|
|
var newYaw = Mathf.MoveTowardsAngle(localYaw, 0, turnDt);
|
|
rootObj.transform.localEulerAngles = new Vector3(0, newYaw, 0);
|
|
}
|
|
else
|
|
{
|
|
// 炮台转向目标
|
|
var targetPos = targetEnemy.transData.pos;
|
|
var selfPos = rootObj.transform.position;
|
|
var toTarget = targetPos - selfPos;
|
|
toTarget.y = 0;
|
|
var targetYaw = Vector3.SignedAngle(Vector3.forward, toTarget, Vector3.up);
|
|
var globalYaw = rootObj.transform.eulerAngles.y;
|
|
var deltaAngle = Mathf.Abs(Mathf.DeltaAngle(globalYaw, targetYaw));
|
|
if (deltaAngle < turnDt)
|
|
{
|
|
rootObj.transform.eulerAngles = new Vector3(0, targetYaw, 0);
|
|
isMatchAngle = true;
|
|
}
|
|
else
|
|
{
|
|
var newYaw = Mathf.MoveTowardsAngle(globalYaw, targetYaw, turnDt);
|
|
rootObj.transform.eulerAngles = new Vector3(0, newYaw, 0);
|
|
}
|
|
}
|
|
return isMatchAngle;
|
|
}
|
|
|
|
public void LogicUpdate(float dt)
|
|
{
|
|
remainShootTime -= dt;
|
|
_fsm.LogicUpdate(dt);
|
|
}
|
|
|
|
}
|
|
}
|