AutoBuild.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { _decorator, Component, instantiate, Node, Prefab, resources, UITransform } from 'cc';
  2. import { PropBase } from './Prop/Base/PropBase';
  3. import { ObjectPoolManager } from './Prop/ObjectPoolManager';
  4. const { ccclass, property } = _decorator;
  5. @ccclass('AutoBuild')
  6. export class AutoBuild extends Component {
  7. protected onLoad(): void {
  8. ObjectPoolManager.getInstance().initializePools().then((ok) => {
  9. if (ok) {
  10. console.log('初始化成功');
  11. this.schedule(() => {
  12. this.BuildOnce(FloorType.diban)
  13. }, 0.1)
  14. }
  15. });
  16. }
  17. BuildOnce(Type: FloorType) {
  18. let Node = ObjectPoolManager.getInstance().getNodeFromPool(Type, this.node)
  19. if (!Node) {
  20. console.error('没有找到节点')
  21. return
  22. }
  23. // this.InitNode(Node)
  24. }
  25. ClerarNode() {
  26. }
  27. InitNode(node: Node) {
  28. this.node.addChild(node);
  29. let size = this.node.getComponent(UITransform).contentSize
  30. let width = (size.width - (node.getComponent(UITransform).contentSize.width * 1.33)) / 2;
  31. let height = -size.height / 2;
  32. height -= 200;
  33. const x = this.getRandomNumber(-width, width);
  34. const y = height
  35. node.setPosition(x, y, 0);
  36. node.getComponent(PropBase).UpSpeed = 20
  37. }
  38. getRandomNumber(min: number, max: number): number {
  39. return Math.floor(Math.random() * (max - min + 1)) + min;
  40. }
  41. }
  42. export enum FloorType {
  43. diban = 'diban',//预制体的名称
  44. dicin = 'dici',
  45. }