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