GameManager.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { _decorator, Component, Node, Prefab, instantiate, Vec3, UITransform, Collider2D, Contact2DType, IPhysics2DContact } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. @ccclass('GameManager')
  4. export class GameManager extends Component {
  5. @property(Prefab)
  6. ballPrefab: Prefab = null;
  7. @property(Prefab)
  8. bulletPrefab: Prefab = null;
  9. @property(Prefab)
  10. blockPrefab: Prefab = null;
  11. @property(Prefab)
  12. enemyPrefab: Prefab = null;
  13. @property(Node)
  14. walls: Node[] = [];
  15. @property
  16. maxEnemies: number = 5;
  17. private _canvasSize: { width: number, height: number } = { width: 0, height: 0 };
  18. private _enemies: Node[] = [];
  19. start() {
  20. // 获取画布大小
  21. const canvas = this.node.parent.getComponent(UITransform);
  22. this._canvasSize.width = canvas.width;
  23. this._canvasSize.height = canvas.height;
  24. // 创建小球
  25. this.createBall();
  26. // 创建方块
  27. this.createBlock();
  28. // 创建敌人
  29. this.scheduleOnce(() => {
  30. this.createEnemies();
  31. }, 1);
  32. }
  33. createBall() {
  34. const ball = instantiate(this.ballPrefab);
  35. this.node.parent.addChild(ball);
  36. // 固定位置在(80, 80)
  37. ball.setPosition(new Vec3(80, 80, 0));
  38. }
  39. createBlock() {
  40. const block = instantiate(this.blockPrefab);
  41. this.node.parent.addChild(block);
  42. block.setPosition(new Vec3(0, 0, 0));
  43. }
  44. createEnemies() {
  45. for (let i = 0; i < this.maxEnemies; i++) {
  46. this.createEnemy();
  47. }
  48. }
  49. createEnemy() {
  50. const enemy = instantiate(this.enemyPrefab);
  51. this.node.parent.addChild(enemy);
  52. // 随机位置(在画布边缘区域)
  53. const side = Math.floor(Math.random() * 4);
  54. let x = 0;
  55. let y = 0;
  56. switch (side) {
  57. case 0: // 上
  58. x = (Math.random() - 0.5) * (this._canvasSize.width - 100);
  59. y = this._canvasSize.height / 2 - 50;
  60. break;
  61. case 1: // 右
  62. x = this._canvasSize.width / 2 - 50;
  63. y = (Math.random() - 0.5) * (this._canvasSize.height - 100);
  64. break;
  65. case 2: // 下
  66. x = (Math.random() - 0.5) * (this._canvasSize.width - 100);
  67. y = -this._canvasSize.height / 2 + 50;
  68. break;
  69. case 3: // 左
  70. x = -this._canvasSize.width / 2 + 50;
  71. y = (Math.random() - 0.5) * (this._canvasSize.height - 100);
  72. break;
  73. }
  74. enemy.setPosition(new Vec3(x, y, 0));
  75. this._enemies.push(enemy);
  76. }
  77. getEnemies() {
  78. return this._enemies.filter(enemy => enemy.isValid);
  79. }
  80. removeEnemy(enemy: Node) {
  81. const index = this._enemies.indexOf(enemy);
  82. if (index !== -1) {
  83. this._enemies.splice(index, 1);
  84. }
  85. // 创建新敌人
  86. this.scheduleOnce(() => {
  87. this.createEnemy();
  88. }, 2);
  89. }
  90. }