PropBase.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { _decorator, Component, Node, RigidBody2D, UITransform, Vec2 } from 'cc';
  2. import { ObjectPoolManager } from '../ObjectPoolManager';
  3. const { ccclass, property } = _decorator;
  4. @ccclass('PropBase')
  5. export class PropBase extends Component {
  6. private _UpSpeed: number = 50;
  7. public get UpSpeed(): number {
  8. return this._UpSpeed;
  9. }
  10. public set UpSpeed(value: number) {
  11. this._UpSpeed = value;
  12. this.setSpeed()
  13. }
  14. ParentSize: any
  15. setSpeed() {
  16. let rigidBody = this.getComponent(RigidBody2D)
  17. if (rigidBody) {
  18. // 设置向上的速度
  19. rigidBody.linearVelocity = new Vec2(0, this.UpSpeed); // 速度值可以根据需要调整
  20. } else {
  21. console.error('RigidBody2D component not found!');
  22. }
  23. }
  24. updateTime: number;
  25. update(deltaTime: number) {
  26. // this.updateTime += deltaTime;
  27. // let _time = 1 / 60//60FPS
  28. // if (this.updateTime < _time) return;
  29. // this.updateTime = this.updateTime - _time;
  30. this.FixUpdate()
  31. }
  32. FixUpdate() {
  33. let height = this.ParentSize.height / 2;
  34. height += 100
  35. console.error(height)
  36. console.error(this.node.getPosition().y)
  37. if (this.node.getPosition().y > height) {
  38. ObjectPoolManager.getInstance().putNodeToPool(this.node)
  39. }
  40. }
  41. unuse() {
  42. }
  43. reuse(ParentNode: Node) {
  44. ParentNode[0].addChild(this.node);
  45. let size = ParentNode[0].getComponent(UITransform).contentSize
  46. this.ParentSize = size
  47. let width = (size.width - (this.node.getComponent(UITransform).contentSize.width * 1.33)) / 2;
  48. let height = -size.height / 2;
  49. height -= 200;
  50. const x = this.getRandomNumber(-width, width);
  51. const y = height
  52. this.node.setPosition(x, y, 0);
  53. this.node.getComponent(PropBase).UpSpeed = 20
  54. }
  55. getRandomNumber(min: number, max: number): number {
  56. return Math.floor(Math.random() * (max - min + 1)) + min;
  57. }
  58. }