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