import { _decorator, Component, Node, TiledMap } from 'cc';
import { TiledMapPathfinder } from './TiledMapPathfinder';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('Manager')
export class Manager extends Component {

    @property(TiledMap)
    tiledMap: TiledMap | null = null;

    @property(Node)
    playerNode: Node | null = null;

    private pathfinder: TiledMapPathfinder | null = null;
    private playerController: PlayerController | null = null;

    start() {
        this.initializeGame();
    }

    private initializeGame() {
        // 初始化寻路系统
        if (this.tiledMap) {
            // 为TiledMap添加寻路组件
            this.pathfinder = this.tiledMap.node.getComponent(TiledMapPathfinder);
            if (!this.pathfinder) {
                this.pathfinder = this.tiledMap.node.addComponent(TiledMapPathfinder);
                this.pathfinder.tiledMap = this.tiledMap;
                this.pathfinder.walkableLayerName = 'WalkableLayer';
                this.pathfinder.tileSize = 32;
            }
        }

        // 初始化玩家控制器
        if (this.playerNode) {
            this.playerController = this.playerNode.getComponent(PlayerController);
            if (this.playerController && this.pathfinder) {
                this.playerController.pathfinder = this.pathfinder;
            }
        }

        console.log('游戏初始化完成');
        this.logMapInfo();
    }

    private logMapInfo() {
        if (this.pathfinder) {
            const mapInfo = this.pathfinder.getMapInfo();
            if (mapInfo) {
                console.log('地图信息:', {
                    mapSize: mapInfo.mapSize,
                    tileSize: mapInfo.tileSize,
                    orientation: mapInfo.orientation
                });
            }
        }
    }

    update(deltaTime: number) {

    }
}