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. [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; private 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();
  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. spriteFrames.SetFrame("default", 0, texture);
  458. }
  459. AnimatedSprite.Play("default");
  460. }
  461.  
  462. /// <summary>
  463. /// 获取默认序列帧动画的第一帧
  464. /// </summary>
  465. public Texture2D GetDefaultTexture()
  466. {
  467. return AnimatedSprite.SpriteFrames.GetFrameTexture("default", 0);
  468. }
  469. /// <summary>
  470. /// 获取当前序列帧动画的 Texture2D
  471. /// </summary>
  472. public Texture2D GetCurrentTexture()
  473. {
  474. var spriteFrames = AnimatedSprite.SpriteFrames;
  475. if (spriteFrames == null)
  476. {
  477. return null;
  478. }
  479. return spriteFrames.GetFrameTexture(AnimatedSprite.Animation, AnimatedSprite.Frame);
  480. }
  481.  
  482. /// <summary>
  483. /// 物体初始化时调用
  484. /// </summary>
  485. public virtual void OnInit()
  486. {
  487. }
  488. /// <summary>
  489. /// 进入场景树时调用
  490. /// </summary>
  491. public virtual void EnterTree()
  492. {
  493. }
  494.  
  495. /// <summary>
  496. /// 离开场景树时调用
  497. /// </summary>
  498. public virtual void ExitTree()
  499. {
  500. }
  501. /// <summary>
  502. /// 返回是否能与其他ActivityObject互动
  503. /// </summary>
  504. /// <param name="master">触发者</param>
  505. public virtual CheckInteractiveResult CheckInteractive(ActivityObject master)
  506. {
  507. return new CheckInteractiveResult(this);
  508. }
  509.  
  510. /// <summary>
  511. /// 与其它ActivityObject互动时调用, 如果要检测是否能互动请 CheckInteractive() 函数, 如果直接调用该函数那么属于强制互动行为, 例如子弹碰到物体
  512. /// </summary>
  513. /// <param name="master">触发者</param>
  514. public virtual void Interactive(ActivityObject master)
  515. {
  516. }
  517.  
  518. /// <summary>
  519. /// 开始投抛该物体时调用
  520. /// </summary>
  521. protected virtual void OnThrowStart()
  522. {
  523. }
  524. /// <summary>
  525. /// 投抛该物体达到最高点时调用
  526. /// </summary>
  527. protected virtual void OnThrowMaxHeight(float height)
  528. {
  529. }
  530.  
  531. /// <summary>
  532. /// 投抛状态下第一次接触地面时调用, 之后的回弹落地将不会调用该函数
  533. /// </summary>
  534. protected virtual void OnFirstFallToGround()
  535. {
  536. }
  537.  
  538. /// <summary>
  539. /// 投抛状态下每次接触地面时调用
  540. /// </summary>
  541. protected virtual void OnFallToGround()
  542. {
  543. }
  544.  
  545. /// <summary>
  546. /// 投抛结束时调用
  547. /// </summary>
  548. protected virtual void OnThrowOver()
  549. {
  550. }
  551.  
  552. /// <summary>
  553. /// 当前物体销毁时调用, 销毁物体请调用 Destroy() 函数
  554. /// </summary>
  555. protected virtual void OnDestroy()
  556. {
  557. }
  558.  
  559. /// <summary>
  560. /// 每帧调用一次, 物体的 Process() 会在组件的 Process() 之前调用
  561. /// </summary>
  562. protected virtual void Process(float delta)
  563. {
  564. }
  565. /// <summary>
  566. /// 每物理帧调用一次, 物体的 PhysicsProcess() 会在组件的 PhysicsProcess() 之前调用
  567. /// </summary>
  568. protected virtual void PhysicsProcess(float delta)
  569. {
  570. }
  571. /// <summary>
  572. /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等
  573. /// </summary>
  574. protected virtual void DebugDraw()
  575. {
  576. }
  577.  
  578. /// <summary>
  579. /// 归属区域发生改变
  580. /// </summary>
  581. /// <param name="prevArea">上一个区域, 注意可能为空</param>
  582. protected virtual void OnAffiliationChange(AffiliationArea prevArea)
  583. {
  584. }
  585.  
  586. /// <summary>
  587. /// 移动并碰撞到物体时调用该函数, 参数为碰撞数据, 该函数由 MoveController 调用
  588. /// </summary>
  589. public virtual void OnMoveCollision(KinematicCollision2D collision)
  590. {
  591. }
  592.  
  593. /// <summary>
  594. /// 撞到墙壁反弹时调用该函数, 参数为反弹的角度, 弧度制, 该函数由 MoveController 调用
  595. /// </summary>
  596. public virtual void OnBounce(float rotation)
  597. {
  598. }
  599.  
  600. /// <summary>
  601. /// 返回当物体 CollisionLayer 是否能与 mask 层碰撞
  602. /// </summary>
  603. public bool CollisionWithMask(uint mask)
  604. {
  605. return (CollisionLayer & mask) != 0;
  606. }
  607. /// <summary>
  608. /// 拾起一个 node 节点, 也就是将其从场景树中移除
  609. /// </summary>
  610. public void Pickup()
  611. {
  612. var parent = GetParent();
  613. if (parent != null)
  614. {
  615. if (IsThrowing)
  616. {
  617. StopThrow();
  618. }
  619.  
  620. parent.RemoveChild(this);
  621. }
  622. }
  623.  
  624. /// <summary>
  625. /// 将一个节点扔到地上
  626. /// <param name="layer">放入的层</param>
  627. /// <param name="showShadow">是否显示阴影</param>
  628. /// </summary>
  629. public virtual void PutDown(RoomLayerEnum layer, bool showShadow = true)
  630. {
  631. DefaultLayer = layer;
  632. var parent = GetParent();
  633. var root = World.Current.GetRoomLayer(layer);
  634. if (parent != root)
  635. {
  636. if (parent != null)
  637. {
  638. Reparent(root);
  639. }
  640. else
  641. {
  642. root.AddChild(this);
  643. }
  644. }
  645.  
  646. if (showShadow)
  647. {
  648. if (IsInsideTree())
  649. {
  650. ShowShadowSprite();
  651. }
  652. else
  653. {
  654. //注意需要延时调用
  655. CallDeferred(nameof(ShowShadowSprite));
  656. CalcShadowTransform();
  657. }
  658. }
  659. else
  660. {
  661. ShadowSprite.Visible = false;
  662. }
  663. }
  664.  
  665. /// <summary>
  666. /// 将一个节点扔到地上
  667. /// </summary>
  668. /// <param name="position">放置的位置</param>
  669. /// <param name="layer">放入的层</param>
  670. /// <param name="showShadow">是否显示阴影</param>
  671. public void PutDown(Vector2 position, RoomLayerEnum layer, bool showShadow = true)
  672. {
  673. PutDown(layer);
  674. Position = position;
  675. }
  676.  
  677. /// <summary>
  678. /// 将该节点投抛出去
  679. /// </summary>
  680. /// <param name="altitude">初始高度</param>
  681. /// <param name="verticalSpeed">纵轴速度</param>
  682. /// <param name="velocity">移动速率</param>
  683. /// <param name="rotateSpeed">旋转速度</param>
  684. public void Throw(float altitude, float verticalSpeed, Vector2 velocity, float rotateSpeed)
  685. {
  686. Altitude = altitude;
  687. //Position = Position + new Vector2(0, altitude);
  688. VerticalSpeed = verticalSpeed;
  689. //ThrowRotationDegreesSpeed = rotateSpeed;
  690. if (_throwForce != null)
  691. {
  692. MoveController.RemoveForce(_throwForce);
  693. }
  694.  
  695. _throwForce = new ExternalForce(ForceNames.Throw);
  696. _throwForce.Velocity = velocity;
  697. _throwForce.RotationSpeed = Mathf.DegToRad(rotateSpeed);
  698. MoveController.AddForce(_throwForce);
  699.  
  700. InitThrowData();
  701. }
  702.  
  703. /// <summary>
  704. /// 将该节点投抛出去
  705. /// </summary>
  706. /// <param name="position">初始位置</param>
  707. /// <param name="altitude">初始高度</param>
  708. /// <param name="verticalSpeed">纵轴速度</param>
  709. /// <param name="velocity">移动速率</param>
  710. /// <param name="rotateSpeed">旋转速度</param>
  711. public void Throw(Vector2 position, float altitude, float verticalSpeed, Vector2 velocity, float rotateSpeed)
  712. {
  713. GlobalPosition = position;
  714. Throw(altitude, verticalSpeed, velocity, rotateSpeed);
  715. }
  716.  
  717.  
  718. /// <summary>
  719. /// 强制停止投抛运动
  720. /// </summary>
  721. public void StopThrow()
  722. {
  723. _isFallOver = true;
  724. RestoreCollision();
  725. }
  726.  
  727. /// <summary>
  728. /// 往当前物体上挂载一个组件
  729. /// </summary>
  730. public T AddComponent<T>() where T : Component, new()
  731. {
  732. var component = new T();
  733. if (_updatingComp)
  734. {
  735. _changeComponents.Add(new KeyValuePair<Component, bool>(component, true));
  736. }
  737. else
  738. {
  739. _components.Add(new KeyValuePair<Type, Component>(typeof(T), component));
  740. }
  741.  
  742. component.Master = this;
  743. component.Ready();
  744. component.OnEnable();
  745. return component;
  746. }
  747.  
  748. /// <summary>
  749. /// 往当前物体上挂载一个组件
  750. /// </summary>
  751. public Component AddComponent(Type type)
  752. {
  753. var component = (Component)Activator.CreateInstance(type);
  754. if (_updatingComp)
  755. {
  756. _changeComponents.Add(new KeyValuePair<Component, bool>(component, true));
  757. }
  758. else
  759. {
  760. _components.Add(new KeyValuePair<Type, Component>(type, component));
  761. }
  762. component.Master = this;
  763. component.Ready();
  764. component.OnEnable();
  765. return component;
  766. }
  767.  
  768. /// <summary>
  769. /// 移除一个组件, 并且销毁
  770. /// </summary>
  771. /// <param name="component">组件对象</param>
  772. public void RemoveComponent(Component component)
  773. {
  774. if (component.IsDestroyed)
  775. {
  776. return;
  777. }
  778.  
  779. if (_updatingComp)
  780. {
  781. _changeComponents.Add(new KeyValuePair<Component, bool>(component, false));
  782. component.Destroy();
  783. }
  784. else
  785. {
  786. for (var i = 0; i < _components.Count; i++)
  787. {
  788. if (_components[i].Value == component)
  789. {
  790. _components.RemoveAt(i);
  791. component.Destroy();
  792. return;
  793. }
  794. }
  795. }
  796. }
  797.  
  798. /// <summary>
  799. /// 根据类型获取一个组件
  800. /// </summary>
  801. public Component GetComponent(Type type)
  802. {
  803. for (int i = 0; i < _components.Count; i++)
  804. {
  805. var temp = _components[i];
  806. if (temp.Key.IsAssignableTo(type))
  807. {
  808. return temp.Value;
  809. }
  810. }
  811.  
  812. if (_updatingComp)
  813. {
  814. for (var i = 0; i < _changeComponents.Count; i++)
  815. {
  816. var temp = _components[i];
  817. if (temp.Value.GetType().IsAssignableTo(type))
  818. {
  819. return temp.Value;
  820. }
  821. }
  822. }
  823.  
  824. return null;
  825. }
  826.  
  827. /// <summary>
  828. /// 根据类型获取一个组件
  829. /// </summary>
  830. public T GetComponent<T>() where T : Component
  831. {
  832. for (int i = 0; i < _components.Count; i++)
  833. {
  834. var temp = _components[i];
  835. if (temp.Value is T component)
  836. {
  837. return component;
  838. }
  839. }
  840.  
  841. if (_updatingComp)
  842. {
  843. for (var i = 0; i < _changeComponents.Count; i++)
  844. {
  845. var temp = _components[i];
  846. if (temp.Value is T component)
  847. {
  848. return component;
  849. }
  850. }
  851. }
  852.  
  853. return null;
  854. }
  855.  
  856. /// <summary>
  857. /// 根据类型获取所有相同类型的组件
  858. /// </summary>
  859. public Component[] GetComponents(Type type)
  860. {
  861. var list = new List<Component>();
  862. for (int i = 0; i < _components.Count; i++)
  863. {
  864. var temp = _components[i];
  865. if (temp.Key.IsAssignableTo(type))
  866. {
  867. list.Add(temp.Value);
  868. }
  869. }
  870.  
  871. if (_updatingComp)
  872. {
  873. for (var i = 0; i < _changeComponents.Count; i++)
  874. {
  875. var temp = _components[i];
  876. if (temp.Value.GetType().IsAssignableTo(type))
  877. {
  878. list.Add(temp.Value);
  879. }
  880. }
  881. }
  882.  
  883. return list.ToArray();
  884. }
  885. /// <summary>
  886. /// 根据类型获取所有相同类型的组件
  887. /// </summary>
  888. public T[] GetComponents<T>() where T : Component
  889. {
  890. var list = new List<T>();
  891. for (int i = 0; i < _components.Count; i++)
  892. {
  893. var temp = _components[i];
  894. if (temp.Value is T component)
  895. {
  896. list.Add(component);
  897. }
  898. }
  899.  
  900. if (_updatingComp)
  901. {
  902. for (var i = 0; i < _changeComponents.Count; i++)
  903. {
  904. var temp = _components[i];
  905. if (temp.Value is T component)
  906. {
  907. list.Add(component);
  908. }
  909. }
  910. }
  911.  
  912. return list.ToArray();
  913. }
  914. /// <summary>
  915. /// 设置混色材质的颜色
  916. /// </summary>
  917. public void SetBlendColor(Color color)
  918. {
  919. _blendShaderMaterial?.SetShaderParameter("blend", color);
  920. }
  921.  
  922. /// <summary>
  923. /// 获取混色材质的颜色
  924. /// </summary>
  925. public Color GetBlendColor()
  926. {
  927. if (_blendShaderMaterial == null)
  928. {
  929. return Colors.White;
  930. }
  931. return _blendShaderMaterial.GetShaderParameter("blend").AsColor();
  932. }
  933. /// <summary>
  934. /// 设置混色材质的强度
  935. /// </summary>
  936. public void SetBlendSchedule(float value)
  937. {
  938. _blendShaderMaterial?.SetShaderParameter("schedule", value);
  939. }
  940.  
  941. /// <summary>
  942. /// 获取混色材质的强度
  943. /// </summary>
  944. public float GetBlendSchedule()
  945. {
  946. if (_blendShaderMaterial == null)
  947. {
  948. return default;
  949. }
  950. return _blendShaderMaterial.GetShaderParameter("schedule").AsSingle();
  951. }
  952.  
  953. /// <summary>
  954. /// 设置混色颜色
  955. /// </summary>
  956. public void SetBlendModulate(Color color)
  957. {
  958. _blendShaderMaterial?.SetShaderParameter("modulate", color);
  959. _shadowBlendShaderMaterial?.SetShaderParameter("modulate", color);
  960. }
  961. /// <summary>
  962. /// 获取混色颜色
  963. /// </summary>
  964. public Color SetBlendModulate()
  965. {
  966. if (_blendShaderMaterial == null)
  967. {
  968. return Colors.White;
  969. }
  970. return _blendShaderMaterial.GetShaderParameter("modulate").AsColor();
  971. }
  972. /// <summary>
  973. /// 每帧调用一次, 为了防止子类覆盖 _Process(), 给 _Process() 加上了 sealed, 子类需要帧循环函数请重写 Process() 函数
  974. /// </summary>
  975. public sealed override void _Process(double delta)
  976. {
  977. #if TOOLS
  978. if (Engine.IsEditorHint())
  979. {
  980. return;
  981. }
  982. #endif
  983. var newDelta = (float)delta;
  984. if (EnableCustomBehavior)
  985. {
  986. Process(newDelta);
  987. }
  988. //更新组件
  989. if (_components.Count > 0)
  990. {
  991. _updatingComp = true;
  992. if (EnableCustomBehavior) //启用所有组件
  993. {
  994. for (int i = 0; i < _components.Count; i++)
  995. {
  996. if (IsDestroyed) return;
  997. var temp = _components[i].Value;
  998. if (temp != null && temp.Enable)
  999. {
  1000. temp.Process(newDelta);
  1001. }
  1002. }
  1003. }
  1004. else //只更新 MoveController 组件
  1005. {
  1006. if (MoveController.Enable)
  1007. {
  1008. MoveController.Process(newDelta);
  1009. }
  1010. }
  1011. _updatingComp = false;
  1012. if (_changeComponents.Count > 0)
  1013. {
  1014. RefreshComponent();
  1015. }
  1016. }
  1017.  
  1018. // 更新下坠处理逻辑
  1019. UpdateFall(newDelta);
  1020.  
  1021. //阴影
  1022. UpdateShadowSprite(newDelta);
  1023. // Hit 动画
  1024. if (_playHit)
  1025. {
  1026. if (_playHitSchedule < 0.05f)
  1027. {
  1028. _blendShaderMaterial?.SetShaderParameter("schedule", 1);
  1029. }
  1030. else if (_playHitSchedule < 0.15f)
  1031. {
  1032. _blendShaderMaterial?.SetShaderParameter("schedule", Mathf.Lerp(1, 0, (_playHitSchedule - 0.05f) / 0.1f));
  1033. }
  1034. if (_playHitSchedule >= 0.15f)
  1035. {
  1036. _blendShaderMaterial?.SetShaderParameter("schedule", 0);
  1037. _playHitSchedule = 0;
  1038. _playHit = false;
  1039. }
  1040. else
  1041. {
  1042. _playHitSchedule += newDelta;
  1043. }
  1044. }
  1045. //协程更新
  1046. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  1047. //调试绘制
  1048. if (IsDebug)
  1049. {
  1050. QueueRedraw();
  1051. }
  1052. }
  1053.  
  1054. /// <summary>
  1055. /// 更新下坠处理逻辑
  1056. /// </summary>
  1057. public void UpdateFall(float delta)
  1058. {
  1059. // 下坠判定
  1060. if (Altitude > 0 || VerticalSpeed != 0)
  1061. {
  1062. if (_isFallOver) // 没有处于下坠状态, 则进入下坠状态
  1063. {
  1064. InitThrowData();
  1065. }
  1066. else
  1067. {
  1068. if (EnableVerticalMotion) //如果启用了纵向运动, 则更新运动
  1069. {
  1070. //GlobalRotationDegrees = GlobalRotationDegrees + ThrowRotationDegreesSpeed * newDelta;
  1071.  
  1072. var ysp = VerticalSpeed;
  1073.  
  1074. _altitude += VerticalSpeed * delta;
  1075. _verticalSpeed -= GameConfig.G * ActivityMaterial.GravityScale * delta;
  1076.  
  1077. //当高度大于32时, 显示在所有物体上, 并且关闭碰撞
  1078. if (Altitude >= 32)
  1079. {
  1080. AnimatedSprite.ZIndex = 20;
  1081. }
  1082. else
  1083. {
  1084. AnimatedSprite.ZIndex = 0;
  1085. }
  1086. //动态开关碰撞器
  1087. if (ActivityMaterial.DynamicCollision)
  1088. {
  1089. Collision.Disabled = Altitude >= 32;
  1090. }
  1091. //达到最高点
  1092. if (ysp > 0 && ysp * VerticalSpeed < 0)
  1093. {
  1094. OnThrowMaxHeight(Altitude);
  1095. }
  1096.  
  1097. //落地判断
  1098. if (Altitude <= 0)
  1099. {
  1100. _altitude = 0;
  1101.  
  1102. //第一次接触地面
  1103. if (_firstFall)
  1104. {
  1105. _firstFall = false;
  1106. OnFirstFallToGround();
  1107. }
  1108.  
  1109. if (_throwForce != null)
  1110. {
  1111. //缩放移动速度
  1112. //MoveController.ScaleAllForce(BounceSpeed);
  1113. _throwForce.Velocity *= ActivityMaterial.FallBounceSpeed;
  1114. //缩放旋转速度
  1115. //MoveController.ScaleAllRotationSpeed(BounceStrength);
  1116. _throwForce.RotationSpeed *= ActivityMaterial.FallBounceRotation;
  1117. }
  1118. //如果落地高度不够低, 再抛一次
  1119. if (ActivityMaterial.Bounce && (!_hasResilienceVerticalSpeed || _resilienceVerticalSpeed > 5))
  1120. {
  1121. if (!_hasResilienceVerticalSpeed)
  1122. {
  1123. _hasResilienceVerticalSpeed = true;
  1124. _resilienceVerticalSpeed = -VerticalSpeed * ActivityMaterial.FallBounceStrength;
  1125. }
  1126. else
  1127. {
  1128. if (_resilienceVerticalSpeed < 25)
  1129. {
  1130. _resilienceVerticalSpeed = _resilienceVerticalSpeed * ActivityMaterial.FallBounceStrength * 0.4f;
  1131. }
  1132. else
  1133. {
  1134. _resilienceVerticalSpeed = _resilienceVerticalSpeed * ActivityMaterial.FallBounceStrength;
  1135. }
  1136. }
  1137. _verticalSpeed = _resilienceVerticalSpeed;
  1138. _isFallOver = false;
  1139.  
  1140. OnFallToGround();
  1141. }
  1142. else //结束
  1143. {
  1144. _verticalSpeed = 0;
  1145.  
  1146. if (_throwForce != null)
  1147. {
  1148. MoveController.RemoveForce(_throwForce);
  1149. _throwForce = null;
  1150. }
  1151. _isFallOver = true;
  1152. OnFallToGround();
  1153. ThrowOver();
  1154. }
  1155. }
  1156. }
  1157.  
  1158. //计算精灵位置
  1159. CalcThrowAnimatedPosition();
  1160. }
  1161. }
  1162.  
  1163. }
  1164.  
  1165. /// <summary>
  1166. /// 更新阴影逻辑
  1167. /// </summary>
  1168. public void UpdateShadowSprite(float delta)
  1169. {
  1170. // 阴影
  1171. if (ShadowSprite.Visible)
  1172. {
  1173. if (!IsCustomShadowSprite)
  1174. {
  1175. //更新阴影贴图, 使其和动画一致
  1176. var anim = AnimatedSprite.Animation;
  1177. var frame = AnimatedSprite.Frame;
  1178. if (_prevAnimation != anim || _prevAnimationFrame != frame)
  1179. {
  1180. //切换阴影动画
  1181. ShadowSprite.Texture = AnimatedSprite.SpriteFrames.GetFrameTexture(anim, AnimatedSprite.Frame);
  1182. }
  1183.  
  1184. _prevAnimation = anim;
  1185. _prevAnimationFrame = frame;
  1186. }
  1187.  
  1188. if (_freezeSprite == null || !_freezeSprite.IsFrozen)
  1189. {
  1190. //计算阴影
  1191. CalcShadowTransform();
  1192. }
  1193. }
  1194.  
  1195. }
  1196. /// <summary>
  1197. /// 每物理帧调用一次, 为了防止子类覆盖 _PhysicsProcess(), 给 _PhysicsProcess() 加上了 sealed, 子类需要帧循环函数请重写 PhysicsProcess() 函数
  1198. /// </summary>
  1199. public sealed override void _PhysicsProcess(double delta)
  1200. {
  1201. #if TOOLS
  1202. if (Engine.IsEditorHint())
  1203. {
  1204. return;
  1205. }
  1206. #endif
  1207. var newDelta = (float)delta;
  1208. if (EnableCustomBehavior)
  1209. {
  1210. PhysicsProcess(newDelta);
  1211. }
  1212. //更新组件
  1213. if (_components.Count > 0)
  1214. {
  1215. _updatingComp = true;
  1216. if (EnableCustomBehavior) //启用所有组件
  1217. {
  1218. for (int i = 0; i < _components.Count; i++)
  1219. {
  1220. if (IsDestroyed) return;
  1221. var temp = _components[i].Value;
  1222. if (temp != null && temp.Enable)
  1223. {
  1224. temp.PhysicsProcess(newDelta);
  1225. }
  1226. }
  1227. }
  1228. else //只更新 MoveController 组件
  1229. {
  1230. if (MoveController.Enable)
  1231. {
  1232. MoveController.PhysicsProcess(newDelta);
  1233. }
  1234. }
  1235. _updatingComp = false;
  1236.  
  1237. if (_changeComponents.Count > 0)
  1238. {
  1239. RefreshComponent();
  1240. }
  1241. }
  1242. }
  1243.  
  1244. //更新新增/移除的组件
  1245. private void RefreshComponent()
  1246. {
  1247. for (var i = 0; i < _changeComponents.Count; i++)
  1248. {
  1249. var item = _changeComponents[i];
  1250. if (item.Value) //添加组件
  1251. {
  1252. _components.Add(new KeyValuePair<Type, Component>(item.Key.GetType(), item.Key));
  1253. }
  1254. else //移除组件
  1255. {
  1256. for (var j = 0; j < _components.Count; j++)
  1257. {
  1258. if (_components[i].Value == item.Key)
  1259. {
  1260. _components.RemoveAt(i);
  1261. break;
  1262. }
  1263. }
  1264. }
  1265. }
  1266. }
  1267.  
  1268. /// <summary>
  1269. /// 绘制函数, 子类不允许重写, 需要绘制函数请重写 DebugDraw()
  1270. /// </summary>
  1271. public sealed override void _Draw()
  1272. {
  1273. #if TOOLS
  1274. if (Engine.IsEditorHint())
  1275. {
  1276. return;
  1277. }
  1278. #endif
  1279. if (IsDebug)
  1280. {
  1281. DebugDraw();
  1282. if (_components.Count > 0)
  1283. {
  1284. var arr = _components.ToArray();
  1285. for (int i = 0; i < arr.Length; i++)
  1286. {
  1287. if (IsDestroyed) return;
  1288. var temp = arr[i].Value;
  1289. if (temp != null && temp.Master == this && temp.Enable)
  1290. {
  1291. temp.DebugDraw();
  1292. }
  1293. }
  1294. }
  1295. }
  1296. }
  1297.  
  1298. /// <summary>
  1299. /// 重新计算物体阴影的位置和旋转信息, 无论是否显示阴影
  1300. /// </summary>
  1301. public void CalcShadowTransform()
  1302. {
  1303. //偏移
  1304. if (!IsCustomShadowSprite)
  1305. {
  1306. ShadowSprite.Offset = AnimatedSprite.Offset;
  1307. }
  1308.  
  1309. //缩放
  1310. ShadowSprite.Scale = AnimatedSprite.Scale;
  1311. //阴影角度
  1312. ShadowSprite.Rotation = 0;
  1313. //阴影位置计算
  1314. var pos = AnimatedSprite.GlobalPosition;
  1315. ShadowSprite.GlobalPosition = new Vector2(pos.X + ShadowOffset.X, pos.Y + ShadowOffset.Y + Altitude);
  1316. }
  1317.  
  1318. //计算位置
  1319. private void CalcThrowAnimatedPosition()
  1320. {
  1321. if (Scale.Y < 0)
  1322. {
  1323. var pos = new Vector2(_fallData.OriginSpritePosition.X, -_fallData.OriginSpritePosition.Y);
  1324. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) - pos.Rotated(Rotation + Mathf.Pi);
  1325. }
  1326. else
  1327. {
  1328. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) + _fallData.OriginSpritePosition.Rotated(Rotation);
  1329. }
  1330. }
  1331.  
  1332.  
  1333. /// <summary>
  1334. /// 销毁物体
  1335. /// </summary>
  1336. public void Destroy()
  1337. {
  1338. if (IsDestroyed)
  1339. {
  1340. return;
  1341. }
  1342. if (_mountObjects != null)
  1343. {
  1344. foreach (var item in _mountObjects)
  1345. {
  1346. item.OnUnmount(this);
  1347. }
  1348.  
  1349. _mountObjects = null;
  1350. }
  1351.  
  1352. IsDestroyed = true;
  1353. if (AffiliationArea != null)
  1354. {
  1355. AffiliationArea.RemoveItem(this);
  1356. }
  1357. QueueFree();
  1358. OnDestroy();
  1359.  
  1360. if (_freezeSprite != null)
  1361. {
  1362. _freezeSprite.Destroy();
  1363. }
  1364. var arr = _components.ToArray();
  1365. for (var i = 0; i < arr.Length; i++)
  1366. {
  1367. arr[i].Value?.Destroy();
  1368. }
  1369.  
  1370. _components.Clear();
  1371. if (_destroySet != null)
  1372. {
  1373. foreach (var destroy in _destroySet)
  1374. {
  1375. destroy.Destroy();
  1376. }
  1377.  
  1378. _destroySet = null;
  1379. }
  1380. }
  1381.  
  1382. /// <summary>
  1383. /// 延时销毁
  1384. /// </summary>
  1385. public void DelayDestroy()
  1386. {
  1387. CallDeferred(nameof(Destroy));
  1388. }
  1389.  
  1390. /// <summary>
  1391. /// 继承指定物体的运动速率
  1392. /// </summary>
  1393. /// <param name="other">目标对象</param>
  1394. /// <param name="scale">继承的速率缩放</param>
  1395. public void InheritVelocity(ActivityObject other, float scale = 0.5f)
  1396. {
  1397. MoveController.AddVelocity(other.Velocity * scale);
  1398. }
  1399.  
  1400. /// <summary>
  1401. /// 获取投抛该物体时所产生的数据, 只在 IsFallOver 为 false 时有效
  1402. /// </summary>
  1403. public ActivityFallData GetFallData()
  1404. {
  1405. if (!IsFallOver && !_fallData.UseOrigin)
  1406. {
  1407. return _fallData;
  1408. }
  1409.  
  1410. return null;
  1411. }
  1412. /// <summary>
  1413. /// 触发投抛动作
  1414. /// </summary>
  1415. private void Throw()
  1416. {
  1417. var parent = GetParent();
  1418. //投抛时必须要加入 YSortLayer 节点下
  1419. if (parent == null)
  1420. {
  1421. this.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  1422. }
  1423. else if (parent != World.Current.YSortLayer)
  1424. {
  1425. Reparent(World.Current.YSortLayer);
  1426. }
  1427.  
  1428. CalcThrowAnimatedPosition();
  1429. //显示阴影
  1430. ShowShadowSprite();
  1431.  
  1432. if (EnableVerticalMotion)
  1433. {
  1434. OnThrowStart();
  1435. }
  1436. }
  1437.  
  1438. /// <summary>
  1439. /// 设置下坠状态下的碰撞器
  1440. /// </summary>
  1441. private void SetFallCollision()
  1442. {
  1443. if (_fallData.UseOrigin)
  1444. {
  1445. _fallData.OriginShape = Collision.Shape;
  1446. _fallData.OriginPosition = Collision.Position;
  1447. _fallData.OriginRotation = Collision.Rotation;
  1448. _fallData.OriginScale = Collision.Scale;
  1449. _fallData.OriginZIndex = ZIndex;
  1450. _fallData.OriginSpritePosition = AnimatedSprite.Position;
  1451. _fallData.OriginCollisionEnable = Collision.Disabled;
  1452. _fallData.OriginCollisionPosition = Collision.Position;
  1453. _fallData.OriginCollisionRotation = Collision.Rotation;
  1454. _fallData.OriginCollisionScale = Collision.Scale;
  1455. _fallData.OriginCollisionMask = CollisionMask;
  1456. _fallData.OriginCollisionLayer = CollisionLayer;
  1457.  
  1458. Collision.Position = Vector2.Zero;
  1459. Collision.Rotation = 0;
  1460. Collision.Scale = Vector2.One;
  1461. ZIndex = 0;
  1462. Collision.Disabled = false;
  1463. Collision.Position = Vector2.Zero;
  1464. Collision.Rotation = 0;
  1465. Collision.Scale = Vector2.One;
  1466. CollisionMask = ThrowCollisionMask;
  1467. CollisionLayer = _fallData.OriginCollisionLayer | PhysicsLayer.Throwing;
  1468. _fallData.UseOrigin = false;
  1469. }
  1470. }
  1471.  
  1472. /// <summary>
  1473. /// 重置碰撞器
  1474. /// </summary>
  1475. private void RestoreCollision()
  1476. {
  1477. if (!_fallData.UseOrigin)
  1478. {
  1479. Collision.Shape = _fallData.OriginShape;
  1480. Collision.Position = _fallData.OriginPosition;
  1481. Collision.Rotation = _fallData.OriginRotation;
  1482. Collision.Scale = _fallData.OriginScale;
  1483. ZIndex = _fallData.OriginZIndex;
  1484. AnimatedSprite.Position = _fallData.OriginSpritePosition;
  1485. Collision.Disabled = _fallData.OriginCollisionEnable;
  1486. Collision.Position = _fallData.OriginCollisionPosition;
  1487. Collision.Rotation = _fallData.OriginCollisionRotation;
  1488. Collision.Scale = _fallData.OriginCollisionScale;
  1489. CollisionMask = _fallData.OriginCollisionMask;
  1490. CollisionLayer = _fallData.OriginCollisionLayer;
  1491.  
  1492. _fallData.UseOrigin = true;
  1493. }
  1494. }
  1495.  
  1496. /// <summary>
  1497. /// 投抛结束
  1498. /// </summary>
  1499. private void ThrowOver()
  1500. {
  1501. var parent = GetParent();
  1502. var roomLayer = World.Current.GetRoomLayer(DefaultLayer);
  1503. if (parent != roomLayer)
  1504. {
  1505. parent.RemoveChild(this);
  1506. roomLayer.AddChild(this);
  1507. }
  1508. RestoreCollision();
  1509.  
  1510. OnThrowOver();
  1511. }
  1512.  
  1513. //初始化投抛状态数据
  1514. private void InitThrowData()
  1515. {
  1516. SetFallCollision();
  1517.  
  1518. _isFallOver = false;
  1519. _firstFall = true;
  1520. _hasResilienceVerticalSpeed = false;
  1521. _resilienceVerticalSpeed = 0;
  1522.  
  1523. Throw();
  1524. }
  1525.  
  1526. /// <summary>
  1527. /// 设置标记, 用于在物体上记录自定义数据
  1528. /// </summary>
  1529. /// <param name="name">标记名称</param>
  1530. /// <param name="v">存入值</param>
  1531. public void SetSign(string name, object v)
  1532. {
  1533. if (_signMap == null)
  1534. {
  1535. _signMap = new Dictionary<string, object>();
  1536. }
  1537.  
  1538. _signMap[name] = v;
  1539. }
  1540.  
  1541. /// <summary>
  1542. /// 返回是否存在指定名称的标记数据
  1543. /// </summary>
  1544. public bool HasSign(string name)
  1545. {
  1546. return _signMap == null ? false : _signMap.ContainsKey(name);
  1547. }
  1548.  
  1549. /// <summary>
  1550. /// 根据名称获取标记值
  1551. /// </summary>
  1552. public object GetSign(string name)
  1553. {
  1554. if (_signMap == null)
  1555. {
  1556. return null;
  1557. }
  1558.  
  1559. _signMap.TryGetValue(name, out var value);
  1560. return value;
  1561. }
  1562.  
  1563. /// <summary>
  1564. /// 根据名称获取标记值
  1565. /// </summary>
  1566. public T GetSign<T>(string name)
  1567. {
  1568. if (_signMap == null)
  1569. {
  1570. return default;
  1571. }
  1572.  
  1573. _signMap.TryGetValue(name, out var value);
  1574. if (value is T v)
  1575. {
  1576. return v;
  1577. }
  1578. return default;
  1579. }
  1580.  
  1581. /// <summary>
  1582. /// 根据名称删除标记
  1583. /// </summary>
  1584. public void RemoveSign(string name)
  1585. {
  1586. if (_signMap != null)
  1587. {
  1588. _signMap.Remove(name);
  1589. }
  1590. }
  1591.  
  1592. /// <summary>
  1593. /// 播放受伤动画, 该动画不与 Animation 节点的动画冲突
  1594. /// </summary>
  1595. public void PlayHitAnimation()
  1596. {
  1597. _playHit = true;
  1598. _playHitSchedule = 0;
  1599. }
  1600.  
  1601. /// <summary>
  1602. /// 获取当前摩擦力
  1603. /// </summary>
  1604. public float GetCurrentFriction()
  1605. {
  1606. return ActivityMaterial.Friction;
  1607. }
  1608. /// <summary>
  1609. /// 获取当前旋转摩擦力
  1610. /// </summary>
  1611. public float GetCurrentRotationFriction()
  1612. {
  1613. return ActivityMaterial.RotationFriction;
  1614. }
  1615. public long StartCoroutine(IEnumerator able)
  1616. {
  1617. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  1618. }
  1619. public void StopCoroutine(long coroutineId)
  1620. {
  1621. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  1622. }
  1623.  
  1624. public bool IsCoroutineOver(long coroutineId)
  1625. {
  1626. return ProxyCoroutineHandler.ProxyIsCoroutineOver(ref _coroutineList, coroutineId);
  1627. }
  1628.  
  1629. public void StopAllCoroutine()
  1630. {
  1631. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  1632. }
  1633. /// <summary>
  1634. /// 播放 AnimatedSprite 上的动画, 如果没有这个动画, 则什么也不会发生
  1635. /// </summary>
  1636. /// <param name="name">动画名称</param>
  1637. public void PlaySpriteAnimation(string name)
  1638. {
  1639. var spriteFrames = AnimatedSprite.SpriteFrames;
  1640. if (spriteFrames != null && spriteFrames.HasAnimation(name))
  1641. {
  1642. AnimatedSprite.Play(name);
  1643. }
  1644. }
  1645.  
  1646. /// <summary>
  1647. /// 将当前 ActivityObject 变成静态图像绘制到地面上, 用于优化渲染大量物体<br/>
  1648. /// 调用该函数后会排队进入渲染队列, 并且禁用所有行为, 当渲染完成后会销毁当前对象, 也就是调用 Destroy() 函数<br/>
  1649. /// </summary>
  1650. public void BecomesStaticImage()
  1651. {
  1652. if (_processingBecomesStaticImage)
  1653. {
  1654. return;
  1655. }
  1656. if (AffiliationArea == null)
  1657. {
  1658. Debug.LogError($"调用函数: BecomesStaticImage() 失败, 物体{Name}没有归属区域, 无法确定绘制到哪个ImageCanvas上, 直接执行销毁");
  1659. Destroy();
  1660. return;
  1661. }
  1662.  
  1663. _processingBecomesStaticImage = true;
  1664. EnableBehavior = false;
  1665. var roomInfo = AffiliationArea.RoomInfo;
  1666. var position = roomInfo.ToCanvasPosition(GlobalPosition);
  1667. roomInfo.StaticImageCanvas.DrawActivityObjectInCanvas(this, position.X, position.Y, () =>
  1668. {
  1669. Destroy();
  1670. });
  1671. }
  1672.  
  1673. /// <summary>
  1674. /// 是否正在处理成为静态图片
  1675. /// </summary>
  1676. public bool IsProcessingBecomesStaticImage()
  1677. {
  1678. return _processingBecomesStaticImage;
  1679. }
  1680. /// <summary>
  1681. /// 冻结物体,多余的节点就会被移出场景树,逻辑也会被暂停,用于优化性能
  1682. /// </summary>
  1683. public void Freeze()
  1684. {
  1685. if (_freezeSprite == null)
  1686. {
  1687. _freezeSprite = new FreezeSprite(this);
  1688. }
  1689. _freezeSprite.Freeze();
  1690. }
  1691.  
  1692. /// <summary>
  1693. /// 解冻物体, 恢复正常逻辑
  1694. /// </summary>
  1695. public void Unfreeze()
  1696. {
  1697. if (_freezeSprite == null)
  1698. {
  1699. return;
  1700. }
  1701. _freezeSprite.Unfreeze();
  1702. }
  1703.  
  1704. /// <summary>
  1705. /// 获取中心点坐标
  1706. /// </summary>
  1707. public Vector2 GetCenterPosition()
  1708. {
  1709. return AnimatedSprite.Position + Position;
  1710. }
  1711.  
  1712. /// <summary>
  1713. /// 设置物体朝向
  1714. /// </summary>
  1715. public void SetForwardDirection(FaceDirection face)
  1716. {
  1717. if ((face == FaceDirection.Left && Scale.X > 0) || (face == FaceDirection.Right && Scale.X < 0))
  1718. {
  1719. Scale *= new Vector2(-1, 1);
  1720. }
  1721. }
  1722. /// <summary>
  1723. /// 添加一个击退力
  1724. /// </summary>
  1725. public void AddRepelForce(Vector2 velocity)
  1726. {
  1727. if (_repelForce == null)
  1728. {
  1729. _repelForce = new ExternalForce(ForceNames.Repel);
  1730. }
  1731.  
  1732. //不在 MoveController 中
  1733. if (_repelForce.MoveController == null)
  1734. {
  1735. _repelForce.Velocity = velocity;
  1736. MoveController.AddForce(_repelForce);
  1737. }
  1738. else
  1739. {
  1740. _repelForce.Velocity += velocity;
  1741. }
  1742. }
  1743.  
  1744. /// <summary>
  1745. /// 获取击退力
  1746. /// </summary>
  1747. public Vector2 GetRepelForce()
  1748. {
  1749. if (_repelForce == null || _repelForce.MoveController == null)
  1750. {
  1751. return Vector2.Zero;
  1752. }
  1753.  
  1754. return _repelForce.Velocity;
  1755. }
  1756.  
  1757. /// <summary>
  1758. /// 根据笔刷 id 在该物体位置绘制液体, 该 id 为 LiquidMaterial 表的 id<br/>
  1759. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1760. /// </summary>
  1761. public void DrawLiquid(string brushId)
  1762. {
  1763. if (AffiliationArea != null)
  1764. {
  1765. DrawLiquid(LiquidBrushManager.GetBrush(brushId));
  1766. }
  1767. }
  1768. /// <summary>
  1769. /// 根据笔刷数据在该物体位置绘制液体<br/>
  1770. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1771. /// </summary>
  1772. public void DrawLiquid(BrushImageData brush)
  1773. {
  1774. if (AffiliationArea != null)
  1775. {
  1776. var pos = AffiliationArea.RoomInfo.LiquidCanvas.ToLiquidCanvasPosition(Position);
  1777. AffiliationArea.RoomInfo.LiquidCanvas.DrawBrush(brush, BrushPrevPosition, pos, 0);
  1778. BrushPrevPosition = pos;
  1779. }
  1780. }
  1781. /// <summary>
  1782. /// 根据笔刷 id 在该物体位置绘制液体, 该 id 为 LiquidMaterial 表的 id<br/>
  1783. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1784. /// </summary>
  1785. public void DrawLiquid(string brushId, Vector2I offset)
  1786. {
  1787. if (AffiliationArea != null)
  1788. {
  1789. DrawLiquid(LiquidBrushManager.GetBrush(brushId), offset);
  1790. }
  1791. }
  1792. /// <summary>
  1793. /// 根据笔刷数据在该物体位置绘制液体<br/>
  1794. /// 需要清除记录的点就请将 BrushPrevPosition 置为 null
  1795. /// </summary>
  1796. public void DrawLiquid(BrushImageData brush, Vector2I offset)
  1797. {
  1798. if (AffiliationArea != null)
  1799. {
  1800. var pos = AffiliationArea.RoomInfo.LiquidCanvas.ToLiquidCanvasPosition(Position) + offset;
  1801. AffiliationArea.RoomInfo.LiquidCanvas.DrawBrush(brush, BrushPrevPosition, pos, 0);
  1802. BrushPrevPosition = pos;
  1803. }
  1804. }
  1805.  
  1806. /// <summary>
  1807. /// 绑定可销毁对象, 绑定的物体会在当前物体销毁时触发销毁
  1808. /// </summary>
  1809. public void AddDestroyObject(IDestroy destroy)
  1810. {
  1811. if (_destroySet == null)
  1812. {
  1813. _destroySet = new HashSet<IDestroy>();
  1814. }
  1815.  
  1816. _destroySet.Add(destroy);
  1817. }
  1818.  
  1819. /// <summary>
  1820. /// 移除绑定可销毁对象
  1821. /// </summary>
  1822. public void RemoveDestroyObject(IDestroy destroy)
  1823. {
  1824. if (_destroySet == null)
  1825. {
  1826. return;
  1827. }
  1828. _destroySet.Remove(destroy);
  1829. }
  1830.  
  1831. /// <summary>
  1832. /// 绑定挂载对象, 绑定的物体会在当前物体销毁时触发扔出
  1833. /// </summary>
  1834. public void AddMountObject(IMountItem target)
  1835. {
  1836. if (_mountObjects == null)
  1837. {
  1838. _mountObjects = new HashSet<IMountItem>();
  1839. }
  1840.  
  1841. if (_mountObjects.Add(target))
  1842. {
  1843. target.OnMount(this);
  1844. }
  1845. }
  1846. /// <summary>
  1847. /// 移除绑定挂载对象
  1848. /// </summary>
  1849. public void RemoveMountObject(IMountItem target)
  1850. {
  1851. if (_mountObjects == null)
  1852. {
  1853. return;
  1854. }
  1855.  
  1856. if (_mountObjects.Remove(target))
  1857. {
  1858. target.OnUnmount(this);
  1859. }
  1860. }
  1861.  
  1862. /// <summary>
  1863. /// 设置是否启用碰撞层, 该函数是设置下载状态下原碰撞层
  1864. /// </summary>
  1865. public void SetOriginCollisionLayerValue(uint layer, bool vale)
  1866. {
  1867. if (vale)
  1868. {
  1869. if (!Utils.CollisionMaskWithLayer(_fallData.OriginCollisionLayer, layer))
  1870. {
  1871. _fallData.OriginCollisionLayer |= layer;
  1872. }
  1873. }
  1874. else
  1875. {
  1876. if (Utils.CollisionMaskWithLayer(_fallData.OriginCollisionLayer, layer))
  1877. {
  1878. _fallData.OriginCollisionLayer ^= layer;
  1879. }
  1880. }
  1881. }
  1882. }