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