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 Affiliation
  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.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. return AnimatedSprite.SpriteFrames.GetFrameTexture(AnimatedSprite.Name, AnimatedSprite.Frame);
  360. }
  361.  
  362. /// <summary>
  363. /// 物体初始化时调用
  364. /// </summary>
  365. public virtual void OnInit()
  366. {
  367. }
  368. /// <summary>
  369. /// 返回是否能与其他ActivityObject互动
  370. /// </summary>
  371. /// <param name="master">触发者</param>
  372. public virtual CheckInteractiveResult CheckInteractive(ActivityObject master)
  373. {
  374. return new CheckInteractiveResult(this);
  375. }
  376.  
  377. /// <summary>
  378. /// 与其它ActivityObject互动时调用, 如果要检测是否能互动请 CheckInteractive() 函数, 如果直接调用该函数那么属于强制互动行为, 例如子弹碰到物体
  379. /// </summary>
  380. /// <param name="master">触发者</param>
  381. public virtual void Interactive(ActivityObject master)
  382. {
  383. }
  384.  
  385. /// <summary>
  386. /// 开始投抛该物体时调用
  387. /// </summary>
  388. protected virtual void OnThrowStart()
  389. {
  390. }
  391. /// <summary>
  392. /// 投抛该物体达到最高点时调用
  393. /// </summary>
  394. protected virtual void OnThrowMaxHeight(float height)
  395. {
  396. }
  397.  
  398. /// <summary>
  399. /// 投抛状态下第一次接触地面时调用, 之后的回弹落地将不会调用该函数
  400. /// </summary>
  401. protected virtual void OnFirstFallToGround()
  402. {
  403. }
  404.  
  405. /// <summary>
  406. /// 投抛状态下每次接触地面时调用
  407. /// </summary>
  408. protected virtual void OnFallToGround()
  409. {
  410. }
  411.  
  412. /// <summary>
  413. /// 投抛结束时调用
  414. /// </summary>
  415. protected virtual void OnThrowOver()
  416. {
  417. }
  418.  
  419. /// <summary>
  420. /// 当前物体销毁时调用, 销毁物体请调用 Destroy() 函数
  421. /// </summary>
  422. protected virtual void OnDestroy()
  423. {
  424. }
  425.  
  426. /// <summary>
  427. /// 每帧调用一次, 物体的 Process() 会在组件的 Process() 之前调用
  428. /// </summary>
  429. protected virtual void Process(float delta)
  430. {
  431. }
  432. /// <summary>
  433. /// 每帧调用一次, ProcessOver() 会在组件的 Process() 之后调用
  434. /// </summary>
  435. protected virtual void ProcessOver(float delta)
  436. {
  437. }
  438. /// <summary>
  439. /// 每物理帧调用一次, 物体的 PhysicsProcess() 会在组件的 PhysicsProcess() 之前调用
  440. /// </summary>
  441. protected virtual void PhysicsProcess(float delta)
  442. {
  443. }
  444. /// <summary>
  445. /// 每物理帧调用一次, PhysicsProcessOver() 会在组件的 PhysicsProcess() 之后调用
  446. /// </summary>
  447. protected virtual void PhysicsProcessOver(float delta)
  448. {
  449. }
  450. /// <summary>
  451. /// 如果开启 debug, 则每帧调用该函数, 可用于绘制文字线段等
  452. /// </summary>
  453. protected virtual void DebugDraw()
  454. {
  455. }
  456.  
  457. /// <summary>
  458. /// 归属区域发生改变
  459. /// </summary>
  460. protected virtual void OnAffiliationChange()
  461. {
  462. }
  463.  
  464. /// <summary>
  465. /// 返回当物体 CollisionLayer 是否能与 mask 层碰撞
  466. /// </summary>
  467. public bool CollisionWithMask(uint mask)
  468. {
  469. return (CollisionLayer & mask) != 0;
  470. }
  471. /// <summary>
  472. /// 拾起一个 node 节点, 也就是将其从场景树中移除
  473. /// </summary>
  474. public void Pickup()
  475. {
  476. var parent = GetParent();
  477. if (parent != null)
  478. {
  479. if (IsThrowing)
  480. {
  481. StopThrow();
  482. }
  483.  
  484. parent.RemoveChild(this);
  485. }
  486. }
  487.  
  488. /// <summary>
  489. /// 将一个节点扔到地上
  490. /// <param name="layer">放入的层</param>
  491. /// <param name="showShadow">是否显示阴影</param>
  492. /// </summary>
  493. public virtual void PutDown(RoomLayerEnum layer, bool showShadow = true)
  494. {
  495. _currLayer = layer;
  496. var parent = GetParent();
  497. var root = GameApplication.Instance.World.GetRoomLayer(layer);
  498. if (parent != root)
  499. {
  500. if (parent != null)
  501. {
  502. parent.RemoveChild(this);
  503. }
  504.  
  505. this.AddToActivityRoot(layer);
  506. }
  507.  
  508. if (showShadow)
  509. {
  510. if (IsInsideTree())
  511. {
  512. ShowShadowSprite();
  513. }
  514. else
  515. {
  516. //注意需要延时调用
  517. CallDeferred(nameof(ShowShadowSprite));
  518. }
  519. }
  520. else
  521. {
  522. ShadowSprite.Visible = false;
  523. }
  524. }
  525.  
  526. /// <summary>
  527. /// 将一个节点扔到地上
  528. /// </summary>
  529. /// <param name="position">放置的位置</param>
  530. /// <param name="layer">放入的层</param>
  531. /// <param name="showShadow">是否显示阴影</param>
  532. public void PutDown(Vector2 position, RoomLayerEnum layer, bool showShadow = true)
  533. {
  534. PutDown(layer);
  535. Position = position;
  536. }
  537.  
  538. /// <summary>
  539. /// 将该节点投抛出去
  540. /// </summary>
  541. /// <param name="altitude">初始高度</param>
  542. /// <param name="verticalSpeed">纵轴速度</param>
  543. /// <param name="velocity">移动速率</param>
  544. /// <param name="rotate">旋转速度</param>
  545. public void Throw(float altitude, float verticalSpeed, Vector2 velocity, float rotate)
  546. {
  547. var parent = GetParent();
  548. if (parent == null)
  549. {
  550. GameApplication.Instance.World.YSortLayer.AddChild(this);
  551. }
  552. else if (parent != GameApplication.Instance.World.YSortLayer)
  553. {
  554. parent.RemoveChild(this);
  555. GameApplication.Instance.World.YSortLayer.AddChild(this);
  556. }
  557. Altitude = altitude;
  558. VerticalSpeed = verticalSpeed;
  559. ThrowRotationDegreesSpeed = rotate;
  560. if (_throwForce != null)
  561. {
  562. MoveController.RemoveForce(_throwForce);
  563. }
  564.  
  565. _throwForce = new ExternalForce("throw");
  566. _throwForce.Velocity = velocity;
  567. MoveController.AddConstantForce(_throwForce);
  568. }
  569.  
  570. /// <summary>
  571. /// 将该节点投抛出去
  572. /// </summary>
  573. /// <param name="position">初始位置</param>
  574. /// <param name="altitude">初始高度</param>
  575. /// <param name="verticalSpeed">纵轴速度</param>
  576. /// <param name="velocity">移动速率</param>
  577. /// <param name="rotate">旋转速度</param>
  578. public void Throw(Vector2 position, float altitude, float verticalSpeed, Vector2 velocity, float rotate)
  579. {
  580. GlobalPosition = position;
  581. Throw(altitude, verticalSpeed, velocity, rotate);
  582. }
  583.  
  584.  
  585. /// <summary>
  586. /// 强制停止投抛运动
  587. /// </summary>
  588. public void StopThrow()
  589. {
  590. _isFallOver = true;
  591. RestoreCollision();
  592. }
  593.  
  594. /// <summary>
  595. /// 往当前物体上挂载一个组件
  596. /// </summary>
  597. public T AddComponent<T>() where T : Component, new()
  598. {
  599. var component = new T();
  600. _components.Add(new KeyValuePair<Type, Component>(typeof(T), component));
  601. component.ActivityInstance = this;
  602. return component;
  603. }
  604.  
  605. /// <summary>
  606. /// 移除一个组件, 并且销毁
  607. /// </summary>
  608. /// <param name="component">组件对象</param>
  609. public void RemoveComponent(Component component)
  610. {
  611. for (int i = 0; i < _components.Count; i++)
  612. {
  613. if (_components[i].Value == component)
  614. {
  615. _components.RemoveAt(i);
  616. component.Destroy();
  617. return;
  618. }
  619. }
  620. }
  621.  
  622. /// <summary>
  623. /// 根据类型获取一个组件
  624. /// </summary>
  625. public Component GetComponent(Type type)
  626. {
  627. for (int i = 0; i < _components.Count; i++)
  628. {
  629. var temp = _components[i];
  630. if (temp.Key == type)
  631. {
  632. return temp.Value;
  633. }
  634. }
  635.  
  636. return null;
  637. }
  638.  
  639. /// <summary>
  640. /// 根据类型获取一个组件
  641. /// </summary>
  642. public T GetComponent<T>() where T : Component
  643. {
  644. var component = GetComponent(typeof(T));
  645. if (component == null) return null;
  646. return (T)component;
  647. }
  648.  
  649. /// <summary>
  650. /// 设置混色材质的颜色
  651. /// </summary>
  652. public void SetBlendColor(Color color)
  653. {
  654. _blendShaderMaterial.SetShaderParameter("blend", color);
  655. }
  656.  
  657. /// <summary>
  658. /// 获取混色材质的颜色
  659. /// </summary>
  660. public Color GetBlendColor()
  661. {
  662. return _blendShaderMaterial.GetShaderParameter("blend").AsColor();
  663. }
  664. /// <summary>
  665. /// 设置混色材质的强度
  666. /// </summary>
  667. public void SetBlendSchedule(float value)
  668. {
  669. _blendShaderMaterial.SetShaderParameter("schedule", value);
  670. }
  671.  
  672. /// <summary>
  673. /// 获取混色材质的强度
  674. /// </summary>
  675. public float GetBlendSchedule()
  676. {
  677. return _blendShaderMaterial.GetShaderParameter("schedule").AsSingle();
  678. }
  679. /// <summary>
  680. /// 每帧调用一次, 为了防止子类覆盖 _Process(), 给 _Process() 加上了 sealed, 子类需要帧循环函数请重写 Process() 函数
  681. /// </summary>
  682. public sealed override void _Process(double delta)
  683. {
  684. var newDelta = (float)delta;
  685. if (EnableCustomBehavior)
  686. {
  687. Process(newDelta);
  688. }
  689. //更新组件
  690. if (_components.Count > 0)
  691. {
  692. if (EnableCustomBehavior) //启用所有组件
  693. {
  694. var arr = _components.ToArray();
  695. for (int i = 0; i < arr.Length; i++)
  696. {
  697. if (IsDestroyed) return;
  698. var temp = arr[i].Value;
  699. if (temp != null && temp.ActivityInstance == this && temp.Enable)
  700. {
  701. if (!temp.IsReady)
  702. {
  703. temp.Ready();
  704. temp.IsReady = true;
  705. }
  706.  
  707. temp.Process(newDelta);
  708. }
  709. }
  710. }
  711. else //只更新 MoveController 组件
  712. {
  713. if (!MoveController.IsReady)
  714. {
  715. MoveController.Ready();
  716. MoveController.IsReady = true;
  717. }
  718. MoveController.Process(newDelta);
  719. }
  720. }
  721.  
  722. // 下坠判定
  723. if (Altitude > 0 || VerticalSpeed != 0)
  724. {
  725. if (_fallData == null)
  726. {
  727. _fallData = new ActivityFallData();
  728. }
  729. if (_isFallOver) // 没有处于下坠状态, 则进入下坠状态
  730. {
  731. SetFallCollision();
  732.  
  733. _isFallOver = false;
  734. _firstFall = true;
  735. _hasResilienceVerticalSpeed = false;
  736. _resilienceVerticalSpeed = 0;
  737. if (ThrowCollisionSize.X < 0 && ThrowCollisionSize.Y < 0)
  738. {
  739. _throwRectangleShape.Size = GetDefaultTexture().GetSize();
  740. }
  741. else
  742. {
  743. _throwRectangleShape.Size = ThrowCollisionSize;
  744. }
  745.  
  746. Throw();
  747. }
  748. else
  749. {
  750. if (EnableVerticalMotion) //如果启用了纵向运动, 则更新运动
  751. {
  752. GlobalRotationDegrees = GlobalRotationDegrees + ThrowRotationDegreesSpeed * newDelta;
  753.  
  754. var ysp = VerticalSpeed;
  755.  
  756. _altitude += VerticalSpeed * newDelta;
  757. _verticalSpeed -= GameConfig.G * newDelta;
  758.  
  759. //当高度大于16时, 显示在所有物体上
  760. if (Altitude >= 16)
  761. {
  762. AnimatedSprite.ZIndex = 20;
  763. }
  764. else
  765. {
  766. AnimatedSprite.ZIndex = 0;
  767. }
  768. //达到最高点
  769. if (ysp > 0 && ysp * VerticalSpeed < 0)
  770. {
  771. OnThrowMaxHeight(Altitude);
  772. }
  773.  
  774. //落地判断
  775. if (Altitude <= 0)
  776. {
  777. _altitude = 0;
  778.  
  779. //第一次接触地面
  780. if (_firstFall)
  781. {
  782. _firstFall = false;
  783. OnFirstFallToGround();
  784. }
  785.  
  786. MoveController.ScaleAllForce(BounceSpeed);
  787. //如果落地高度不够低, 再抛一次
  788. if (Bounce && (!_hasResilienceVerticalSpeed || _resilienceVerticalSpeed > 1))
  789. {
  790. if (!_hasResilienceVerticalSpeed)
  791. {
  792. _hasResilienceVerticalSpeed = true;
  793. _resilienceVerticalSpeed = -VerticalSpeed * BounceStrength;
  794. }
  795. else
  796. {
  797. _resilienceVerticalSpeed = _resilienceVerticalSpeed * BounceStrength;
  798. }
  799. _verticalSpeed = _resilienceVerticalSpeed;
  800. ThrowRotationDegreesSpeed = ThrowRotationDegreesSpeed * BounceStrength;
  801. _isFallOver = false;
  802.  
  803. OnFallToGround();
  804. }
  805. else //结束
  806. {
  807. _verticalSpeed = 0;
  808.  
  809. if (_throwForce != null)
  810. {
  811. MoveController.RemoveForce(_throwForce);
  812. _throwForce = null;
  813. }
  814. _isFallOver = true;
  815. OnFallToGround();
  816. ThrowOver();
  817. }
  818. }
  819. }
  820.  
  821. //计算精灵位置
  822. CalcThrowAnimatedPosition();
  823. }
  824. }
  825.  
  826. //阴影
  827. if (ShadowSprite.Visible)
  828. {
  829. //更新阴影贴图, 使其和动画一致
  830. var anim = AnimatedSprite.Animation;
  831. var frame = AnimatedSprite.Frame;
  832. if (_prevAnimation != anim || _prevAnimationFrame != frame)
  833. {
  834. //切换阴影动画
  835. ShadowSprite.Texture = AnimatedSprite.SpriteFrames.GetFrameTexture(anim, AnimatedSprite.Frame);
  836. }
  837.  
  838. _prevAnimation = anim;
  839. _prevAnimationFrame = frame;
  840.  
  841. //计算阴影
  842. CalcShadow();
  843. }
  844.  
  845. // Hit 动画
  846. if (_playHit)
  847. {
  848. if (_playHitSchedule < 0.05f)
  849. {
  850. _blendShaderMaterial.SetShaderParameter("schedule", 1);
  851. }
  852. else if (_playHitSchedule < 0.15f)
  853. {
  854. _blendShaderMaterial.SetShaderParameter("schedule", Mathf.Lerp(1, 0, (_playHitSchedule - 0.05f) / 0.1f));
  855. }
  856. if (_playHitSchedule >= 0.15f)
  857. {
  858. _blendShaderMaterial.SetShaderParameter("schedule", 0);
  859. _playHitSchedule = 0;
  860. _playHit = false;
  861. }
  862. else
  863. {
  864. _playHitSchedule += newDelta;
  865. }
  866. }
  867. //协程更新
  868. if (_coroutineList != null)
  869. {
  870. ProxyCoroutineHandler.ProxyUpdateCoroutine(ref _coroutineList, newDelta);
  871. }
  872.  
  873. ProcessOver(newDelta);
  874. //调试绘制
  875. if (IsDebug)
  876. {
  877. QueueRedraw();
  878. }
  879. }
  880.  
  881. /// <summary>
  882. /// 每物理帧调用一次, 为了防止子类覆盖 _PhysicsProcess(), 给 _PhysicsProcess() 加上了 sealed, 子类需要帧循环函数请重写 PhysicsProcess() 函数
  883. /// </summary>
  884. public sealed override void _PhysicsProcess(double delta)
  885. {
  886. var newDelta = (float)delta;
  887. if (EnableCustomBehavior)
  888. {
  889. PhysicsProcess(newDelta);
  890. }
  891. //更新组件
  892. if (_components.Count > 0)
  893. {
  894. if (EnableCustomBehavior) //启用所有组件
  895. {
  896. var arr = _components.ToArray();
  897. for (int i = 0; i < arr.Length; i++)
  898. {
  899. if (IsDestroyed) return;
  900. var temp = arr[i].Value;
  901. if (temp != null && temp.ActivityInstance == this && temp.Enable)
  902. {
  903. if (!temp.IsReady)
  904. {
  905. temp.Ready();
  906. temp.IsReady = true;
  907. }
  908.  
  909. temp.PhysicsProcess(newDelta);
  910. }
  911. }
  912. }
  913. else //只更新 MoveController 组件
  914. {
  915. if (!MoveController.IsReady)
  916. {
  917. MoveController.Ready();
  918. MoveController.IsReady = true;
  919. }
  920. MoveController.PhysicsProcess(newDelta);
  921. }
  922. }
  923.  
  924. PhysicsProcessOver(newDelta);
  925. }
  926.  
  927. /// <summary>
  928. /// 绘制函数, 子类不允许重写, 需要绘制函数请重写 DebugDraw()
  929. /// </summary>
  930. public sealed override void _Draw()
  931. {
  932. if (IsDebug)
  933. {
  934. DebugDraw();
  935. var arr = _components.ToArray();
  936. for (int i = 0; i < arr.Length; i++)
  937. {
  938. if (IsDestroyed) return;
  939. var temp = arr[i].Value;
  940. if (temp != null && temp.ActivityInstance == this && temp.Enable)
  941. {
  942. temp.DebugDraw();
  943. }
  944. }
  945. }
  946. }
  947.  
  948. /// <summary>
  949. /// 重新计算物体阴影的位置和旋转信息, 无论是否显示阴影
  950. /// </summary>
  951. public void CalcShadow()
  952. {
  953. //缩放
  954. ShadowSprite.Scale = AnimatedSprite.Scale;
  955. //阴影角度
  956. ShadowSprite.Rotation = 0;
  957. //阴影位置计算
  958. var pos = AnimatedSprite.GlobalPosition;
  959. ShadowSprite.GlobalPosition = new Vector2(pos.X + ShadowOffset.X, pos.Y + ShadowOffset.Y + Altitude);
  960. }
  961.  
  962. //计算位置
  963. private void CalcThrowAnimatedPosition()
  964. {
  965. if (Scale.Y < 0)
  966. {
  967. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) - _fallData.OriginSpritePosition.Rotated(Rotation) * Scale.Abs();
  968. }
  969. else
  970. {
  971. AnimatedSprite.GlobalPosition = GlobalPosition + new Vector2(0, -Altitude) + _fallData.OriginSpritePosition.Rotated(Rotation);
  972. }
  973. }
  974.  
  975.  
  976. /// <summary>
  977. /// 销毁物体
  978. /// </summary>
  979. public void Destroy()
  980. {
  981. if (IsDestroyed)
  982. {
  983. return;
  984. }
  985.  
  986. IsDestroyed = true;
  987. QueueFree();
  988. OnDestroy();
  989.  
  990. var arr = _components.ToArray();
  991. for (int i = 0; i < arr.Length; i++)
  992. {
  993. arr[i].Value?.Destroy();
  994. }
  995. if (Affiliation != null)
  996. {
  997. Affiliation.RemoveItem(this);
  998. }
  999. //临时处理, 4.0 有bug, 不能提前销毁模板实例, 不然关闭游戏会报错!!!
  1000. _templateInstance.QueueFree();
  1001. }
  1002.  
  1003. /// <summary>
  1004. /// 延时销毁
  1005. /// </summary>
  1006. public void DelayDestroy()
  1007. {
  1008. CallDeferred(nameof(Destroy));
  1009. }
  1010.  
  1011. /// <summary>
  1012. /// 继承指定物体的运动速率, 该速率可能会有衰减
  1013. /// </summary>
  1014. public void InheritVelocity(ActivityObject other)
  1015. {
  1016. var velocity = other.Velocity;
  1017. if (velocity != Vector2.Zero)
  1018. {
  1019. var force = MoveController.AddConstantForce(velocity * 0.5f, 15);
  1020. force.EnableResistanceInTheAir = false;
  1021. }
  1022. }
  1023.  
  1024. /// <summary>
  1025. /// 触发投抛动作
  1026. /// </summary>
  1027. private void Throw()
  1028. {
  1029. var parent = GetParent();
  1030. //投抛时必须要加入 YSortLayer 节点下
  1031. if (parent == null)
  1032. {
  1033. this.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  1034. }
  1035. else if (parent == GameApplication.Instance.World.NormalLayer)
  1036. {
  1037. parent.RemoveChild(this);
  1038. this.AddToActivityRoot(RoomLayerEnum.YSortLayer);
  1039. }
  1040.  
  1041. CalcThrowAnimatedPosition();
  1042. //显示阴影
  1043. ShowShadowSprite();
  1044.  
  1045. if (EnableVerticalMotion)
  1046. {
  1047. OnThrowStart();
  1048. }
  1049. }
  1050.  
  1051. /// <summary>
  1052. /// 设置下坠状态下的碰撞器
  1053. /// </summary>
  1054. private void SetFallCollision()
  1055. {
  1056. if (_fallData != null && _fallData.UseOrigin)
  1057. {
  1058. _fallData.OriginShape = Collision.Shape;
  1059. _fallData.OriginPosition = Collision.Position;
  1060. _fallData.OriginRotation = Collision.Rotation;
  1061. _fallData.OriginScale = Collision.Scale;
  1062. _fallData.OriginZIndex = ZIndex;
  1063. _fallData.OriginSpritePosition = AnimatedSprite.Position;
  1064. _fallData.OriginCollisionEnable = Collision.Disabled;
  1065. _fallData.OriginCollisionPosition = Collision.Position;
  1066. _fallData.OriginCollisionRotation = Collision.Rotation;
  1067. _fallData.OriginCollisionScale = Collision.Scale;
  1068. _fallData.OriginCollisionMask = CollisionMask;
  1069. _fallData.OriginCollisionLayer = CollisionLayer;
  1070.  
  1071. if (_throwRectangleShape == null)
  1072. {
  1073. _throwRectangleShape = new RectangleShape2D();
  1074. }
  1075.  
  1076. Collision.Shape = _throwRectangleShape;
  1077. Collision.Position = Vector2.Zero;
  1078. Collision.Rotation = 0;
  1079. Collision.Scale = Vector2.One;
  1080. ZIndex = 0;
  1081. Collision.Disabled = false;
  1082. Collision.Position = Vector2.Zero;
  1083. Collision.Rotation = 0;
  1084. Collision.Scale = Vector2.One;
  1085. CollisionMask = 1;
  1086. CollisionLayer = 0;
  1087. _fallData.UseOrigin = false;
  1088. }
  1089. }
  1090.  
  1091. /// <summary>
  1092. /// 重置碰撞器
  1093. /// </summary>
  1094. private void RestoreCollision()
  1095. {
  1096. if (_fallData != null && !_fallData.UseOrigin)
  1097. {
  1098. Collision.Shape = _fallData.OriginShape;
  1099. Collision.Position = _fallData.OriginPosition;
  1100. Collision.Rotation = _fallData.OriginRotation;
  1101. Collision.Scale = _fallData.OriginScale;
  1102. ZIndex = _fallData.OriginZIndex;
  1103. AnimatedSprite.Position = _fallData.OriginSpritePosition;
  1104. Collision.Disabled = _fallData.OriginCollisionEnable;
  1105. Collision.Position = _fallData.OriginCollisionPosition;
  1106. Collision.Rotation = _fallData.OriginCollisionRotation;
  1107. Collision.Scale = _fallData.OriginCollisionScale;
  1108. CollisionMask = _fallData.OriginCollisionMask;
  1109. CollisionLayer = _fallData.OriginCollisionLayer;
  1110.  
  1111. _fallData.UseOrigin = true;
  1112. }
  1113. }
  1114.  
  1115. /// <summary>
  1116. /// 投抛结束
  1117. /// </summary>
  1118. private void ThrowOver()
  1119. {
  1120. var parent = GetParent();
  1121. var roomLayer = GameApplication.Instance.World.GetRoomLayer(_currLayer);
  1122. if (parent != roomLayer)
  1123. {
  1124. parent.RemoveChild(this);
  1125. roomLayer.AddChild(this);
  1126. }
  1127. RestoreCollision();
  1128.  
  1129. OnThrowOver();
  1130. }
  1131.  
  1132. /// <summary>
  1133. /// 设置标记, 用于在物体上记录自定义数据
  1134. /// </summary>
  1135. /// <param name="name">标记名称</param>
  1136. /// <param name="v">存入值</param>
  1137. public void SetSign(string name, object v)
  1138. {
  1139. if (_signMap == null)
  1140. {
  1141. _signMap = new Dictionary<string, object>();
  1142. }
  1143.  
  1144. _signMap[name] = v;
  1145. }
  1146.  
  1147. /// <summary>
  1148. /// 返回是否存在指定名称的标记数据
  1149. /// </summary>
  1150. public bool HasSign(string name)
  1151. {
  1152. return _signMap == null ? false : _signMap.ContainsKey(name);
  1153. }
  1154.  
  1155. /// <summary>
  1156. /// 根据名称获取标记值
  1157. /// </summary>
  1158. public object GetSign(string name)
  1159. {
  1160. if (_signMap == null)
  1161. {
  1162. return null;
  1163. }
  1164.  
  1165. _signMap.TryGetValue(name, out var value);
  1166. return value;
  1167. }
  1168.  
  1169. /// <summary>
  1170. /// 根据名称获取标记值
  1171. /// </summary>
  1172. public T GetSign<T>(string name)
  1173. {
  1174. if (_signMap == null)
  1175. {
  1176. return default;
  1177. }
  1178.  
  1179. _signMap.TryGetValue(name, out var value);
  1180. if (value is T v)
  1181. {
  1182. return v;
  1183. }
  1184. return default;
  1185. }
  1186.  
  1187. /// <summary>
  1188. /// 根据名称删除标记
  1189. /// </summary>
  1190. public void RemoveSign(string name)
  1191. {
  1192. if (_signMap != null)
  1193. {
  1194. _signMap.Remove(name);
  1195. }
  1196. }
  1197.  
  1198. /// <summary>
  1199. /// 播放受伤动画, 该动画不与 Animation 节点的动画冲突
  1200. /// </summary>
  1201. public void PlayHitAnimation()
  1202. {
  1203. _playHit = true;
  1204. _playHitSchedule = 0;
  1205. }
  1206.  
  1207. /// <summary>
  1208. /// 开启一个协程, 返回协程 id, 协程是在普通帧执行的, 支持: 协程嵌套, WaitForSeconds, WaitForFixedProcess, Task, SignalAwaiter
  1209. /// </summary>
  1210. public long StartCoroutine(IEnumerator able)
  1211. {
  1212. return ProxyCoroutineHandler.ProxyStartCoroutine(ref _coroutineList, able);
  1213. }
  1214.  
  1215. /// <summary>
  1216. /// 根据协程 id 停止协程
  1217. /// </summary>
  1218. public void StopCoroutine(long coroutineId)
  1219. {
  1220. ProxyCoroutineHandler.ProxyStopCoroutine(ref _coroutineList, coroutineId);
  1221. }
  1222. /// <summary>
  1223. /// 停止所有协程
  1224. /// </summary>
  1225. public void StopAllCoroutine()
  1226. {
  1227. ProxyCoroutineHandler.ProxyStopAllCoroutine(ref _coroutineList);
  1228. }
  1229. }