import { _decorator, Component, Prefab, Animation, Node, instantiate, v3, UITransform, JsonAsset } from 'cc'; import { ResourcesUtils } from '../core/resourceManager/ResourcesUtils'; import { Sound } from '../core/sound/Sound'; import { OpenWindow } from '../core/ui/window/OpenWindow'; import { WindowManager } from '../core/ui/window/WindowManager'; import { WindowOpenMode } from '../core/ui/window/WindowOpenMode'; import { Utils } from '../core/utils/Utils'; import { ConfigData } from '../Data/ConfigData'; import { g } from '../Data/g'; import { FactoryCountDown } from '../UI/Factory/FactoryCountDown'; import { ProductShow } from '../UI/Factory/ProductShow'; const { ccclass, property } = _decorator; @ccclass('MapIcon') export class MapIcon extends Component { @property({ tooltip: "点击是否缩放动画" }) touchAmition: boolean = true; @property({ type: Animation, tooltip: "点击动画" }) animation: Animation; @property({ type: Node, tooltip: "建筑状态组" }) stateGroup: Node; @property({ type: Node, tooltip: "完成品展示节点" }) showGroup: Node; @property({ tooltip: "配置ID" }) configID: number = 0; @property({ type: FactoryCountDown, tooltip: "计时装置" }) countDown: FactoryCountDown; private _state = FactroyState.Lock; private hasActive = false; private havrestList: { id: number, node: Node }[] = []; start() { if (g.gameData.hasBuildData(this.configID)) { this.activeFactroy(); } } update() { if (this._state == FactroyState.Lock && g.gameData.hasBuildData(this.configID)) { this.activeFactroy(); } let producting = g.gameData.getProductingList(this.configID); if (producting.length > 0) { this.setState(FactroyState.Producting); } else { this.setState(FactroyState.Free); } } public activeFactroy(): void { this.hasActive = true; } public async inPolygon() { (this.touchAmition && this.animation) && this.animation.play(); var sound = this.getComponent(Sound); sound && sound.play(); if (this._state != FactroyState.Lock) { if (this.havrestList.length == 0) { this.getComponent(OpenWindow).open(this.configID, this.countDown); } else { WindowManager.open("Prefabs/HarvestWindow", WindowOpenMode.CloseAndCover, this.configID, this.onHarvest.bind(this)); } } else { let lvl = ConfigData.configMap.get("build")[this.configID]["lvl"]; WindowManager.showTips(`${lvl}级解锁`); } } /**生产完成回调 */ public async onProductComplete(id: number) { if (this.showGroup) { let prefab = await ResourcesUtils.load("Prefabs/ProductShow", null, this.node); let item = instantiate(prefab); item.getComponent(ProductShow).id = id; item.setParent(this.showGroup); let x = Utils.random_both(-this.showGroup.getComponent(UITransform).width / 2, this.showGroup.getComponent(UITransform).width / 2); let y = Utils.random_both(-this.showGroup.getComponent(UITransform).height / 2, this.showGroup.getComponent(UITransform).height / 2); item.setPosition(v3(x, y, 0)); this.havrestList.push({ id: id, node: item }); } } public clean(): void { this.showGroup.removeAllChildren(); this.havrestList = []; } private onHarvest(mult: number): void { this.havrestList.shift().node.destroy(); } private setState(value: FactroyState): void { if (this.hasActive) { if (this._state != value) { this._state = value; if (this._state == FactroyState.Producting) { this.stateGroup && (this.stateGroup.active = true); } else { this.stateGroup && (this.stateGroup.active = false); } } } } } export enum FactroyState { Lock, Producting, Free }