import { _decorator, Component, find, Node, RigidBody2D, UITransform, v3, Vec2, Vec3 } from 'cc'; import { ObjectPoolManager } from '../ObjectPoolManager'; import { EventManager } from '../../EventManager'; import { DifficultyManager } from '../../DifficultyManager'; const { ccclass, property } = _decorator; @ccclass('PropBase') export class PropBase extends Component { private _UpSpeed: number = 50; public get UpSpeed(): number { return this._UpSpeed; } public set UpSpeed(value: number) { this._UpSpeed = value; this.setSpeed() } ParentSize: any setSpeed() { let rigidBody = this.getComponent(RigidBody2D) if (rigidBody) { // 设置向上的速度 rigidBody.linearVelocity = new Vec2(0, this.UpSpeed); // 速度值可以根据需要调整 if (this.getComponentInChildren(RigidBody2D)) { this.getComponentInChildren(RigidBody2D).linearVelocity = new Vec2(0, this.UpSpeed); } } else { console.error('RigidBody2D component not found!'); } } updateTime: number; update(deltaTime: number) { this.updateTime += deltaTime; let _time = 1 / 8//60FPS if (this.updateTime < _time) return; this.updateTime = this.updateTime - _time; this.FixUpdate() } FixUpdate() { let height = this.ParentSize.height / 2; height += 50 if (this.node.getPosition().y > height) { ObjectPoolManager.getInstance().putNodeToPool(this.node) } if (!this.node.active || !this.node.parent) { return } if (this.UpSpeed != DifficultyManager.UpSpeed) { this.UpSpeed = DifficultyManager.UpSpeed } let roleNode = this.node.parent.getChildByName('Role') if (roleNode && this.addSourceOnce) { if (this.node.getPosition().y > roleNode.getPosition().y) { EventManager.instance.et.emit(EventManager.EventType.Add_Score); this.addSourceOnce = false } } } addSourceOnce = false unuse() { this.addSourceOnce = true } reuse(ParentNode: Node) { ParentNode[0].addChild(this.node); let size = ParentNode[0].getComponent(UITransform).contentSize this.ParentSize = size let width = (size.width - (this.node.getComponent(UITransform).contentSize.width * 1.33)) / 2; let height = -size.height / 2; height -= 50; const x = this.getRandomNumber(-width, width); const y = height this.node.setPosition(x, y, 0); this.UpSpeed = DifficultyManager.UpSpeed } getRandomNumber(min: number, max: number): number { return Math.floor(Math.random() * (max - min + 1)) + min; } }