Bottom.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { _decorator, Component, Label, Node, Sprite } from 'cc';
  2. import { ModulerBase } from '../../GameFrameWork/ModulerBase';
  3. import { GameInfo } from '../../../GameInfo';
  4. import { resMgr } from '../../../Frames/ResourcesMgr';
  5. import { messageMgr } from '../../../Frames/MessageMgr';
  6. import { RoleData } from '../../../DataItem/ItemData';
  7. import { dataMgr } from '../../../Frames/DataManager';
  8. import { PopupUIDataConfig } from './Data/PopupUIDataConfig';
  9. const { ccclass, property } = _decorator;
  10. @ccclass('Bottom')
  11. export class Bottom extends ModulerBase {
  12. private _oreSpeed: Node = null;
  13. private _characterSlot: Node = null;
  14. // 每多少秒生成多少个矿石
  15. private _speed: number = 0;
  16. // 初始矿石数量
  17. oreCount: number = 100;
  18. // 记录时间间隔
  19. private _elapsedTime: number = 0;
  20. private _roleDts: RoleData[] = null;
  21. protected onLoad(): void {
  22. this._roleDts = dataMgr.getAllDataByName("RoleCardData");
  23. this._oreSpeed = this.node.getChildByPath("OreSpeed/_oreSpeed");
  24. this._characterSlot = this.node.getChildByName("CharacterSlot");
  25. }
  26. onStart() {
  27. this.getLabel("_lvNumber").string = String(GameInfo.Instance.getCurlv());
  28. this.getLabel("_oreGrade").string = `等级:${GameInfo.Instance.getOreGrade()}`;
  29. this.getLabel("_ownNumber").string = String(GameInfo.Instance.getOwnDiamondNum());
  30. this.getLabel("_needNumber").string = String(GameInfo.Instance.getNeedDiamondNum());
  31. this.getSprite("_skillImg").spriteFrame = resMgr.getSpriteFrame(GameInfo.Instance.getSkill());
  32. this._updateLabel();
  33. this._speed = GameInfo.Instance.getOreSpeed();
  34. this._setRoleImg();
  35. this.closeLock();
  36. //注册消息
  37. messageMgr.addEvent("addOreCount", this.onEnemyDeath, this)
  38. }
  39. protected update(dt: number): void {
  40. //累加时间
  41. this._elapsedTime += dt;
  42. //每秒更新
  43. if (this._elapsedTime >= 1) {
  44. //增加矿石数量
  45. this.oreCount += this._speed / 60;
  46. //向下取整
  47. this.oreCount = Math.ceil(this.oreCount);
  48. this._updateLabel();
  49. //重置时间间隔
  50. this._elapsedTime = 0;
  51. }
  52. }
  53. private _setRoleImg() {
  54. for (let i = 0; i < GameInfo.Instance.getRoleImgNames().length; i++) {
  55. const role: Node = this._characterSlot.children[i];
  56. const lock: Node = role.getChildByName("Lock");
  57. if(lock.active){
  58. lock.active = false;
  59. }
  60. const sprite: Node = role.getChildByName("Sprite");
  61. const imgName: string = GameInfo.Instance.getRoleImgNames()[i];
  62. sprite.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(imgName);
  63. for (const roleDt of this._roleDts) {
  64. if (roleDt.imgName === imgName) {
  65. const count: Node = role.getChildByName("Count");
  66. if (count.active) {
  67. count.getComponent(Label).string = String(roleDt.consume);
  68. } else {
  69. count.active = true;
  70. count.getComponent(Label).string = String(roleDt.consume);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. onEnemyDeath() {
  77. this.oreCount += 30;
  78. this._updateLabel();
  79. }
  80. onReduce(count: number) {
  81. if (this.oreCount <= 0) {
  82. this.oreCount = 0;
  83. } else {
  84. this.oreCount -= count;
  85. }
  86. this._updateLabel();
  87. }
  88. private closeLock(){
  89. const ownCardSlot: number = PopupUIDataConfig.Instance.getAvailableCardSlot();
  90. for (const cardSlot of this._characterSlot.children) {
  91. if(cardSlot.getChildByName("Lock").active){
  92. if(cardSlot.getSiblingIndex() < ownCardSlot){
  93. cardSlot.getChildByName("Lock").active = false;
  94. }
  95. }
  96. }
  97. }
  98. private _updateLabel() {
  99. if (this._oreSpeed) {
  100. this.getLabel("_oreSpeed").string = String(this.oreCount);
  101. }
  102. }
  103. }