Bottom.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.initData();
  28. this.getLabel("_lvNumber").string = String(GameInfo.Instance.getCurlv());
  29. this.getLabel("_oreGrade").string = `等级:${GameInfo.Instance.getOreGrade()}`;
  30. this.getLabel("_ownNumber").string = String(GameInfo.Instance.getOwnDiamondNum());
  31. this.getLabel("_needNumber").string = String(GameInfo.Instance.getNeedDiamondNum());
  32. this.getSprite("_skillImg").spriteFrame = resMgr.getSpriteFrame(GameInfo.Instance.getSkill());
  33. this._updateLabel();
  34. this._speed = GameInfo.Instance.getOreSpeed();
  35. this._setRoleImg();
  36. this.closeLock();
  37. //注册消息
  38. messageMgr.addEvent("addOreCount", this.onEnemyDeath, this)
  39. }
  40. initData(){
  41. this.getLabel("_lvNumber").string = String(GameInfo.Instance.getCurlv());
  42. this.getLabel("_ownNumber").string = String(GameInfo.Instance.getOwnDiamondNum());
  43. this.getLabel("_ownNumber").string = String(GameInfo.Instance.getOwnDiamondNum());
  44. this.oreCount = 100;
  45. }
  46. protected update(dt: number): void {
  47. //累加时间
  48. this._elapsedTime += dt;
  49. //每秒更新
  50. if (this._elapsedTime >= 1) {
  51. //增加矿石数量
  52. this.oreCount += this._speed / 60;
  53. //向下取整
  54. this.oreCount = Math.ceil(this.oreCount);
  55. this._updateLabel();
  56. //重置时间间隔
  57. this._elapsedTime = 0;
  58. }
  59. }
  60. private _setRoleImg() {
  61. for (let i = 0; i < GameInfo.Instance.getRoleImgNames().length; i++) {
  62. const role: Node = this._characterSlot.children[i];
  63. const lock: Node = role.getChildByName("Lock");
  64. if(lock.active){
  65. lock.active = false;
  66. }
  67. //设置角色图片
  68. const sprite: Node = role.getChildByName("Sprite");
  69. const imgName: string = GameInfo.Instance.getRoleImgNames()[i];
  70. sprite.getComponent(Sprite).spriteFrame = resMgr.getSpriteFrame(imgName);
  71. //设置消耗量
  72. for (const roleDt of this._roleDts) {
  73. if (roleDt.imgName === imgName) {
  74. const count: Node = role.getChildByName("Count");
  75. if (count.active) {
  76. count.getComponent(Label).string = String(roleDt.consume);
  77. } else {
  78. count.active = true;
  79. count.getComponent(Label).string = String(roleDt.consume);
  80. }
  81. }
  82. }
  83. }
  84. }
  85. onEnemyDeath() {
  86. this.oreCount += 30;
  87. this._updateLabel();
  88. }
  89. onReduce(count: number) {
  90. if (this.oreCount <= 0) {
  91. this.oreCount = 0;
  92. } else {
  93. this.oreCount -= count;
  94. }
  95. this._updateLabel();
  96. }
  97. private closeLock(){
  98. const ownCardSlot: number = PopupUIDataConfig.Instance.getAvailableCardSlot();
  99. for (const cardSlot of this._characterSlot.children) {
  100. if(cardSlot.getChildByName("Lock").active){
  101. if(cardSlot.getSiblingIndex() < ownCardSlot){
  102. cardSlot.getChildByName("Lock").active = false;
  103. }
  104. }
  105. }
  106. }
  107. private _updateLabel() {
  108. if (this._oreSpeed) {
  109. this.getLabel("_oreSpeed").string = String(this.oreCount);
  110. }
  111. }
  112. }