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