import { _decorator, ccenum, Component, Enum, isValid, Node, Sprite, SpriteFrame } from 'cc'; import { EDITOR } from 'cc/env'; import { GBundle, GPath } from '../../../game/ConfigRes'; import { TableNames } from '../../../game/ConfigTableData'; import { SpriteLoad } from 'db://assets/mx/components/SpriteLoad'; import { MapGrid } from './MapGrid'; /** 障碍物类型 */ export enum ObstacleType { 无 = 999999, 静态障碍物 = 0, 普通障碍物 = 1, 炸弹 = 2, 银币 = 3, 减速 = 4, 火炉 = 5, 吹风机 = 6, 灯塔 = 7, 藤蔓 = 8, 加速门 = 9, 血包 = 10, 屏蔽 = 11, } const { ccclass, property, executeInEditMode } = _decorator; ccenum(ObstacleType); @ccclass('MapObstacle') @executeInEditMode export class MapObstacle extends Component { private static _openedCoinPanelByEditAction = false; public static consumeOpenedCoinPanelByEditAction(): boolean { const v = MapObstacle._openedCoinPanelByEditAction; MapObstacle._openedCoinPanelByEditAction = false; return v; } @property({ type: Enum(ObstacleType) }) type: ObstacleType = ObstacleType.无; @property({ type: SpriteFrame }) set img(value: SpriteFrame) { this._img = value; if (this.icon) { this.icon.spriteFrame = value; } } get img(): SpriteFrame { return this._img; } @property({ type: SpriteFrame }) private _img: SpriteFrame = null; icon: Sprite = null; Grid: MapGrid = null; ObstacleNode: Node = null; Data: Record | null = null; MechanismData: Record | null = null; protected onLoad(): void { const iconNode = this.node.getChildByName('icon'); this.icon = iconNode?.getComponent(Sprite) ?? null; this.Grid = this.getComponent(MapGrid); if (this.img && this.icon) { this.icon.spriteFrame = this.img; } if (this.type == ObstacleType.静态障碍物 && this.Grid?.isCanWalk) { this.type = ObstacleType.无; } } protected start(): void { if (EDITOR) return; // 新塔防:普通障碍物由 MapGrid 字段驱动,不加载摆放路障贴图 if (this.type === ObstacleType.普通障碍物) { return; } this.init(); } init() { // const rootSprite = this.node.getComponent(Sprite); // if (rootSprite) { // rootSprite.spriteFrame = null; // } this.setObstacle(this.type, true); } /** 设置障碍物外观 */ setObstacle(type: ObstacleType, remove = false) { if (type == ObstacleType.静态障碍物) return; this.type = type; if (type == ObstacleType.无) { if (!remove) { this.remove(); } return; } console.log('[MapObstacle] Obstacle/Playmode 表已移除', this.type); this.Data = null; this.MechanismData = null; // if (this.Grid) { // this.Grid.isCanWalk = this.Data.canPass == 1; // } if (this.type == ObstacleType.普通障碍物) { const battle = gg.game.CurentBattle as { DefaultBarrier?: string } | null; const name = battle?.DefaultBarrier || String(this.Data?.icon ?? ''); this.setIcon(name); if (this.ObstacleNode) { gg.res.putNode(this.ObstacleNode); this.ObstacleNode = null; } } else if (this.Data?.res) { if (this.icon) { this.icon.node.active = false; } if (this.ObstacleNode) { gg.res.putNode(this.ObstacleNode); } this.ObstacleNode = gg.res.getNode('prefab/障碍物/', String(this.Data.res), GBundle.BattleMap); if (this.ObstacleNode?.isValid) { this.ObstacleNode.parent = this.node; this.ObstacleNode.setSiblingIndex(0); } } this.updateGrid(); } /** 通过摆放路障图集名设置 icon */ setIcon(name: string) { if (!this.icon || !name) return; this.icon.node.active = true; this.icon.node.getOrAddComponent(SpriteLoad).setSprite(GPath.ObstacleItem(name), GBundle.BattleMap); } /** 移除障碍物外观 */ remove() { if (this.icon) { this.icon.spriteFrame = null; this.icon.node.getComponent(SpriteLoad)?.clear(); } if (this.ObstacleNode) { gg.res.putNode(this.ObstacleNode); this.ObstacleNode = null; } this.type = ObstacleType.无; if (this.Grid) { this.Grid.isCanWalk = true; this.Grid.isCanPutText = false; this.Grid.isCanRemove = false; this.Grid.isCanBeThunder = false; } const map = gg.game.CurentBattle?.Map; if (map && isValid(map)) { map.notifyMapWalkStateChanged(); } this.updateGrid(); } thunderAtk() { this.remove(); } tryEditAction(_reportClick = false): void {} updateGrid() { this.getComponent(MapGrid)?.updateGrid(); } }