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