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