import { v2 } from "cc";
import { Vec2 } from "cc";
import { GTowards } from "../base/GObject";

//阵法类
export class GTactical{

    tactical:number[][];
    
    //阵容朝向
    towards:GTowards;

    //位置偏差
    offset:Vec2;
    
    //阵法位置
    static pos:Vec2[][] = [
        [v2(-100,150),v2(0,150),v2(100,150)],
        [v2(-100,0),v2(0,0),v2(100,0)],
        [v2(-100,-150),v2(0,-150),v2(100,-150)],
    ];

    constructor(tactical:number[][],towards:GTowards = GTowards.RIGHT,offset:Vec2 = Vec2.ZERO){
        this.tactical = tactical;
        this.towards = towards;
        this.offset = offset;
    }

    setOffset(offset:Vec2):this{
        this.offset = offset;
        return this;
    }
    
    //获取阵法
    static getTactical(isReversed:boolean = false): GTactical{
        let tactical = [
            [7,4,1],
            [8,5,2],
            [9,6,3],
        ];
        if(isReversed){
            tactical = this.getTacticalFlipX(tactical);
            return new GTactical(tactical,GTowards.LEFT);
        }
        return new GTactical(tactical);
    }
    //获取阵法
    static getTactical1(isReversed:boolean = false): GTactical{
        let tactical = [
            [0,3,0],
            [0,1,0],
            [0,2,0],
        ];
        if(isReversed){
            tactical = this.getTacticalFlipX(tactical);
            return new GTactical(tactical,GTowards.LEFT);
        }
        return new GTactical(tactical);
    }

    //获取阵法
    static getTactical2(isReversed:boolean = false): GTactical{
        let tactical = [
            [0,0,3],
            [0,1,0],
            [2,0,0],
        ];
        if(isReversed){
            tactical = this.getTacticalFlipX(tactical);
            return new GTactical(tactical,GTowards.LEFT);
        }
        return new GTactical(tactical);
    }

    //阵法取反
    static getTacticalFlipX(tactical:number[][]){
        return tactical.map(row => row.reverse());
    }

    //返回阵法位置
    getPosition(index:number,father:Vec2 = this.offset){

        let pos;
        if(pos = this.getXY(index)){
            return father.clone().add(GTactical.pos[pos.y][pos.x].clone());
        }
        return null;
        
    }

    //返回XY
    getXY(index:number):Vec2{
        for(let i = 0;i < 3;i++){
            for(let j = 0;j < 3;j++){
                let tag = this.tactical[i][j];
                if(tag == index){
                    return v2(j,i);
                }
            }
        }
        return null;
    }

    //获取XY的位置
    getPosXYPosition(x:number,y:number,father:Vec2 = this.offset){
        return this.getPosition(this.getPosXY(x,y),father);
    }

    //获取XY的 Index
    getPosXY(x:number,y:number):number{
        return this.tactical[y][x];
    }


    //返回我在第几排
    getRow(index:number):number{
        let pos;
        if(pos = this.getXY(index)){
            return pos.y;
        }
        return null;
    }

}

