MapIcon.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Component, Prefab, Animation, Node, instantiate, v3, UITransform, JsonAsset } from 'cc';
  2. import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils';
  3. import { Sound } from '../core/sound/Sound';
  4. import { OpenWindow } from '../core/ui/window/OpenWindow';
  5. import { WindowManager } from '../core/ui/window/WindowManager';
  6. import { WindowOpenMode } from '../core/ui/window/WindowOpenMode';
  7. import { Utils } from '../core/utils/Utils';
  8. import { ConfigData } from '../Data/ConfigData';
  9. import { g } from '../Data/g';
  10. import { FactoryCountDown } from '../UI/Factory/FactoryCountDown';
  11. import { ProductShow } from '../UI/Factory/ProductShow';
  12. const { ccclass, property } = _decorator;
  13. @ccclass('MapIcon')
  14. export class MapIcon extends Component {
  15. @property({ tooltip: "点击是否缩放动画" }) touchAmition: boolean = true;
  16. @property({ type: Animation, tooltip: "点击动画" }) animation: Animation;
  17. @property({ type: Node, tooltip: "建筑状态组" }) stateGroup: Node;
  18. @property({ type: Node, tooltip: "完成品展示节点" }) showGroup: Node;
  19. @property({ tooltip: "配置ID" }) configID: number = 0;
  20. @property({ type: FactoryCountDown, tooltip: "计时装置" }) countDown: FactoryCountDown;
  21. private _state = FactroyState.Lock;
  22. private hasActive = false;
  23. private havrestList: { id: number, node: Node }[] = [];
  24. start() {
  25. if (g.gameData.hasBuildData(this.configID)) {
  26. this.activeFactroy();
  27. }
  28. }
  29. update() {
  30. if (this._state == FactroyState.Lock && g.gameData.hasBuildData(this.configID)) {
  31. this.activeFactroy();
  32. }
  33. let producting = g.gameData.getProductingList(this.configID);
  34. if (producting.length > 0) {
  35. this.setState(FactroyState.Producting);
  36. } else {
  37. this.setState(FactroyState.Free);
  38. }
  39. }
  40. public activeFactroy(): void {
  41. this.hasActive = true;
  42. }
  43. public async inPolygon() {
  44. (this.touchAmition && this.animation) && this.animation.play();
  45. var sound = this.getComponent(Sound);
  46. sound && sound.play();
  47. if (this._state != FactroyState.Lock) {
  48. if (this.havrestList.length == 0) {
  49. this.getComponent(OpenWindow).open(this.configID, this.countDown);
  50. } else {
  51. WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this));
  52. }
  53. } else {
  54. let lvl = ConfigData.configMap.get("build")[this.configID]["lvl"];
  55. WindowManager.showTips(`${lvl}级解锁`);
  56. }
  57. }
  58. /**生产完成回调 */
  59. public async onProductComplete(id: number) {
  60. if (this.showGroup) {
  61. let prefab = await ResourcesUtils.load<Prefab>("Prefabs/ProductShow", null, this.node);
  62. let item = instantiate(prefab);
  63. item.getComponent(ProductShow).id = id;
  64. item.setParent(this.showGroup);
  65. let x = Utils.random_both(-this.showGroup.getComponent(UITransform).width / 2, this.showGroup.getComponent(UITransform).width / 2);
  66. let y = Utils.random_both(-this.showGroup.getComponent(UITransform).height / 2, this.showGroup.getComponent(UITransform).height / 2);
  67. item.setPosition(v3(x, y, 0));
  68. this.havrestList.push({ id: id, node: item });
  69. }
  70. }
  71. public clean(): void {
  72. this.showGroup.removeAllChildren();
  73. this.havrestList = [];
  74. }
  75. private onHarvest(mult: number): void {
  76. this.havrestList.shift().node.destroy();
  77. }
  78. private setState(value: FactroyState): void {
  79. if (this.hasActive) {
  80. if (this._state != value) {
  81. this._state = value;
  82. if (this._state == FactroyState.Producting) {
  83. this.stateGroup && (this.stateGroup.active = true);
  84. } else {
  85. this.stateGroup && (this.stateGroup.active = false);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. export enum FactroyState {
  92. Lock,
  93. Producting,
  94. Free
  95. }