PropBase.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Component, find, Node, RigidBody2D, UITransform, v3, Vec2, Vec3 } from 'cc';
  2. import { ObjectPoolManager } from '../ObjectPoolManager';
  3. import { EventManager } from '../../EventManager';
  4. import { DifficultyManager } from '../../DifficultyManager';
  5. const { ccclass, property } = _decorator;
  6. @ccclass('PropBase')
  7. export class PropBase extends Component {
  8. private _UpSpeed: number = 50;
  9. public get UpSpeed(): number {
  10. return this._UpSpeed;
  11. }
  12. public set UpSpeed(value: number) {
  13. this._UpSpeed = value;
  14. this.setSpeed()
  15. }
  16. ParentSize: any
  17. setSpeed() {
  18. let rigidBody = this.getComponent(RigidBody2D)
  19. if (rigidBody) {
  20. // 设置向上的速度
  21. rigidBody.linearVelocity = new Vec2(0, this.UpSpeed); // 速度值可以根据需要调整
  22. if (this.getComponentInChildren(RigidBody2D)) {
  23. this.getComponentInChildren(RigidBody2D).linearVelocity = new Vec2(0, this.UpSpeed);
  24. }
  25. } else {
  26. console.error('RigidBody2D component not found!');
  27. }
  28. }
  29. updateTime: number;
  30. update(deltaTime: number) {
  31. this.updateTime += deltaTime;
  32. let _time = 1 / 8//60FPS
  33. if (this.updateTime < _time) return;
  34. this.updateTime = this.updateTime - _time;
  35. this.FixUpdate()
  36. }
  37. FixUpdate() {
  38. let height = this.ParentSize.height / 2;
  39. height += 50
  40. if (this.node.getPosition().y > height) {
  41. ObjectPoolManager.getInstance().putNodeToPool(this.node)
  42. }
  43. if (!this.node.active || !this.node.parent) {
  44. return
  45. }
  46. if (this.UpSpeed != DifficultyManager.UpSpeed) {
  47. this.UpSpeed = DifficultyManager.UpSpeed
  48. }
  49. let roleNode = this.node.parent.getChildByName('Role')
  50. if (roleNode && this.addSourceOnce) {
  51. if (this.node.getPosition().y > roleNode.getPosition().y) {
  52. EventManager.instance.et.emit(EventManager.EventType.Add_Score);
  53. this.addSourceOnce = false
  54. }
  55. }
  56. }
  57. addSourceOnce = false
  58. unuse() {
  59. this.addSourceOnce = true
  60. }
  61. reuse(ParentNode: Node) {
  62. ParentNode[0].addChild(this.node);
  63. let size = ParentNode[0].getComponent(UITransform).contentSize
  64. this.ParentSize = size
  65. let width = (size.width - (this.node.getComponent(UITransform).contentSize.width * 1.33)) / 2;
  66. let height = -size.height / 2;
  67. height -= 50;
  68. const x = this.getRandomNumber(-width, width);
  69. const y = height
  70. this.node.setPosition(x, y, 0);
  71. this.UpSpeed = DifficultyManager.UpSpeed
  72. }
  73. getRandomNumber(min: number, max: number): number {
  74. return Math.floor(Math.random() * (max - min + 1)) + min;
  75. }
  76. }