import { Vec2 } from "cc";
import GBaseMode from "../GBaseMode";
import { v2 } from "cc";
import { JNGSyncProtoBase } from "../../components/JNComponent";

export enum GTowards{
    RIGHT,LEFT
}

export default class GObject<T> extends JNGSyncProtoBase<T>{

    //当前模式
    _mode:GBaseMode<{},{}>;

    //是否镜像
    _isMirror:boolean = false;
    get isMirror(){
        return this._isMirror;
    }
    set isMirror(value:boolean){
        if(value){
            GObject.SetMirror(this);
        }else{
            GObject.SetMirror(this,false);
        }
        this._isMirror = value;
    }

    get mode():GBaseMode<{},{}>{
        return this._mode;
    }
    set mode(value:GBaseMode<{},{}>){
        this._mode = value;
    }

    //设置镜像
    static SetMirror(role:GObject<{}>,flipX:boolean = true,flipY:boolean = false){
        let node = role.node;
        node.setScale(flipX ? -Math.abs(node.scale.x) : Math.abs(node.scale.x),flipY ? -Math.abs(node.scale.y) : Math.abs(node.scale.y))
    }

    //获取v2世界坐标
    get v2World():Vec2{
        let world = this.node.worldPosition;
        return v2(world.x,world.y);
    }

    //向后添加距离
    getWorldBackLen(add:Vec2){
        if(this.isMirror){
            return this.v2World.add(add);
        }else{
            add.y = 0-add.y;
            return this.v2World.subtract(add);
        }
    }

    //设置朝向
    setTowards(towards:GTowards){
        if(towards == GTowards.LEFT){
            this.isMirror = true;
        }else{
            this.isMirror = false;
        }
    }

    //镜像值处理
    getMirrorValue(value:number){
        if(this.isMirror){
            return value * -1;
        }else{
            return value;
        }
    }

}

