import { Button } from 'cc'; import { _decorator, Component, Node } from 'cc'; import { GameGlobalData } from '../global/GameGlobalData'; import { Label } from 'cc'; import { Size } from 'cc'; import { UmUtil } from '../../../cc-common/cc-util/UmUtil'; import { instantiate } from 'cc'; import { ObjectConfigPreviewDebug } from './ObjectConfigPreviewDebug'; import { Vec3 } from 'cc'; import { Color } from 'cc'; import { director } from 'cc'; import { GameDefine } from '../config/GameDefine'; const { ccclass, property } = _decorator; @ccclass('MapConfigPreviewDebug') export class MapConfigPreviewDebug extends Component { @property(Button) btnNext: Button = null!; @property(Button) btnPrev: Button = null!; @property(Button) btnClose: Button = null!; @property(Button) btnLoadMap: Button = null!; @property(Node) battleSpace: Node = null!; @property(Node) objectPrefab: Node = null!; @property(Label) txtMapName: Label = null!; @property(Label) txtNotice: Label = null!; objectSize: Size = new Size(84, 83); listColors: Color[] = new Array(); mapIndex = 1; NOTICE_LOADING = "Loading..."; NOTICE_FAIL = "Map $x failed to load or does not exist"; delayTime = 0.5; protected onEnable(): void { this.btnNext?.node.on(Button.EventType.CLICK, this.onBtnNextClicked, this); this.btnPrev?.node.on(Button.EventType.CLICK, this.onBtnPrevClicked, this); this.btnClose?.node.on(Button.EventType.CLICK, this.onBtnCloseClicked, this); this.mapIndex = 1; this.initColor(); this.showMap(this.getMapName(this.mapIndex)); } protected onDisable(): void { this.destroyAllMapObject(); this.listColors.clearArray(); } getMapName(mapIndex: number) { return `F${mapIndex}`; } async showMap(mapName: string) { this.txtMapName.string = "Map: " + mapName; this.setNotice(this.NOTICE_LOADING); this.destroyAllMapObject(); await UmUtil.asyncDelay(this.delayTime); this.delayTime = 0; GameGlobalData.Instance.mapDataConfig.loadMapByName(mapName, (mapConfigPoints) => { if (!this.node?.active || mapConfigPoints == null) { this.setNotice(this.NOTICE_FAIL.replace("$x", this.getMapName(this.mapIndex))); return; } var objectConfigInfo = GameGlobalData.Instance.gameDataConfig.objectInfoMap; var totalRow = GameGlobalData.Instance.mapDataConfig.totalRow; for (var configPoint of mapConfigPoints) { var object = instantiate(this.objectPrefab).getComponent(ObjectConfigPreviewDebug); object.node.setParent(this.battleSpace); object.node.setNodeActive(true); var x = this.objectSize.width / 2 + this.objectSize.width * configPoint.column; var y = this.objectSize.height * totalRow - (this.objectSize.height / 2 + this.objectSize.height * (configPoint.row)); var point = new Vec3(x, y, 0); object.node.position = point; var objectNo = configPoint.objectNo; var objectId = objectConfigInfo[objectNo.toString()].id; var color = this.listColors[objectNo - 1]; object.setObjectInfo(objectNo, objectId, color); } this.setNotice(""); }); } initColor() { this.listColors.clearArray(); var totalColor = Object.keys(GameGlobalData.Instance.gameDataConfig.objectInfoMap).length; for (let i = 0; i < totalColor; i++) { this.listColors.push(new Color(UmUtil.getRandomInt(100, 255), UmUtil.getRandomInt(100, 255), UmUtil.getRandomInt(100, 255))); } } destroyAllMapObject() { this.battleSpace.destroyAllChildren(); } setNotice(notice: string) { this.txtNotice.string = notice; } onBtnNextClicked() { if (this.txtNotice.string == this.NOTICE_LOADING) return; this.mapIndex++; this.showMap(this.getMapName(this.mapIndex)); } onBtnPrevClicked() { if (this.txtNotice.string == this.NOTICE_LOADING) return; this.mapIndex--; this.showMap(this.getMapName(this.mapIndex)); } onBtnCloseClicked() { this.node.setNodeActive(false); const layoutManager = director.getScene().getChildByName('Canvas').getComponentInChildren(GameDefine.LAYOUT_MANAGER) as any; layoutManager?.showHome(); } }