Bottom.ts 3.4 KB

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