BulletMagazine.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { _decorator, Node, Sprite, SpriteFrame} from 'cc';
  2. import { PoolManager } from '../core/manager/PoolManager';
  3. import { Game } from './Game';
  4. import { Constants } from '../data/Constants';
  5. import { BaseExp } from '../core/base/BaseExp';
  6. const { ccclass, property } = _decorator;
  7. @ccclass('BulletMagazine')
  8. export class BulletMagazine extends BaseExp {
  9. @property({type:Node,tooltip:"滚动内容"})
  10. public content: Node;
  11. @property({type:Node,tooltip:"子弹预制体"})
  12. public bullet: Node;
  13. @property({type:SpriteFrame,tooltip:"用过的子弹图片"})
  14. public use_sf: SpriteFrame;
  15. @property({type:SpriteFrame,tooltip:"正常的子弹图片"})
  16. public nor_sf: SpriteFrame;
  17. //当前子弹总数量
  18. private _magazineNum: number = 0;
  19. public set magazineNum(value: number) {
  20. this._magazineNum = value;
  21. this.loadMagazine()
  22. }
  23. public get magazineNum(): number {
  24. return this._magazineNum!;
  25. }
  26. //弹夹中的子弹图片节点
  27. public bullets: Node[] = [];
  28. start() {
  29. this.bullet.active = false;
  30. //更换弹夹
  31. this.register(Constants.eventName.magazine_num_change,this.loadMagazine.bind(this));
  32. }
  33. /**
  34. * 加载子弹 弹夹
  35. */
  36. public loadMagazine(){
  37. if(!Game.I.player.gun
  38. ||Game.I.isGameOver)return;
  39. this.recycle();
  40. const gData:any = Game.I.player.pData;
  41. let s_bullet: number = gData.magazine - Game.I.player.gun.shotBullets;
  42. for(let idx = 0; idx < this._magazineNum; idx++) {
  43. let n: Node = PoolManager.getNode(this.bullet,this.content);
  44. this.bullets.push(n);
  45. n.getChildByName("kd_sp").getComponent(Sprite).spriteFrame = idx < s_bullet ? this.nor_sf : this.use_sf;
  46. }
  47. }
  48. protected onDisable(): void {
  49. this.recycle();
  50. }
  51. public recycle(){
  52. this.bullets.forEach((item: Node) => {
  53. if(item.parent){
  54. PoolManager.putNode(item);
  55. }
  56. });
  57. this.bullets = [];
  58. }
  59. }