Bottom.ts 3.7 KB

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