CellBullet.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { _decorator, Component, find, instantiate, Node, Prefab, Quat, v2 } from 'cc';
  2. import { GameAudioMgr } from './GameAudioMgr';
  3. import { Cell } from './Cell';
  4. import { IGamePlayer } from '../../module_basic/shared/protocols/public/game/GameTypeDef';
  5. import { GameMgr } from '../../module_basic/scripts/GameMgr';
  6. import { CellMgr, CellType } from './CellMgr';
  7. import { GameResMgr } from './GameResMgr';
  8. const { ccclass, property } = _decorator;
  9. @ccclass('CellBullet')
  10. export class CellBullet extends Component {
  11. @property
  12. moveSpeed: number = 400;
  13. @property
  14. lifeTime: number = 3000;
  15. /**
  16. * @en the bullet belongs to which player
  17. * @zh 子弹所属的玩家 id
  18. */
  19. player: IGamePlayer;
  20. damage: number = 10;
  21. private _lifeStart = 0;
  22. private _moveDir = v2();
  23. public set moveDir(v) {
  24. this._moveDir.set(v);
  25. }
  26. start() {
  27. this._lifeStart = Date.now();
  28. this.damage = Math.floor(Math.random() * 15 + 10);
  29. }
  30. update(deltaTime: number) {
  31. if (this._lifeStart + this.lifeTime < Date.now()) {
  32. this.lifeEnd();
  33. return;
  34. }
  35. let dist = this.moveSpeed * deltaTime;
  36. let dx = this._moveDir.x * dist;
  37. let dy = this._moveDir.y * dist;
  38. let pos = this.node.position;
  39. this.node.setPosition(pos.x + dx, pos.y + dy, pos.z);
  40. }
  41. lifeEnd() {
  42. this.node.destroy();
  43. }
  44. protected lateUpdate(dt: number): void {
  45. const tankBodyRadius = 50;
  46. let cellList = CellMgr.inst.cellList;
  47. for (let i = 0; i < cellList.length; i++) {
  48. let cell = cellList[i] as CellType;
  49. let bp = this.node.worldPosition;
  50. let tp = cell.node.worldPosition;
  51. let dx = bp.x - tp.x;
  52. let dy = bp.y - tp.y;
  53. if (dx * dx + dy * dy < tankBodyRadius * tankBodyRadius) {
  54. if(this.player != cell.player){
  55. this.lifeEnd();
  56. }
  57. /**
  58. * @en if this bullet belongs to other player, then this.player is null, no damage calculation
  59. * @zh 如果是其他玩家的子弹,this.player 为 null,不做伤害计算
  60. */
  61. if (this.player && this.player.color != cell.player.color) {
  62. //cell.beAttack(this.player.playerId, this.damage, this.node.worldPosition);
  63. }
  64. break;
  65. }
  66. }
  67. }
  68. }