
/**
 * 游戏工具类
 * @description 依据引擎Api的工具类，用于操作引擎相关系统的开关、操作引擎相关的api属性，比如坐标
 * @author 冯聪
 */
export default class GameUtil {

    /** 获取屏幕尺寸 */
    public getWinSize(): cc.Size {
        return cc.winSize;
    }

    /** 激活物理系统 */
    public enablePhysics() {
        cc.director.getPhysicsManager().enabled = true;
    }

    /** 绘制物理信息 */
    public debugDrawPhysicsFlag(ifDraw: boolean = false) {
        if (ifDraw) {
            cc.director.getPhysicsManager().debugDrawFlags =
                cc.PhysicsManager.DrawBits.e_aabbBit |
                cc.PhysicsManager.DrawBits.e_jointBit |
                cc.PhysicsManager.DrawBits.e_shapeBit;
        }
        else {
            cc.director.getPhysicsManager().debugDrawFlags = 0;
        }
    }

    /**
     * 获取世界坐标
     * @param 需要获取的节点
     */
    public getWorldPos(node: cc.Node): cc.Vec2 {
        let originX = node.x;
        let originY = node.y;
        let curNode = node;

        while (curNode.parent) {
            let parent = curNode.parent;
            if (parent.name == 'Canvas') {
                break;
            }
            originX += parent.x;
            originY += parent.y;
            curNode = parent;
        }
        return new cc.Vec2(originX, originY);
    }
}
