Newer
Older
DungeonShooting / DungeonShooting_Godot / src / framework / activity / ActivityObject.cs
@小李xl 小李xl on 15 Mar 2024 55 KB buff系统重构中
  1.  
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Config;
  6. using Godot;
  7.  
  8. /// <summary>
  9. /// 房间内活动物体基类, 所有物体都必须继承该类,<br/>
  10. /// 该类提供基础物体运动模拟, 互动接口, 自定义组件, 协程等功能<br/>
  11. /// ActivityObject 子类实例化请不要直接使用 new, 而用该在类上标上 [Tool], 并在 ActivityObject.xlsx 配置文件中注册物体, 导出配置表后使用 ActivityObject.Create(id) 来创建实例.<br/>
  12. /// </summary>
  13. [Tool]
  14. public partial class ActivityObject : CharacterBody2D, IDestroy, ICoroutine
  15. {
  16. /// <summary>
  17. /// 是否是调试模式
  18. /// </summary>
  19. public static bool IsDebug { get; set; }
  20.  
  21. /// <summary>
  22. /// 实例唯一 Id
  23. /// </summary>
  24. public long Id { get; set; }
  25. /// <summary>
  26. /// 当前物体对应的配置数据, 如果不是通过 ActivityObject.Create() 函数创建出来的对象那么 ItemConfig 为 null
  27. /// </summary>
  28. public ExcelConfig.ActivityBase ActivityBase { get; private set; }
  29.  
  30. /// <summary>
  31. /// 是否是静态物体, 如果为true, 则会禁用移动处理
  32. /// </summary>
  33. public bool IsStatic
  34. {
  35. get => MoveController != null ? !MoveController.Enable : true;
  36. set
  37. {
  38. if (MoveController != null)
  39. {
  40. MoveController.Enable = !value;
  41. }
  42. }
  43. }
  44.  
  45. /// <summary>
  46. /// 是否显示阴影
  47. /// </summary>
  48. public bool IsShowShadow { get; private set; }
  49. /// <summary>
  50. /// 当前物体显示的阴影图像, 节点名称必须叫 "ShadowSprite", 类型为 Sprite2D
  51. /// </summary>
  52. [Export, ExportFillNode]
  53. public Sprite2D ShadowSprite { get; set; }
  54. /// <summary>
  55. /// 当前物体显示的精灵图像, 节点名称必须叫 "AnimatedSprite2D", 类型为 AnimatedSprite2D
  56. /// </summary>
  57. [Export, ExportFillNode]
  58. public AnimatedSprite2D AnimatedSprite { get; set; }
  59.  
  60. /// <summary>
  61. /// 当前物体碰撞器节点, 节点名称必须叫 "Collision", 类型为 CollisionShape2D
  62. /// </summary>
  63. [Export, ExportFillNode]
  64. public CollisionShape2D Collision { get; set; }
  65.  
  66. /// <summary>
  67. /// 是否调用过 Destroy() 函数
  68. /// </summary>
  69. public bool IsDestroyed { get; private set; }
  70. /// <summary>
  71. /// 阴影偏移
  72. /// </summary>
  73. [Export]
  74. public Vector2 ShadowOffset { get; set; } = new Vector2(0, 2);
  75. /// <summary>
  76. /// 移动控制器
  77. /// </summary>
  78. public MoveController MoveController { get; private set; }
  79.  
  80. /// <summary>
  81. /// 物体移动基础速率
  82. /// </summary>
  83. public Vector2 BasisVelocity
  84. {
  85. get
  86. {
  87. if (MoveController != null)
  88. {
  89. return MoveController.BasisVelocity;
  90. }
  91.  
  92. return Vector2.Zero;
  93. }
  94. set
  95. {
  96. if (MoveController != null)
  97. {
  98. MoveController.BasisVelocity = value;
  99. }
  100. }
  101. }
  102.  
  103. /// <summary>
  104. /// 当前物体归属的区域, 如果为 null 代表不属于任何一个区域
  105. /// </summary>
  106. public AffiliationArea AffiliationArea
  107. {
  108. get => _affiliationArea;
  109. set
  110. {
  111. if (value != _affiliationArea)
  112. {
  113. var prev = _affiliationArea;
  114. _affiliationArea = value;
  115. if (!IsDestroyed)
  116. {
  117. OnAffiliationChange(prev);
  118. }
  119. }
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// 下坠逻辑是否执行完成
  125. /// </summary>
  126. public bool IsFallOver => _isFallOver;
  127.  
  128. /// <summary>
  129. /// 是否正在投抛过程中
  130. /// </summary>
  131. public bool IsThrowing => VerticalSpeed != 0 && !_isFallOver;
  132.  
  133. /// <summary>
  134. /// 当前物体的海拔高度, 如果大于0, 则会做自由落体运动, 也就是执行投抛逻辑
  135. /// </summary>
  136. public float Altitude
  137. {
  138. get => _altitude;
  139. set
  140. {
  141. _altitude = value;
  142. _hasResilienceVerticalSpeed = false;
  143. }
  144. }
  145.  
  146. private float _altitude = 0;
  147.  
  148. /// <summary>
  149. /// 物体纵轴移动速度, 如果设置大于0, 就可以营造向上投抛物体的效果, 该值会随着重力加速度衰减
  150. /// </summary>
  151. public float VerticalSpeed
  152. {
  153. get => _verticalSpeed;
  154. set
  155. {
  156. _verticalSpeed = value;
  157. _hasResilienceVerticalSpeed = false;
  158. }
  159. }
  160.  
  161. private float _verticalSpeed;
  162.  
  163. /// <summary>
  164. /// 是否启用垂直方向上的运动模拟, 默认开启, 如果禁用, 那么下落和投抛效果, 同样 Throw() 函数也将失效
  165. /// </summary>
  166. public bool EnableVerticalMotion { get; set; } = true;
  167. /// <summary>
  168. /// 是否启用物体更新行为, 默认 true, 如果禁用, 则会停止当前物体的 Process(), PhysicsProcess() 调用, 并且禁用 Collision 节点, 禁用后所有组件也同样被禁用行为
  169. /// </summary>
  170. public bool EnableBehavior
  171. {
  172. get => _enableBehavior;
  173. set
  174. {
  175. if (value != _enableBehavior)
  176. {
  177. _enableBehavior = value;
  178. SetProcess(value);
  179. SetPhysicsProcess(value);
  180. if (value)
  181. {
  182. Collision.Disabled = _enableBehaviorCollisionDisabledFlag;
  183. }
  184. else
  185. {
  186. _enableBehaviorCollisionDisabledFlag = Collision.Disabled;
  187. Collision.Disabled = true;
  188. }
  189. }
  190. }
  191. }
  192.  
  193. /// <summary>
  194. /// 是否启用自定义行为, 默认 true, 如果禁用, 则会停止调用子类重写的 Process(), PhysicsProcess() 函数, 并且当前物体除 MoveController 以外的组件 Process(), PhysicsProcess() 也会停止调用
  195. /// </summary>
  196. public bool EnableCustomBehavior { get; set; } = true;
  197. /// <summary>
  198. /// 物体材质数据
  199. /// </summary>
  200. public ExcelConfig.ActivityMaterial ActivityMaterial { get; private set; }
  201.  
  202. /// <summary>
  203. /// 所在的 World 对象
  204. /// </summary>
  205. public World World { get; set; }
  206.  
  207. /// <summary>
  208. /// 是否开启描边
  209. /// </summary>
  210. public bool ShowOutline
  211. {
  212. get => _blendShaderMaterial == null ? false : _blendShaderMaterial.GetShaderParameter(ShaderParamNames.ShowOutline).AsBool();
  213. set
  214. {
  215. _blendShaderMaterial?.SetShaderParameter(ShaderParamNames.ShowOutline, value);
  216. _shadowBlendShaderMaterial?.SetShaderParameter(ShaderParamNames.ShowOutline, value);
  217. }
  218. }
  219.  
  220. /// <summary>
  221. /// 描边颜色
  222. /// </summary>
  223. public Color OutlineColor
  224. {
  225. get => _blendShaderMaterial == null ? Colors.Black : _blendShaderMaterial.GetShaderParameter(ShaderParamNames.OutlineColor).AsColor();
  226. set => _blendShaderMaterial?.SetShaderParameter(ShaderParamNames.OutlineColor, value);
  227. }
  228.  
  229. /// <summary>
  230. /// 灰度
  231. /// </summary>
  232. public float Grey
  233. {
  234. get => _blendShaderMaterial == null ? 0 : _blendShaderMaterial.GetShaderParameter(ShaderParamNames.Grey).AsSingle();
  235. set => _blendShaderMaterial?.SetShaderParameter(ShaderParamNames.Grey, value);
  236. }
  237. /// <summary>
  238. /// 是否是自定义阴影纹理
  239. /// </summary>
  240. public bool IsCustomShadowSprite { get; set; }
  241.  
  242. /// <summary>
  243. /// 记录绘制液体的笔刷上一次绘制的位置<br/>
  244. /// 每次调用 DrawLiquid() 后都会记录这一次绘制的位置, 记录这个位置用作执行补间操作, 但是一旦停止绘制了, 需要手动清理记录的位置, 也就是将 BrushPrevPosition 置为 null
  245. /// </summary>
  246. public Vector2I? BrushPrevPosition { get; set; }
  247. /// <summary>
  248. /// 默认所在层级
  249. /// </summary>
  250. public RoomLayerEnum DefaultLayer { get; set; }
  251.  
  252. /// <summary>
  253. /// 投抛状态下的碰撞器层级
  254. /// </summary>
  255. public uint ThrowCollisionMask { get; set; } = PhysicsLayer.Wall;
  256. // --------------------------------------------------------------------------------
  257.  
  258. //是否正在调用组件 Update 函数
  259. private bool _updatingComp = false;
  260. //组件集合
  261. private readonly List<KeyValuePair<Type, Component>> _components = new List<KeyValuePair<Type, Component>>();
  262. //修改的组件集合, value 为 true 表示添加组件, false 表示移除组件
  263. private readonly List<KeyValuePair<Component, bool>> _changeComponents = new List<KeyValuePair<Component, bool>>();
  264. //上一帧动画名称
  265. private StringName _prevAnimation;
  266. //上一帧动画
  267. private int _prevAnimationFrame;
  268.  
  269. //播放 Hit 动画
  270. private bool _playHit;
  271. private float _playHitSchedule;
  272.  
  273. //混色shader材质
  274. private ShaderMaterial _blendShaderMaterial;
  275. private ShaderMaterial _shadowBlendShaderMaterial;
  276. //存储投抛该物体时所产生的数据
  277. private readonly ActivityFallData _fallData = new ActivityFallData();
  278. //标记字典
  279. private Dictionary<string, object> _signMap;
  280. //开启的协程
  281. private List<CoroutineData> _coroutineList;
  282.  
  283. //物体所在区域
  284. private AffiliationArea _affiliationArea;
  285.  
  286. //是否是第一次下坠
  287. private bool _firstFall = true;
  288. //下坠是否已经结束
  289. private bool _isFallOver = true;
  290.  
  291. //投抛移动速率
  292. private ExternalForce _throwForce;
  293. //落到地上回弹的速度
  294. private float _resilienceVerticalSpeed = 0;
  295. private bool _hasResilienceVerticalSpeed = false;
  296.  
  297. //是否启用物体行为
  298. private bool _enableBehavior = true;
  299. private bool _enableBehaviorCollisionDisabledFlag;
  300.  
  301. private bool _processingBecomesStaticImage = false;
  302.  
  303. //击退外力
  304. private ExternalForce _repelForce;
  305.  
  306. //绑定销毁物体集合
  307. private HashSet<IDestroy> _destroySet;
  308.  
  309. //挂载的物体集合
  310. private HashSet<IMountItem> _mountObjects;
  311.  
  312. // --------------------------------------------------------------------------------
  313. //实例索引
  314. private static long _instanceIndex = 0;
  315. //冻结显示的Sprite
  316. private FreezeSprite _freezeSprite;
  317.  
  318. //初始化节点
  319. private void _InitNode(ExcelConfig.ActivityBase config, World world)
  320. {
  321. #if TOOLS
  322. if (!Engine.IsEditorHint())
  323. {
  324. if (GetType().GetCustomAttributes(typeof(ToolAttribute), false).Length == 0)
  325. {
  326. throw new Exception($"ActivityObject子类'{GetType().FullName}'没有加[Tool]标记!");
  327. }
  328. }
  329. #endif
  330. if (config.Material == null)
  331. {
  332. ActivityMaterial = ExcelConfig.ActivityMaterial_List[0];
  333. }
  334. else
  335. {
  336. ActivityMaterial = config.Material;
  337. }
  338.  
  339. //GravityScale 为 0 时关闭重力
  340. if (ActivityMaterial.GravityScale == 0)
  341. {
  342. EnableVerticalMotion = false;
  343. }
  344. World = world;
  345. ActivityBase = config;
  346. #if TOOLS
  347. Name = GetType().Name + (_instanceIndex++);
  348. #endif
  349. Id = _instanceIndex;
  350. _blendShaderMaterial = AnimatedSprite.Material as ShaderMaterial;
  351. IsCustomShadowSprite = ShadowSprite.Texture != null;
  352. if (!IsCustomShadowSprite) //没有自定义阴影纹理
  353. {
  354. _shadowBlendShaderMaterial = ShadowSprite.Material as ShaderMaterial;
  355. if (_shadowBlendShaderMaterial != null && _blendShaderMaterial != null)
  356. {
  357. var value = _blendShaderMaterial.GetShaderParameter(ShaderParamNames.ShowOutline);
  358. _shadowBlendShaderMaterial.SetShaderParameter(ShaderParamNames.ShowOutline, value);
  359. }
  360. ShadowSprite.Visible = false;
  361. }
  362.  
  363. MotionMode = MotionModeEnum.Floating;
  364. MoveController = AddComponent<MoveController>();
  365. IsStatic = config.IsStatic;
  366. OnInit();
  367. }
  368.  
  369. /// <summary>
  370. /// 子类重写的 _Ready() 可能会比 _InitNode() 函数调用晚, 所以禁止子类重写, 如需要 _Ready() 类似的功能需重写 OnInit()
  371. /// </summary>
  372. public sealed override void _Ready()
  373. {
  374.  
  375. }
  376. /// <summary>
  377. /// 子类需要重写 _EnterTree() 函数, 请重写 EnterTree()
  378. /// </summary>
  379. public sealed override void _EnterTree()
  380. {
  381. #if TOOLS
  382. // 在工具模式下创建的 template 节点自动创建对应的必要子节点
  383. if (Engine.IsEditorHint())
  384. {
  385. _InitNodeInEditor();
  386. return;
  387. }
  388. #endif
  389. EnterTree();
  390. }
  391. /// <summary>
  392. /// 子类需要重写 _ExitTree() 函数, 请重写 ExitTree()
  393. /// </summary>
  394. public sealed override void _ExitTree()
  395. {
  396. #if TOOLS
  397. // 在工具模式下创建的 template 节点自动创建对应的必要子节点
  398. if (Engine.IsEditorHint())
  399. {
  400. return;
  401. }
  402. #endif
  403. ExitTree();
  404. }
  405.  
  406. /// <summary>
  407. /// 显示并更新阴影
  408. /// </summary>
  409. public void ShowShadowSprite()
  410. {
  411. if (!IsCustomShadowSprite)
  412. {
  413. var anim = AnimatedSprite.Animation;
  414. var frame = AnimatedSprite.Frame;
  415. if (_prevAnimation != anim || _prevAnimationFrame != frame)
  416. {
  417. var frames = AnimatedSprite.SpriteFrames;
  418. if (frames != null && frames.HasAnimation(anim))
  419. {
  420. //切换阴影动画
  421. ShadowSprite.Texture = frames.GetFrameTexture(anim, frame);
  422. }
  423. }
  424.  
  425. _prevAnimation = anim;
  426. _prevAnimationFrame = frame;
  427. }
  428.  
  429. IsShowShadow = true;
  430. CalcShadowTransform(IsInsideTree());
  431. ShadowSprite.Visible = true;
  432. }
  433.  
  434. /// <summary>
  435. /// 隐藏阴影
  436. /// </summary>
  437. public void HideShadowSprite()
  438. {
  439. ShadowSprite.Visible = false;
  440. IsShowShadow = false;
  441. }
  442.  
  443. /// <summary>
  444. /// 设置默认序列帧动画的第一帧
  445. /// </summary>
  446. public void SetDefaultTexture(Texture2D texture)
  447. {
  448. if (AnimatedSprite.SpriteFrames == null)
  449. {
  450. SpriteFrames spriteFrames = new SpriteFrames();
  451. AnimatedSprite.SpriteFrames = spriteFrames;
  452. spriteFrames.AddFrame("default", texture);
  453. }
  454. else
  455. {
  456. SpriteFrames spriteFrames = AnimatedSprite.SpriteFrames;
  457. if (spriteFrames.GetFrameCount("default") > 0)
  458. {
  459. spriteFrames.SetFrame("default", 0, texture);
  460. }
  461. else
  462. {
  463. spriteFrames.AddFrame("default", texture);
  464. }
  465. }
  466. AnimatedSprite.Play("default");
  467. }
  468.  
  469. /// <summary>
  470. /// 获取默认序列帧动画的第一帧
  471. /// </summary>
  472. public Texture2D GetDefaultTexture()
  473. {
  474. return AnimatedSprite.SpriteFrames.GetFrameTexture("default", 0);
  475. }
  476. /// <summary>
  477. /// 获取当前序列帧动画的 Texture2D
  478. /// </summary>
  479. public Texture2D GetCurrentTexture()
  480. {
  481. var spriteFrames = AnimatedSprite.SpriteFrames;
  482. if (spriteFrames == null)
  483. {
  484. return null;
  485. }
  486. return spriteFrames.GetFrameTexture(AnimatedSprite.Animation, AnimatedSprite.Frame);
  487. }
  488.  
  489. /// <summary>
  490. /// 物体初始化时调用
  491. /// </summary>
  492. public virtual void OnInit()
  493. {
  494. }
  495. /// <summary>
  496. /// 进入场景树时调用
  497. /// </summary>
  498. public virtual void EnterTree()
  499. {
  500. }
  501.  
  502. /// <summary>
  503. /// 离开场景树时调用
  504. /// </summary>
  505. public virtual void ExitTree()
  506. {
  507. }
  508. /// <summary>
  509. /// 返回是否能与其他ActivityObject互动
  510. /// </summary>
  511. /// <param name="master">触发者</param>
  512. public virtual CheckInteractiveResult CheckInteractive(ActivityObject master)
  513. {
  514. return new CheckInteractiveResult(this);
  515. }
  516.  
  517. /// <summary>
  518. /// 与其它ActivityObject互动时调用, 如果要检测是否能互动请 CheckInteractive() 函数, 如果直接调用该函数那么属于强制互动行为, 例如子弹碰到物体
  519. /// </summary>
  520. /// <param name="master">触发者</param>
  521. public virtual void Interactive(ActivityObject master)
  522. {
  523. }
  524.  
  525. /// <summary>
  526. /// 开始投抛该物体时调用
  527. /// </summary>
  528. protected virtual void OnThrowStart()
  529. {
  530. }
  531. /// <summary>
  532. /// 投抛该物体达到最高点时调用
  533. /// </summary>
  534. protected virtual void OnThrowMaxHeight(float height)
  535. {
  536. }
  537.  
  538. /// <summary>
  539. /// 投抛状态下第一次接触地面时调用, 之后的回弹落地将不会调用该函数
  540. /// </summary>
  541. protected virtual void OnFirstFallToGround()
  542. {
  543. }
  544.  
  545. /// <summary>
  546. /// 投抛状态下每次接触地面时调用
  547. /// </summary>
  548. protected virtual void OnFallToGround()
  549. {
  550. }
  551.  
  552. /// <summary>
  553. /// 投抛结束时调用
  554. /// </summary>
  555. protected virtual void OnThrowOver()
  556. {
  557. }
  558.  
  559. /// <summary>
  560. /// 当前物体销毁时调用, 销毁物体请调用 Destroy() 函数
  561. /// </summary>
  562. protected virtual void OnDestroy()
  563. {
  564. }
  565.  
  566. /// <summary>
  567. /// 每帧调用一次, 物体的 Process() 会在组件的 Process() 之前调用
  568. /// </summary>
  569. protected virtual void Process(float delta)
  570. {
  571. }
  572. /// <summary>
  573. /// 每物理帧调用一次, 物体的 PhysicsProcess() 会在组件的 PhysicsProcess() 之前调用
  574. /// </summary>
  575. protected virtual void PhysicsProcess(float delta)
  576. {
  577. }
  578. /// <summary>
  579. /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等
  580. /// </summary>
  581. protected virtual void DebugDraw()
  582. {
  583. }
  584.  
  585. /// <summary>
  586. /// 归属区域发生改变
  587. /// </summary>
  588. /// <param name="prevArea">上一个区域, 注意可能为空</param>
  589. protected virtual void OnAffiliationChange(AffiliationArea prevArea)
  590. {
  591. }
  592.  
  593. /// <summary>
  594. /// 移动并碰撞到物体时调用该函数, 参数为碰撞数据, 该函数由 MoveController 调用
  595. /// </summary>
  596. public virtual void OnMoveCollision(KinematicCollision2D collision)
  597. {
  598. }
  599.  
  600. /// <summary>
  601. /// 撞到墙壁反弹时调用该函数, 参数为反弹的角度, 弧度制, 该函数由 MoveController 调用
  602. /// </summary>
  603. public virtual void OnBounce(float rotation)
  604. {
  605. }
  606.  
  607. /// <summary>
  608. /// 添加组件时调用
  609. /// </summary>
  610. public virtual void OnAddComponent(Component component)
  611. {
  612. }
  613.  
  614. /// <summary>
  615. /// 移除组件时调用
  616. /// </summary>
  617. public virtual void OnRemoveComponent(Component component)
  618. {
  619. }
  620.  
  621. /// <summary>
  622. /// 返回当物体 CollisionLayer 是否能与 mask 层碰撞
  623. /// </summary>
  624. public bool CollisionWithMask(uint mask)
  625. {
  626. return (CollisionLayer & mask) != 0;
  627. }
  628. /// <summary>
  629. /// 拾起一个 node 节点, 也就是将其从场景树中移除
  630. /// </summary>
  631. public void Pickup()
  632. {
  633. var parent = GetParent();
  634. if (parent != null)
  635. {
  636. if (IsThrowing)
  637. {
  638. StopThrow();
  639. }
  640.  
  641. parent.RemoveChild(this);
  642. }
  643. }
  644.  
  645. /// <summary>
  646. /// 将一个节点扔到地上
  647. /// <param name="layer">放入的层</param>
  648. /// <param name="showShadow">是否显示阴影</param>
  649. /// </summary>
  650. public virtual void PutDown(RoomLayerEnum layer, bool showShadow = true)
  651. {
  652. DefaultLayer = layer;
  653. var parent = GetParent();
  654. var root = World.Current.GetRoomLayer(layer);
  655. if (parent != root)
  656. {
  657. if (parent != null)
  658. {
  659. Reparent(root);
  660. }
  661. else
  662. {
  663. root.AddChild(this);
  664. }
  665. }
  666.  
  667. if (showShadow)
  668. {
  669. if (IsInsideTree())
  670. {
  671. ShowShadowSprite();
  672. }
  673. else
  674. {
  675. //注意需要延时调用
  676. CallDeferred(nameof(ShowShadowSprite));
  677. CalcShadowTransform(false);
  678. }
  679. }
  680. else
  681. {
  682. ShadowSprite.Visible = false;
  683. }
  684. }
  685.  
  686. /// <summary>
  687. /// 将一个节点扔到地上
  688. /// </summary>
  689. /// <param name="position">放置的位置</param>
  690. /// <param name="layer">放入的层</param>
  691. /// <param name="showShadow">是否显示阴影</param>
  692. public void PutDown(Vector2 position, RoomLayerEnum layer, bool showShadow = true)
  693. {
  694. PutDown(layer);
  695. Position = position;
  696. }
  697.  
  698. /// <summary>
  699. /// 将该节点投抛出去
  700. /// </summary>
  701. /// <param name="altitude">初始高度</param>
  702. /// <param name="verticalSpeed">纵轴速度</param>
  703. /// <param name="velocity">移动速率</param>
  704. /// <param name="rotateSpeed">旋转速度</param>
  705. public void Throw(float altitude, float verticalSpeed, Vector2 velocity, float rotateSpeed)
  706. {
  707. Altitude = altitude;
  708. //Position = Position + new Vector2(0, altitude);
  709. VerticalSpeed = verticalSpeed;
  710. //ThrowRotationDegreesSpeed = rotateSpeed;
  711. if (_throwForce != null)
  712. {
  713. MoveController.RemoveForce(_throwForce);
  714. }
  715.  
  716. _throwForce = new ExternalForce(ForceNames.Throw);
  717. _throwForce.Velocity = velocity;
  718. _throwForce.RotationSpeed = Mathf.DegToRad(rotateSpeed);
  719. MoveController.AddForce(_throwForce);
  720.  
  721. InitThrowData();
  722. }
  723.  
  724. /// <summary>
  725. /// 将该节点投抛出去
  726. /// </summary>
  727. /// <param name="position">初始位置</param>
  728. /// <param name="altitude">初始高度</param>
  729. /// <param name="verticalSpeed">纵轴速度</param>
  730. /// <param name="velocity">移动速率</param>
  731. /// <param name="rotateSpeed">旋转速度</param>
  732. public void Throw(Vector2 position, float altitude, float verticalSpeed, Vector2 velocity, float rotateSpeed)
  733. {
  734. GlobalPosition = position;
  735. Throw(altitude, verticalSpeed, velocity, rotateSpeed);
  736. }
  737.  
  738.  
  739. /// <summary>
  740. /// 强制停止投抛运动
  741. /// </summary>
  742. public void StopThrow()
  743. {
  744. _isFallOver = true;
  745. RestoreCollision();
  746. }
  747.  
  748. /// <summary>
  749. /// 往当前物体上挂载一个组件
  750. /// </summary>
  751. public T AddComponent<T>() where T : Component, new()
  752. {
  753. var component = new T();
  754. if (_updatingComp)
  755. {
  756. _changeComponents.Add(new KeyValuePair<Component, bool>(component, true));
  757. }
  758. else
  759. {
  760. _components.Add(new KeyValuePair<Type, Component>(typeof(T), component));
  761. }
  762.  
  763. component.Master = this;
  764. component.Ready();
  765. component.OnEnable();
  766. OnAddComponent(component);
  767. return component;
  768. }
  769.  
  770. /// <summary>
  771. /// 往当前物体上挂载一个组件
  772. /// </summary>
  773. public Component AddComponent(Type type)
  774. {
  775. var component = (Component)Activator.CreateInstance(type);
  776. if (_updatingComp)
  777. {
  778. _changeComponents.Add(new KeyValuePair<Component, bool>(component, true));
  779. }
  780. else
  781. {
  782. _components.Add(new KeyValuePair<Type, Component>(type, component));
  783. }
  784. component.Master = this;
  785. component.Ready();
  786. component.OnEnable();
  787. OnAddComponent(component);
  788. return component;
  789. }
  790.  
  791. /// <summary>
  792. /// 移除一个组件, 并且销毁
  793. /// </summary>
  794. /// <param name="component">组件对象</param>
  795. public void RemoveComponent(Component component)
  796. {
  797. if (component.IsDestroyed)
  798. {
  799. return;
  800. }
  801.  
  802. if (_updatingComp)
  803. {
  804. _changeComponents.Add(new KeyValuePair<Component, bool>(component, false));
  805. OnRemoveComponent(component);
  806. component.Destroy();
  807. }
  808. else
  809. {
  810. for (var i = 0; i < _components.Count; i++)
  811. {
  812. if (_components[i].Value == component)
  813. {
  814. _components.RemoveAt(i);
  815. OnRemoveComponent(component);
  816. component.Destroy();
  817. return;
  818. }
  819. }
  820. }
  821. }
  822.  
  823. /// <summary>
  824. /// 根据类型获取一个组件
  825. /// </summary>
  826. public Component GetComponent(Type type)
  827. {
  828. for (int i = 0; i < _components.Count; i++)
  829. {
  830. var temp = _components[i];
  831. if (temp.Key.IsAssignableTo(type))
  832. {
  833. return temp.Value;
  834. }
  835. }
  836.  
  837. if (_updatingComp)
  838. {
  839. for (var i = 0; i < _changeComponents.Count; i++)
  840. {
  841. var temp = _components[i];
  842. if (temp.Value.GetType().IsAssignableTo(type))
  843. {
  844. return temp.Value;
  845. }
  846. }
  847. }
  848.  
  849. return null;
  850. }
  851.  
  852. /// <summary>
  853. /// 根据类型获取一个组件
  854. /// </summary>
  855. public T GetComponent<T>() where T : Component
  856. {
  857. for (int i = 0; i < _components.Count; i++)
  858. {
  859. var temp = _components[i];
  860. if (temp.Value is T component)
  861. {
  862. return component;
  863. }
  864. }
  865.  
  866. if (_updatingComp)
  867. {
  868. for (var i = 0; i < _changeComponents.Count; i++)
  869. {
  870. var temp = _components[i];
  871. if (temp.Value is T component)
  872. {
  873. return component;
  874. }
  875. }
  876. }
  877.  
  878. return null;
  879. }
  880.  
  881. /// <summary>
  882. /// 根据类型获取所有相同类型的组件
  883. /// </summary>
  884. public Component[] GetComponents(Type type)
  885. {
  886. var list = new List<Component>();
  887. for (int i = 0; i < _components.Count; i++)
  888. {
  889. var temp = _components[i];
  890. if (temp.Key.IsAssignableTo(type))
  891. {
  892. list.Add(temp.Value);
  893. }
  894. }
  895.  
  896. if (_updatingComp)
  897. {
  898. for (var i = 0; i < _changeComponents.Count; i++)
  899. {
  900. var temp = _components[i];
  901. if (temp.Value.GetType().IsAssignableTo(type))
  902. {
  903. list.Add(temp.Value);
  904. }
  905. }
  906. }
  907.  
  908. return list.ToArray();
  909. }
  910. /// <summary>
  911. /// 根据类型获取所有相同类型的组件
  912. /// </summary>
  913. public T[] GetComponents<T>() where T : Component
  914. {
  915. var list = new List<T>();
  916. for (int i = 0; i < _components.Count; i++)
  917. {
  918. var temp = _components[i];
  919. if (temp.Value is T component)
  920. {
  921. list.Add(component);
  922. }
  923. }
  924.  
  925. if (_updatingComp)
  926. {
  927. for (var i = 0; i < _changeComponents.Count; i++)
  928. {
  929. var temp = _components[i];
  930. if (temp.Value is T component)
  931. {
  932. list.Add(component);
  933. }
  934. }
  935. }
  936.  
  937. return list.ToArray();
  938. }
  939. /// <summary>
  940. /// 设置混色材质的颜色
  941. /// </summary>
  942. public void SetBlendColor(Color color)
  943. {
  944. _blendShaderMaterial?.SetShaderParameter("blend", color);
  945. }
  946.  
  947. /// <summary>
  948. /// 获取混色材质的颜色
  949. /// </summary>
  950. public Color GetBlendColor()
  951. {
  952. if (_blendShaderMaterial == null)
  953. {
  954. return Colors.White;
  955. }
  956. return _blendShaderMaterial.GetShaderParameter("blend").AsColor();
  957. }
  958. /// <summary>
  959. /// 设置混色材质的强度
  960. /// </summary>
  961. public void SetBlendSchedule(float value)
  962. {
  963. _blendShaderMaterial?.SetShaderParameter("schedule", value);
  964. }
  965.  
  966. /// <summary>
  967. /// 获取混色材质的强度
  968. /// </summary>
  969. public float GetBlendSchedule()
  970. {
  971. if (_blendShaderMaterial == null)
  972. {
  973. return default;
  974. }
  975. return _blendShaderMaterial.GetShaderParameter("schedule").AsSingle();
  976. }
  977.  
  978. /// <summary>
  979. /// 设置混色颜色
  980. /// </summary>
  981. public void SetBlendModulate(Color color)
  982. {
  983. _blendShaderMaterial?.SetShaderParameter("modulate", color);
  984. _shadowBlendShaderMaterial?.SetShaderParameter("modulate", color);
  985. }
  986. /// <summary>
  987. /// 获取混色颜色
  988. /// </summary>
  989. public Color SetBlendModulate()
  990. {
  991. if (_blendShaderMaterial == null)
  992. {
  993. return Colors.White;
  994. }
  995. return _blendShaderMaterial.GetShaderParameter("modulate").AsColor();
  996. }
  997. /// <summary>
  998. /// 每帧调用一次, 为了防止子类覆盖 _Process(), 给 _Process() 加上了 sealed, 子类需要帧循环函数请重写 Process() 函数
  999. /// </summary>
  1000. public sealed override void _Process(double delta)
  1001. {
  1002. #if TOOLS
  1003. if (Engine.IsEditorHint())
  1004. {
  1005. return;
  1006. }
  1007. #endif
  1008. var newDelta = (float)delta;
  1009. if (EnableCustomBehavior)
  1010. {
  1011. Process(newDelta);
  1012. }
  1013. //更新组件
  1014. if (_components.Count > 0)
  1015. {
  1016. _updatingComp = true;
  1017. if (EnableCustomBehavior) //启用所有组件
  1018. {
  1019. for (int i = 0; i < _components.Count; i++)
  1020. {
  1021. if (IsDestroyed) return;
  1022. var temp = _components[i].Value;
  1023. if (temp != null && temp.Enable)
  1024. {
  1025. temp.Process(newDelta);
  1026. }
  1027. }
  1028. }
  1029. else //只更新 MoveController 组件
  1030. {
  1031. if (MoveController.Enable)
  1032. {
  1033. MoveController.Process(newDelta);
  1034. }
  1035. }
  1036. _updatingComp = false;
  1037. if (_changeComponents.Count > 0)
  1038. {
  1039. RefreshComponent();
  1040. }
  1041. }
  1042.  
  1043. // 更新下坠处理逻辑
  1044. UpdateFall(newDelta);
  1045.  
  1046. //阴影
  1047. UpdateShadowSprite(newDelta);
  1048. // Hit 动画
  1049. if (_playHit)
  1050. {
  1051. if (_playHitSchedule < 0.05f)
  1052. {
  1053. _blendShaderMaterial?.SetShaderParameter("schedule", 1);
  1054. }
  1055. else if (_playHitSchedule < 0.15f)
  1056. {
  1057. _blendShaderMaterial?.SetShaderParameter("schedule", Mathf.Lerp(1, 0, (_playHitSchedule - 0.05f) / 0.1f));
  1058. }
  1059. if (_playHitSchedule >= 0.15f)
  1060. {
  1061. _blendShaderMaterial?.SetShaderParameter("schedule", 0);
  1062. _playHitSchedule = 0;
  1063. _playHit = false;
  1064. }
  1065. else
  1066. {
  1067. _playHitSchedule += newDelta;
  1068. }
  1069. }
  1070. //协程更新
  1071. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  1072. //调试绘制
  1073. if (IsDebug)
  1074. {
  1075. QueueRedraw();
  1076. }
  1077. }
  1078.  
  1079. /// <summary>
  1080. /// 更新下坠处理逻辑
  1081. /// </summary>
  1082. public void UpdateFall(float delta)
  1083. {
  1084. // 下坠判定
  1085. if (Altitude > 0 || VerticalSpeed != 0)
  1086. {
  1087. if (_isFallOver) // 没有处于下坠状态, 则进入下坠状态
  1088. {
  1089. InitThrowData();
  1090. }
  1091. else
  1092. {
  1093. if (EnableVerticalMotion) //如果启用了纵向运动, 则更新运动
  1094. {
  1095. //GlobalRotationDegrees = GlobalRotationDegrees + ThrowRotationDegreesSpeed * newDelta;
  1096.  
  1097. var ysp = VerticalSpeed;
  1098.  
  1099. _altitude += VerticalSpeed * delta;
  1100. _verticalSpeed -= GameConfig.G * ActivityMaterial.GravityScale * delta;
  1101.  
  1102. //当高度大于32时, 显示在所有物体上, 并且关闭碰撞
  1103. if (Altitude >= 32)
  1104. {
  1105. AnimatedSprite.ZIndex = 20;
  1106. }
  1107. else
  1108. {
  1109. AnimatedSprite.ZIndex = 0;
  1110. }
  1111. //动态开关碰撞器
  1112. if (ActivityMaterial.DynamicCollision)
  1113. {
  1114. Collision.Disabled = Altitude >= 32;
  1115. }
  1116. //达到最高点
  1117. if (ysp > 0 && ysp * VerticalSpeed < 0)
  1118. {
  1119. OnThrowMaxHeight(Altitude);
  1120. }
  1121.  
  1122. //落地判断
  1123. if (Altitude <= 0)
  1124. {
  1125. _altitude = 0;
  1126.  
  1127. //第一次接触地面
  1128. if (_firstFall)
  1129. {
  1130. _firstFall = false;
  1131. OnFirstFallToGround();
  1132. }
  1133.  
  1134. if (_throwForce != null)
  1135. {
  1136. //缩放移动速度
  1137. //MoveController.ScaleAllForce(BounceSpeed);
  1138. _throwForce.Velocity *= ActivityMaterial.FallBounceSpeed;
  1139. //缩放旋转速度
  1140. //MoveController.ScaleAllRotationSpeed(BounceStrength);
  1141. _throwForce.RotationSpeed *= ActivityMaterial.FallBounceRotation;
  1142. }
  1143. //如果落地高度不够低, 再抛一次
  1144. if (ActivityMaterial.Bounce && (!_hasResilienceVerticalSpeed || _resilienceVerticalSpeed > 5))
  1145. {
  1146. if (!_hasResilienceVerticalSpeed)
  1147. {
  1148. _hasResilienceVerticalSpeed = true;
  1149. _resilienceVerticalSpeed = -VerticalSpeed * ActivityMaterial.FallBounceStrength;
  1150. }
  1151. else
  1152. {
  1153. if (_resilienceVerticalSpeed < 25)
  1154. {
  1155. _resilienceVerticalSpeed = _resilienceVerticalSpeed * ActivityMaterial.FallBounceStrength * 0.4f;
  1156. }
  1157. else
  1158. {
  1159. _resilienceVerticalSpeed = _resilienceVerticalSpeed * ActivityMaterial.FallBounceStrength;
  1160. }
  1161. }
  1162. _verticalSpeed = _resilienceVerticalSpeed;
  1163. _isFallOver = false;
  1164.  
  1165. OnFallToGround();
  1166. }
  1167. else //结束
  1168. {
  1169. _verticalSpeed = 0;
  1170.  
  1171. if (_throwForce != null)
  1172. {
  1173. MoveController.RemoveForce(_throwForce);
  1174. _throwForce = null;
  1175. }
  1176. _isFallOver = true;
  1177. OnFallToGround();
  1178. ThrowOver();
  1179. }
  1180. }
  1181. }
  1182.  
  1183. //计算精灵位置
  1184. CalcThrowAnimatedPosition();
  1185. }
  1186. }
  1187.  
  1188. }
  1189.  
  1190. /// <summary>
  1191. /// 更新阴影逻辑
  1192. /// </summary>
  1193. public void UpdateShadowSprite(float delta)
  1194. {
  1195. // 阴影
  1196. if (ShadowSprite.Visible)
  1197. {
  1198. if (!IsCustomShadowSprite)
  1199. {
  1200. //更新阴影贴图, 使其和动画一致
  1201. var anim = AnimatedSprite.Animation;
  1202. var frame = AnimatedSprite.Frame;
  1203. if (_prevAnimation != anim || _prevAnimationFrame != frame)
  1204. {
  1205. //切换阴影动画
  1206. ShadowSprite.Texture = AnimatedSprite.SpriteFrames.GetFrameTexture(anim, AnimatedSprite.Frame);
  1207. }
  1208.  
  1209. _prevAnimation = anim;
  1210. _prevAnimationFrame = frame;
  1211. }
  1212.  
  1213. if (_freezeSprite == null || !_freezeSprite.IsFrozen)
  1214. {
  1215. //计算阴影
  1216. CalcShadowTransform(true);
  1217. }
  1218. }
  1219.  
  1220. }
  1221. /// <summary>
  1222. /// 每物理帧调用一次, 为了防止子类覆盖 _PhysicsProcess(), 给 _PhysicsProcess() 加上了 sealed, 子类需要帧循环函数请重写 PhysicsProcess() 函数
  1223. /// </summary>
  1224. public sealed override void _PhysicsProcess(double delta)
  1225. {
  1226. #if TOOLS
  1227. if (Engine.IsEditorHint())
  1228. {
  1229. return;
  1230. }
  1231. #endif
  1232. var newDelta = (float)delta;
  1233. if (EnableCustomBehavior)
  1234. {
  1235. PhysicsProcess(newDelta);
  1236. }
  1237. //更新组件
  1238. if (_components.Count > 0)
  1239. {
  1240. _updatingComp = true;
  1241. if (EnableCustomBehavior) //启用所有组件
  1242. {
  1243. for (int i = 0; i < _components.Count; i++)
  1244. {
  1245. if (IsDestroyed) return;
  1246. var temp = _components[i].Value;
  1247. if (temp != null && temp.Enable)
  1248. {
  1249. temp.PhysicsProcess(newDelta);
  1250. }
  1251. }
  1252. }
  1253. else //只更新 MoveController 组件
  1254. {
  1255. if (MoveController.Enable)
  1256. {
  1257. MoveController.PhysicsProcess(newDelta);
  1258. }
  1259. }
  1260. _updatingComp = false;
  1261.  
  1262. if (_changeComponents.Count > 0)
  1263. {
  1264. RefreshComponent();
  1265. }
  1266. }
  1267. }
  1268.  
  1269. //更新新增/移除的组件
  1270. private void RefreshComponent()
  1271. {
  1272. for (var i = 0; i < _changeComponents.Count; i++)
  1273. {
  1274. var item = _changeComponents[i];
  1275. if (item.Value) //添加组件
  1276. {
  1277. _components.Add(new KeyValuePair<Type, Component>(item.Key.GetType(), item.Key));
  1278. }
  1279. else //移除组件
  1280. {
  1281. for (var j = 0; j < _components.Count; j++)
  1282. {
  1283. if (_components[i].Value == item.Key)
  1284. {
  1285. _components.RemoveAt(i);
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292.  
  1293. /// <summary>
  1294. /// 绘制函数, 子类不允许重写, 需要绘制函数请重写 DebugDraw()
  1295. /// </summary>
  1296. public sealed override void _Draw()
  1297. {
  1298. #if TOOLS
  1299. if (Engine.IsEditorHint())
  1300. {
  1301. return;
  1302. }
  1303. #endif
  1304. if (IsDebug)
  1305. {
  1306. DebugDraw();
  1307. if (_components.Count > 0)
  1308. {
  1309. var arr = _components.ToArray();
  1310. for (int i = 0; i < arr.Length; i++)
  1311. {
  1312. if (IsDestroyed) return;
  1313. var temp = arr[i].Value;
  1314. if (temp != null && temp.Master == this && temp.Enable)
  1315. {
  1316. temp.DebugDraw();
  1317. }
  1318. }
  1319. }
  1320. }
  1321. }
  1322.  
  1323. /// <summary>
  1324. /// 重新计算物体阴影的位置和旋转信息, 无论是否显示阴影
  1325. /// </summary>
  1326. public void CalcShadowTransform(bool isInTree)
  1327. {
  1328. //偏移
  1329. if (!IsCustomShadowSprite)
  1330. {
  1331. ShadowSprite.Offset = AnimatedSprite.Offset;
  1332. }
  1333.  
  1334. //缩放
  1335. ShadowSprite.Scale = AnimatedSprite.Scale;
  1336. //阴影角度
  1337. ShadowSprite.Rotation = 0;
  1338. //阴影位置计算
  1339. if (isInTree)
  1340. {
  1341. var pos = AnimatedSprite.GlobalPosition;
  1342. ShadowSprite.GlobalPosition = new Vector2(pos.X + ShadowOffset.X, pos.Y + ShadowOffset.Y + Altitude);
  1343. }
  1344. else
  1345. {
  1346. var pos = AnimatedSprite.Position;
  1347. ShadowSprite.Position = new Vector2(pos.X + ShadowOffset.X, pos.Y + ShadowOffset.Y + Altitude);
  1348. }
  1349. }
  1350.  
  1351. /// <summary>
  1352. /// 计算物体精灵和阴影位置
  1353. /// </summary>
  1354. public void CalcThrowAnimatedPosition()
  1355. {
  1356. if (Scale.Y < 0)
  1357. {
  1358. var pos = new Vector2(_fallData.OriginSpritePosition.X, -_fallData.OriginSpritePosition.Y);
  1359. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) - pos.Rotated(Rotation + Mathf.Pi);
  1360. }
  1361. else
  1362. {
  1363. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) + _fallData.OriginSpritePosition.Rotated(Rotation);
  1364. }
  1365. }
  1366.  
  1367.  
  1368. /// <summary>
  1369. /// 销毁物体
  1370. /// </summary>
  1371. public void Destroy()
  1372. {
  1373. if (IsDestroyed)
  1374. {
  1375. return;
  1376. }
  1377. if (_mountObjects != null)
  1378. {
  1379. foreach (var item in _mountObjects)
  1380. {
  1381. item.OnUnmount(this);
  1382. }
  1383.  
  1384. _mountObjects = null;
  1385. }
  1386.  
  1387. IsDestroyed = true;
  1388. if (AffiliationArea != null)
  1389. {
  1390. AffiliationArea.RemoveItem(this);
  1391. }
  1392. QueueFree();
  1393. OnDestroy();
  1394.  
  1395. if (_freezeSprite != null)
  1396. {
  1397. _freezeSprite.Destroy();
  1398. }
  1399. var arr = _components.ToArray();
  1400. for (var i = 0; i < arr.Length; i++)
  1401. {
  1402. arr[i].Value?.Destroy();
  1403. }
  1404.  
  1405. _components.Clear();
  1406. if (_destroySet != null)
  1407. {
  1408. foreach (var destroy in _destroySet)
  1409. {
  1410. destroy.Destroy();
  1411. }
  1412.  
  1413. _destroySet = null;
  1414. }
  1415. }
  1416.  
  1417. /// <summary>
  1418. /// 延时销毁
  1419. /// </summary>
  1420. public void DelayDestroy()
  1421. {
  1422. CallDeferred(nameof(Destroy));
  1423. }
  1424.  
  1425. /// <summary>
  1426. /// 继承指定物体的运动速率
  1427. /// </summary>
  1428. /// <param name="other">目标对象</param>
  1429. /// <param name="scale">继承的速率缩放</param>
  1430. public void InheritVelocity(ActivityObject other, float scale = 0.5f)
  1431. {
  1432. MoveController.AddVelocity(other.Velocity * scale);
  1433. }
  1434.  
  1435. /// <summary>
  1436. /// 获取投抛该物体时所产生的数据, 只在 IsFallOver 为 false 时有效
  1437. /// </summary>
  1438. public ActivityFallData GetFallData()
  1439. {
  1440. if (!IsFallOver && !_fallData.UseOrigin)
  1441. {
  1442. return _fallData;
  1443. }
  1444.  
  1445. return null;
  1446. }
  1447. /// <summary>
  1448. /// 触发投抛动作
  1449. /// </summary>
  1450. private void Throw()
  1451. {
  1452. var parent = GetParent();
  1453. //投抛时必须要加入 YSortLayer 节点下
  1454. if (parent == null)
  1455. {
  1456. this.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  1457. }
  1458. else if (parent != World.Current.YSortLayer)
  1459. {
  1460. Reparent(World.Current.YSortLayer);
  1461. }
  1462.  
  1463. CalcThrowAnimatedPosition();
  1464. //显示阴影
  1465. ShowShadowSprite();
  1466.  
  1467. if (EnableVerticalMotion)
  1468. {
  1469. OnThrowStart();
  1470. }
  1471. }
  1472.  
  1473. /// <summary>
  1474. /// 设置下坠状态下的碰撞器
  1475. /// </summary>
  1476. private void SetFallCollision()
  1477. {
  1478. if (_fallData.UseOrigin)
  1479. {
  1480. _fallData.OriginShape = Collision.Shape;
  1481. _fallData.OriginPosition = Collision.Position;
  1482. _fallData.OriginRotation = Collision.Rotation;
  1483. _fallData.OriginScale = Collision.Scale;
  1484. _fallData.OriginZIndex = ZIndex;
  1485. _fallData.OriginSpritePosition = AnimatedSprite.Position;
  1486. _fallData.OriginCollisionEnable = Collision.Disabled;
  1487. _fallData.OriginCollisionPosition = Collision.Position;
  1488. _fallData.OriginCollisionRotation = Collision.Rotation;
  1489. _fallData.OriginCollisionScale = Collision.Scale;
  1490. _fallData.OriginCollisionMask = CollisionMask;
  1491. _fallData.OriginCollisionLayer = CollisionLayer;
  1492.  
  1493. Collision.Position = Vector2.Zero;
  1494. Collision.Rotation = 0;
  1495. Collision.Scale = Vector2.One;
  1496. ZIndex = 0;
  1497. Collision.Disabled = false;
  1498. Collision.Position = Vector2.Zero;
  1499. Collision.Rotation = 0;
  1500. Collision.Scale = Vector2.One;
  1501. CollisionMask = ThrowCollisionMask;
  1502. CollisionLayer = _fallData.OriginCollisionLayer | PhysicsLayer.Throwing;
  1503. _fallData.UseOrigin = false;
  1504. }
  1505. }
  1506.  
  1507. /// <summary>
  1508. /// 重置碰撞器
  1509. /// </summary>
  1510. private void RestoreCollision()
  1511. {
  1512. if (!_fallData.UseOrigin)
  1513. {
  1514. Collision.Shape = _fallData.OriginShape;
  1515. Collision.Position = _fallData.OriginPosition;
  1516. Collision.Rotation = _fallData.OriginRotation;
  1517. Collision.Scale = _fallData.OriginScale;
  1518. ZIndex = _fallData.OriginZIndex;
  1519. AnimatedSprite.Position = _fallData.OriginSpritePosition;
  1520. Collision.Disabled = _fallData.OriginCollisionEnable;
  1521. Collision.Position = _fallData.OriginCollisionPosition;
  1522. Collision.Rotation = _fallData.OriginCollisionRotation;
  1523. Collision.Scale = _fallData.OriginCollisionScale;
  1524. CollisionMask = _fallData.OriginCollisionMask;
  1525. CollisionLayer = _fallData.OriginCollisionLayer;
  1526.  
  1527. _fallData.UseOrigin = true;
  1528. }
  1529. }
  1530.  
  1531. /// <summary>
  1532. /// 投抛结束
  1533. /// </summary>
  1534. private void ThrowOver()
  1535. {
  1536. var parent = GetParent();
  1537. var roomLayer = World.Current.GetRoomLayer(DefaultLayer);
  1538. if (parent != roomLayer)
  1539. {
  1540. parent.RemoveChild(this);
  1541. roomLayer.AddChild(this);
  1542. }
  1543. RestoreCollision();
  1544.  
  1545. OnThrowOver();
  1546. }
  1547.  
  1548. //初始化投抛状态数据
  1549. private void InitThrowData()
  1550. {
  1551. SetFallCollision();
  1552.  
  1553. _isFallOver = false;
  1554. _firstFall = true;
  1555. _hasResilienceVerticalSpeed = false;
  1556. _resilienceVerticalSpeed = 0;
  1557.  
  1558. Throw();
  1559. }
  1560.  
  1561. /// <summary>
  1562. /// 设置标记, 用于在物体上记录自定义数据
  1563. /// </summary>
  1564. /// <param name="name">标记名称</param>
  1565. /// <param name="v">存入值</param>
  1566. public void SetSign(string name, object v)
  1567. {
  1568. if (_signMap == null)
  1569. {
  1570. _signMap = new Dictionary<string, object>();
  1571. }
  1572.  
  1573. _signMap[name] = v;
  1574. }
  1575.  
  1576. /// <summary>
  1577. /// 返回是否存在指定名称的标记数据
  1578. /// </summary>
  1579. public bool HasSign(string name)
  1580. {
  1581. return _signMap == null ? false : _signMap.ContainsKey(name);
  1582. }
  1583.  
  1584. /// <summary>
  1585. /// 根据名称获取标记值
  1586. /// </summary>
  1587. public object GetSign(string name)
  1588. {
  1589. if (_signMap == null)
  1590. {
  1591. return null;
  1592. }
  1593.  
  1594. _signMap.TryGetValue(name, out var value);
  1595. return value;
  1596. }
  1597.  
  1598. /// <summary>
  1599. /// 根据名称获取标记值
  1600. /// </summary>
  1601. public T GetSign<T>(string name)
  1602. {
  1603. if (_signMap == null)
  1604. {
  1605. return default;
  1606. }
  1607.  
  1608. _signMap.TryGetValue(name, out var value);
  1609. if (value is T v)
  1610. {
  1611. return v;
  1612. }
  1613. return default;
  1614. }
  1615.  
  1616. /// <summary>
  1617. /// 根据名称删除标记
  1618. /// </summary>
  1619. public void RemoveSign(string name)
  1620. {
  1621. if (_signMap != null)
  1622. {
  1623. _signMap.Remove(name);
  1624. }
  1625. }
  1626.  
  1627. /// <summary>
  1628. /// 播放受伤动画, 该动画不与 Animation 节点的动画冲突
  1629. /// </summary>
  1630. public void PlayHitAnimation()
  1631. {
  1632. _playHit = true;
  1633. _playHitSchedule = 0;
  1634. }
  1635.  
  1636. /// <summary>
  1637. /// 获取当前摩擦力
  1638. /// </summary>
  1639. public float GetCurrentFriction()
  1640. {
  1641. return ActivityMaterial.Friction;
  1642. }
  1643. /// <summary>
  1644. /// 获取当前旋转摩擦力
  1645. /// </summary>
  1646. public float GetCurrentRotationFriction()
  1647. {
  1648. return ActivityMaterial.RotationFriction;
  1649. }
  1650. public long StartCoroutine(IEnumerator able)
  1651. {
  1652. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  1653. }
  1654. public void StopCoroutine(long coroutineId)
  1655. {
  1656. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  1657. }
  1658.  
  1659. public bool IsCoroutineOver(long coroutineId)
  1660. {
  1661. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  1662. }
  1663.  
  1664. public void StopAllCoroutine()
  1665. {
  1666. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  1667. }
  1668. /// <summary>
  1669. /// 播放 AnimatedSprite 上的动画, 如果没有这个动画, 则什么也不会发生
  1670. /// </summary>
  1671. /// <param name="name">动画名称</param>
  1672. public void PlaySpriteAnimation(string name)
  1673. {
  1674. var spriteFrames = AnimatedSprite.SpriteFrames;
  1675. if (spriteFrames != null && spriteFrames.HasAnimation(name))
  1676. {
  1677. AnimatedSprite.Play(name);
  1678. }
  1679. }
  1680.  
  1681. /// <summary>
  1682. /// 将当前 ActivityObject 变成静态图像绘制到地面上, 用于优化渲染大量物体<br/>
  1683. /// 调用该函数后会排队进入渲染队列, 并且禁用所有行为, 当渲染完成后会销毁当前对象, 也就是调用 Destroy() 函数<br/>
  1684. /// </summary>
  1685. public void BecomesStaticImage()
  1686. {
  1687. if (_processingBecomesStaticImage)
  1688. {
  1689. return;
  1690. }
  1691. if (AffiliationArea == null)
  1692. {
  1693. Debug.LogError($"调用函数: BecomesStaticImage() 失败, 物体{Name}没有归属区域, 无法确定绘制到哪个ImageCanvas上, 直接执行销毁");
  1694. Destroy();
  1695. return;
  1696. }
  1697.  
  1698. _processingBecomesStaticImage = true;
  1699. EnableBehavior = false;
  1700. var roomInfo = AffiliationArea.RoomInfo;
  1701. var position = roomInfo.ToCanvasPosition(GlobalPosition);
  1702. roomInfo.StaticImageCanvas.DrawActivityObjectInCanvas(this, position.X, position.Y, () =>
  1703. {
  1704. Destroy();
  1705. });
  1706. }
  1707.  
  1708. /// <summary>
  1709. /// 是否正在处理成为静态图片
  1710. /// </summary>
  1711. public bool IsProcessingBecomesStaticImage()
  1712. {
  1713. return _processingBecomesStaticImage;
  1714. }
  1715. /// <summary>
  1716. /// 冻结物体,多余的节点就会被移出场景树,逻辑也会被暂停,用于优化性能
  1717. /// </summary>
  1718. public void Freeze()
  1719. {
  1720. if (_freezeSprite == null)
  1721. {
  1722. _freezeSprite = new FreezeSprite(this);
  1723. }
  1724. _freezeSprite.Freeze();
  1725. }
  1726.  
  1727. /// <summary>
  1728. /// 解冻物体, 恢复正常逻辑
  1729. /// </summary>
  1730. public void Unfreeze()
  1731. {
  1732. if (_freezeSprite == null)
  1733. {
  1734. return;
  1735. }
  1736. _freezeSprite.Unfreeze();
  1737. }
  1738.  
  1739. /// <summary>
  1740. /// 获取中心点坐标
  1741. /// </summary>
  1742. public Vector2 GetCenterPosition()
  1743. {
  1744. return AnimatedSprite.Position + Position;
  1745. }
  1746.  
  1747. /// <summary>
  1748. /// 设置物体朝向
  1749. /// </summary>
  1750. public void SetForwardDirection(FaceDirection face)
  1751. {
  1752. if ((face == FaceDirection.Left && Scale.X > 0) || (face == FaceDirection.Right && Scale.X < 0))
  1753. {
  1754. Scale *= new Vector2(-1, 1);
  1755. }
  1756. }
  1757. /// <summary>
  1758. /// 添加一个击退力
  1759. /// </summary>
  1760. public void AddRepelForce(Vector2 velocity)
  1761. {
  1762. if (_repelForce == null)
  1763. {
  1764. _repelForce = new ExternalForce(ForceNames.Repel);
  1765. }
  1766.  
  1767. //不在 MoveController 中
  1768. if (_repelForce.MoveController == null)
  1769. {
  1770. _repelForce.Velocity = velocity;
  1771. MoveController.AddForce(_repelForce);
  1772. }
  1773. else
  1774. {
  1775. _repelForce.Velocity += velocity;
  1776. }
  1777. }
  1778.  
  1779. /// <summary>
  1780. /// 获取击退力
  1781. /// </summary>
  1782. public Vector2 GetRepelForce()
  1783. {
  1784. if (_repelForce == null || _repelForce.MoveController == null)
  1785. {
  1786. return Vector2.Zero;
  1787. }
  1788.  
  1789. return _repelForce.Velocity;
  1790. }
  1791.  
  1792. /// <summary>
  1793. /// 根据笔刷 id 在该物体位置绘制液体, 该 id 为 LiquidMaterial 表的 id<br/>
  1794. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1795. /// </summary>
  1796. public void DrawLiquid(string brushId)
  1797. {
  1798. if (AffiliationArea != null)
  1799. {
  1800. DrawLiquid(LiquidBrushManager.GetBrush(brushId));
  1801. }
  1802. }
  1803. /// <summary>
  1804. /// 根据笔刷数据在该物体位置绘制液体<br/>
  1805. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1806. /// </summary>
  1807. public void DrawLiquid(BrushImageData brush)
  1808. {
  1809. if (AffiliationArea != null)
  1810. {
  1811. var pos = AffiliationArea.RoomInfo.LiquidCanvas.ToLiquidCanvasPosition(Position);
  1812. AffiliationArea.RoomInfo.LiquidCanvas.DrawBrush(brush, BrushPrevPosition, pos, 0);
  1813. BrushPrevPosition = pos;
  1814. }
  1815. }
  1816. /// <summary>
  1817. /// 根据笔刷 id 在该物体位置绘制液体, 该 id 为 LiquidMaterial 表的 id<br/>
  1818. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1819. /// </summary>
  1820. public void DrawLiquid(string brushId, Vector2I offset)
  1821. {
  1822. if (AffiliationArea != null)
  1823. {
  1824. DrawLiquid(LiquidBrushManager.GetBrush(brushId), offset);
  1825. }
  1826. }
  1827. /// <summary>
  1828. /// 根据笔刷数据在该物体位置绘制液体<br/>
  1829. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1830. /// </summary>
  1831. public void DrawLiquid(BrushImageData brush, Vector2I offset)
  1832. {
  1833. if (AffiliationArea != null)
  1834. {
  1835. var pos = AffiliationArea.RoomInfo.LiquidCanvas.ToLiquidCanvasPosition(Position) + offset;
  1836. AffiliationArea.RoomInfo.LiquidCanvas.DrawBrush(brush, BrushPrevPosition, pos, 0);
  1837. BrushPrevPosition = pos;
  1838. }
  1839. }
  1840.  
  1841. /// <summary>
  1842. /// 绑定可销毁对象, 绑定的物体会在当前物体销毁时触发销毁
  1843. /// </summary>
  1844. public void AddDestroyObject(IDestroy destroy)
  1845. {
  1846. if (_destroySet == null)
  1847. {
  1848. _destroySet = new HashSet<IDestroy>();
  1849. }
  1850.  
  1851. _destroySet.Add(destroy);
  1852. }
  1853.  
  1854. /// <summary>
  1855. /// 移除绑定可销毁对象
  1856. /// </summary>
  1857. public void RemoveDestroyObject(IDestroy destroy)
  1858. {
  1859. if (_destroySet == null)
  1860. {
  1861. return;
  1862. }
  1863. _destroySet.Remove(destroy);
  1864. }
  1865.  
  1866. /// <summary>
  1867. /// 绑定挂载对象, 绑定的物体会在当前物体销毁时触发扔出
  1868. /// </summary>
  1869. public void AddMountObject(IMountItem target)
  1870. {
  1871. if (_mountObjects == null)
  1872. {
  1873. _mountObjects = new HashSet<IMountItem>();
  1874. }
  1875.  
  1876. if (_mountObjects.Add(target))
  1877. {
  1878. target.OnMount(this);
  1879. }
  1880. }
  1881. /// <summary>
  1882. /// 移除绑定挂载对象
  1883. /// </summary>
  1884. public void RemoveMountObject(IMountItem target)
  1885. {
  1886. if (_mountObjects == null)
  1887. {
  1888. return;
  1889. }
  1890.  
  1891. if (_mountObjects.Remove(target))
  1892. {
  1893. target.OnUnmount(this);
  1894. }
  1895. }
  1896.  
  1897. /// <summary>
  1898. /// 设置是否启用碰撞层, 该函数是设置下坠状态下原碰撞层
  1899. /// </summary>
  1900. public void SetOriginCollisionLayerValue(uint layer, bool vale)
  1901. {
  1902. if (vale)
  1903. {
  1904. if (!Utils.CollisionMaskWithLayer(_fallData.OriginCollisionLayer, layer))
  1905. {
  1906. _fallData.OriginCollisionLayer |= layer;
  1907. }
  1908. }
  1909. else
  1910. {
  1911. if (Utils.CollisionMaskWithLayer(_fallData.OriginCollisionLayer, layer))
  1912. {
  1913. _fallData.OriginCollisionLayer ^= layer;
  1914. }
  1915. }
  1916. }
  1917. }