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