import { BinaryClassUpgrader } from "../../ecs/storage/binary/BinaryClassUpgrader.js";

export class GridPosition2TransformSerializationUpgrader_0_1 extends BinaryClassUpgrader {
    constructor() {
        super();

        //
        this.__startVersion = 0;
        this.__targetVersion = 1;
    }

    upgrade(source, target) {
        const offsetX = source.readFloat64();
        const offsetY = source.readFloat64();

        let header = 3;
        if (offsetX === 0 && offsetY === 0) {
            header = 0;
        } else if (offsetX === 0) {
            header = 2;
        } else if (offsetY === 0) {
            header = 1;
        } else {
            //both coordinates are present
            header = 3;
        }

        target.writeUint8(header);

        if ((header & 1) !== 0) {
            //write X
            target.writeFloat32(offsetX);
        }

        if ((header & 2) !== 0) {
            //write Y
            target.writeFloat32(offsetY);
        }
    }
}
