bulletObject.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {EVENT_TYPE, GAME_OBJECT_TYPE} from "../../utrl/gameEnum";
  2. import gameEventManager from "../../utrl/gameEventManager";
  3. import GameObject from "../gameObject";
  4. import PlayerObject from "../player/playerObject";
  5. const {ccclass, property} = cc._decorator;
  6. /**子弹实体类 */
  7. @ccclass
  8. export default class BulletObject extends GameObject {
  9. /**子弹开始位置 */
  10. private start_pos: cc.Vec3 = null;
  11. /**方向速度 */
  12. private v: cc.Vec2 = null;
  13. /**子弹伤害 */
  14. damage = 0;
  15. /**所属玩家 */
  16. from_p: PlayerObject = null;
  17. /**飞行距离上限 */
  18. max_dis = 600;
  19. /**子弹速度 */
  20. private speed = 3000;
  21. /**初始化完成 */
  22. private inited = false;
  23. /**初始化
  24. * @param start_pos 子弹开始位置
  25. * @param rad 弧度角
  26. * @param damage 子弹伤害
  27. * @param from_p 所属玩家
  28. */
  29. destroyCall: Function = null;
  30. init(start_pos: cc.Vec3, rad: number, damage: number, from_p: PlayerObject) {
  31. this.start_pos = start_pos;
  32. this.v = cc.v2(Math.cos(rad), Math.sin(rad)).mul(this.speed);
  33. this.node.setPosition(start_pos.add(cc.v3(this.v).mul(50 / this.speed)));
  34. this.damage = damage;
  35. this.from_p = from_p;
  36. this.node.angle = rad / Math.PI * 180;
  37. this.inited = true;
  38. this.type = GAME_OBJECT_TYPE.bullet;
  39. }
  40. /**运行
  41. * @param dt 每帧时间
  42. */
  43. run(dt: number) {
  44. if (!this.inited) {
  45. return;
  46. }
  47. this.node.setPosition(this.node.position.add(cc.v3(this.v.mul(dt))));
  48. let dis = this.node.position.sub(this.start_pos).mag();
  49. if (dis > this.max_dis) {
  50. this.node.destroy();
  51. return;
  52. }
  53. }
  54. update(dt) {
  55. if (!this.inited) {
  56. return;
  57. }
  58. this.node.setPosition(this.node.position.add(cc.v3(this.v.mul(dt))));
  59. let dis = this.node.position.sub(this.start_pos).mag();
  60. if (dis > this.max_dis) {
  61. gameEventManager.emit(EVENT_TYPE.RECOVER_PLAYER_BULLET);
  62. this.node.destroy();
  63. return;
  64. }
  65. }
  66. onDestroy() {
  67. this.destroyCall && this.destroyCall();
  68. }
  69. /**销毁 */
  70. die() {
  71. this.node.destroy();
  72. }
  73. }