Newer
Older
DungeonShooting / DungeonShooting_Godot / src / game / activity / weapon / Weapon.cs
@小李xl 小李xl on 21 Feb 2024 66 KB 游戏大厅, 开发中
  1. using Godot;
  2. using System;
  3. using System.Collections.Generic;
  4. using Config;
  5.  
  6. /// <summary>
  7. /// 武器的基类
  8. /// </summary>
  9. public abstract partial class Weapon : ActivityObject, IPackageItem<Role>
  10. {
  11. /// <summary>
  12. /// 武器使用的属性数据, 该属性会根据是否是玩家使用武器, 如果是Ai使用武器, 则会返回 AiUseAttribute 的属性对象
  13. /// </summary>
  14. public ExcelConfig.WeaponBase Attribute => _weaponAttribute;
  15.  
  16. /// <summary>
  17. /// Ai使用该武器的属性
  18. /// </summary>
  19. public ExcelConfig.WeaponBase AiUseAttribute => _aiWeaponAttribute;
  20.  
  21. /// <summary>
  22. /// 玩家使用该武器的属性
  23. /// </summary>
  24. public ExcelConfig.WeaponBase PlayerUseAttribute => _playerWeaponAttribute;
  25. private ExcelConfig.WeaponBase _weaponAttribute;
  26. private ExcelConfig.WeaponBase _playerWeaponAttribute;
  27. private ExcelConfig.WeaponBase _aiWeaponAttribute;
  28.  
  29. /// <summary>
  30. /// 武器攻击的目标阵营
  31. /// </summary>
  32. public CampEnum TargetCamp { get; set; }
  33.  
  34. public Role Master { get; set; }
  35.  
  36. public int PackageIndex { get; set; } = -1;
  37.  
  38. /// <summary>
  39. /// 当前弹夹弹药剩余量
  40. /// </summary>
  41. public int CurrAmmo { get; private set; }
  42.  
  43. /// <summary>
  44. /// 剩余弹药量(备用弹药)
  45. /// </summary>
  46. public int ResidueAmmo { get; private set; }
  47.  
  48. /// <summary>
  49. /// 武器管的开火点
  50. /// </summary>
  51. [Export, ExportFillNode]
  52. public Marker2D FirePoint { get; set; }
  53.  
  54. /// <summary>
  55. /// 弹壳抛出的点
  56. /// </summary>
  57. [Export, ExportFillNode]
  58. public Marker2D ShellPoint { get; set; }
  59. /// <summary>
  60. /// 武器的当前散射半径
  61. /// </summary>
  62. public float CurrScatteringRange { get; private set; } = 0;
  63.  
  64. /// <summary>
  65. /// 是否在换弹中
  66. /// </summary>
  67. /// <value></value>
  68. public bool Reloading { get; private set; } = false;
  69.  
  70. /// <summary>
  71. /// 换弹进度 (从 0 到 1)
  72. /// </summary>
  73. public float ReloadProgress
  74. {
  75. get
  76. {
  77. if (!Reloading)
  78. {
  79. return 1;
  80. }
  81.  
  82. if (Attribute.AloneReload)
  83. {
  84. //总时间
  85. var total = Attribute.AloneReloadBeginIntervalTime + (Attribute.ReloadTime * Attribute.AmmoCapacity) + Attribute.AloneReloadFinishIntervalTime;
  86. //当前时间
  87. float current;
  88. if (_aloneReloadState == 1)
  89. {
  90. current = (Attribute.AloneReloadBeginIntervalTime - _reloadTimer) + Attribute.ReloadTime * CurrAmmo;
  91. }
  92. else if (_aloneReloadState == 2)
  93. {
  94. current = Attribute.AloneReloadBeginIntervalTime + (Attribute.ReloadTime * (CurrAmmo + (1 - _reloadTimer / Attribute.ReloadTime)));
  95. }
  96. else
  97. {
  98. current = Attribute.AloneReloadBeginIntervalTime + (Attribute.ReloadTime * CurrAmmo) + (Attribute.AloneReloadFinishIntervalTime - _reloadTimer);
  99. }
  100.  
  101. return current / total;
  102. }
  103.  
  104. return 1 - _reloadTimer / Attribute.ReloadTime;
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 返回是否在蓄力中,
  110. /// 注意, 属性仅在 Attribute.LooseShoot == false 时有正确的返回值, 否则返回 false
  111. /// </summary>
  112. public bool IsCharging => _looseShootFlag;
  113.  
  114. /// <summary>
  115. /// 返回武器是否在武器背包中
  116. /// </summary>
  117. public bool IsInHolster => Master != null;
  118.  
  119. /// <summary>
  120. /// 返回是否真正使用该武器
  121. /// </summary>
  122. public bool IsActive => Master != null && Master.WeaponPack.ActiveItem == this;
  123. /// <summary>
  124. /// 动画播放器
  125. /// </summary>
  126. [Export, ExportFillNode]
  127. public AnimationPlayer AnimationPlayer { get; set; }
  128.  
  129. /// <summary>
  130. /// 是否自动播放 SpriteFrames 的动画
  131. /// </summary>
  132. public bool IsAutoPlaySpriteFrames { get; set; } = true;
  133.  
  134. /// <summary>
  135. /// 在没有所属 Master 的时候是否可以触发扳机
  136. /// </summary>
  137. public bool NoMasterCanTrigger { get; set; } = true;
  138. /// <summary>
  139. /// 上一次触发改武器开火的角色, 可能为 null
  140. /// </summary>
  141. public Role TriggerRole { get; private set; }
  142. /// <summary>
  143. /// 上一次触发改武器开火的触发开火攻击的层级, 数据源自于: <see cref="PhysicsLayer"/>
  144. /// </summary>
  145. public long TriggerRoleAttackLayer { get; private set; }
  146. /// <summary>
  147. /// 武器当前射速
  148. /// </summary>
  149. public float CurrentFiringSpeed { get; private set; }
  150. //--------------------------------------------------------------------------------------------
  151.  
  152. //用于记录是否有角色操作过这把武器
  153. private bool _triggerRoleFlag = false;
  154. //是否按下
  155. private bool _triggerFlag = false;
  156.  
  157. //扳机计时器
  158. private float _triggerTimer = 0;
  159.  
  160. //开火前延时时间
  161. private float _delayedTime = 0;
  162.  
  163. //开火间隙时间
  164. private float _fireInterval = 0;
  165.  
  166. //开火武器口角度
  167. private float _fireAngle = 0;
  168.  
  169. //攻击冷却计时
  170. private float _attackTimer = 0;
  171.  
  172. //攻击状态
  173. private bool _attackFlag = false;
  174. //多久没开火了
  175. private float _noAttackTime = 0;
  176.  
  177. //按下的时间
  178. private float _downTimer = 0;
  179.  
  180. //松开的时间
  181. private float _upTimer = 0;
  182.  
  183. //连发次数
  184. private int _continuousCount = 0;
  185.  
  186. //连发状态记录
  187. private bool _continuousShootFlag = false;
  188.  
  189. //松开扳机是否开火
  190. private bool _looseShootFlag = false;
  191.  
  192. //蓄力攻击时长
  193. private float _chargeTime = 0;
  194.  
  195. //是否需要重置武器数据
  196. private bool _dirtyFlag = false;
  197.  
  198. //当前后坐力导致的偏移长度
  199. private float _currBacklashLength = 0;
  200.  
  201. //握把位置
  202. private Vector2 _gripPoint;
  203.  
  204. //持握时 Sprite 偏移
  205. private Vector2 _gripOffset;
  206.  
  207. //碰撞器位置
  208. private Vector2 _collPoint1;
  209. private Vector2 _collPoint2;
  210.  
  211. //换弹计时器
  212. private float _reloadTimer = 0;
  213. //单独换弹设置下的换弹状态, 0: 未换弹, 1: 装第一颗子弹之前, 2: 单独装弹中, 3: 单独装弹完成
  214. private byte _aloneReloadState = 3;
  215.  
  216. //单独换弹状态下是否强制结束换弹过程
  217. private bool _aloneReloadStop = false;
  218. //本次换弹已用时间
  219. private float _reloadUseTime = 0;
  220.  
  221. //是否播放过换弹完成音效
  222. private bool _playReloadFinishSoundFlag = false;
  223.  
  224. //上膛状态,-1: 等待执行自动上膛 , 0: 未上膛, 1: 上膛中, 2: 已经完成上膛
  225. private sbyte _beLoadedState = 2;
  226.  
  227. //上膛计时器
  228. private float _beLoadedStateTimer = -1;
  229.  
  230. //换弹投抛弹壳记录
  231. private bool _reloadShellFlag = false;
  232.  
  233. //抖动强度
  234. private float _shakeStrength = 0;
  235. //抖动间隔
  236. private float _shakeInterval = 1 / 60f;
  237. //抖动计时器
  238. private float _shakeTimer = 0;
  239. // ----------------------------------------------
  240. private uint _tempLayer;
  241.  
  242. private static bool _init = false;
  243. private static Dictionary<string, ExcelConfig.WeaponBase> _weaponAttributeMap =
  244. new Dictionary<string, ExcelConfig.WeaponBase>();
  245.  
  246. /// <summary>
  247. /// 初始化武器属性数据
  248. /// </summary>
  249. public static void InitWeaponAttribute()
  250. {
  251. if (_init)
  252. {
  253. return;
  254. }
  255.  
  256. _init = true;
  257. foreach (var weaponAttr in ExcelConfig.WeaponBase_List)
  258. {
  259. if (weaponAttr.Activity != null)
  260. {
  261. if (!_weaponAttributeMap.TryAdd(weaponAttr.Activity.Id, weaponAttr))
  262. {
  263. Debug.LogError("发现重复注册的武器属性: " + weaponAttr.Id);
  264. }
  265. }
  266. }
  267. }
  268. /// <summary>
  269. /// 根据 ActivityBase.Id 获取对应武器的属性数据
  270. /// </summary>
  271. public static ExcelConfig.WeaponBase GetWeaponAttribute(string itemId)
  272. {
  273. if (itemId == null)
  274. {
  275. return null;
  276. }
  277. if (_weaponAttributeMap.TryGetValue(itemId, out var attr))
  278. {
  279. return attr;
  280. }
  281.  
  282. throw new Exception($"武器'{itemId}'没有在 WeaponBase 表中配置属性数据!");
  283. }
  284. public override void OnInit()
  285. {
  286. InitWeapon(GetWeaponAttribute(ActivityBase.Id).Clone());
  287. AnimatedSprite.AnimationFinished += OnAnimatedSpriteFinished;
  288. AnimationPlayer.AnimationFinished += OnAnimationPlayerFinished;
  289. _gripPoint = AnimatedSprite.Position;
  290. _gripOffset = AnimatedSprite.Offset;
  291. _collPoint1 = Collision.Position;
  292. _collPoint2 = _collPoint1 - AnimatedSprite.Offset - AnimatedSprite.Position;
  293. AnimatedSprite.Position = Vector2.Zero;
  294. AnimatedSprite.Offset = Vector2.Zero;
  295. Collision.Position = _collPoint2;
  296. }
  297.  
  298. /// <summary>
  299. /// 初始化武器属性
  300. /// </summary>
  301. private void InitWeapon(ExcelConfig.WeaponBase attribute)
  302. {
  303. _playerWeaponAttribute = attribute;
  304. SetCurrentWeaponAttribute(attribute);
  305. if (ExcelConfig.WeaponBase_Map.TryGetValue(attribute.Id + "_ai", out var aiAttr))
  306. {
  307. _aiWeaponAttribute = aiAttr;
  308. }
  309. else
  310. {
  311. Debug.LogError("警告: 未找到 AI 武器属性: " + attribute.Id);
  312. _aiWeaponAttribute = attribute;
  313. }
  314.  
  315. if (Attribute.AmmoCapacity > Attribute.MaxAmmoCapacity)
  316. {
  317. Attribute.AmmoCapacity = Attribute.MaxAmmoCapacity;
  318. Debug.LogError("弹夹的容量不能超过弹药上限, 武器id: " + ActivityBase.Id);
  319. }
  320. //弹药量
  321. CurrAmmo = Attribute.AmmoCapacity;
  322. //剩余弹药量
  323. ResidueAmmo = Mathf.Min(Attribute.StandbyAmmoCapacity + CurrAmmo, Attribute.MaxAmmoCapacity) - CurrAmmo;
  324. }
  325.  
  326. /// <summary>
  327. /// 单次开火时调用的函数
  328. /// </summary>
  329. protected abstract void OnFire();
  330.  
  331. /// <summary>
  332. /// 发射子弹时调用的函数, 每发射一枚子弹调用一次,
  333. /// 如果做霰弹武器效果, 一次开火发射5枚子弹, 则该函数调用5次
  334. /// </summary>
  335. /// <param name="fireRotation">开火时枪口旋转角度, 弧度制</param>
  336. protected abstract void OnShoot(float fireRotation);
  337.  
  338. /// <summary>
  339. /// 上膛开始时调用
  340. /// </summary>
  341. protected virtual void OnBeginBeLoaded()
  342. {
  343. }
  344. /// <summary>
  345. /// 上膛结束时调用
  346. /// </summary>
  347. protected virtual void OnBeLoadedFinish()
  348. {
  349. }
  350. /// <summary>
  351. /// 当按下扳机时调用
  352. /// </summary>
  353. protected virtual void OnDownTrigger()
  354. {
  355. }
  356.  
  357. /// <summary>
  358. /// 当松开扳机时调用
  359. /// </summary>
  360. protected virtual void OnUpTrigger()
  361. {
  362. }
  363.  
  364. /// <summary>
  365. /// 开始蓄力时调用,
  366. /// 注意, 该函数仅在 Attribute.LooseShoot == false 时才能被调用
  367. /// </summary>
  368. protected virtual void OnBeginCharge()
  369. {
  370. }
  371.  
  372. /// <summary>
  373. /// 结束蓄力时调用
  374. /// 注意, 该函数仅在 Attribute.LooseShoot == false 时才能被调用
  375. /// </summary>
  376. protected virtual void OnEndCharge()
  377. {
  378. }
  379. /// <summary>
  380. /// 蓄力完成时调用
  381. /// </summary>
  382. protected virtual void OnChargeFinish()
  383. {
  384. }
  385. /// <summary>
  386. /// 蓄力时每帧调用
  387. /// 注意, 该函数仅在 Attribute.LooseShoot == false 时才能被调用
  388. /// </summary>
  389. /// <param name="delta"></param>
  390. /// <param name="charge">蓄力时长</param>
  391. protected virtual void OnChargeProcess(float delta, float charge)
  392. {
  393. }
  394.  
  395. /// <summary>
  396. /// 当换弹时调用, 如果设置单独装弹, 则每装一次弹调用一次该函数
  397. /// </summary>
  398. protected virtual void OnReload()
  399. {
  400. }
  401.  
  402. /// <summary>
  403. /// 当开始换弹时调用
  404. /// </summary>
  405. protected virtual void OnBeginReload()
  406. {
  407. }
  408. /// <summary>
  409. /// 当换弹完成时调用
  410. /// </summary>
  411. protected virtual void OnReloadFinish()
  412. {
  413. }
  414.  
  415. /// <summary>
  416. /// 单独装弹完成时调用
  417. /// </summary>
  418. protected virtual void OnAloneReloadStateFinish()
  419. {
  420. }
  421. /// <summary>
  422. /// 当武器被拾起时调用
  423. /// </summary>
  424. /// <param name="master">拾起该武器的角色</param>
  425. protected virtual void OnPickUp(Role master)
  426. {
  427. }
  428.  
  429. /// <summary>
  430. /// 当武器从武器背包中移除时调用
  431. /// </summary>
  432. /// <param name="master">移除该武器的角色</param>
  433. protected virtual void OnRemove(Role master)
  434. {
  435. }
  436.  
  437. /// <summary>
  438. /// 当武器被激活时调用, 也就是使用当武器时调用
  439. /// </summary>
  440. protected virtual void OnActive()
  441. {
  442. }
  443.  
  444. /// <summary>
  445. /// 当武器被收起时调用
  446. /// </summary>
  447. protected virtual void OnConceal()
  448. {
  449. }
  450.  
  451. /// <summary>
  452. /// 射击时调用, 返回消耗弹药数量, 默认为1, 如果返回为 0, 则不消耗弹药
  453. /// </summary>
  454. protected virtual int UseAmmoCount()
  455. {
  456. return 1;
  457. }
  458.  
  459. public override void EnterTree()
  460. {
  461. //收集落在地上的武器
  462. if (IsInGround())
  463. {
  464. World.Weapon_UnclaimedWeapons.Add(this);
  465. }
  466. }
  467.  
  468. public override void ExitTree()
  469. {
  470. World.Weapon_UnclaimedWeapons.Remove(this);
  471. }
  472.  
  473. protected override void Process(float delta)
  474. {
  475. //抖动 AnimatedSprite
  476. if (_shakeStrength != 0)
  477. {
  478. _shakeTimer += delta;
  479. if (_shakeTimer >= _shakeInterval)
  480. {
  481. _shakeTimer = 0;
  482. AnimatedSprite.Offset = new Vector2(
  483. Utils.Random.RandomRangeFloat(-_shakeStrength, _shakeStrength),
  484. Utils.Random.RandomRangeFloat(-_shakeStrength, _shakeStrength)
  485. );
  486. }
  487. }
  488. else
  489. {
  490. _shakeTimer = 0;
  491. }
  492. //未开火时间
  493. _noAttackTime += delta;
  494. //这把武器被扔在地上, 或者当前武器没有被使用
  495. //if (Master == null || Master.WeaponPack.ActiveItem != this)
  496. if ((Master != null && Master.WeaponPack.ActiveItem != this) || !_triggerRoleFlag) //在背上, 或者被扔出去了
  497. {
  498. //_triggerTimer
  499. _triggerTimer = _triggerTimer > 0 ? _triggerTimer - delta : 0;
  500. //攻击冷却计时
  501. _attackTimer = _attackTimer > 0 ? _attackTimer - delta : 0;
  502. //武器的当前散射半径
  503. ScatteringRangeBackHandler(delta);
  504. //武器当前射速
  505. FiringSpeedBackHandler(delta);
  506. //松开扳机
  507. if (_triggerFlag || _downTimer > 0)
  508. {
  509. UpTrigger();
  510. _downTimer = 0;
  511. }
  512. _triggerFlag = false;
  513.  
  514. //重置数据
  515. if (_dirtyFlag)
  516. {
  517. _dirtyFlag = false;
  518. //_aloneReloadState = 0;
  519. StopReload();
  520. _attackFlag = false;
  521. _continuousCount = 0;
  522. _delayedTime = 0;
  523. _upTimer = 0;
  524. _looseShootFlag = false;
  525. _chargeTime = 0;
  526. _beLoadedStateTimer = -1;
  527. }
  528. }
  529. else //正在使用中
  530. {
  531. _dirtyFlag = true;
  532. //上膛
  533. if (_beLoadedState == 1)
  534. {
  535. _beLoadedStateTimer -= delta;
  536. //上膛完成
  537. if (_beLoadedStateTimer <= 0)
  538. {
  539. _beLoadedStateTimer = -1;
  540. _beLoadedState = 2;
  541. OnBeLoadedFinish();
  542. }
  543. }
  544. //换弹
  545. if (Reloading)
  546. {
  547. //换弹用时
  548. _reloadUseTime += delta;
  549. _reloadTimer -= delta;
  550.  
  551. if (Attribute.AloneReload) //单独装弹模式
  552. {
  553. switch (_aloneReloadState)
  554. {
  555. case 0:
  556. Debug.LogError("AloneReload状态错误!");
  557. break;
  558. case 1: //装第一颗子弹之前
  559. {
  560. if (_reloadTimer <= 0)
  561. {
  562. //开始装第一颗子弹
  563. _aloneReloadState = 2;
  564. ReloadHandler();
  565. }
  566. _aloneReloadStop = false;
  567. }
  568. break;
  569. case 2: //单独装弹中
  570. {
  571. if (_reloadTimer <= 0)
  572. {
  573. ReloadSuccess();
  574. if (_aloneReloadStop || ResidueAmmo == 0 || CurrAmmo == Attribute.AmmoCapacity) //单独装弹完成
  575. {
  576. AloneReloadStateFinish();
  577. if (Attribute.AloneReloadFinishIntervalTime <= 0)
  578. {
  579. //换弹完成
  580. StopReload();
  581. ReloadFinishHandler();
  582. }
  583. else
  584. {
  585. _reloadTimer = Attribute.AloneReloadFinishIntervalTime;
  586. _aloneReloadState = 3;
  587. }
  588. }
  589. }
  590. }
  591. break;
  592. case 3: //单独装弹完成
  593. {
  594. //播放换弹完成音效
  595. if (!_playReloadFinishSoundFlag && Attribute.ReloadFinishSound != null && _reloadTimer <= Attribute.ReloadFinishSoundAdvanceTime)
  596. {
  597. _playReloadFinishSoundFlag = true;
  598. // Debug.Log("播放换弹完成音效.");
  599. PlayReloadFinishSound();
  600. }
  601. if (_reloadTimer <= 0)
  602. {
  603. //换弹完成
  604. StopReload();
  605. ReloadFinishHandler();
  606. }
  607. _aloneReloadStop = false;
  608. }
  609. break;
  610. }
  611. }
  612. else //普通换弹模式
  613. {
  614. //播放换弹完成音效
  615. if (!_playReloadFinishSoundFlag && Attribute.ReloadFinishSound != null && _reloadTimer <= Attribute.ReloadFinishSoundAdvanceTime)
  616. {
  617. _playReloadFinishSoundFlag = true;
  618. // Debug.Log("播放换弹完成音效.");
  619. PlayReloadFinishSound();
  620. }
  621.  
  622. if (_reloadTimer <= 0)
  623. {
  624. ReloadSuccess();
  625. }
  626. }
  627. }
  628.  
  629. //攻击的计时器
  630. if (_attackTimer > 0)
  631. {
  632. _attackTimer -= delta;
  633. if (_attackTimer < 0)
  634. {
  635. _delayedTime += _attackTimer;
  636. _attackTimer = 0;
  637. //枪口默认角度
  638. if (Master != null)
  639. {
  640. RotationDegrees = -Attribute.DefaultAngle;
  641. }
  642.  
  643. //自动上膛
  644. if (_beLoadedState == -1)
  645. {
  646. BeLoaded();
  647. }
  648. //子弹换弹
  649. if (CurrAmmo <= 0 && Attribute.AutoReload)
  650. {
  651. Reload();
  652. }
  653. }
  654. }
  655. else if (_delayedTime > 0) //攻击延时
  656. {
  657. _delayedTime -= delta;
  658. if (_attackTimer < 0)
  659. {
  660. _delayedTime = 0;
  661. }
  662. }
  663. //扳机判定
  664. if (_triggerFlag)
  665. {
  666. if (_looseShootFlag) //蓄力时长
  667. {
  668. var pv = _chargeTime;
  669. _chargeTime += delta;
  670. OnChargeProcess(delta, _chargeTime);
  671. //蓄力完成
  672. if (pv < Attribute.MinChargeTime && _chargeTime >= Attribute.MinChargeTime)
  673. {
  674. OnChargeFinish();
  675. }
  676. }
  677.  
  678. _downTimer += delta;
  679. if (_upTimer > 0) //第一帧按下扳机
  680. {
  681. DownTrigger();
  682. _upTimer = 0;
  683. }
  684. }
  685. else
  686. {
  687. _upTimer += delta;
  688. if (_downTimer > 0) //第一帧松开扳机
  689. {
  690. UpTrigger();
  691. _downTimer = 0;
  692. }
  693. }
  694.  
  695. //连发判断
  696. if (!_looseShootFlag && _continuousCount > 0 && _delayedTime <= 0 && _attackTimer <= 0)
  697. {
  698. //连发开火
  699. TriggerFire();
  700. //连发最后一发打完了
  701. if (Attribute.ManualBeLoaded && _continuousCount <= 0)
  702. {
  703. //执行上膛逻辑
  704. RunBeLoaded();
  705. }
  706. }
  707.  
  708. //散射值消退
  709. if (_noAttackTime >= Attribute.ScatteringRangeBackDelayTime)
  710. {
  711. ScatteringRangeBackHandler(delta);
  712. }
  713.  
  714. if (_triggerFlag) //射速增加
  715. {
  716. FiringSpeedAddHandler(delta);
  717. }
  718. else if (_noAttackTime >= Attribute.FiringSpeedBackTime) //射速消退
  719. {
  720. FiringSpeedBackHandler(delta);
  721. }
  722.  
  723. _triggerTimer = _triggerTimer > 0 ? _triggerTimer - delta : 0;
  724. _triggerFlag = false;
  725. _attackFlag = false;
  726. //武器身回归
  727. //Position = Position.MoveToward(Vector2.Zero, Attribute.BacklashRegressionSpeed * delta).Rotated(Rotation);
  728. _currBacklashLength = Mathf.MoveToward(_currBacklashLength, 0, Attribute.BacklashRegressionSpeed * delta);
  729. if (Master != null)
  730. {
  731. Position = new Vector2(_currBacklashLength, 0).Rotated(Rotation);
  732. if (_attackTimer > 0)
  733. {
  734. RotationDegrees = Mathf.Lerp(
  735. _fireAngle, -Attribute.DefaultAngle,
  736. Mathf.Clamp((_fireInterval - _attackTimer) * Attribute.UpliftAngleRestore / _fireInterval, 0, 1)
  737. );
  738. }
  739. }
  740. }
  741. }
  742.  
  743. /// <summary>
  744. /// 设置抖动强度
  745. /// </summary>
  746. public void SetShake(float strength)
  747. {
  748. _shakeStrength = strength;
  749. }
  750. /// <summary>
  751. /// 设置抖动间隔
  752. /// </summary>
  753. public void SetShakeInterval(float interval)
  754. {
  755. _shakeInterval = interval;
  756. }
  757.  
  758. /// <summary>
  759. /// 返回武器是否在地上
  760. /// </summary>
  761. /// <returns></returns>
  762. public bool IsInGround()
  763. {
  764. return Master == null && GetParent() == World.Current.NormalLayer;
  765. }
  766.  
  767. /// <summary>
  768. /// 扳机函数, 调用即视为按下扳机
  769. /// </summary>
  770. /// <param name="triggerRole">按下扳机的角色, 如果传 null, 则视为走火</param>
  771. public void Trigger(Role triggerRole)
  772. {
  773. //不能触发扳机
  774. if (!NoMasterCanTrigger && Master == null) return;
  775.  
  776. //这一帧已经按过了, 不需要再按下
  777. if (_triggerFlag) return;
  778.  
  779. //更新武器属性信息
  780. _triggerFlag = true;
  781. _triggerRoleFlag = true;
  782. if (triggerRole != null)
  783. {
  784. TriggerRole = triggerRole;
  785. TriggerRoleAttackLayer = triggerRole.AttackLayer;
  786. SetCurrentWeaponAttribute(triggerRole.IsAi ? _aiWeaponAttribute : _playerWeaponAttribute);
  787. }
  788. else if (Master != null)
  789. {
  790. TriggerRole = Master;
  791. TriggerRoleAttackLayer = Master.AttackLayer;
  792. SetCurrentWeaponAttribute(Master.IsAi ? _aiWeaponAttribute : _playerWeaponAttribute);
  793. }
  794.  
  795. //是否第一帧按下
  796. var justDown = _downTimer == 0;
  797. if (_beLoadedState == 0 || _beLoadedState == -1) //需要执行上膛操作
  798. {
  799. if (justDown && !Reloading && triggerRole != null)
  800. {
  801. if (CurrAmmo <= 0)
  802. {
  803. //子弹不够, 触发换弹
  804. Reload();
  805. }
  806. else if (_attackTimer <= 0)
  807. {
  808. //触发上膛操作
  809. BeLoaded();
  810. }
  811. }
  812. }
  813. else if (_beLoadedState == 1) //上膛中
  814. {
  815.  
  816. }
  817. else //上膛完成
  818. {
  819. //是否能发射
  820. var flag = false;
  821. if (_continuousCount <= 0) //不能处于连发状态下
  822. {
  823. if (Attribute.ContinuousShoot) //自动射击
  824. {
  825. if (_triggerTimer > 0)
  826. {
  827. if (_continuousShootFlag)
  828. {
  829. flag = true;
  830. }
  831. }
  832. else
  833. {
  834. flag = true;
  835. if (_delayedTime <= 0 && _attackTimer <= 0)
  836. {
  837. _continuousShootFlag = true;
  838. }
  839. }
  840. }
  841. else //半自动
  842. {
  843. if (justDown && _triggerTimer <= 0 && _attackTimer <= 0)
  844. {
  845. flag = true;
  846. }
  847. }
  848. }
  849.  
  850. if (flag)
  851. {
  852. var fireFlag = true; //是否能开火
  853. if (Reloading) //换弹中
  854. {
  855. fireFlag = false;
  856. if (CurrAmmo > 0 && Attribute.AloneReload && Attribute.AloneReloadCanShoot)
  857. {
  858. //检查是否允许停止换弹
  859. if (_aloneReloadState == 2 || _aloneReloadState == 1)
  860. {
  861. //强制结束
  862. _aloneReloadStop = true;
  863. }
  864. }
  865. }
  866. else if (CurrAmmo <= 0) //子弹不够
  867. {
  868. fireFlag = false;
  869. if (justDown && triggerRole != null)
  870. {
  871. //第一帧按下, 触发换弹
  872. Reload();
  873. }
  874. }
  875.  
  876. if (fireFlag)
  877. {
  878. if (justDown)
  879. {
  880. //开火前延时
  881. if (!Attribute.LooseShoot)
  882. {
  883. _delayedTime = Attribute.DelayedTime;
  884. }
  885.  
  886. //扳机按下间隔
  887. _triggerTimer = Attribute.TriggerInterval;
  888. //连发数量
  889. if (!Attribute.ContinuousShoot)
  890. {
  891. _continuousCount = Utils.Random.RandomConfigRange(Attribute.ContinuousCountRange);
  892. }
  893. }
  894.  
  895. if (_delayedTime <= 0 && _attackTimer <= 0)
  896. {
  897. if (Attribute.LooseShoot) //松发开火
  898. {
  899. _looseShootFlag = true;
  900. OnBeginCharge();
  901. }
  902. else
  903. {
  904. //开火
  905. TriggerFire();
  906.  
  907. //非连射模式
  908. if (!Attribute.ContinuousShoot && Attribute.ManualBeLoaded && _continuousCount <= 0)
  909. {
  910. //执行上膛逻辑
  911. RunBeLoaded();
  912. }
  913. }
  914. }
  915. }
  916. }
  917. else //不能开火
  918. {
  919. if (CurrAmmo <= 0 && justDown && triggerRole != null) //子弹不够
  920. {
  921. //第一帧按下, 触发换弹
  922. Reload();
  923. }
  924. }
  925. }
  926. }
  927.  
  928. /// <summary>
  929. /// 返回是否按下扳机
  930. /// </summary>
  931. public bool IsPressTrigger()
  932. {
  933. return _triggerFlag;
  934. }
  935. /// <summary>
  936. /// 获取本次扳机按下的时长, 单位: 秒
  937. /// </summary>
  938. public float GetTriggerDownTime()
  939. {
  940. return _downTimer;
  941. }
  942.  
  943. /// <summary>
  944. /// 获取扳机蓄力时长, 计算按下扳机后从可以开火到当前一共经过了多长时间, 可用于计算蓄力攻击
  945. /// 注意, 该函数仅在 Attribute.LooseShoot == false 时有正确的返回值, 否则返回 0
  946. /// </summary>
  947. public float GetTriggerChargeTime()
  948. {
  949. return _chargeTime;
  950. }
  951. /// <summary>
  952. /// 获取蓄力是否结束
  953. /// 注意, 该函数仅在 Attribute.LooseShoot == false 时有正确的返回值, 否则返回 false
  954. /// </summary>
  955. public bool IsChargeFinish()
  956. {
  957. return _chargeTime >= Attribute.MinChargeTime;
  958. }
  959. /// <summary>
  960. /// 获取延时射击倒计时, 单位: 秒
  961. /// </summary>
  962. public float GetDelayedAttackTime()
  963. {
  964. return _delayedTime;
  965. }
  966.  
  967. /// <summary>
  968. /// 获取当前需要连发弹药的数量, 配置了 ContinuousCountRange 时生效
  969. /// </summary>
  970. public int GetContinuousCount()
  971. {
  972. return _continuousCount;
  973. }
  974.  
  975. /// <summary>
  976. /// 获取攻击冷却计时时间, 小于等于 0 表示冷却完成
  977. /// </summary>
  978. public float GetAttackTimer()
  979. {
  980. return _attackTimer;
  981. }
  982.  
  983. /// <summary>
  984. /// 返回是否是攻击间隙时间
  985. /// </summary>
  986. public bool IsAttackIntervalTime()
  987. {
  988. return _attackTimer > 0 || _triggerTimer > 0;
  989. }
  990.  
  991. /// <summary>
  992. /// 是否可以按下扳机并发射了
  993. /// </summary>
  994. public bool TriggerIsReady()
  995. {
  996. return GetBeLoadedStateState() >= 2 && !IsAttackIntervalTime();
  997. }
  998.  
  999. /// <summary>
  1000. /// 获取上膛状态,-1: 等待执行自动上膛 , 0: 未上膛, 1: 上膛中, 2: 已经完成上膛
  1001. /// </summary>
  1002. public sbyte GetBeLoadedStateState()
  1003. {
  1004. return _beLoadedState;
  1005. }
  1006. /// <summary>
  1007. /// 刚按下扳机
  1008. /// </summary>
  1009. private void DownTrigger()
  1010. {
  1011. OnDownTrigger();
  1012. }
  1013.  
  1014. /// <summary>
  1015. /// 刚松开扳机
  1016. /// </summary>
  1017. private void UpTrigger()
  1018. {
  1019. _continuousShootFlag = false;
  1020. if (_delayedTime > 0)
  1021. {
  1022. _continuousCount = 0;
  1023. }
  1024.  
  1025. //松发开火执行
  1026. if (_looseShootFlag)
  1027. {
  1028. _looseShootFlag = false;
  1029. if (_chargeTime >= Attribute.MinChargeTime) //判断蓄力是否够了
  1030. {
  1031. TriggerFire();
  1032. //非连射模式
  1033. if (!Attribute.ContinuousShoot && Attribute.ManualBeLoaded && _continuousCount <= 0)
  1034. {
  1035. //执行上膛逻辑
  1036. RunBeLoaded();
  1037. }
  1038. }
  1039. else //不能攻击
  1040. {
  1041. _continuousCount = 0;
  1042. }
  1043. _chargeTime = 0;
  1044. OnEndCharge();
  1045. }
  1046.  
  1047. OnUpTrigger();
  1048. }
  1049.  
  1050. /// <summary>
  1051. /// 触发开火
  1052. /// </summary>
  1053. private void TriggerFire()
  1054. {
  1055. _attackFlag = true;
  1056. _noAttackTime = 0;
  1057. _reloadShellFlag = false;
  1058.  
  1059. //减子弹数量
  1060. if (_playerWeaponAttribute != _weaponAttribute) //Ai使用该武器, 有一定概率不消耗弹药
  1061. {
  1062. var count = UseAmmoCount();
  1063. CurrAmmo -= count;
  1064. if (Utils.Random.RandomRangeFloat(0, 1) >= _weaponAttribute.AiAttackAttr.AmmoConsumptionProbability) //不消耗弹药
  1065. {
  1066. ResidueAmmo += count;
  1067. }
  1068. }
  1069. else
  1070. {
  1071. CurrAmmo -= UseAmmoCount();
  1072. }
  1073.  
  1074. if (CurrAmmo == 0)
  1075. {
  1076. _continuousCount = 0;
  1077. }
  1078. else
  1079. {
  1080. _continuousCount = _continuousCount > 0 ? _continuousCount - 1 : 0;
  1081. }
  1082.  
  1083. //开火间隙, 这里的60指的是60秒
  1084. _fireInterval = 60 / CurrentFiringSpeed;
  1085. //攻击冷却
  1086. _attackTimer += _fireInterval;
  1087.  
  1088. //播放开火动画
  1089. if (IsAutoPlaySpriteFrames)
  1090. {
  1091. PlaySpriteAnimation(AnimatorNames.Fire);
  1092. PlayAnimationPlayer(AnimatorNames.Fire);
  1093. }
  1094.  
  1095. //播放射击音效
  1096. PlayShootSound();
  1097. //抛弹
  1098. if (!Attribute.ReloadThrowShell && (Attribute.ContinuousShoot || !Attribute.ManualBeLoaded))
  1099. {
  1100. ThrowShellHandler(1f);
  1101. }
  1102. //触发开火函数
  1103. OnFire();
  1104.  
  1105.  
  1106. //武器口角度
  1107. var angle = new Vector2(GameConfig.ScatteringDistance, CurrScatteringRange).Angle();
  1108.  
  1109. //先算武器口方向
  1110. var tempRotation = Utils.Random.RandomRangeFloat(-angle, angle);
  1111. var tempAngle = Mathf.RadToDeg(tempRotation);
  1112.  
  1113. //开火时枪口角度
  1114. var fireRotation = tempRotation;
  1115. //开火发射的子弹数量
  1116. var bulletCount = Utils.Random.RandomConfigRange(Attribute.FireBulletCountRange);
  1117. if (Master != null)
  1118. {
  1119. bulletCount = Master.RoleState.CalcBulletCount(bulletCount);
  1120. fireRotation += Master.MountPoint.RealRotation;
  1121. }
  1122. else
  1123. {
  1124. fireRotation += GlobalRotation;
  1125. }
  1126.  
  1127. //创建子弹
  1128. for (var i = 0; i < bulletCount; i++)
  1129. {
  1130. //发射子弹
  1131. OnShoot(fireRotation);
  1132. }
  1133.  
  1134. //开火添加散射值
  1135. ScatteringRangeAddHandler();
  1136. //武器的旋转角度
  1137. tempAngle -= Attribute.UpliftAngle;
  1138. _fireAngle = tempAngle;
  1139. if (Master != null) //被拾起
  1140. {
  1141. //武器身位置
  1142. var max = Mathf.Abs(Mathf.Max(Utils.GetConfigRangeStart(Attribute.BacklashRange), Utils.GetConfigRangeEnd(Attribute.BacklashRange)));
  1143. _currBacklashLength = Mathf.Clamp(
  1144. _currBacklashLength - Utils.Random.RandomConfigRange(Attribute.BacklashRange),
  1145. -max, max
  1146. );
  1147. Position = new Vector2(_currBacklashLength, 0).Rotated(Rotation);
  1148. RotationDegrees = tempAngle;
  1149. }
  1150. else //在地上
  1151. {
  1152. var v = Utils.Random.RandomConfigRange(Attribute.BacklashRange) * 5;
  1153. var externalForce = MoveController.AddForce(new Vector2(-v, 0).Rotated(Rotation));
  1154. externalForce.RotationSpeed = -Mathf.DegToRad(40);
  1155. externalForce.RotationResistance = Mathf.DegToRad(80);
  1156. }
  1157.  
  1158. if (IsInGround())
  1159. {
  1160. //在地上弹药打光
  1161. if (IsTotalAmmoEmpty())
  1162. {
  1163. //停止动画
  1164. AnimationPlayer.Stop();
  1165. //清除泛白效果
  1166. SetBlendSchedule(0);
  1167. }
  1168. }
  1169. }
  1170.  
  1171. // /// <summary>
  1172. // /// 触发武器的近战攻击
  1173. // /// </summary>
  1174. // public virtual void TriggerMeleeAttack(Role trigger)
  1175. // {
  1176. //
  1177. // }
  1178.  
  1179. /// <summary>
  1180. /// 根据触扳机的角色对象判断该角色使用的武器数据
  1181. /// </summary>
  1182. public ExcelConfig.WeaponBase GetUseAttribute(Role triggerRole)
  1183. {
  1184. if (triggerRole == null || !triggerRole.IsAi)
  1185. {
  1186. return PlayerUseAttribute;
  1187. }
  1188.  
  1189. return AiUseAttribute;
  1190. }
  1191. /// <summary>
  1192. /// 获取武器攻击的目标层级
  1193. /// </summary>
  1194. /// <returns></returns>
  1195. public uint GetAttackLayer()
  1196. {
  1197. if (TriggerRoleAttackLayer > 0)
  1198. {
  1199. return (uint)TriggerRoleAttackLayer;
  1200. }
  1201. return Master != null ? Master.AttackLayer : Role.DefaultAttackLayer;
  1202. }
  1203. /// <summary>
  1204. /// 返回弹药是否到达上限
  1205. /// </summary>
  1206. public bool IsAmmoFull()
  1207. {
  1208. return CurrAmmo + ResidueAmmo >= Attribute.MaxAmmoCapacity;
  1209. }
  1210.  
  1211. /// <summary>
  1212. /// 返回弹夹是否打空
  1213. /// </summary>
  1214. public bool IsAmmoEmpty()
  1215. {
  1216. return CurrAmmo == 0;
  1217. }
  1218. /// <summary>
  1219. /// 返回是否弹药耗尽
  1220. /// </summary>
  1221. public bool IsTotalAmmoEmpty()
  1222. {
  1223. return CurrAmmo + ResidueAmmo == 0;
  1224. }
  1225.  
  1226. /// <summary>
  1227. /// 强制修改当前弹夹弹药量
  1228. /// </summary>
  1229. public void SetCurrAmmo(int count)
  1230. {
  1231. CurrAmmo = Mathf.Clamp(count, 0, Attribute.AmmoCapacity);
  1232. }
  1233.  
  1234. /// <summary>
  1235. /// 强制修改备用弹药量
  1236. /// </summary>
  1237. public void SetResidueAmmo(int count)
  1238. {
  1239. ResidueAmmo = Mathf.Clamp(count, 0, Attribute.MaxAmmoCapacity - CurrAmmo);
  1240. }
  1241. /// <summary>
  1242. /// 强制修改弹药量, 优先改动备用弹药
  1243. /// </summary>
  1244. public void SetTotalAmmo(int total)
  1245. {
  1246. if (total < 0)
  1247. {
  1248. return;
  1249. }
  1250. var totalAmmo = CurrAmmo + ResidueAmmo;
  1251. if (totalAmmo == total)
  1252. {
  1253. return;
  1254. }
  1255. if (total > totalAmmo) //弹药增加
  1256. {
  1257. ResidueAmmo = Mathf.Min(total - CurrAmmo, Attribute.MaxAmmoCapacity - CurrAmmo);
  1258. }
  1259. else //弹药减少
  1260. {
  1261. if (CurrAmmo < total)
  1262. {
  1263. ResidueAmmo = total - CurrAmmo;
  1264. }
  1265. else
  1266. {
  1267. CurrAmmo = total;
  1268. ResidueAmmo = 0;
  1269. }
  1270. }
  1271. }
  1272.  
  1273. /// <summary>
  1274. /// 拾起的弹药数量, 如果到达容量上限, 则返回拾取完毕后剩余的弹药数量
  1275. /// </summary>
  1276. /// <param name="count">弹药数量</param>
  1277. private int PickUpAmmo(int count)
  1278. {
  1279. var num = ResidueAmmo;
  1280. ResidueAmmo = Mathf.Min(ResidueAmmo + count, Attribute.MaxAmmoCapacity - CurrAmmo);
  1281. return count - ResidueAmmo + num;
  1282. }
  1283.  
  1284. /// <summary>
  1285. /// 触发换弹
  1286. /// </summary>
  1287. public void Reload()
  1288. {
  1289. if (!Reloading && CurrAmmo < Attribute.AmmoCapacity && ResidueAmmo > 0 && _beLoadedState != 1)
  1290. {
  1291. Reloading = true;
  1292. _playReloadFinishSoundFlag = false;
  1293.  
  1294. //播放开始换弹音效
  1295. PlayBeginReloadSound();
  1296. // Debug.Log("开始换弹.");
  1297. //抛弹
  1298. if (!Attribute.ReloadThrowShell && !Attribute.ContinuousShoot &&
  1299. (_beLoadedState == 0 || _beLoadedState == -1) && Attribute.BeLoadedTime > 0)
  1300. {
  1301. ThrowShellHandler(0.6f);
  1302. }
  1303. //第一次换弹
  1304. OnBeginReload();
  1305.  
  1306. if (Attribute.AloneReload)
  1307. {
  1308. //单独换弹, 特殊处理
  1309. AloneReloadHandler();
  1310. }
  1311. else
  1312. {
  1313. //普通换弹处理
  1314. ReloadHandler();
  1315. }
  1316. //上膛标记完成
  1317. _beLoadedState = 2;
  1318. }
  1319. }
  1320.  
  1321. /// <summary>
  1322. /// 强制停止换弹, 或者结束换弹状态
  1323. /// </summary>
  1324. public void StopReload()
  1325. {
  1326. _aloneReloadState = 0;
  1327. if (_beLoadedState == -1)
  1328. {
  1329. _beLoadedState = 0;
  1330. }
  1331. else if (_beLoadedState == 1)
  1332. {
  1333. _beLoadedState = 2;
  1334. }
  1335.  
  1336. Reloading = false;
  1337. _reloadTimer = 0;
  1338. _reloadUseTime = 0;
  1339. }
  1340.  
  1341. /// <summary>
  1342. /// 触发上膛
  1343. /// </summary>
  1344. public void BeLoaded()
  1345. {
  1346. if (_beLoadedState > 0)
  1347. {
  1348. return;
  1349. }
  1350. //上膛抛弹
  1351. if (!Attribute.ReloadThrowShell && !Attribute.ContinuousShoot && Attribute.BeLoadedTime > 0)
  1352. {
  1353. ThrowShellHandler(0.6f);
  1354. }
  1355.  
  1356. //开始上膛
  1357. OnBeginBeLoaded();
  1358.  
  1359. //上膛时间为0, 直接结束上膛
  1360. if (Attribute.BeLoadedTime <= 0)
  1361. {
  1362. //直接上膛完成
  1363. _beLoadedState = 2;
  1364. OnBeLoadedFinish();
  1365. return;
  1366. }
  1367. //上膛中
  1368. _beLoadedState = 1;
  1369. _beLoadedStateTimer = Attribute.BeLoadedTime;
  1370. //播放上膛动画
  1371. if (IsAutoPlaySpriteFrames)
  1372. {
  1373. if (Attribute.BeLoadedSoundDelayTime <= 0)
  1374. {
  1375. PlaySpriteAnimation(AnimatorNames.BeLoaded);
  1376. PlayAnimationPlayer(AnimatorNames.BeLoaded);
  1377. }
  1378. else
  1379. {
  1380. this.CallDelay(Attribute.BeLoadedSoundDelayTime, () =>
  1381. {
  1382. PlaySpriteAnimation(AnimatorNames.BeLoaded);
  1383. PlayAnimationPlayer(AnimatorNames.BeLoaded);
  1384. });
  1385. }
  1386. }
  1387.  
  1388. //播放上膛音效
  1389. PlayBeLoadedSound();
  1390. }
  1391. //播放换弹开始音效
  1392. private void PlayBeginReloadSound()
  1393. {
  1394. if (Attribute.BeginReloadSound != null)
  1395. {
  1396. if (Attribute.BeginReloadSoundDelayTime <= 0)
  1397. {
  1398. SoundManager.PlaySoundByConfig(Attribute.BeginReloadSound, GlobalPosition, TriggerRole);
  1399. }
  1400. else
  1401. {
  1402. SoundManager.PlaySoundByConfigDelay(Attribute.BeginReloadSound, GlobalPosition, Attribute.BeginReloadSoundDelayTime, TriggerRole);
  1403. }
  1404. }
  1405. }
  1406. //播放换弹音效
  1407. private void PlayReloadSound()
  1408. {
  1409. if (Attribute.ReloadSound != null)
  1410. {
  1411. if (Attribute.ReloadSoundDelayTime <= 0)
  1412. {
  1413. SoundManager.PlaySoundByConfig(Attribute.ReloadSound, GlobalPosition, TriggerRole);
  1414. }
  1415. else
  1416. {
  1417. SoundManager.PlaySoundByConfigDelay(Attribute.ReloadSound, GlobalPosition, Attribute.ReloadSoundDelayTime, TriggerRole);
  1418. }
  1419. }
  1420. }
  1421. //播放换弹完成音效
  1422. private void PlayReloadFinishSound()
  1423. {
  1424. if (Attribute.ReloadFinishSound != null)
  1425. {
  1426. SoundManager.PlaySoundByConfig(Attribute.ReloadFinishSound, GlobalPosition, TriggerRole);
  1427. }
  1428. }
  1429.  
  1430. //播放射击音效
  1431. private void PlayShootSound()
  1432. {
  1433. if (Attribute.ShootSound != null)
  1434. {
  1435. SoundManager.PlaySoundByConfig(Attribute.ShootSound, GlobalPosition, TriggerRole);
  1436. }
  1437. }
  1438.  
  1439. //播放上膛音效
  1440. private void PlayBeLoadedSound()
  1441. {
  1442. if (Attribute.BeLoadedSound != null)
  1443. {
  1444. if (Attribute.BeLoadedSoundDelayTime <= 0)
  1445. {
  1446. SoundManager.PlaySoundByConfig(Attribute.BeLoadedSound, GlobalPosition, TriggerRole);
  1447. }
  1448. else
  1449. {
  1450. SoundManager.PlaySoundByConfigDelay(Attribute.BeLoadedSound, GlobalPosition, Attribute.BeLoadedSoundDelayTime, TriggerRole);
  1451. }
  1452. }
  1453. }
  1454.  
  1455. //执行上膛逻辑
  1456. private void RunBeLoaded()
  1457. {
  1458. if (Attribute.AutoManualBeLoaded)
  1459. {
  1460. if (_attackTimer <= 0)
  1461. {
  1462. //执行自动上膛
  1463. BeLoaded();
  1464. }
  1465. else if (CurrAmmo > 0)
  1466. {
  1467. //等待执行自动上膛
  1468. _beLoadedState = -1;
  1469. }
  1470. else
  1471. {
  1472. //没子弹了, 需要手动上膛
  1473. _beLoadedState = 0;
  1474. }
  1475. }
  1476. else
  1477. {
  1478. //手动上膛
  1479. _beLoadedState = 0;
  1480. }
  1481. }
  1482.  
  1483. //单独换弹处理
  1484. private void AloneReloadHandler()
  1485. {
  1486. if (Attribute.AloneReloadBeginIntervalTime <= 0)
  1487. {
  1488. //开始装第一颗子弹
  1489. _aloneReloadState = 2;
  1490. ReloadHandler();
  1491. }
  1492. else
  1493. {
  1494. _aloneReloadState = 1;
  1495. _reloadTimer = Attribute.AloneReloadBeginIntervalTime;
  1496. }
  1497. }
  1498.  
  1499. //换弹处理逻辑
  1500. private void ReloadHandler()
  1501. {
  1502. _reloadTimer = Attribute.ReloadTime;
  1503. //播放换弹动画
  1504. if (IsAutoPlaySpriteFrames)
  1505. {
  1506. PlaySpriteAnimation(AnimatorNames.Reloading);
  1507. PlayAnimationPlayer(AnimatorNames.Reloading);
  1508. }
  1509. //播放换弹音效
  1510. PlayReloadSound();
  1511. //抛出弹壳
  1512. if (Attribute.ReloadThrowShell && !_reloadShellFlag)
  1513. {
  1514. ThrowShellHandler(0.6f);
  1515. }
  1516. OnReload();
  1517. // Debug.Log("装弹.");
  1518. }
  1519. //换弹完成处理逻辑
  1520. private void ReloadFinishHandler()
  1521. {
  1522. // Debug.Log("装弹完成.");
  1523. OnReloadFinish();
  1524. }
  1525.  
  1526. //单独装弹完成
  1527. private void AloneReloadStateFinish()
  1528. {
  1529. // Debug.Log("单独装弹完成.");
  1530. OnAloneReloadStateFinish();
  1531. }
  1532.  
  1533. //抛弹逻辑
  1534. private void ThrowShellHandler(float speedScale)
  1535. {
  1536. var attribute = Attribute;
  1537. if (attribute.Shell == null)
  1538. {
  1539. return;
  1540. }
  1541. //创建一个弹壳
  1542. if (attribute.ThrowShellDelayTime > 0)
  1543. {
  1544. this.CallDelay(attribute.ThrowShellDelayTime, () =>
  1545. {
  1546. _reloadShellFlag = true;
  1547. for (var i = 0; i < attribute.ThrowShellCount; i++)
  1548. {
  1549. FireManager.ThrowShell(this, attribute.Shell, speedScale);
  1550. }
  1551. });
  1552. }
  1553. else if (attribute.ThrowShellDelayTime == 0)
  1554. {
  1555. _reloadShellFlag = true;
  1556. for (var i = 0; i < attribute.ThrowShellCount; i++)
  1557. {
  1558. FireManager.ThrowShell(this, attribute.Shell, speedScale);
  1559. }
  1560. }
  1561. }
  1562.  
  1563. //散射值消退处理
  1564. private void ScatteringRangeBackHandler(float delta)
  1565. {
  1566. var startScatteringRange = Attribute.StartScatteringRange;
  1567. var finalScatteringRange = Attribute.FinalScatteringRange;
  1568. if (Master != null)
  1569. {
  1570. startScatteringRange = Master.RoleState.CalcStartScattering(startScatteringRange);
  1571. finalScatteringRange = Master.RoleState.CalcFinalScattering(finalScatteringRange);
  1572. }
  1573. if (startScatteringRange <= finalScatteringRange)
  1574. {
  1575. CurrScatteringRange = Mathf.Max(CurrScatteringRange - Attribute.ScatteringRangeBackSpeed * delta,
  1576. startScatteringRange);
  1577. }
  1578. else
  1579. {
  1580. CurrScatteringRange = Mathf.Min(CurrScatteringRange + Attribute.ScatteringRangeBackSpeed * delta,
  1581. startScatteringRange);
  1582. }
  1583. }
  1584.  
  1585. //散射值添加处理
  1586. private void ScatteringRangeAddHandler()
  1587. {
  1588. var startScatteringRange = Attribute.StartScatteringRange;
  1589. var finalScatteringRange = Attribute.FinalScatteringRange;
  1590. if (Master != null)
  1591. {
  1592. startScatteringRange = Master.RoleState.CalcStartScattering(startScatteringRange);
  1593. finalScatteringRange = Master.RoleState.CalcFinalScattering(finalScatteringRange);
  1594. }
  1595. if (startScatteringRange <= finalScatteringRange)
  1596. {
  1597. CurrScatteringRange = Mathf.Min(CurrScatteringRange + Attribute.ScatteringRangeAddValue,
  1598. finalScatteringRange);
  1599. }
  1600. else
  1601. {
  1602. CurrScatteringRange = Mathf.Min(CurrScatteringRange - Attribute.ScatteringRangeAddValue,
  1603. finalScatteringRange);
  1604. }
  1605. }
  1606.  
  1607. //射速增加处理
  1608. private void FiringSpeedAddHandler(float delta)
  1609. {
  1610. if (Attribute.ContinuousShoot)
  1611. {
  1612. CurrentFiringSpeed = Mathf.MoveToward(CurrentFiringSpeed, Attribute.FinalFiringSpeed,
  1613. Attribute.FiringSpeedAddSpeed * delta);
  1614. }
  1615. else
  1616. {
  1617. CurrentFiringSpeed = Attribute.StartFiringSpeed;
  1618. }
  1619. }
  1620. //射速衰减处理
  1621. private void FiringSpeedBackHandler(float delta)
  1622. {
  1623. if (Attribute.ContinuousShoot)
  1624. {
  1625. CurrentFiringSpeed = Mathf.MoveToward(CurrentFiringSpeed, Attribute.StartFiringSpeed,
  1626. Attribute.FiringSpeedBackSpeed * delta);
  1627. }
  1628. else
  1629. {
  1630. CurrentFiringSpeed = Attribute.StartFiringSpeed;
  1631. }
  1632. }
  1633.  
  1634. /// <summary>
  1635. /// 换弹计时器时间到, 执行换弹操作
  1636. /// </summary>
  1637. private void ReloadSuccess()
  1638. {
  1639. if (Attribute.AloneReload) //单独装填
  1640. {
  1641. if (ResidueAmmo >= Attribute.AloneReloadCount) //剩余子弹充足
  1642. {
  1643. if (CurrAmmo + Attribute.AloneReloadCount <= Attribute.AmmoCapacity)
  1644. {
  1645. ResidueAmmo -= Attribute.AloneReloadCount;
  1646. CurrAmmo += Attribute.AloneReloadCount;
  1647. }
  1648. else //子弹满了
  1649. {
  1650. var num = Attribute.AmmoCapacity - CurrAmmo;
  1651. CurrAmmo = Attribute.AmmoCapacity;
  1652. ResidueAmmo -= num;
  1653. }
  1654. }
  1655. else if (ResidueAmmo != 0) //剩余子弹不足
  1656. {
  1657. if (ResidueAmmo + CurrAmmo <= Attribute.AmmoCapacity)
  1658. {
  1659. CurrAmmo += ResidueAmmo;
  1660. ResidueAmmo = 0;
  1661. }
  1662. else //子弹满了
  1663. {
  1664. var num = Attribute.AmmoCapacity - CurrAmmo;
  1665. CurrAmmo = Attribute.AmmoCapacity;
  1666. ResidueAmmo -= num;
  1667. }
  1668. }
  1669.  
  1670. if (!_aloneReloadStop && ResidueAmmo != 0 && CurrAmmo != Attribute.AmmoCapacity) //继续装弹
  1671. {
  1672. ReloadHandler();
  1673. }
  1674. }
  1675. else //换弹结束
  1676. {
  1677. if (CurrAmmo + ResidueAmmo >= Attribute.AmmoCapacity)
  1678. {
  1679. ResidueAmmo -= Attribute.AmmoCapacity - CurrAmmo;
  1680. CurrAmmo = Attribute.AmmoCapacity;
  1681. }
  1682. else
  1683. {
  1684. CurrAmmo += ResidueAmmo;
  1685. ResidueAmmo = 0;
  1686. }
  1687.  
  1688. StopReload();
  1689. ReloadFinishHandler();
  1690. }
  1691. }
  1692.  
  1693. //播放动画
  1694. private void PlayAnimationPlayer(string name)
  1695. {
  1696. if (AnimationPlayer != null && AnimationPlayer.HasAnimation(name))
  1697. {
  1698. AnimationPlayer.Play(name);
  1699. }
  1700. }
  1701.  
  1702. //帧动画播放结束
  1703. private void OnAnimatedSpriteFinished()
  1704. {
  1705. // Debug.Log("帧动画播放结束...");
  1706. AnimatedSprite.Play(AnimatorNames.Default);
  1707. }
  1708.  
  1709. //动画播放器播放结束
  1710. private void OnAnimationPlayerFinished(StringName name)
  1711. {
  1712. if (Master != null && !IsActive)
  1713. {
  1714. Master.OnPutBackMount(this, PackageIndex);
  1715. }
  1716. }
  1717.  
  1718. public override CheckInteractiveResult CheckInteractive(ActivityObject master)
  1719. {
  1720. var result = new CheckInteractiveResult(this);
  1721.  
  1722. if (master is Role roleMaster) //碰到角色
  1723. {
  1724. if (Master == null)
  1725. {
  1726. if (roleMaster.WeaponPack.Capacity == 0)
  1727. {
  1728. //容量为0
  1729. return result;
  1730. }
  1731. var masterWeapon = roleMaster.WeaponPack.ActiveItem;
  1732. //查找是否有同类型武器
  1733. var index = roleMaster.WeaponPack.FindIndex(ActivityBase.Id);
  1734. if (index != -1) //如果有这个武器
  1735. {
  1736. if (CurrAmmo + ResidueAmmo != 0) //子弹不为空
  1737. {
  1738. var targetWeapon = roleMaster.WeaponPack.GetItem(index);
  1739. if (!targetWeapon.IsAmmoFull()) //背包里面的武器子弹未满
  1740. {
  1741. //可以互动拾起弹药
  1742. result.CanInteractive = true;
  1743. result.Type = CheckInteractiveResult.InteractiveType.Bullet;
  1744. return result;
  1745. }
  1746. }
  1747. }
  1748. else //没有武器
  1749. {
  1750. if (roleMaster.WeaponPack.HasVacancy()) //有空位, 能拾起武器
  1751. {
  1752. //可以互动, 拾起武器
  1753. result.CanInteractive = true;
  1754. result.Type = CheckInteractiveResult.InteractiveType.PickUp;
  1755. return result;
  1756. }
  1757. else if (masterWeapon != null) //替换武器 // && masterWeapon.Attribute.WeightType == Attribute.WeightType)
  1758. {
  1759. //可以互动, 切换武器
  1760. result.CanInteractive = true;
  1761. result.Type = CheckInteractiveResult.InteractiveType.Replace;
  1762. return result;
  1763. }
  1764. }
  1765. }
  1766. }
  1767.  
  1768. return result;
  1769. }
  1770.  
  1771. public override void Interactive(ActivityObject master)
  1772. {
  1773. if (master is Role roleMaster) //与role互动
  1774. {
  1775. if (roleMaster.WeaponPack.Capacity == 0)
  1776. {
  1777. //容量为0
  1778. return;
  1779. }
  1780. var holster = roleMaster.WeaponPack;
  1781. //查找是否有同类型武器
  1782. var index = holster.FindIndex(ActivityBase.Id);
  1783. if (index != -1) //如果有这个武器
  1784. {
  1785. if (CurrAmmo + ResidueAmmo == 0) //没有子弹了
  1786. {
  1787. return;
  1788. }
  1789.  
  1790. var weapon = holster.GetItem(index);
  1791. //子弹上限
  1792. var maxCount = Attribute.MaxAmmoCapacity;
  1793. //是否捡到子弹
  1794. var flag = false;
  1795. if (ResidueAmmo > 0 && weapon.CurrAmmo + weapon.ResidueAmmo < maxCount)
  1796. {
  1797. var count = weapon.PickUpAmmo(ResidueAmmo);
  1798. if (count != ResidueAmmo)
  1799. {
  1800. ResidueAmmo = count;
  1801. flag = true;
  1802. }
  1803. }
  1804.  
  1805. if (CurrAmmo > 0 && weapon.CurrAmmo + weapon.ResidueAmmo < maxCount)
  1806. {
  1807. var count = weapon.PickUpAmmo(CurrAmmo);
  1808. if (count != CurrAmmo)
  1809. {
  1810. CurrAmmo = count;
  1811. flag = true;
  1812. }
  1813. }
  1814.  
  1815. //播放互动效果
  1816. if (flag)
  1817. {
  1818. Throw(GlobalPosition, 0, Utils.Random.RandomRangeInt(20, 50), Vector2.Zero, Utils.Random.RandomRangeInt(-180, 180));
  1819. //没有子弹了, 停止播放泛白效果
  1820. if (IsTotalAmmoEmpty())
  1821. {
  1822. //停止动画
  1823. AnimationPlayer.Stop();
  1824. //清除泛白效果
  1825. SetBlendSchedule(0);
  1826. }
  1827. }
  1828. }
  1829. else //没有武器
  1830. {
  1831. if (!holster.HasVacancy()) //没有空位置, 扔掉当前武器
  1832. {
  1833. //替换武器
  1834. roleMaster.ThrowWeapon();
  1835. }
  1836. roleMaster.PickUpWeapon(this);
  1837. }
  1838. }
  1839. }
  1840.  
  1841. /// <summary>
  1842. /// 获取当前武器真实的旋转角度(弧度制), 由于武器旋转时加入了旋转吸附, 所以需要通过该函数来来知道当前武器的真实旋转角度
  1843. /// </summary>
  1844. public float GetRealGlobalRotation()
  1845. {
  1846. return Mathf.DegToRad(Master.MountPoint.RealRotationDegrees) + Rotation;
  1847. }
  1848.  
  1849. /// <summary>
  1850. /// 触发扔掉武器时抛出的效果, 并不会管武器是否在武器背包中
  1851. /// </summary>
  1852. /// <param name="master">触发扔掉该武器的的角色</param>
  1853. public void ThrowWeapon(Role master)
  1854. {
  1855. ThrowWeapon(master, master.GlobalPosition);
  1856. }
  1857.  
  1858. /// <summary>
  1859. /// 触发扔掉武器时抛出的效果, 并不会管武器是否在武器背包中
  1860. /// </summary>
  1861. /// <param name="master">触发扔掉该武器的的角色</param>
  1862. /// <param name="startPosition">投抛起始位置</param>
  1863. public void ThrowWeapon(Role master, Vector2 startPosition)
  1864. {
  1865. //阴影偏移
  1866. ShadowOffset = new Vector2(0, 2);
  1867.  
  1868. if (master.Face == FaceDirection.Left)
  1869. {
  1870. Scale *= new Vector2(1, -1);
  1871. }
  1872.  
  1873. var rotation = master.MountPoint.GlobalRotation;
  1874. GlobalRotation = rotation;
  1875.  
  1876. if (master.Face == FaceDirection.Right)
  1877. {
  1878. startPosition += _gripPoint.Rotated(rotation);
  1879. }
  1880. else
  1881. {
  1882. startPosition += new Vector2(_gripPoint.X, -_gripPoint.Y).Rotated(rotation);
  1883. }
  1884.  
  1885. var startHeight = -master.MountPoint.Position.Y;
  1886. var velocity = new Vector2(20, 0).Rotated(rotation);
  1887. var yf = Utils.Random.RandomRangeInt(50, 70);
  1888. Throw(startPosition, startHeight, yf, velocity, 0);
  1889. //继承role的移动速度
  1890. InheritVelocity(master);
  1891. }
  1892.  
  1893. protected override void OnThrowStart()
  1894. {
  1895. //禁用碰撞
  1896. //Collision.Disabled = true;
  1897. //AnimationPlayer.Play(AnimatorNames.Floodlight);
  1898. }
  1899.  
  1900. protected override void OnThrowOver()
  1901. {
  1902. //启用碰撞
  1903. //Collision.Disabled = false;
  1904. //还有弹药, 播放泛白效果
  1905. if (!IsTotalAmmoEmpty())
  1906. {
  1907. AnimationPlayer.Play(AnimatorNames.Floodlight);
  1908. }
  1909. }
  1910.  
  1911. /// <summary>
  1912. /// 触发启用武器, 这个函数由 Holster 对象调用
  1913. /// </summary>
  1914. private void Active()
  1915. {
  1916. //调整阴影
  1917. //ShadowOffset = new Vector2(0, Master.GlobalPosition.Y - GlobalPosition.Y);
  1918. //ShadowOffset = new Vector2(0, -Master.MountPoint.Position.Y + 2);
  1919. ShadowOffset = new Vector2(0, -Master.MountPoint.Position.Y);
  1920. //枪口默认抬起角度
  1921. RotationDegrees = -Attribute.DefaultAngle;
  1922. ShowShadowSprite();
  1923. OnActive();
  1924. }
  1925.  
  1926. /// <summary>
  1927. /// 触发收起武器, 这个函数由 Holster 对象调用
  1928. /// </summary>
  1929. private void Conceal()
  1930. {
  1931. //停止换弹动画
  1932. if (AnimationPlayer.CurrentAnimation == AnimatorNames.Reloading && AnimationPlayer.IsPlaying())
  1933. {
  1934. AnimationPlayer.Play(AnimatorNames.Reset);
  1935. }
  1936. HideShadowSprite();
  1937. OnConceal();
  1938. }
  1939. public void OnRemoveItem()
  1940. {
  1941. //要重置部分重要属性属性
  1942. if (Master.IsAi)
  1943. {
  1944. _triggerTimer = 0;
  1945. }
  1946. GetParent().RemoveChild(this);
  1947. _triggerRoleFlag = false;
  1948. SetCurrentWeaponAttribute(_playerWeaponAttribute);
  1949. CollisionLayer = _tempLayer;
  1950.  
  1951. //精灵位置, 旋转中心点
  1952. AnimatedSprite.Position = Vector2.Zero;
  1953. AnimatedSprite.Offset = Vector2.Zero;
  1954. Collision.Position = _collPoint2;
  1955. //清除 Ai 拾起标记
  1956. RemoveSign(SignNames.AiFindWeaponSign);
  1957. //停止换弹
  1958. if (Reloading)
  1959. {
  1960. StopReload();
  1961. }
  1962. //停止换弹动画
  1963. if (AnimationPlayer.CurrentAnimation == AnimatorNames.Reloading && AnimationPlayer.IsPlaying())
  1964. {
  1965. AnimationPlayer.Play(AnimatorNames.Reset);
  1966. }
  1967. OnRemove(Master);
  1968. }
  1969.  
  1970. public void OnPickUpItem()
  1971. {
  1972. Pickup();
  1973. _triggerRoleFlag = true;
  1974. SetCurrentWeaponAttribute(Master.IsAi ? _aiWeaponAttribute : _playerWeaponAttribute);
  1975. //停止动画
  1976. AnimationPlayer.Stop();
  1977. //清除泛白效果
  1978. SetBlendSchedule(0);
  1979. ZIndex = 0;
  1980. //禁用碰撞
  1981. //Collision.Disabled = true;
  1982. AnimatedSprite.Position = _gripPoint;
  1983. AnimatedSprite.Offset = _gripOffset;
  1984. Collision.Position = _collPoint1;
  1985. //修改层级
  1986. _tempLayer = CollisionLayer;
  1987. CollisionLayer = PhysicsLayer.OnHand;
  1988. //清除 Ai 拾起标记
  1989. RemoveSign(SignNames.AiFindWeaponSign);
  1990. OnPickUp(Master);
  1991. }
  1992.  
  1993. public void OnActiveItem()
  1994. {
  1995. //更改父节点
  1996. var parent = GetParentOrNull<Node>();
  1997. if (parent == null)
  1998. {
  1999. Master.MountPoint.AddChild(this);
  2000. }
  2001. else if (parent != Master.MountPoint)
  2002. {
  2003. parent.RemoveChild(this);
  2004. Master.MountPoint.AddChild(this);
  2005. }
  2006.  
  2007. Position = Vector2.Zero;
  2008. Scale = Vector2.One;
  2009. RotationDegrees = 0;
  2010. Visible = true;
  2011. Active();
  2012. }
  2013.  
  2014. public void OnConcealItem()
  2015. {
  2016. var tempParent = GetParentOrNull<Node2D>();
  2017. if (tempParent != null)
  2018. {
  2019. tempParent.RemoveChild(this);
  2020. Master.BackMountPoint.AddChild(this);
  2021. Master.OnPutBackMount(this, PackageIndex);
  2022. Conceal();
  2023. }
  2024. }
  2025.  
  2026. public void OnOverflowItem()
  2027. {
  2028. Master.ThrowWeapon(PackageIndex);
  2029. }
  2030.  
  2031. /// <summary>
  2032. /// 获取相对于武器本地坐标的开火位置
  2033. /// </summary>
  2034. public Vector2 GetLocalFirePosition()
  2035. {
  2036. return AnimatedSprite.Position + FirePoint.Position;
  2037. }
  2038. /// <summary>
  2039. /// 获取相对于武器本地坐标的抛壳位置
  2040. /// </summary>
  2041. public Vector2 GetLocalShellPosition()
  2042. {
  2043. return AnimatedSprite.Position + ShellPoint.Position;
  2044. }
  2045.  
  2046. /// <summary>
  2047. /// 获取握把位置, 相当于武器的原点坐标
  2048. /// </summary>
  2049. public Vector2 GetGripPosition()
  2050. {
  2051. return _gripPoint + _gripOffset;
  2052. }
  2053.  
  2054. //设置当前使用的武器属性
  2055. private void SetCurrentWeaponAttribute(ExcelConfig.WeaponBase attr)
  2056. {
  2057. if (attr != _weaponAttribute)
  2058. {
  2059. _weaponAttribute = attr;
  2060. //重置开火速率
  2061. CurrentFiringSpeed = _weaponAttribute.StartFiringSpeed;
  2062. }
  2063. }
  2064.  
  2065. //-------------------------------- Ai相关 -----------------------------
  2066. /// <summary>
  2067. /// Ai调用, 触发扣动扳机, 并返回攻击状态
  2068. /// </summary>
  2069. public AiAttackEnum AiTriggerAttackState(AiAttackEnum prevState)
  2070. {
  2071. var chargeFinish = false;
  2072. if (prevState == AiAttackEnum.AttackCharge) //蓄力中
  2073. {
  2074. var enemy = (Enemy)Master;
  2075. if (!IsChargeFinish())
  2076. {
  2077. enemy.Attack();
  2078. return prevState;
  2079. }
  2080.  
  2081. chargeFinish = true;
  2082. enemy.LockingTime = 0;
  2083. }
  2084. AiAttackEnum flag;
  2085. if (IsTotalAmmoEmpty()) //当前武器弹药打空
  2086. {
  2087. //切换到有子弹的武器
  2088. var index = Master.WeaponPack.FindIndex((we, i) => !we.IsTotalAmmoEmpty());
  2089. if (index != -1)
  2090. {
  2091. flag = AiAttackEnum.ExchangeWeapon;
  2092. Master.WeaponPack.ExchangeByIndex(index);
  2093. }
  2094. else //所有子弹打光
  2095. {
  2096. flag = AiAttackEnum.NoAmmo;
  2097. }
  2098. }
  2099. else if (Reloading) //换弹中
  2100. {
  2101. flag = AiAttackEnum.TriggerReload;
  2102. }
  2103. else if (IsAmmoEmpty()) //弹夹已经打空
  2104. {
  2105. flag = AiAttackEnum.Reloading;
  2106. Reload();
  2107. }
  2108. else if (_beLoadedState == 0 || _beLoadedState == -1) //需要上膛
  2109. {
  2110. flag = AiAttackEnum.AttackInterval;
  2111. if (_attackTimer <= 0)
  2112. {
  2113. BeLoaded();
  2114. }
  2115. }
  2116. else if (_beLoadedState == 1) //上膛中
  2117. {
  2118. flag = AiAttackEnum.AttackInterval;
  2119. }
  2120. else if (_continuousCount >= 1 && (!chargeFinish || _continuousCount >= 2)) //连发中
  2121. {
  2122. flag = AiAttackEnum.Attack;
  2123. }
  2124. else if (IsAttackIntervalTime()) //开火间隙
  2125. {
  2126. flag = AiAttackEnum.AttackInterval;
  2127. }
  2128. else
  2129. {
  2130. var enemy = (Enemy)Master;
  2131. if (enemy.LockTargetTime >= Attribute.AiAttackAttr.LockingTime) //正常射击
  2132. {
  2133. if (GetDelayedAttackTime() > 0)
  2134. {
  2135. flag = AiAttackEnum.Attack;
  2136. enemy.Attack();
  2137. if (_attackFlag)
  2138. {
  2139. enemy.LockingTime = 0;
  2140. }
  2141. }
  2142. else
  2143. {
  2144. if (Attribute.ContinuousShoot) //连发
  2145. {
  2146. flag = AiAttackEnum.Attack;
  2147. enemy.Attack();
  2148. if (_attackFlag)
  2149. {
  2150. enemy.LockingTime = 0;
  2151. }
  2152. }
  2153. else //单发
  2154. {
  2155. if (Attribute.LooseShoot && Attribute.MinChargeTime > 0) //松发并蓄力攻击
  2156. {
  2157. flag = AiAttackEnum.AttackCharge;
  2158. enemy.Attack();
  2159. }
  2160. else
  2161. {
  2162. flag = AiAttackEnum.Attack;
  2163. enemy.Attack();
  2164. if (_attackFlag && _attackTimer > 0)
  2165. {
  2166. enemy.LockingTime = 0;
  2167. }
  2168. }
  2169. }
  2170. }
  2171. }
  2172. else //锁定时间没到
  2173. {
  2174. flag = AiAttackEnum.LockingTime;
  2175. }
  2176. }
  2177.  
  2178. return flag;
  2179. }
  2180.  
  2181. // /// <summary>
  2182. // /// 获取 Ai 对于该武器的评分, 评分越高, 代表 Ai 会越优先选择该武器, 如果为 -1, 则表示 Ai 不会使用该武器
  2183. // /// </summary>
  2184. // public float GetAiScore()
  2185. // {
  2186. // return 1;
  2187. // }
  2188. }