enemyPlane.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { _decorator, Collider, Component, ITriggerEvent, Node, sp } from 'cc';
  2. import { constant } from '../framework/constant';
  3. import { gameMangager } from '../framework/gameMangager';
  4. const { ccclass, property } = _decorator;
  5. const OUTOFBOUNCE = 50
  6. @ccclass('enemyPlane')
  7. export class enemyPlane extends Component {
  8. // @property
  9. // public enemySpeed = 0;
  10. private _enemySpeed = 0;
  11. public enemyType = constant.EnemyPlaneType.TYPE1
  12. private _gameManager: gameMangager = null;
  13. start() {
  14. }
  15. update(deltaTime: number) {
  16. const pos = this.node.position;
  17. const movePos = pos.x - this._enemySpeed;
  18. this.node.setPosition(movePos, pos.y, pos.z)
  19. if (movePos > OUTOFBOUNCE) {
  20. this.node.destroy()
  21. }
  22. }
  23. show(gameManager: any, speed: number) {
  24. this._gameManager = gameManager
  25. this._enemySpeed = speed
  26. }
  27. // 碰撞业务
  28. onEnable() {
  29. const collider = this.getComponent(Collider)
  30. collider.on('onTriggerEnter', this._onTriggerEnter, this)
  31. }
  32. onDisable() {
  33. const collider = this.getComponent(Collider)
  34. collider.off('onTriggerEnter', this._onTriggerEnter, this)
  35. }
  36. private _onTriggerEnter(event: ITriggerEvent) {
  37. const collisionGroup = event.otherCollider.getGroup();
  38. console.log('敌人');
  39. if (collisionGroup === constant.CollisionType.SELF_BULLET || collisionGroup === constant.CollisionType.SELF_PLANE) {
  40. this.node.destroy()
  41. this._gameManager.addScore()
  42. }
  43. }
  44. }