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