BulletCreater.ts 1.1 KB

12345678910111213141516171819202122232425262728293031
  1. import { _decorator, Component, Node, instantiate, Prefab } from 'cc';
  2. import { DataSystem } from '../core/data/DataSystem';
  3. import { ResourceLoader } from '../core/resourceManager/ResourceLoader';
  4. import { BattleData } from './BattleData';
  5. import { Bullet } from './Bullet';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('BulletCreater')
  8. export class BulletCreater extends Component {
  9. @property({ type: Node, tooltip: "子弹父级" }) bulletLayer: Node;
  10. @property({ type: Prefab, tooltip: "子弹预制体" }) bulletPrefab:Prefab
  11. private data: BattleData;
  12. start() {
  13. this.data = DataSystem.getData(BattleData);
  14. }
  15. update(): void {
  16. DataSystem.watch(BattleData, "_bullets") && this.createBullet();
  17. }
  18. /**生产子弹 */
  19. private async createBullet() {
  20. for (let i = 0; i < this.data._bullets.length; i++) {
  21. let node = instantiate(this.bulletPrefab);
  22. let bullet = node.getComponent(Bullet);
  23. node.setParent(this.bulletLayer);
  24. bullet.setData(this.data._bullets[i]);
  25. }
  26. this.data._bullets = [];
  27. }
  28. }